path_2d_editor_plugin.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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/os/keyboard.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/menu_button.h"
  39. void Path2DEditor::_notification(int p_what) {
  40. switch (p_what) {
  41. case NOTIFICATION_THEME_CHANGED: {
  42. curve_edit->set_button_icon(get_editor_theme_icon(SNAME("CurveEdit")));
  43. curve_edit_curve->set_button_icon(get_editor_theme_icon(SNAME("CurveCurve")));
  44. curve_create->set_button_icon(get_editor_theme_icon(SNAME("CurveCreate")));
  45. curve_del->set_button_icon(get_editor_theme_icon(SNAME("CurveDelete")));
  46. curve_close->set_button_icon(get_editor_theme_icon(SNAME("CurveClose")));
  47. curve_clear_points->set_button_icon(get_editor_theme_icon(SNAME("Clear")));
  48. create_curve_button->set_button_icon(get_editor_theme_icon(SNAME("Curve2D")));
  49. } break;
  50. }
  51. }
  52. void Path2DEditor::_node_removed(Node *p_node) {
  53. if (p_node == node) {
  54. node = nullptr;
  55. hide();
  56. }
  57. }
  58. bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
  59. if (!node) {
  60. return false;
  61. }
  62. if (!node->is_visible_in_tree()) {
  63. return false;
  64. }
  65. Viewport *vp = node->get_viewport();
  66. if (vp && !vp->is_visible_subviewport()) {
  67. return false;
  68. }
  69. if (node->get_curve().is_null()) {
  70. return false;
  71. }
  72. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  73. Ref<InputEventMouseButton> mb = p_event;
  74. if (mb.is_valid()) {
  75. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  76. Vector2 gpoint = mb->get_position();
  77. Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
  78. cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));
  79. if (mb->is_pressed() && action == ACTION_NONE) {
  80. Ref<Curve2D> curve = node->get_curve();
  81. original_mouse_pos = gpoint;
  82. for (int i = 0; i < curve->get_point_count(); i++) {
  83. real_t dist_to_p = gpoint.distance_to(xform.xform(curve->get_point_position(i)));
  84. real_t dist_to_p_out = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_out(i)));
  85. real_t dist_to_p_in = gpoint.distance_to(xform.xform(curve->get_point_position(i) + curve->get_point_in(i)));
  86. // Check for point movement start (for point + in/out controls).
  87. if (mb->get_button_index() == MouseButton::LEFT) {
  88. if (mode == MODE_EDIT && !mb->is_shift_pressed() && dist_to_p < grab_threshold) {
  89. // Points can only be moved in edit mode.
  90. action = ACTION_MOVING_POINT;
  91. action_point = i;
  92. moving_from = curve->get_point_position(i);
  93. moving_screen_from = gpoint;
  94. return true;
  95. } else if (mode == MODE_EDIT || mode == MODE_EDIT_CURVE) {
  96. control_points_in_range = 0;
  97. // In/out controls can be moved in multiple modes.
  98. if (dist_to_p_out < grab_threshold && i < (curve->get_point_count() - 1)) {
  99. action = ACTION_MOVING_OUT;
  100. action_point = i;
  101. moving_from = curve->get_point_out(i);
  102. moving_screen_from = gpoint;
  103. orig_in_length = curve->get_point_in(action_point).length();
  104. control_points_in_range += 1;
  105. }
  106. if (dist_to_p_in < grab_threshold && i > 0) {
  107. action = ACTION_MOVING_IN;
  108. action_point = i;
  109. moving_from = curve->get_point_in(i);
  110. moving_screen_from = gpoint;
  111. orig_out_length = curve->get_point_out(action_point).length();
  112. control_points_in_range += 1;
  113. }
  114. if (control_points_in_range > 0) {
  115. return true;
  116. }
  117. }
  118. }
  119. // Check for point deletion.
  120. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  121. if ((mb->get_button_index() == MouseButton::RIGHT && (mode == MODE_EDIT || mode == MODE_CREATE)) || (mb->get_button_index() == MouseButton::LEFT && mode == MODE_DELETE)) {
  122. if (dist_to_p < grab_threshold) {
  123. undo_redo->create_action(TTR("Remove Point from Curve"));
  124. undo_redo->add_do_method(curve.ptr(), "remove_point", i);
  125. 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);
  126. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  127. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  128. undo_redo->commit_action();
  129. return true;
  130. } else if (dist_to_p_out < grab_threshold) {
  131. undo_redo->create_action(TTR("Remove Out-Control from Curve"));
  132. undo_redo->add_do_method(curve.ptr(), "set_point_out", i, Vector2());
  133. undo_redo->add_undo_method(curve.ptr(), "set_point_out", i, curve->get_point_out(i));
  134. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  135. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  136. undo_redo->commit_action();
  137. return true;
  138. } else if (dist_to_p_in < grab_threshold) {
  139. undo_redo->create_action(TTR("Remove In-Control from Curve"));
  140. undo_redo->add_do_method(curve.ptr(), "set_point_in", i, Vector2());
  141. undo_redo->add_undo_method(curve.ptr(), "set_point_in", i, curve->get_point_in(i));
  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. return true;
  146. }
  147. }
  148. }
  149. }
  150. if (action != ACTION_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  151. _cancel_current_action();
  152. return true;
  153. }
  154. // Check for point creation.
  155. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
  156. Ref<Curve2D> curve = node->get_curve();
  157. curve->add_point(cpoint);
  158. moving_from = cpoint;
  159. action = ACTION_MOVING_NEW_POINT;
  160. action_point = curve->get_point_count() - 1;
  161. moving_from = curve->get_point_position(action_point);
  162. moving_screen_from = gpoint;
  163. canvas_item_editor->update_viewport();
  164. return true;
  165. }
  166. // Check for segment split.
  167. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && mode == MODE_EDIT && on_edge) {
  168. Vector2 gpoint2 = mb->get_position();
  169. Ref<Curve2D> curve = node->get_curve();
  170. int insertion_point = -1;
  171. float mbLength = curve->get_closest_offset(xform.affine_inverse().xform(gpoint2));
  172. int len = curve->get_point_count();
  173. for (int i = 0; i < len - 1; i++) {
  174. float compareLength = curve->get_closest_offset(curve->get_point_position(i + 1));
  175. if (mbLength >= curve->get_closest_offset(curve->get_point_position(i)) && mbLength <= compareLength) {
  176. insertion_point = i;
  177. }
  178. }
  179. if (insertion_point == -1) {
  180. insertion_point = curve->get_point_count() - 2;
  181. }
  182. const Vector2 new_point = xform.affine_inverse().xform(gpoint2);
  183. curve->add_point(new_point, Vector2(0, 0), Vector2(0, 0), insertion_point + 1);
  184. action = ACTION_MOVING_NEW_POINT_FROM_SPLIT;
  185. action_point = insertion_point + 1;
  186. moving_from = curve->get_point_position(action_point);
  187. moving_screen_from = gpoint2;
  188. canvas_item_editor->update_viewport();
  189. on_edge = false;
  190. return true;
  191. }
  192. // Check for point movement completion.
  193. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && action != ACTION_NONE) {
  194. Ref<Curve2D> curve = node->get_curve();
  195. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  196. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  197. switch (action) {
  198. case ACTION_NONE:
  199. // N/A, handled in above condition.
  200. break;
  201. case ACTION_MOVING_POINT:
  202. if (original_mouse_pos != gpoint) {
  203. undo_redo->create_action(TTR("Move Point in Curve"));
  204. undo_redo->add_undo_method(curve.ptr(), "set_point_position", action_point, moving_from);
  205. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  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(false);
  209. }
  210. break;
  211. case ACTION_MOVING_NEW_POINT: {
  212. undo_redo->create_action(TTR("Add Point to Curve"));
  213. undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
  214. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  215. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  216. undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
  217. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  218. undo_redo->commit_action(false);
  219. } break;
  220. case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {
  221. undo_redo->create_action(TTR("Split Curve"));
  222. undo_redo->add_do_method(curve.ptr(), "add_point", Vector2(), Vector2(), Vector2(), action_point);
  223. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  224. undo_redo->add_undo_method(curve.ptr(), "remove_point", action_point);
  225. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  226. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  227. undo_redo->commit_action(false);
  228. } break;
  229. case ACTION_MOVING_IN: {
  230. if (original_mouse_pos != gpoint) {
  231. undo_redo->create_action(TTR("Move In-Control in Curve"));
  232. undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, new_pos);
  233. undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, moving_from);
  234. if (mirror_handle_angle) {
  235. undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));
  236. undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));
  237. }
  238. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  239. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  240. undo_redo->commit_action();
  241. }
  242. } break;
  243. case ACTION_MOVING_OUT: {
  244. if (original_mouse_pos != gpoint) {
  245. undo_redo->create_action(TTR("Move Out-Control in Curve"));
  246. undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, new_pos);
  247. undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, moving_from);
  248. if (mirror_handle_angle) {
  249. undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));
  250. undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));
  251. }
  252. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  253. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  254. undo_redo->commit_action();
  255. }
  256. } break;
  257. }
  258. action = ACTION_NONE;
  259. return true;
  260. }
  261. }
  262. Ref<InputEventMouseMotion> mm = p_event;
  263. if (mm.is_valid()) {
  264. // When both control points were in range of click,
  265. // pick the point that drags the curve outwards.
  266. if (control_points_in_range == 2) {
  267. control_points_in_range = 0;
  268. Ref<Curve2D> curve = node->get_curve();
  269. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  270. Point2 relative = xform.affine_inverse().basis_xform(mm->get_relative());
  271. real_t angle_in = relative.angle_to(curve->get_point_position(action_point - 1) - curve->get_point_position(action_point));
  272. real_t angle_out = relative.angle_to(curve->get_point_position(action_point + 1) - curve->get_point_position(action_point));
  273. if (Math::abs(angle_in) < Math::abs(angle_out)) {
  274. action = ACTION_MOVING_IN;
  275. moving_from = curve->get_point_in(action_point);
  276. orig_out_length = curve->get_point_out(action_point).length();
  277. } else {
  278. action = ACTION_MOVING_OUT;
  279. moving_from = curve->get_point_out(action_point);
  280. orig_in_length = curve->get_point_in(action_point).length();
  281. }
  282. }
  283. if (action == ACTION_NONE && mode == MODE_EDIT) {
  284. // Handle Edge Follow
  285. bool old_edge = on_edge;
  286. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  287. Vector2 gpoint = mm->get_position();
  288. Ref<Curve2D> curve = node->get_curve();
  289. if (curve.is_null()) {
  290. return true;
  291. }
  292. if (curve->get_point_count() < 2) {
  293. return true;
  294. }
  295. // Find edge
  296. edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position())));
  297. on_edge = false;
  298. if (edge_point.distance_to(gpoint) <= grab_threshold) {
  299. on_edge = true;
  300. }
  301. // However, if near a control point or its in-out handles then not on edge
  302. int len = curve->get_point_count();
  303. for (int i = 0; i < len; i++) {
  304. Vector2 pp = curve->get_point_position(i);
  305. Vector2 p = xform.xform(pp);
  306. if (p.distance_to(gpoint) <= grab_threshold) {
  307. on_edge = false;
  308. break;
  309. }
  310. p = xform.xform(pp + curve->get_point_in(i));
  311. if (p.distance_to(gpoint) <= grab_threshold) {
  312. on_edge = false;
  313. break;
  314. }
  315. p = xform.xform(pp + curve->get_point_out(i));
  316. if (p.distance_to(gpoint) <= grab_threshold) {
  317. on_edge = false;
  318. break;
  319. }
  320. }
  321. if (on_edge || old_edge != on_edge) {
  322. canvas_item_editor->update_viewport();
  323. return true;
  324. }
  325. }
  326. if (action != ACTION_NONE) {
  327. // Handle point/control movement.
  328. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  329. Vector2 gpoint = mm->get_position();
  330. Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
  331. cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));
  332. Ref<Curve2D> curve = node->get_curve();
  333. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  334. switch (action) {
  335. case ACTION_NONE:
  336. // N/A, handled in above condition.
  337. break;
  338. case ACTION_MOVING_POINT:
  339. case ACTION_MOVING_NEW_POINT:
  340. case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {
  341. curve->set_point_position(action_point, cpoint);
  342. } break;
  343. case ACTION_MOVING_IN: {
  344. curve->set_point_in(action_point, new_pos);
  345. if (mirror_handle_angle) {
  346. curve->set_point_out(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));
  347. }
  348. } break;
  349. case ACTION_MOVING_OUT: {
  350. curve->set_point_out(action_point, new_pos);
  351. if (mirror_handle_angle) {
  352. curve->set_point_in(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));
  353. }
  354. } break;
  355. }
  356. canvas_item_editor->update_viewport();
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  363. if (!node || !node->is_visible_in_tree() || node->get_curve().is_null()) {
  364. return;
  365. }
  366. Viewport *vp = node->get_viewport();
  367. if (vp && !vp->is_visible_subviewport()) {
  368. return;
  369. }
  370. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  371. const Ref<Texture2D> path_sharp_handle = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));
  372. const Ref<Texture2D> path_smooth_handle = get_editor_theme_icon(SNAME("EditorPathSmoothHandle"));
  373. // Both handle icons must be of the same size
  374. const Size2 handle_size = path_sharp_handle->get_size();
  375. const Ref<Texture2D> curve_handle = get_editor_theme_icon(SNAME("EditorCurveHandle"));
  376. const Size2 curve_handle_size = curve_handle->get_size();
  377. Ref<Curve2D> curve = node->get_curve();
  378. int len = curve->get_point_count();
  379. Control *vpc = canvas_item_editor->get_viewport_control();
  380. debug_handle_lines.clear();
  381. debug_handle_curve_transforms.clear();
  382. debug_handle_sharp_transforms.clear();
  383. debug_handle_smooth_transforms.clear();
  384. Transform2D handle_curve_transform = Transform2D().scaled(curve_handle_size * 0.5);
  385. Transform2D handle_point_transform = Transform2D().scaled(handle_size * 0.5);
  386. for (int i = 0; i < len; i++) {
  387. Vector2 point = xform.xform(curve->get_point_position(i));
  388. // Determines the point icon to be used
  389. bool smooth = false;
  390. if (i < len - 1) {
  391. Vector2 point_out = xform.xform(curve->get_point_position(i) + curve->get_point_out(i));
  392. if (point != point_out) {
  393. smooth = true;
  394. debug_handle_lines.push_back(point);
  395. debug_handle_lines.push_back(point_out);
  396. handle_curve_transform.set_origin(point_out);
  397. debug_handle_curve_transforms.push_back(handle_curve_transform);
  398. }
  399. }
  400. if (i > 0) {
  401. Vector2 point_in = xform.xform(curve->get_point_position(i) + curve->get_point_in(i));
  402. if (point != point_in) {
  403. smooth = true;
  404. debug_handle_lines.push_back(point);
  405. debug_handle_lines.push_back(point_in);
  406. handle_curve_transform.set_origin(point_in);
  407. debug_handle_curve_transforms.push_back(handle_curve_transform);
  408. }
  409. }
  410. handle_point_transform.set_origin(point);
  411. if (smooth) {
  412. debug_handle_smooth_transforms.push_back(handle_point_transform);
  413. } else {
  414. debug_handle_sharp_transforms.push_back(handle_point_transform);
  415. }
  416. }
  417. if (on_edge) {
  418. Ref<Texture2D> add_handle = get_editor_theme_icon(SNAME("EditorHandleAdd"));
  419. p_overlay->draw_texture(add_handle, edge_point - add_handle->get_size() * 0.5);
  420. }
  421. RenderingServer *rs = RS::get_singleton();
  422. rs->mesh_clear(debug_mesh_rid);
  423. if (!debug_handle_lines.is_empty()) {
  424. Array handles_array;
  425. handles_array.resize(Mesh::ARRAY_MAX);
  426. handles_array[Mesh::ARRAY_VERTEX] = Vector<Vector2>(debug_handle_lines);
  427. rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, handles_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  428. rs->canvas_item_add_mesh(vpc->get_canvas_item(), debug_mesh_rid, Transform2D(), Color(0.5, 0.5, 0.5, 1.0));
  429. }
  430. // Add texture rects multimeshes for handle vertices.
  431. uint32_t handle_curve_count = debug_handle_curve_transforms.size();
  432. uint32_t handle_sharp_count = debug_handle_sharp_transforms.size();
  433. uint32_t handle_smooth_count = debug_handle_smooth_transforms.size();
  434. // Add texture rects for curve handle vertices.
  435. rs->multimesh_set_visible_instances(debug_handle_curve_multimesh_rid, 0);
  436. if (handle_curve_count > 0) {
  437. if (rs->multimesh_get_instance_count(debug_handle_curve_multimesh_rid) != int(handle_curve_count)) {
  438. rs->multimesh_allocate_data(debug_handle_curve_multimesh_rid, handle_curve_count, RS::MULTIMESH_TRANSFORM_2D);
  439. }
  440. Vector<float> multimesh_buffer;
  441. multimesh_buffer.resize(8 * handle_curve_count);
  442. float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();
  443. const Transform2D *debug_handle_transforms_ptr = debug_handle_curve_transforms.ptr();
  444. for (uint32_t i = 0; i < handle_curve_count; i++) {
  445. const Transform2D &handle_transform = debug_handle_transforms_ptr[i];
  446. multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];
  447. multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];
  448. multimesh_buffer_ptrw[i * 8 + 2] = 0;
  449. multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];
  450. multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];
  451. multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];
  452. multimesh_buffer_ptrw[i * 8 + 6] = 0;
  453. multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];
  454. }
  455. rs->multimesh_set_buffer(debug_handle_curve_multimesh_rid, multimesh_buffer);
  456. rs->multimesh_set_visible_instances(debug_handle_curve_multimesh_rid, handle_curve_count);
  457. rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_curve_multimesh_rid, curve_handle->get_rid());
  458. }
  459. // Add texture rects for sharp handle vertices.
  460. rs->multimesh_set_visible_instances(debug_handle_sharp_multimesh_rid, 0);
  461. if (handle_sharp_count > 0) {
  462. if (rs->multimesh_get_instance_count(debug_handle_sharp_multimesh_rid) != int(handle_sharp_count)) {
  463. rs->multimesh_allocate_data(debug_handle_sharp_multimesh_rid, handle_sharp_count, RS::MULTIMESH_TRANSFORM_2D);
  464. }
  465. Vector<float> multimesh_buffer;
  466. multimesh_buffer.resize(8 * handle_sharp_count);
  467. float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();
  468. const Transform2D *debug_handle_transforms_ptr = debug_handle_sharp_transforms.ptr();
  469. for (uint32_t i = 0; i < handle_sharp_count; i++) {
  470. const Transform2D &handle_transform = debug_handle_transforms_ptr[i];
  471. multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];
  472. multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];
  473. multimesh_buffer_ptrw[i * 8 + 2] = 0;
  474. multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];
  475. multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];
  476. multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];
  477. multimesh_buffer_ptrw[i * 8 + 6] = 0;
  478. multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];
  479. }
  480. rs->multimesh_set_buffer(debug_handle_sharp_multimesh_rid, multimesh_buffer);
  481. rs->multimesh_set_visible_instances(debug_handle_sharp_multimesh_rid, handle_sharp_count);
  482. rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_sharp_multimesh_rid, curve_handle->get_rid());
  483. }
  484. // Add texture rects for smooth handle vertices.
  485. rs->multimesh_set_visible_instances(debug_handle_smooth_multimesh_rid, 0);
  486. if (handle_smooth_count > 0) {
  487. if (rs->multimesh_get_instance_count(debug_handle_smooth_multimesh_rid) != int(handle_smooth_count)) {
  488. rs->multimesh_allocate_data(debug_handle_smooth_multimesh_rid, handle_smooth_count, RS::MULTIMESH_TRANSFORM_2D);
  489. }
  490. Vector<float> multimesh_buffer;
  491. multimesh_buffer.resize(8 * handle_smooth_count);
  492. float *multimesh_buffer_ptrw = multimesh_buffer.ptrw();
  493. const Transform2D *debug_handle_transforms_ptr = debug_handle_smooth_transforms.ptr();
  494. for (uint32_t i = 0; i < handle_smooth_count; i++) {
  495. const Transform2D &handle_transform = debug_handle_transforms_ptr[i];
  496. multimesh_buffer_ptrw[i * 8 + 0] = handle_transform[0][0];
  497. multimesh_buffer_ptrw[i * 8 + 1] = handle_transform[1][0];
  498. multimesh_buffer_ptrw[i * 8 + 2] = 0;
  499. multimesh_buffer_ptrw[i * 8 + 3] = handle_transform[2][0];
  500. multimesh_buffer_ptrw[i * 8 + 4] = handle_transform[0][1];
  501. multimesh_buffer_ptrw[i * 8 + 5] = handle_transform[1][1];
  502. multimesh_buffer_ptrw[i * 8 + 6] = 0;
  503. multimesh_buffer_ptrw[i * 8 + 7] = handle_transform[2][1];
  504. }
  505. rs->multimesh_set_buffer(debug_handle_smooth_multimesh_rid, multimesh_buffer);
  506. rs->multimesh_set_visible_instances(debug_handle_smooth_multimesh_rid, handle_smooth_count);
  507. rs->canvas_item_add_multimesh(vpc->get_canvas_item(), debug_handle_smooth_multimesh_rid, curve_handle->get_rid());
  508. }
  509. }
  510. void Path2DEditor::_node_visibility_changed() {
  511. if (!node) {
  512. return;
  513. }
  514. canvas_item_editor->update_viewport();
  515. _update_toolbar();
  516. }
  517. void Path2DEditor::_update_toolbar() {
  518. if (!node) {
  519. return;
  520. }
  521. bool has_curve = node->get_curve().is_valid();
  522. toolbar->set_visible(has_curve);
  523. create_curve_button->set_visible(!has_curve);
  524. }
  525. void Path2DEditor::edit(Node *p_path2d) {
  526. if (!canvas_item_editor) {
  527. canvas_item_editor = CanvasItemEditor::get_singleton();
  528. }
  529. if (action != ACTION_NONE) {
  530. _cancel_current_action();
  531. }
  532. if (p_path2d) {
  533. node = Object::cast_to<Path2D>(p_path2d);
  534. _update_toolbar();
  535. if (!node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {
  536. node->connect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));
  537. }
  538. } else {
  539. // The node may have been deleted at this point.
  540. if (node && node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed))) {
  541. node->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Path2DEditor::_node_visibility_changed));
  542. }
  543. node = nullptr;
  544. }
  545. canvas_item_editor->update_viewport();
  546. }
  547. void Path2DEditor::_bind_methods() {
  548. ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path2DEditor::_update_toolbar);
  549. ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path2DEditor::_clear_curve_points);
  550. ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path2DEditor::_restore_curve_points);
  551. }
  552. void Path2DEditor::_mode_selected(int p_mode) {
  553. if (p_mode == MODE_CREATE) {
  554. curve_create->set_pressed(true);
  555. curve_edit->set_pressed(false);
  556. curve_edit_curve->set_pressed(false);
  557. curve_del->set_pressed(false);
  558. } else if (p_mode == MODE_EDIT) {
  559. curve_create->set_pressed(false);
  560. curve_edit->set_pressed(true);
  561. curve_edit_curve->set_pressed(false);
  562. curve_del->set_pressed(false);
  563. } else if (p_mode == MODE_EDIT_CURVE) {
  564. curve_create->set_pressed(false);
  565. curve_edit->set_pressed(false);
  566. curve_edit_curve->set_pressed(true);
  567. curve_del->set_pressed(false);
  568. } else if (p_mode == MODE_DELETE) {
  569. curve_create->set_pressed(false);
  570. curve_edit->set_pressed(false);
  571. curve_edit_curve->set_pressed(false);
  572. curve_del->set_pressed(true);
  573. } else if (p_mode == MODE_CLOSE) {
  574. if (node->get_curve().is_null()) {
  575. return;
  576. }
  577. if (node->get_curve()->get_point_count() < 3) {
  578. return;
  579. }
  580. Vector2 begin = node->get_curve()->get_point_position(0);
  581. Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1);
  582. if (begin.is_equal_approx(end)) {
  583. return;
  584. }
  585. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  586. undo_redo->create_action(TTR("Close the Curve"));
  587. undo_redo->add_do_method(node->get_curve().ptr(), "add_point", begin);
  588. undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point", node->get_curve()->get_point_count());
  589. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  590. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  591. undo_redo->commit_action();
  592. return;
  593. } else if (p_mode == MODE_CLEAR_POINTS) {
  594. if (node->get_curve().is_null()) {
  595. return;
  596. }
  597. if (node->get_curve()->get_point_count() == 0) {
  598. return;
  599. }
  600. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  601. PackedVector2Array points = node->get_curve()->get_points().duplicate();
  602. undo_redo->create_action(TTR("Clear Curve Points"), UndoRedo::MERGE_DISABLE, node);
  603. undo_redo->add_do_method(this, "_clear_curve_points", node);
  604. undo_redo->add_undo_method(this, "_restore_curve_points", node, points);
  605. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  606. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  607. undo_redo->commit_action();
  608. return;
  609. }
  610. mode = Mode(p_mode);
  611. }
  612. void Path2DEditor::_handle_option_pressed(int p_option) {
  613. PopupMenu *pm;
  614. pm = handle_menu->get_popup();
  615. switch (p_option) {
  616. case HANDLE_OPTION_ANGLE: {
  617. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  618. mirror_handle_angle = !is_checked;
  619. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  620. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  621. } break;
  622. case HANDLE_OPTION_LENGTH: {
  623. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  624. mirror_handle_length = !is_checked;
  625. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  626. } break;
  627. }
  628. }
  629. void Path2DEditor::_cancel_current_action() {
  630. ERR_FAIL_NULL(node);
  631. Ref<Curve2D> curve = node->get_curve();
  632. ERR_FAIL_COND(curve.is_null());
  633. switch (action) {
  634. case ACTION_MOVING_POINT: {
  635. curve->set_point_position(action_point, moving_from);
  636. } break;
  637. case ACTION_MOVING_NEW_POINT: {
  638. curve->remove_point(curve->get_point_count() - 1);
  639. } break;
  640. case ACTION_MOVING_NEW_POINT_FROM_SPLIT: {
  641. curve->remove_point(action_point);
  642. } break;
  643. case ACTION_MOVING_IN: {
  644. curve->set_point_in(action_point, moving_from);
  645. curve->set_point_out(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));
  646. } break;
  647. case ACTION_MOVING_OUT: {
  648. curve->set_point_out(action_point, moving_from);
  649. curve->set_point_in(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));
  650. } break;
  651. default: {
  652. }
  653. }
  654. canvas_item_editor->update_viewport();
  655. action = ACTION_NONE;
  656. }
  657. void Path2DEditor::_create_curve() {
  658. ERR_FAIL_NULL(node);
  659. Ref<Curve2D> new_curve;
  660. new_curve.instantiate();
  661. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  662. undo_redo->create_action(TTR("Create Curve in Path2D"));
  663. undo_redo->add_do_property(node, "curve", new_curve);
  664. undo_redo->add_undo_property(node, "curve", Ref<Curve2D>());
  665. undo_redo->add_do_method(this, "_update_toolbar");
  666. undo_redo->add_undo_method(this, "_update_toolbar");
  667. undo_redo->commit_action();
  668. }
  669. void Path2DEditor::_confirm_clear_points() {
  670. if (!node || node->get_curve().is_null()) {
  671. return;
  672. }
  673. if (node->get_curve()->get_point_count() == 0) {
  674. return;
  675. }
  676. clear_points_dialog->reset_size();
  677. clear_points_dialog->popup_centered();
  678. }
  679. void Path2DEditor::_clear_curve_points(Path2D *p_path2d) {
  680. if (!p_path2d || p_path2d->get_curve().is_null()) {
  681. return;
  682. }
  683. Ref<Curve2D> curve = p_path2d->get_curve();
  684. if (curve->get_point_count() == 0) {
  685. return;
  686. }
  687. curve->clear_points();
  688. if (node == p_path2d) {
  689. _mode_selected(MODE_CREATE);
  690. }
  691. }
  692. void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points) {
  693. if (!p_path2d || p_path2d->get_curve().is_null()) {
  694. return;
  695. }
  696. Ref<Curve2D> curve = p_path2d->get_curve();
  697. if (curve->get_point_count() > 0) {
  698. curve->clear_points();
  699. }
  700. for (int i = 0; i < p_points.size(); i += 3) {
  701. 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].
  702. }
  703. if (node == p_path2d) {
  704. _mode_selected(MODE_EDIT);
  705. }
  706. }
  707. Path2DEditor::Path2DEditor() {
  708. toolbar = memnew(HBoxContainer);
  709. curve_edit = memnew(Button);
  710. curve_edit->set_theme_type_variation(SceneStringName(FlatButton));
  711. curve_edit->set_toggle_mode(true);
  712. curve_edit->set_pressed(true);
  713. curve_edit->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  714. 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"));
  715. curve_edit->set_accessibility_name(TTRC("Select Points"));
  716. curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT));
  717. toolbar->add_child(curve_edit);
  718. curve_edit_curve = memnew(Button);
  719. curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));
  720. curve_edit_curve->set_toggle_mode(true);
  721. curve_edit_curve->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  722. curve_edit_curve->set_tooltip_text(TTR("Select Control Points (Shift+Drag)"));
  723. curve_edit_curve->set_accessibility_name(TTRC("Select Control Points"));
  724. curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_EDIT_CURVE));
  725. toolbar->add_child(curve_edit_curve);
  726. curve_create = memnew(Button);
  727. curve_create->set_theme_type_variation(SceneStringName(FlatButton));
  728. curve_create->set_toggle_mode(true);
  729. curve_create->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  730. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Right Click: Delete Point"));
  731. curve_create->set_accessibility_name(TTRC("Add Point"));
  732. curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CREATE));
  733. toolbar->add_child(curve_create);
  734. curve_del = memnew(Button);
  735. curve_del->set_theme_type_variation(SceneStringName(FlatButton));
  736. curve_del->set_toggle_mode(true);
  737. curve_del->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  738. curve_del->set_tooltip_text(TTR("Delete Point"));
  739. curve_del->set_accessibility_name(TTRC("Delete Point"));
  740. curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_DELETE));
  741. toolbar->add_child(curve_del);
  742. curve_close = memnew(Button);
  743. curve_close->set_theme_type_variation(SceneStringName(FlatButton));
  744. curve_close->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  745. curve_close->set_tooltip_text(TTR("Close Curve"));
  746. curve_close->set_accessibility_name(TTRC("Close Curve"));
  747. curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLOSE));
  748. toolbar->add_child(curve_close);
  749. curve_clear_points = memnew(Button);
  750. curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));
  751. curve_clear_points->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  752. curve_clear_points->set_tooltip_text(TTR("Clear Points"));
  753. curve_clear_points->set_accessibility_name(TTRC("Clear Points"));
  754. curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_confirm_clear_points));
  755. toolbar->add_child(curve_clear_points);
  756. clear_points_dialog = memnew(ConfirmationDialog);
  757. clear_points_dialog->set_title(TTR("Please Confirm..."));
  758. clear_points_dialog->set_text(TTR("Remove all curve points?"));
  759. clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path2DEditor::_mode_selected).bind(MODE_CLEAR_POINTS));
  760. toolbar->add_child(clear_points_dialog);
  761. handle_menu = memnew(MenuButton);
  762. handle_menu->set_flat(false);
  763. handle_menu->set_theme_type_variation("FlatMenuButton");
  764. handle_menu->set_text(TTR("Options"));
  765. toolbar->add_child(handle_menu);
  766. PopupMenu *menu = handle_menu->get_popup();
  767. menu->add_check_item(TTR("Mirror Handle Angles"));
  768. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  769. menu->add_check_item(TTR("Mirror Handle Lengths"));
  770. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  771. menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path2DEditor::_handle_option_pressed));
  772. add_child(toolbar);
  773. create_curve_button = memnew(Button);
  774. create_curve_button->set_text(TTR("Create Curve"));
  775. create_curve_button->hide();
  776. add_child(create_curve_button);
  777. create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path2DEditor::_create_curve));
  778. ERR_FAIL_NULL(RS::get_singleton());
  779. RenderingServer *rs = RS::get_singleton();
  780. debug_mesh_rid = rs->mesh_create();
  781. {
  782. debug_handle_mesh_rid = rs->mesh_create();
  783. Vector<Vector2> vertex_array;
  784. vertex_array.resize(4);
  785. Vector2 *vertex_array_ptrw = vertex_array.ptrw();
  786. vertex_array_ptrw[0] = Vector2(-1.0, -1.0);
  787. vertex_array_ptrw[1] = Vector2(1.0, -1.0);
  788. vertex_array_ptrw[2] = Vector2(1.0, 1.0);
  789. vertex_array_ptrw[3] = Vector2(-1.0, 1.0);
  790. Vector<Vector2> uv_array;
  791. uv_array.resize(4);
  792. Vector2 *uv_array_ptrw = uv_array.ptrw();
  793. uv_array_ptrw[0] = Vector2(0.0, 0.0);
  794. uv_array_ptrw[1] = Vector2(1.0, 0.0);
  795. uv_array_ptrw[2] = Vector2(1.0, 1.0);
  796. uv_array_ptrw[3] = Vector2(0.0, 1.0);
  797. Vector<int> index_array;
  798. index_array.resize(6);
  799. int *index_array_ptrw = index_array.ptrw();
  800. index_array_ptrw[0] = 0;
  801. index_array_ptrw[1] = 1;
  802. index_array_ptrw[2] = 3;
  803. index_array_ptrw[3] = 1;
  804. index_array_ptrw[4] = 2;
  805. index_array_ptrw[5] = 3;
  806. Array mesh_arrays;
  807. mesh_arrays.resize(RS::ARRAY_MAX);
  808. mesh_arrays[RS::ARRAY_VERTEX] = vertex_array;
  809. mesh_arrays[RS::ARRAY_TEX_UV] = uv_array;
  810. mesh_arrays[RS::ARRAY_INDEX] = index_array;
  811. rs->mesh_add_surface_from_arrays(debug_handle_mesh_rid, RS::PRIMITIVE_TRIANGLES, mesh_arrays, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
  812. debug_handle_curve_multimesh_rid = rs->multimesh_create();
  813. debug_handle_sharp_multimesh_rid = rs->multimesh_create();
  814. debug_handle_smooth_multimesh_rid = rs->multimesh_create();
  815. rs->multimesh_set_mesh(debug_handle_curve_multimesh_rid, debug_handle_mesh_rid);
  816. rs->multimesh_set_mesh(debug_handle_sharp_multimesh_rid, debug_handle_mesh_rid);
  817. rs->multimesh_set_mesh(debug_handle_smooth_multimesh_rid, debug_handle_mesh_rid);
  818. }
  819. }
  820. Path2DEditor::~Path2DEditor() {
  821. ERR_FAIL_NULL(RS::get_singleton());
  822. RS::get_singleton()->free(debug_mesh_rid);
  823. RS::get_singleton()->free(debug_handle_curve_multimesh_rid);
  824. RS::get_singleton()->free(debug_handle_sharp_multimesh_rid);
  825. RS::get_singleton()->free(debug_handle_smooth_multimesh_rid);
  826. RS::get_singleton()->free(debug_handle_mesh_rid);
  827. }
  828. void Path2DEditorPlugin::edit(Object *p_object) {
  829. path2d_editor->edit(Object::cast_to<Node>(p_object));
  830. }
  831. bool Path2DEditorPlugin::handles(Object *p_object) const {
  832. return p_object->is_class("Path2D");
  833. }
  834. void Path2DEditorPlugin::make_visible(bool p_visible) {
  835. if (p_visible) {
  836. path2d_editor->show();
  837. } else {
  838. path2d_editor->hide();
  839. path2d_editor->edit(nullptr);
  840. }
  841. }
  842. Path2DEditorPlugin::Path2DEditorPlugin() {
  843. path2d_editor = memnew(Path2DEditor);
  844. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor);
  845. path2d_editor->hide();
  846. }