path_editor_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*************************************************************************/
  2. /* path_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "path_editor_plugin.h"
  31. #include "os/keyboard.h"
  32. #include "scene/resources/curve.h"
  33. #include "spatial_editor_plugin.h"
  34. String PathSpatialGizmo::get_handle_name(int p_idx) const {
  35. Ref<Curve3D> c = path->get_curve();
  36. if (c.is_null())
  37. return "";
  38. if (p_idx < c->get_point_count()) {
  39. return TTR("Curve Point #") + itos(p_idx);
  40. }
  41. p_idx = p_idx - c->get_point_count() + 1;
  42. int idx = p_idx / 2;
  43. int t = p_idx % 2;
  44. String n = TTR("Curve Point #") + itos(idx);
  45. if (t == 0)
  46. n += " In";
  47. else
  48. n += " Out";
  49. return n;
  50. }
  51. Variant PathSpatialGizmo::get_handle_value(int p_idx) const {
  52. Ref<Curve3D> c = path->get_curve();
  53. if (c.is_null())
  54. return Variant();
  55. if (p_idx < c->get_point_count()) {
  56. original = c->get_point_pos(p_idx);
  57. return original;
  58. }
  59. p_idx = p_idx - c->get_point_count() + 1;
  60. int idx = p_idx / 2;
  61. int t = p_idx % 2;
  62. Vector3 ofs;
  63. if (t == 0)
  64. ofs = c->get_point_in(idx);
  65. else
  66. ofs = c->get_point_out(idx);
  67. original = ofs + c->get_point_pos(idx);
  68. return ofs;
  69. }
  70. void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  71. Ref<Curve3D> c = path->get_curve();
  72. if (c.is_null())
  73. return;
  74. Transform gt = path->get_global_transform();
  75. Transform gi = gt.affine_inverse();
  76. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  77. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  78. if (p_idx < c->get_point_count()) {
  79. Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
  80. Vector3 inters;
  81. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  82. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  83. float snap = SpatialEditor::get_singleton()->get_translate_snap();
  84. inters.snap(snap);
  85. }
  86. Vector3 local = gi.xform(inters);
  87. c->set_point_pos(p_idx, local);
  88. }
  89. return;
  90. }
  91. p_idx = p_idx - c->get_point_count() + 1;
  92. int idx = p_idx / 2;
  93. int t = p_idx % 2;
  94. Vector3 base = c->get_point_pos(idx);
  95. Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
  96. Vector3 inters;
  97. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  98. Vector3 local = gi.xform(inters) - base;
  99. if (t == 0) {
  100. c->set_point_in(idx, local);
  101. } else {
  102. c->set_point_out(idx, local);
  103. }
  104. }
  105. }
  106. void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  107. Ref<Curve3D> c = path->get_curve();
  108. if (c.is_null())
  109. return;
  110. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  111. if (p_idx < c->get_point_count()) {
  112. if (p_cancel) {
  113. c->set_point_pos(p_idx, p_restore);
  114. return;
  115. }
  116. ur->create_action(TTR("Set Curve Point Pos"));
  117. ur->add_do_method(c.ptr(), "set_point_pos", p_idx, c->get_point_pos(p_idx));
  118. ur->add_undo_method(c.ptr(), "set_point_pos", p_idx, p_restore);
  119. ur->commit_action();
  120. return;
  121. }
  122. p_idx = p_idx - c->get_point_count() + 1;
  123. int idx = p_idx / 2;
  124. int t = p_idx % 2;
  125. Vector3 ofs;
  126. if (p_cancel) {
  127. return;
  128. }
  129. if (t == 0) {
  130. if (p_cancel) {
  131. c->set_point_in(p_idx, p_restore);
  132. return;
  133. }
  134. ur->create_action(TTR("Set Curve In Pos"));
  135. ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
  136. ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
  137. ur->commit_action();
  138. } else {
  139. if (p_cancel) {
  140. c->set_point_out(idx, p_restore);
  141. return;
  142. }
  143. ur->create_action(TTR("Set Curve Out Pos"));
  144. ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
  145. ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
  146. ur->commit_action();
  147. }
  148. }
  149. void PathSpatialGizmo::redraw() {
  150. clear();
  151. Ref<Curve3D> c = path->get_curve();
  152. if (c.is_null())
  153. return;
  154. Vector3Array v3a = c->tesselate();
  155. //Vector3Array v3a=c->get_baked_points();
  156. int v3s = v3a.size();
  157. if (v3s == 0)
  158. return;
  159. Vector<Vector3> v3p;
  160. Vector3Array::Read r = v3a.read();
  161. for (int i = 0; i < v3s - 1; i++) {
  162. v3p.push_back(r[i]);
  163. v3p.push_back(r[i + 1]);
  164. //v3p.push_back(r[i]);
  165. //v3p.push_back(r[i]+Vector3(0,0.2,0));
  166. }
  167. add_lines(v3p, PathEditorPlugin::singleton->path_material);
  168. add_collision_segments(v3p);
  169. if (PathEditorPlugin::singleton->get_edited_path() == path) {
  170. v3p.clear();
  171. Vector<Vector3> handles;
  172. Vector<Vector3> sec_handles;
  173. for (int i = 0; i < c->get_point_count(); i++) {
  174. Vector3 p = c->get_point_pos(i);
  175. handles.push_back(p);
  176. if (i > 0) {
  177. v3p.push_back(p);
  178. v3p.push_back(p + c->get_point_in(i));
  179. sec_handles.push_back(p + c->get_point_in(i));
  180. }
  181. if (i < c->get_point_count() - 1) {
  182. v3p.push_back(p);
  183. v3p.push_back(p + c->get_point_out(i));
  184. sec_handles.push_back(p + c->get_point_out(i));
  185. }
  186. }
  187. add_lines(v3p, PathEditorPlugin::singleton->path_thin_material);
  188. add_handles(handles);
  189. add_handles(sec_handles, false, true);
  190. }
  191. }
  192. PathSpatialGizmo::PathSpatialGizmo(Path *p_path) {
  193. path = p_path;
  194. set_spatial_node(p_path);
  195. }
  196. Ref<SpatialEditorGizmo> PathEditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
  197. if (p_spatial->cast_to<Path>()) {
  198. return memnew(PathSpatialGizmo(p_spatial->cast_to<Path>()));
  199. }
  200. return Ref<SpatialEditorGizmo>();
  201. }
  202. bool PathEditorPlugin::forward_spatial_input_event(Camera *p_camera, const InputEvent &p_event) {
  203. if (!path)
  204. return false;
  205. Ref<Curve3D> c = path->get_curve();
  206. if (c.is_null())
  207. return false;
  208. Transform gt = path->get_global_transform();
  209. Transform it = gt.affine_inverse();
  210. static const int click_dist = 10; //should make global
  211. if (p_event.type == InputEvent::MOUSE_BUTTON) {
  212. const InputEventMouseButton &mb = p_event.mouse_button;
  213. Point2 mbpos(mb.x, mb.y);
  214. if (mb.pressed && mb.button_index == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb.mod.control))) {
  215. //click into curve, break it down
  216. Vector3Array v3a = c->tesselate();
  217. int idx = 0;
  218. int rc = v3a.size();
  219. int closest_seg = -1;
  220. Vector3 closest_seg_point;
  221. float closest_d = 1e20;
  222. if (rc >= 2) {
  223. Vector3Array::Read r = v3a.read();
  224. if (p_camera->unproject_position(gt.xform(c->get_point_pos(0))).distance_to(mbpos) < click_dist)
  225. return false; //nope, existing
  226. for (int i = 0; i < c->get_point_count() - 1; i++) {
  227. //find the offset and point index of the place to break up
  228. int j = idx;
  229. if (p_camera->unproject_position(gt.xform(c->get_point_pos(i + 1))).distance_to(mbpos) < click_dist)
  230. return false; //nope, existing
  231. while (j < rc && c->get_point_pos(i + 1) != r[j]) {
  232. Vector3 from = r[j];
  233. Vector3 to = r[j + 1];
  234. real_t cdist = from.distance_to(to);
  235. from = gt.xform(from);
  236. to = gt.xform(to);
  237. if (cdist > 0) {
  238. Vector2 s[2];
  239. s[0] = p_camera->unproject_position(from);
  240. s[1] = p_camera->unproject_position(to);
  241. Vector2 inters = Geometry::get_closest_point_to_segment_2d(mbpos, s);
  242. float d = inters.distance_to(mbpos);
  243. if (d < 10 && d < closest_d) {
  244. closest_d = d;
  245. closest_seg = i;
  246. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  247. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  248. Vector3 ra, rb;
  249. Geometry::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
  250. closest_seg_point = it.xform(rb);
  251. }
  252. }
  253. j++;
  254. }
  255. if (idx == j)
  256. idx++; //force next
  257. else
  258. idx = j; //swap
  259. if (j == rc)
  260. break;
  261. }
  262. }
  263. UndoRedo *ur = editor->get_undo_redo();
  264. if (closest_seg != -1) {
  265. //subdivide
  266. ur->create_action(TTR("Split Path"));
  267. ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
  268. ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
  269. ur->commit_action();
  270. return true;
  271. } else {
  272. Vector3 org;
  273. if (c->get_point_count() == 0)
  274. org = path->get_transform().get_origin();
  275. else
  276. org = gt.xform(c->get_point_pos(c->get_point_count()));
  277. Plane p(org, p_camera->get_transform().basis.get_axis(2));
  278. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  279. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  280. Vector3 inters;
  281. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  282. ur->create_action(TTR("Add Point to Curve"));
  283. ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
  284. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  285. ur->commit_action();
  286. return true;
  287. }
  288. //add new at pos
  289. }
  290. } else if (mb.pressed && ((mb.button_index == BUTTON_LEFT && curve_del->is_pressed()) || (mb.button_index == BUTTON_RIGHT && curve_edit->is_pressed()))) {
  291. int erase_idx = -1;
  292. for (int i = 0; i < c->get_point_count(); i++) {
  293. //find the offset and point index of the place to break up
  294. if (p_camera->unproject_position(gt.xform(c->get_point_pos(i))).distance_to(mbpos) < click_dist) {
  295. erase_idx = i;
  296. break;
  297. }
  298. }
  299. if (erase_idx != -1) {
  300. UndoRedo *ur = editor->get_undo_redo();
  301. ur->create_action(TTR("Remove Path Point"));
  302. ur->add_do_method(c.ptr(), "remove_point", erase_idx);
  303. ur->add_undo_method(c.ptr(), "add_point", c->get_point_pos(erase_idx), c->get_point_in(erase_idx), c->get_point_out(erase_idx), erase_idx);
  304. ur->commit_action();
  305. return true;
  306. }
  307. }
  308. }
  309. return false;
  310. }
  311. void PathEditorPlugin::edit(Object *p_object) {
  312. if (p_object) {
  313. path = p_object->cast_to<Path>();
  314. if (path) {
  315. if (path->get_curve().is_valid()) {
  316. path->get_curve()->emit_signal("changed");
  317. }
  318. }
  319. } else {
  320. Path *pre = path;
  321. path = NULL;
  322. if (pre) {
  323. pre->get_curve()->emit_signal("changed");
  324. }
  325. }
  326. // collision_polygon_editor->edit(p_object->cast_to<Node>());
  327. }
  328. bool PathEditorPlugin::handles(Object *p_object) const {
  329. return p_object->is_type("Path");
  330. }
  331. void PathEditorPlugin::make_visible(bool p_visible) {
  332. if (p_visible) {
  333. curve_create->show();
  334. curve_edit->show();
  335. curve_del->show();
  336. curve_close->show();
  337. sep->show();
  338. } else {
  339. curve_create->hide();
  340. curve_edit->hide();
  341. curve_del->hide();
  342. curve_close->hide();
  343. sep->hide();
  344. {
  345. Path *pre = path;
  346. path = NULL;
  347. if (pre && pre->get_curve().is_valid()) {
  348. pre->get_curve()->emit_signal("changed");
  349. }
  350. }
  351. }
  352. }
  353. void PathEditorPlugin::_mode_changed(int p_idx) {
  354. curve_create->set_pressed(p_idx == 0);
  355. curve_edit->set_pressed(p_idx == 1);
  356. curve_del->set_pressed(p_idx == 2);
  357. }
  358. void PathEditorPlugin::_close_curve() {
  359. Ref<Curve3D> c = path->get_curve();
  360. if (c.is_null())
  361. return;
  362. if (c->get_point_count() < 2)
  363. return;
  364. c->add_point(c->get_point_pos(0), c->get_point_in(0), c->get_point_out(0));
  365. }
  366. void PathEditorPlugin::_notification(int p_what) {
  367. if (p_what == NOTIFICATION_ENTER_TREE) {
  368. curve_create->connect("pressed", this, "_mode_changed", make_binds(0));
  369. curve_edit->connect("pressed", this, "_mode_changed", make_binds(1));
  370. curve_del->connect("pressed", this, "_mode_changed", make_binds(2));
  371. curve_close->connect("pressed", this, "_close_curve");
  372. }
  373. }
  374. void PathEditorPlugin::_bind_methods() {
  375. ObjectTypeDB::bind_method(_MD("_mode_changed"), &PathEditorPlugin::_mode_changed);
  376. ObjectTypeDB::bind_method(_MD("_close_curve"), &PathEditorPlugin::_close_curve);
  377. }
  378. PathEditorPlugin *PathEditorPlugin::singleton = NULL;
  379. PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) {
  380. path = NULL;
  381. editor = p_node;
  382. singleton = this;
  383. path_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  384. path_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.5, 0.5, 1.0, 0.8));
  385. path_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  386. path_material->set_line_width(3);
  387. path_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  388. path_material->set_flag(Material::FLAG_UNSHADED, true);
  389. path_thin_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  390. path_thin_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.5, 0.5, 1.0, 0.4));
  391. path_thin_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  392. path_thin_material->set_line_width(1);
  393. path_thin_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  394. path_thin_material->set_flag(Material::FLAG_UNSHADED, true);
  395. // SpatialEditor::get_singleton()->add_gizmo_plugin(this);
  396. sep = memnew(VSeparator);
  397. sep->hide();
  398. SpatialEditor::get_singleton()->add_control_to_menu_panel(sep);
  399. curve_edit = memnew(ToolButton);
  400. curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
  401. curve_edit->set_toggle_mode(true);
  402. curve_edit->hide();
  403. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  404. curve_edit->set_tooltip(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
  405. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
  406. curve_create = memnew(ToolButton);
  407. curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
  408. curve_create->set_toggle_mode(true);
  409. curve_create->hide();
  410. curve_create->set_focus_mode(Control::FOCUS_NONE);
  411. curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  412. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_create);
  413. curve_del = memnew(ToolButton);
  414. curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
  415. curve_del->set_toggle_mode(true);
  416. curve_del->hide();
  417. curve_del->set_focus_mode(Control::FOCUS_NONE);
  418. curve_del->set_tooltip(TTR("Delete Point"));
  419. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del);
  420. curve_close = memnew(ToolButton);
  421. curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
  422. curve_close->hide();
  423. curve_close->set_focus_mode(Control::FOCUS_NONE);
  424. curve_close->set_tooltip(TTR("Close Curve"));
  425. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close);
  426. curve_edit->set_pressed(true);
  427. /*
  428. collision_polygon_editor = memnew( PathEditor(p_node) );
  429. editor->get_viewport()->add_child(collision_polygon_editor);
  430. collision_polygon_editor->set_margin(MARGIN_LEFT,200);
  431. collision_polygon_editor->set_margin(MARGIN_RIGHT,230);
  432. collision_polygon_editor->set_margin(MARGIN_TOP,0);
  433. collision_polygon_editor->set_margin(MARGIN_BOTTOM,10);
  434. collision_polygon_editor->hide();
  435. */
  436. }
  437. PathEditorPlugin::~PathEditorPlugin() {
  438. }