path_2d_editor_plugin.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /**************************************************************************/
  2. /* path_2d_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/io/file_access.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/dialogs.h"
  39. #include "scene/gui/menu_button.h"
  40. void Path2DEditor::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_ENTER_TREE:
  43. case NOTIFICATION_THEME_CHANGED: {
  44. curve_edit->set_icon(get_editor_theme_icon(SNAME("CurveEdit")));
  45. curve_edit_curve->set_icon(get_editor_theme_icon(SNAME("CurveCurve")));
  46. curve_create->set_icon(get_editor_theme_icon(SNAME("CurveCreate")));
  47. curve_del->set_icon(get_editor_theme_icon(SNAME("CurveDelete")));
  48. curve_close->set_icon(get_editor_theme_icon(SNAME("CurveClose")));
  49. curve_clear_points->set_icon(get_editor_theme_icon(SNAME("Clear")));
  50. } break;
  51. }
  52. }
  53. void Path2DEditor::_node_removed(Node *p_node) {
  54. if (p_node == node) {
  55. node = nullptr;
  56. hide();
  57. }
  58. }
  59. bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
  60. if (!node) {
  61. return false;
  62. }
  63. if (!node->is_visible_in_tree()) {
  64. return false;
  65. }
  66. if (node->get_curve().is_null()) {
  67. return false;
  68. }
  69. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  70. Ref<InputEventMouseButton> mb = p_event;
  71. if (mb.is_valid()) {
  72. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  73. Vector2 gpoint = mb->get_position();
  74. Vector2 cpoint = node->to_local(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
  75. if (mb->is_pressed() && action == ACTION_NONE) {
  76. Ref<Curve2D> curve = node->get_curve();
  77. original_mouse_pos = gpoint;
  78. for (int i = 0; i < curve->get_point_count(); i++) {
  79. real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i)));
  80. real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i)));
  81. real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i)));
  82. // Check for point movement start (for point + in/out controls).
  83. if (mb->get_button_index() == MouseButton::LEFT) {
  84. if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p < grab_threshold) {
  85. // Points can only be moved in edit mode.
  86. action = ACTION_MOVING_POINT;
  87. action_point = i;
  88. moving_from = curve->get_point_position(i);
  89. moving_screen_from = gpoint;
  90. return true;
  91. } else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) {
  92. // In/out controls can be moved in multiple modes.
  93. if (dist_to_p_out < grab_threshold && i < (curve->get_point_count() - 1)) {
  94. action = ACTION_MOVING_OUT;
  95. action_point = i;
  96. moving_from = curve->get_point_out(i);
  97. moving_screen_from = gpoint;
  98. orig_in_length = curve->get_point_in(action_point).length();
  99. return true;
  100. } else if (dist_to_p_in < grab_threshold && i > 0) {
  101. action = ACTION_MOVING_IN;
  102. action_point = i;
  103. moving_from = curve->get_point_in(i);
  104. moving_screen_from = gpoint;
  105. orig_out_length = curve->get_point_out(action_point).length();
  106. return true;
  107. }
  108. }
  109. }
  110. // Check for point deletion.
  111. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  112. if ((mb->get_button_index() == MouseButton::RIGHT && (mode == MODE_EDIT || mode == MODE_CREATE)) || (mb->get_button_index() == MouseButton::LEFT && mode == MODE_DELETE)) {
  113. if (dist_to_p < grab_threshold) {
  114. undo_redo->create_action(TTR("Remove Point from Curve"));
  115. undo_redo->add_do_method(curve.ptr(), "remove_point", i);
  116. undo_redo->add_undo_method(curve.ptr(), "add_point", curve->get_point_position(i), curve->get_point_in(i), curve->get_point_out(i), i);
  117. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  118. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  119. undo_redo->commit_action();
  120. return true;
  121. } else if (dist_to_p_out < grab_threshold) {
  122. undo_redo->create_action(TTR("Remove Out-Control from Curve"));
  123. undo_redo->add_do_method(curve.ptr(), "set_point_out", i, Vector2());
  124. undo_redo->add_undo_method(curve.ptr(), "set_point_out", i, curve->get_point_out(i));
  125. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  126. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  127. undo_redo->commit_action();
  128. return true;
  129. } else if (dist_to_p_in < grab_threshold) {
  130. undo_redo->create_action(TTR("Remove In-Control from Curve"));
  131. undo_redo->add_do_method(curve.ptr(), "set_point_in", i, Vector2());
  132. undo_redo->add_undo_method(curve.ptr(), "set_point_in", i, curve->get_point_in(i));
  133. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  134. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  135. undo_redo->commit_action();
  136. return true;
  137. }
  138. }
  139. }
  140. }
  141. // Check for point creation.
  142. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
  143. Ref<Curve2D> curve = node->get_curve();
  144. curve->add_point(cpoint);
  145. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  146. undo_redo->create_action(TTR("Add Point to Curve"));
  147. undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
  148. undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
  149. action = ACTION_MOVING_NEW_POINT;
  150. action_point = curve->get_point_count() - 1;
  151. moving_from = curve->get_point_position(action_point);
  152. moving_screen_from = gpoint;
  153. canvas_item_editor->update_viewport();
  154. return true;
  155. }
  156. // Check for segment split.
  157. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && mode == MODE_EDIT && on_edge) {
  158. Vector2 gpoint2 = mb->get_position();
  159. Ref<Curve2D> curve = node->get_curve();
  160. int insertion_point = -1;
  161. float mbLength = curve->get_closest_offset(xform.affine_inverse().xform(gpoint2));
  162. int len = curve->get_point_count();
  163. for (int i = 0; i < len - 1; i++) {
  164. float compareLength = curve->get_closest_offset(curve->get_point_position(i + 1));
  165. if (mbLength >= curve->get_closest_offset(curve->get_point_position(i)) && mbLength <= compareLength) {
  166. insertion_point = i;
  167. }
  168. }
  169. if (insertion_point == -1) {
  170. insertion_point = curve->get_point_count() - 2;
  171. }
  172. const Vector2 new_point = xform.affine_inverse().xform(gpoint2);
  173. curve->add_point(new_point, Vector2(0, 0), Vector2(0, 0), insertion_point + 1);
  174. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  175. undo_redo->create_action(TTR("Split Curve"));
  176. undo_redo->add_do_method(curve.ptr(), "add_point", new_point, Vector2(0, 0), Vector2(0, 0), insertion_point + 1);
  177. undo_redo->add_undo_method(curve.ptr(), "remove_point", insertion_point + 1);
  178. action = ACTION_MOVING_NEW_POINT;
  179. action_point = insertion_point + 1;
  180. moving_from = curve->get_point_position(action_point);
  181. moving_screen_from = gpoint2;
  182. canvas_item_editor->update_viewport();
  183. on_edge = false;
  184. return true;
  185. }
  186. // Check for point movement completion.
  187. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && action != ACTION_NONE) {
  188. Ref<Curve2D> curve = node->get_curve();
  189. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  190. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  191. switch (action) {
  192. case ACTION_NONE:
  193. // N/A, handled in above condition.
  194. break;
  195. case ACTION_MOVING_POINT:
  196. if (original_mouse_pos != gpoint) {
  197. undo_redo->create_action(TTR("Move Point in Curve"));
  198. undo_redo->add_undo_method(curve.ptr(), "set_point_position", action_point, moving_from);
  199. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  200. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  201. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  202. undo_redo->commit_action(false);
  203. }
  204. break;
  205. case ACTION_MOVING_NEW_POINT: {
  206. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  207. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  208. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  209. undo_redo->commit_action(false);
  210. } break;
  211. case ACTION_MOVING_IN: {
  212. if (original_mouse_pos != gpoint) {
  213. undo_redo->create_action(TTR("Move In-Control in Curve"));
  214. undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, new_pos);
  215. undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, moving_from);
  216. if (mirror_handle_angle) {
  217. undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));
  218. undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));
  219. }
  220. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  221. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  222. undo_redo->commit_action();
  223. }
  224. } break;
  225. case ACTION_MOVING_OUT: {
  226. if (original_mouse_pos != gpoint) {
  227. undo_redo->create_action(TTR("Move Out-Control in Curve"));
  228. undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, new_pos);
  229. undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, moving_from);
  230. if (mirror_handle_angle) {
  231. undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));
  232. undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));
  233. }
  234. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  235. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  236. undo_redo->commit_action();
  237. }
  238. } break;
  239. }
  240. action = ACTION_NONE;
  241. return true;
  242. }
  243. }
  244. Ref<InputEventMouseMotion> mm = p_event;
  245. if (mm.is_valid()) {
  246. if (action == ACTION_NONE && mode == MODE_EDIT) {
  247. // Handle Edge Follow
  248. bool old_edge = on_edge;
  249. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  250. Vector2 gpoint = mm->get_position();
  251. Ref<Curve2D> curve = node->get_curve();
  252. if (curve == nullptr) {
  253. return true;
  254. }
  255. if (curve->get_point_count() < 2) {
  256. return true;
  257. }
  258. // Find edge
  259. edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position())));
  260. on_edge = false;
  261. if (edge_point.distance_to(gpoint) <= grab_threshold) {
  262. on_edge = true;
  263. }
  264. // However, if near a control point or its in-out handles then not on edge
  265. int len = curve->get_point_count();
  266. for (int i = 0; i < len; i++) {
  267. Vector2 pp = curve->get_point_position(i);
  268. Vector2 p = xform.xform(pp);
  269. if (p.distance_to(gpoint) <= grab_threshold) {
  270. on_edge = false;
  271. break;
  272. }
  273. p = xform.xform(pp + curve->get_point_in(i));
  274. if (p.distance_to(gpoint) <= grab_threshold) {
  275. on_edge = false;
  276. break;
  277. }
  278. p = xform.xform(pp + curve->get_point_out(i));
  279. if (p.distance_to(gpoint) <= grab_threshold) {
  280. on_edge = false;
  281. break;
  282. }
  283. }
  284. if (on_edge || old_edge != on_edge) {
  285. canvas_item_editor->update_viewport();
  286. return true;
  287. }
  288. }
  289. if (action != ACTION_NONE) {
  290. // Handle point/control movement.
  291. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  292. Vector2 gpoint = mm->get_position();
  293. Vector2 cpoint = node->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position())));
  294. Ref<Curve2D> curve = node->get_curve();
  295. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  296. switch (action) {
  297. case ACTION_NONE:
  298. // N/A, handled in above condition.
  299. break;
  300. case ACTION_MOVING_POINT:
  301. case ACTION_MOVING_NEW_POINT: {
  302. curve->set_point_position(action_point, cpoint);
  303. } break;
  304. case ACTION_MOVING_IN: {
  305. curve->set_point_in(action_point, new_pos);
  306. if (mirror_handle_angle) {
  307. curve->set_point_out(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));
  308. }
  309. } break;
  310. case ACTION_MOVING_OUT: {
  311. curve->set_point_out(action_point, new_pos);
  312. if (mirror_handle_angle) {
  313. curve->set_point_in(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));
  314. }
  315. } break;
  316. }
  317. canvas_item_editor->update_viewport();
  318. return true;
  319. }
  320. }
  321. return false;
  322. }
  323. void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  324. if (!node || !node->is_visible_in_tree() || node->get_curve().is_null()) {
  325. return;
  326. }
  327. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  328. const Ref<Texture2D> path_sharp_handle = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));
  329. const Ref<Texture2D> path_smooth_handle = get_editor_theme_icon(SNAME("EditorPathSmoothHandle"));
  330. // Both handle icons must be of the same size
  331. const Size2 handle_size = path_sharp_handle->get_size();
  332. const Ref<Texture2D> curve_handle = get_editor_theme_icon(SNAME("EditorCurveHandle"));
  333. const Size2 curve_handle_size = curve_handle->get_size();
  334. Ref<Curve2D> curve = node->get_curve();
  335. int len = curve->get_point_count();
  336. Control *vpc = canvas_item_editor->get_viewport_control();
  337. for (int i = 0; i < len; i++) {
  338. Vector2 point = xform.xform(curve->get_point_position(i));
  339. // Determines the point icon to be used
  340. bool smooth = false;
  341. if (i < len - 1) {
  342. Vector2 point_out = xform.xform(curve->get_point_position(i) + curve->get_point_out(i));
  343. if (point != point_out) {
  344. smooth = true;
  345. // Draw the line with a dark and light color to be visible on all backgrounds
  346. vpc->draw_line(point, point_out, Color(0, 0, 0, 0.5), Math::round(EDSCALE));
  347. vpc->draw_line(point, point_out, Color(1, 1, 1, 0.5), Math::round(EDSCALE));
  348. vpc->draw_texture_rect(curve_handle, Rect2(point_out - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75));
  349. }
  350. }
  351. if (i > 0) {
  352. Vector2 point_in = xform.xform(curve->get_point_position(i) + curve->get_point_in(i));
  353. if (point != point_in) {
  354. smooth = true;
  355. // Draw the line with a dark and light color to be visible on all backgrounds
  356. vpc->draw_line(point, point_in, Color(0, 0, 0, 0.5), Math::round(EDSCALE));
  357. vpc->draw_line(point, point_in, Color(1, 1, 1, 0.5), Math::round(EDSCALE));
  358. vpc->draw_texture_rect(curve_handle, Rect2(point_in - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75));
  359. }
  360. }
  361. vpc->draw_texture_rect(
  362. smooth ? path_smooth_handle : path_sharp_handle,
  363. Rect2(point - handle_size * 0.5, handle_size),
  364. false);
  365. }
  366. if (on_edge) {
  367. Ref<Texture2D> add_handle = get_editor_theme_icon(SNAME("EditorHandleAdd"));
  368. p_overlay->draw_texture(add_handle, edge_point - add_handle->get_size() * 0.5);
  369. }
  370. }
  371. void Path2DEditor::_node_visibility_changed() {
  372. if (!node) {
  373. return;
  374. }
  375. canvas_item_editor->update_viewport();
  376. }
  377. void Path2DEditor::edit(Node *p_path2d) {
  378. if (!canvas_item_editor) {
  379. canvas_item_editor = CanvasItemEditor::get_singleton();
  380. }
  381. if (p_path2d) {
  382. node = Object::cast_to<Path2D>(p_path2d);
  383. if (!node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {
  384. node->connect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));
  385. }
  386. } else {
  387. // The node may have been deleted at this point.
  388. if (node && node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {
  389. node->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));
  390. }
  391. node = nullptr;
  392. }
  393. }
  394. void Path2DEditor::_bind_methods() {
  395. //ClassDB::bind_method(D_METHOD("_menu_option"),&Path2DEditor::_menu_option);
  396. ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path2DEditor::_clear_curve_points);
  397. ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path2DEditor::_restore_curve_points);
  398. }
  399. void Path2DEditor::_mode_selected(int p_mode) {
  400. if (p_mode == MODE_CREATE) {
  401. curve_create->set_pressed(true);
  402. curve_edit->set_pressed(false);
  403. curve_edit_curve->set_pressed(false);
  404. curve_del->set_pressed(false);
  405. } else if (p_mode == MODE_EDIT) {
  406. curve_create->set_pressed(false);
  407. curve_edit->set_pressed(true);
  408. curve_edit_curve->set_pressed(false);
  409. curve_del->set_pressed(false);
  410. } else if (p_mode == MODE_EDIT_CURVE) {
  411. curve_create->set_pressed(false);
  412. curve_edit->set_pressed(false);
  413. curve_edit_curve->set_pressed(true);
  414. curve_del->set_pressed(false);
  415. } else if (p_mode == MODE_DELETE) {
  416. curve_create->set_pressed(false);
  417. curve_edit->set_pressed(false);
  418. curve_edit_curve->set_pressed(false);
  419. curve_del->set_pressed(true);
  420. } else if (p_mode == MODE_CLOSE) {
  421. if (node->get_curve().is_null()) {
  422. return;
  423. }
  424. if (node->get_curve()->get_point_count() < 3) {
  425. return;
  426. }
  427. Vector2 begin = node->get_curve()->get_point_position(0);
  428. Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1);
  429. if (begin.is_equal_approx(end)) {
  430. return;
  431. }
  432. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  433. undo_redo->create_action(TTR("Close the Curve"));
  434. undo_redo->add_do_method(node->get_curve().ptr(), "add_point", begin);
  435. undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point", node->get_curve()->get_point_count());
  436. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  437. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  438. undo_redo->commit_action();
  439. return;
  440. } else if (p_mode == MODE_CLEAR_POINTS) {
  441. if (node->get_curve().is_null()) {
  442. return;
  443. }
  444. if (node->get_curve()->get_point_count() == 0) {
  445. return;
  446. }
  447. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  448. PackedVector2Array points = node->get_curve()->get_points().duplicate();
  449. undo_redo->create_action(TTR("Clear Curve Points"), UndoRedo::MERGE_DISABLE, node);
  450. undo_redo->add_do_method(this, "_clear_curve_points", node);
  451. undo_redo->add_undo_method(this, "_restore_curve_points", node, points);
  452. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  453. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  454. undo_redo->commit_action();
  455. return;
  456. }
  457. mode = Mode(p_mode);
  458. }
  459. void Path2DEditor::_handle_option_pressed(int p_option) {
  460. PopupMenu *pm;
  461. pm = handle_menu->get_popup();
  462. switch (p_option) {
  463. case HANDLE_OPTION_ANGLE: {
  464. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  465. mirror_handle_angle = !is_checked;
  466. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  467. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  468. } break;
  469. case HANDLE_OPTION_LENGTH: {
  470. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  471. mirror_handle_length = !is_checked;
  472. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  473. } break;
  474. }
  475. }
  476. void Path2DEditor::_confirm_clear_points() {
  477. if (!node || node->get_curve().is_null()) {
  478. return;
  479. }
  480. if (node->get_curve()->get_point_count() == 0) {
  481. return;
  482. }
  483. clear_points_dialog->reset_size();
  484. clear_points_dialog->popup_centered();
  485. }
  486. void Path2DEditor::_clear_curve_points(Path2D *p_path2d) {
  487. if (!p_path2d || p_path2d->get_curve().is_null()) {
  488. return;
  489. }
  490. Ref<Curve2D> curve = p_path2d->get_curve();
  491. if (curve->get_point_count() == 0) {
  492. return;
  493. }
  494. curve->clear_points();
  495. if (node == p_path2d) {
  496. _mode_selected(MODE_CREATE);
  497. }
  498. }
  499. void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points) {
  500. if (!p_path2d || p_path2d->get_curve().is_null()) {
  501. return;
  502. }
  503. Ref<Curve2D> curve = p_path2d->get_curve();
  504. if (curve->get_point_count() > 0) {
  505. curve->clear_points();
  506. }
  507. for (int i = 0; i < p_points.size(); i += 3) {
  508. curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]); // The Curve2D::points pattern is [point_in, point_out, point_position].
  509. }
  510. if (node == p_path2d) {
  511. _mode_selected(MODE_EDIT);
  512. }
  513. }
  514. Path2DEditor::Path2DEditor() {
  515. canvas_item_editor = nullptr;
  516. mirror_handle_angle = true;
  517. mirror_handle_length = true;
  518. on_edge = false;
  519. mode = MODE_EDIT;
  520. action = ACTION_NONE;
  521. curve_edit = memnew(Button);
  522. curve_edit->set_theme_type_variation("FlatButton");
  523. curve_edit->set_toggle_mode(true);
  524. curve_edit->set_pressed(true);
  525. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  526. curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point") + "\n" + TTR("Left Click: Split Segment (in curve)") + "\n" + TTR("Right Click: Delete Point"));
  527. curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT));
  528. add_child(curve_edit);
  529. curve_edit_curve = memnew(Button);
  530. curve_edit_curve->set_theme_type_variation("FlatButton");
  531. curve_edit_curve->set_toggle_mode(true);
  532. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  533. curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)"));
  534. curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT_CURVE));
  535. add_child(curve_edit_curve);
  536. curve_create = memnew(Button);
  537. curve_create->set_theme_type_variation("FlatButton");
  538. curve_create->set_toggle_mode(true);
  539. curve_create->set_focus_mode(Control::FOCUS_NONE);
  540. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Right Click: Delete Point"));
  541. curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CREATE));
  542. add_child(curve_create);
  543. curve_del = memnew(Button);
  544. curve_del->set_theme_type_variation("FlatButton");
  545. curve_del->set_toggle_mode(true);
  546. curve_del->set_focus_mode(Control::FOCUS_NONE);
  547. curve_del->set_tooltip_text(TTR("Delete Point"));
  548. curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_DELETE));
  549. add_child(curve_del);
  550. curve_close = memnew(Button);
  551. curve_close->set_theme_type_variation("FlatButton");
  552. curve_close->set_focus_mode(Control::FOCUS_NONE);
  553. curve_close->set_tooltip_text(TTR("Close Curve"));
  554. curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLOSE));
  555. add_child(curve_close);
  556. curve_clear_points = memnew(Button);
  557. curve_clear_points->set_theme_type_variation("FlatButton");
  558. curve_clear_points->set_focus_mode(Control::FOCUS_NONE);
  559. curve_clear_points->set_tooltip_text(TTR("Clear Points"));
  560. curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_confirm_clear_points));
  561. add_child(curve_clear_points);
  562. clear_points_dialog = memnew(ConfirmationDialog);
  563. clear_points_dialog->set_title(TTR("Please Confirm..."));
  564. clear_points_dialog->set_text(TTR("Remove all curve points?"));
  565. clear_points_dialog->connect("confirmed", callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLEAR_POINTS));
  566. add_child(clear_points_dialog);
  567. PopupMenu *menu;
  568. handle_menu = memnew(MenuButton);
  569. handle_menu->set_flat(false);
  570. handle_menu->set_theme_type_variation("FlatMenuButton");
  571. handle_menu->set_text(TTR("Options"));
  572. add_child(handle_menu);
  573. menu = handle_menu->get_popup();
  574. menu->add_check_item(TTR("Mirror Handle Angles"));
  575. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  576. menu->add_check_item(TTR("Mirror Handle Lengths"));
  577. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  578. menu->connect("id_pressed", callable_mp(this, &Path2DEditor::_handle_option_pressed));
  579. }
  580. void Path2DEditorPlugin::edit(Object *p_object) {
  581. path2d_editor->edit(Object::cast_to<Node>(p_object));
  582. }
  583. bool Path2DEditorPlugin::handles(Object *p_object) const {
  584. return p_object->is_class("Path2D");
  585. }
  586. void Path2DEditorPlugin::make_visible(bool p_visible) {
  587. if (p_visible) {
  588. path2d_editor->show();
  589. } else {
  590. path2d_editor->hide();
  591. path2d_editor->edit(nullptr);
  592. }
  593. }
  594. Path2DEditorPlugin::Path2DEditorPlugin() {
  595. path2d_editor = memnew(Path2DEditor);
  596. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor);
  597. path2d_editor->hide();
  598. }
  599. Path2DEditorPlugin::~Path2DEditorPlugin() {
  600. }