path_2d_editor_plugin.cpp 23 KB

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