path_2d_editor_plugin.cpp 23 KB

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