2
0

path_2d_editor_plugin.cpp 23 KB

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