path_editor_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_position(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_position(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(Vector3(snap, snap, snap));
  85. }
  86. Vector3 local = gi.xform(inters);
  87. c->set_point_position(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_position(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_position(p_idx, p_restore);
  114. return;
  115. }
  116. ur->create_action(TTR("Set Curve Point Position"));
  117. ur->add_do_method(c.ptr(), "set_point_position", p_idx, c->get_point_position(p_idx));
  118. ur->add_undo_method(c.ptr(), "set_point_position", 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 Position"));
  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 Position"));
  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. PoolVector<Vector3> v3a = c->tessellate();
  155. //PoolVector<Vector3> v3a=c->get_baked_points();
  156. int v3s = v3a.size();
  157. if (v3s == 0)
  158. return;
  159. Vector<Vector3> v3p;
  160. PoolVector<Vector3>::Read r = v3a.read();
  161. // BUG: the following won't work when v3s, avoid drawing as a temporary workaround.
  162. for (int i = 0; i < v3s - 1; i++) {
  163. v3p.push_back(r[i]);
  164. v3p.push_back(r[i + 1]);
  165. //v3p.push_back(r[i]);
  166. //v3p.push_back(r[i]+Vector3(0,0.2,0));
  167. }
  168. if (v3p.size() > 1) {
  169. add_lines(v3p, PathEditorPlugin::singleton->path_material);
  170. add_collision_segments(v3p);
  171. }
  172. if (PathEditorPlugin::singleton->get_edited_path() == path) {
  173. v3p.clear();
  174. Vector<Vector3> handles;
  175. Vector<Vector3> sec_handles;
  176. for (int i = 0; i < c->get_point_count(); i++) {
  177. Vector3 p = c->get_point_position(i);
  178. handles.push_back(p);
  179. if (i > 0) {
  180. v3p.push_back(p);
  181. v3p.push_back(p + c->get_point_in(i));
  182. sec_handles.push_back(p + c->get_point_in(i));
  183. }
  184. if (i < c->get_point_count() - 1) {
  185. v3p.push_back(p);
  186. v3p.push_back(p + c->get_point_out(i));
  187. sec_handles.push_back(p + c->get_point_out(i));
  188. }
  189. }
  190. if (v3p.size() > 1) {
  191. add_lines(v3p, PathEditorPlugin::singleton->path_thin_material);
  192. }
  193. if (handles.size()) {
  194. add_handles(handles);
  195. }
  196. if (sec_handles.size()) {
  197. add_handles(sec_handles, false, true);
  198. }
  199. }
  200. }
  201. PathSpatialGizmo::PathSpatialGizmo(Path *p_path) {
  202. path = p_path;
  203. set_spatial_node(p_path);
  204. }
  205. Ref<SpatialEditorGizmo> PathEditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
  206. if (Object::cast_to<Path>(p_spatial)) {
  207. return memnew(PathSpatialGizmo(Object::cast_to<Path>(p_spatial)));
  208. }
  209. return Ref<SpatialEditorGizmo>();
  210. }
  211. bool PathEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event) {
  212. if (!path)
  213. return false;
  214. Ref<Curve3D> c = path->get_curve();
  215. if (c.is_null())
  216. return false;
  217. Transform gt = path->get_global_transform();
  218. Transform it = gt.affine_inverse();
  219. static const int click_dist = 10; //should make global
  220. Ref<InputEventMouseButton> mb = p_event;
  221. if (mb.is_valid()) {
  222. Point2 mbpos(mb->get_position().x, mb->get_position().y);
  223. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->get_control()))) {
  224. //click into curve, break it down
  225. PoolVector<Vector3> v3a = c->tessellate();
  226. int idx = 0;
  227. int rc = v3a.size();
  228. int closest_seg = -1;
  229. Vector3 closest_seg_point;
  230. float closest_d = 1e20;
  231. if (rc >= 2) {
  232. PoolVector<Vector3>::Read r = v3a.read();
  233. if (p_camera->unproject_position(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist)
  234. return false; //nope, existing
  235. for (int i = 0; i < c->get_point_count() - 1; i++) {
  236. //find the offset and point index of the place to break up
  237. int j = idx;
  238. if (p_camera->unproject_position(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist)
  239. return false; //nope, existing
  240. while (j < rc && c->get_point_position(i + 1) != r[j]) {
  241. Vector3 from = r[j];
  242. Vector3 to = r[j + 1];
  243. real_t cdist = from.distance_to(to);
  244. from = gt.xform(from);
  245. to = gt.xform(to);
  246. if (cdist > 0) {
  247. Vector2 s[2];
  248. s[0] = p_camera->unproject_position(from);
  249. s[1] = p_camera->unproject_position(to);
  250. Vector2 inters = Geometry::get_closest_point_to_segment_2d(mbpos, s);
  251. float d = inters.distance_to(mbpos);
  252. if (d < 10 && d < closest_d) {
  253. closest_d = d;
  254. closest_seg = i;
  255. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  256. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  257. Vector3 ra, rb;
  258. Geometry::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
  259. closest_seg_point = it.xform(rb);
  260. }
  261. }
  262. j++;
  263. }
  264. if (idx == j)
  265. idx++; //force next
  266. else
  267. idx = j; //swap
  268. if (j == rc)
  269. break;
  270. }
  271. }
  272. UndoRedo *ur = editor->get_undo_redo();
  273. if (closest_seg != -1) {
  274. //subdivide
  275. ur->create_action(TTR("Split Path"));
  276. ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
  277. ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
  278. ur->commit_action();
  279. return true;
  280. } else {
  281. Vector3 org;
  282. if (c->get_point_count() == 0)
  283. org = path->get_transform().get_origin();
  284. else
  285. org = gt.xform(c->get_point_position(c->get_point_count() - 1));
  286. Plane p(org, p_camera->get_transform().basis.get_axis(2));
  287. Vector3 ray_from = p_camera->project_ray_origin(mbpos);
  288. Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
  289. Vector3 inters;
  290. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  291. ur->create_action(TTR("Add Point to Curve"));
  292. ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
  293. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  294. ur->commit_action();
  295. return true;
  296. }
  297. //add new at pos
  298. }
  299. } else if (mb->is_pressed() && ((mb->get_button_index() == BUTTON_LEFT && curve_del->is_pressed()) || (mb->get_button_index() == BUTTON_RIGHT && curve_edit->is_pressed()))) {
  300. for (int i = 0; i < c->get_point_count(); i++) {
  301. real_t dist_to_p = p_camera->unproject_position(gt.xform(c->get_point_position(i))).distance_to(mbpos);
  302. real_t dist_to_p_out = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos);
  303. real_t dist_to_p_in = p_camera->unproject_position(gt.xform(c->get_point_position(i) + c->get_point_in(i))).distance_to(mbpos);
  304. // Find the offset and point index of the place to break up.
  305. // Also check for the control points.
  306. if (dist_to_p < click_dist) {
  307. UndoRedo *ur = editor->get_undo_redo();
  308. ur->create_action(TTR("Remove Path Point"));
  309. ur->add_do_method(c.ptr(), "remove_point", i);
  310. ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
  311. ur->commit_action();
  312. return true;
  313. } else if (dist_to_p_out < click_dist) {
  314. UndoRedo *ur = editor->get_undo_redo();
  315. ur->create_action(TTR("Remove Out-Control Point"));
  316. ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
  317. ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
  318. ur->commit_action();
  319. return true;
  320. } else if (dist_to_p_in < click_dist) {
  321. UndoRedo *ur = editor->get_undo_redo();
  322. ur->create_action(TTR("Remove In-Control Point"));
  323. ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
  324. ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
  325. ur->commit_action();
  326. return true;
  327. }
  328. }
  329. }
  330. }
  331. return false;
  332. }
  333. void PathEditorPlugin::edit(Object *p_object) {
  334. if (p_object) {
  335. path = Object::cast_to<Path>(p_object);
  336. if (path) {
  337. if (path->get_curve().is_valid()) {
  338. path->get_curve()->emit_signal("changed");
  339. }
  340. }
  341. } else {
  342. Path *pre = path;
  343. path = NULL;
  344. if (pre) {
  345. pre->get_curve()->emit_signal("changed");
  346. }
  347. }
  348. //collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
  349. }
  350. bool PathEditorPlugin::handles(Object *p_object) const {
  351. return p_object->is_class("Path");
  352. }
  353. void PathEditorPlugin::make_visible(bool p_visible) {
  354. if (p_visible) {
  355. curve_create->show();
  356. curve_edit->show();
  357. curve_del->show();
  358. curve_close->show();
  359. sep->show();
  360. } else {
  361. curve_create->hide();
  362. curve_edit->hide();
  363. curve_del->hide();
  364. curve_close->hide();
  365. sep->hide();
  366. {
  367. Path *pre = path;
  368. path = NULL;
  369. if (pre && pre->get_curve().is_valid()) {
  370. pre->get_curve()->emit_signal("changed");
  371. }
  372. }
  373. }
  374. }
  375. void PathEditorPlugin::_mode_changed(int p_idx) {
  376. curve_create->set_pressed(p_idx == 0);
  377. curve_edit->set_pressed(p_idx == 1);
  378. curve_del->set_pressed(p_idx == 2);
  379. }
  380. void PathEditorPlugin::_close_curve() {
  381. Ref<Curve3D> c = path->get_curve();
  382. if (c.is_null())
  383. return;
  384. if (c->get_point_count() < 2)
  385. return;
  386. c->add_point(c->get_point_position(0), c->get_point_in(0), c->get_point_out(0));
  387. }
  388. void PathEditorPlugin::_notification(int p_what) {
  389. if (p_what == NOTIFICATION_ENTER_TREE) {
  390. curve_create->connect("pressed", this, "_mode_changed", make_binds(0));
  391. curve_edit->connect("pressed", this, "_mode_changed", make_binds(1));
  392. curve_del->connect("pressed", this, "_mode_changed", make_binds(2));
  393. curve_close->connect("pressed", this, "_close_curve");
  394. }
  395. }
  396. void PathEditorPlugin::_bind_methods() {
  397. ClassDB::bind_method(D_METHOD("_mode_changed"), &PathEditorPlugin::_mode_changed);
  398. ClassDB::bind_method(D_METHOD("_close_curve"), &PathEditorPlugin::_close_curve);
  399. }
  400. PathEditorPlugin *PathEditorPlugin::singleton = NULL;
  401. PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) {
  402. path = NULL;
  403. editor = p_node;
  404. singleton = this;
  405. path_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  406. path_material->set_albedo(Color(0.5, 0.5, 1.0, 0.8));
  407. path_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  408. path_material->set_line_width(3);
  409. path_material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  410. path_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  411. path_thin_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  412. path_thin_material->set_albedo(Color(0.5, 0.5, 1.0, 0.4));
  413. path_thin_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  414. path_thin_material->set_line_width(1);
  415. path_thin_material->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  416. path_thin_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  417. //SpatialEditor::get_singleton()->add_gizmo_plugin(this);
  418. sep = memnew(VSeparator);
  419. sep->hide();
  420. SpatialEditor::get_singleton()->add_control_to_menu_panel(sep);
  421. curve_edit = memnew(ToolButton);
  422. curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
  423. curve_edit->set_toggle_mode(true);
  424. curve_edit->hide();
  425. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  426. 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"));
  427. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
  428. curve_create = memnew(ToolButton);
  429. curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
  430. curve_create->set_toggle_mode(true);
  431. curve_create->hide();
  432. curve_create->set_focus_mode(Control::FOCUS_NONE);
  433. curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  434. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_create);
  435. curve_del = memnew(ToolButton);
  436. curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
  437. curve_del->set_toggle_mode(true);
  438. curve_del->hide();
  439. curve_del->set_focus_mode(Control::FOCUS_NONE);
  440. curve_del->set_tooltip(TTR("Delete Point"));
  441. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del);
  442. curve_close = memnew(ToolButton);
  443. curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
  444. curve_close->hide();
  445. curve_close->set_focus_mode(Control::FOCUS_NONE);
  446. curve_close->set_tooltip(TTR("Close Curve"));
  447. SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close);
  448. curve_edit->set_pressed(true);
  449. /*
  450. collision_polygon_editor = memnew( PathEditor(p_node) );
  451. editor->get_viewport()->add_child(collision_polygon_editor);
  452. collision_polygon_editor->set_margin(MARGIN_LEFT,200);
  453. collision_polygon_editor->set_margin(MARGIN_RIGHT,230);
  454. collision_polygon_editor->set_margin(MARGIN_TOP,0);
  455. collision_polygon_editor->set_margin(MARGIN_BOTTOM,10);
  456. collision_polygon_editor->hide();
  457. */
  458. }
  459. PathEditorPlugin::~PathEditorPlugin() {
  460. }