path_2d_editor_plugin.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 = NULL;
  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 == NULL) return true;
  229. if (curve->get_point_count() < 2) return true;
  230. // Find edge
  231. edge_point = xform.xform(curve->get_closest_point(xform.affine_inverse().xform(mm->get_position())));
  232. on_edge = false;
  233. if (edge_point.distance_to(gpoint) <= grab_threshold) {
  234. on_edge = true;
  235. }
  236. // However, if near a control point or its in-out handles then not on edge
  237. int len = curve->get_point_count();
  238. for (int i = 0; i < len; i++) {
  239. Vector2 pp = curve->get_point_position(i);
  240. Vector2 p = xform.xform(pp);
  241. if (p.distance_to(gpoint) <= grab_threshold) {
  242. on_edge = false;
  243. break;
  244. }
  245. p = xform.xform(pp + curve->get_point_in(i));
  246. if (p.distance_to(gpoint) <= grab_threshold) {
  247. on_edge = false;
  248. break;
  249. }
  250. p = xform.xform(pp + curve->get_point_out(i));
  251. if (p.distance_to(gpoint) <= grab_threshold) {
  252. on_edge = false;
  253. break;
  254. }
  255. }
  256. if (on_edge || old_edge != on_edge) {
  257. canvas_item_editor->update_viewport();
  258. return true;
  259. }
  260. }
  261. if (action != ACTION_NONE) {
  262. // Handle point/control movement.
  263. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  264. Vector2 gpoint = mm->get_position();
  265. 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())));
  266. Ref<Curve2D> curve = node->get_curve();
  267. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  268. switch (action) {
  269. case ACTION_NONE:
  270. // N/A, handled in above condition.
  271. break;
  272. case ACTION_MOVING_POINT: {
  273. curve->set_point_position(action_point, cpoint);
  274. } break;
  275. case ACTION_MOVING_IN: {
  276. curve->set_point_in(action_point, new_pos);
  277. if (mirror_handle_angle)
  278. curve->set_point_out(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_out_length));
  279. } break;
  280. case ACTION_MOVING_OUT: {
  281. curve->set_point_out(action_point, new_pos);
  282. if (mirror_handle_angle)
  283. curve->set_point_in(action_point, mirror_handle_length ? -new_pos : (-new_pos.normalized() * orig_in_length));
  284. } break;
  285. }
  286. canvas_item_editor->update_viewport();
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. void Path2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  293. if (!node || !node->is_visible_in_tree() || !node->get_curve().is_valid())
  294. return;
  295. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  296. const Ref<Texture> path_sharp_handle = get_icon("EditorPathSharpHandle", "EditorIcons");
  297. const Ref<Texture> path_smooth_handle = get_icon("EditorPathSmoothHandle", "EditorIcons");
  298. // Both handle icons must be of the same size
  299. const Size2 handle_size = path_sharp_handle->get_size();
  300. const Ref<Texture> curve_handle = get_icon("EditorCurveHandle", "EditorIcons");
  301. const Size2 curve_handle_size = curve_handle->get_size();
  302. Ref<Curve2D> curve = node->get_curve();
  303. int len = curve->get_point_count();
  304. Control *vpc = canvas_item_editor->get_viewport_control();
  305. for (int i = 0; i < len; i++) {
  306. Vector2 point = xform.xform(curve->get_point_position(i));
  307. // Determines the point icon to be used
  308. bool smooth = false;
  309. if (i < len - 1) {
  310. Vector2 pointout = xform.xform(curve->get_point_position(i) + curve->get_point_out(i));
  311. if (point != pointout) {
  312. smooth = true;
  313. // Draw the line with a dark and light color to be visible on all backgrounds
  314. vpc->draw_line(point, pointout, Color(0, 0, 0, 0.5), Math::round(EDSCALE), true);
  315. vpc->draw_line(point, pointout, Color(1, 1, 1, 0.5), Math::round(EDSCALE), true);
  316. vpc->draw_texture_rect(curve_handle, Rect2(pointout - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75));
  317. }
  318. }
  319. if (i > 0) {
  320. Vector2 pointin = xform.xform(curve->get_point_position(i) + curve->get_point_in(i));
  321. if (point != pointin) {
  322. smooth = true;
  323. // Draw the line with a dark and light color to be visible on all backgrounds
  324. vpc->draw_line(point, pointin, Color(0, 0, 0, 0.5), Math::round(EDSCALE), true);
  325. vpc->draw_line(point, pointin, Color(1, 1, 1, 0.5), Math::round(EDSCALE), true);
  326. vpc->draw_texture_rect(curve_handle, Rect2(pointin - curve_handle_size * 0.5, curve_handle_size), false, Color(1, 1, 1, 0.75));
  327. }
  328. }
  329. vpc->draw_texture_rect(
  330. smooth ? path_smooth_handle : path_sharp_handle,
  331. Rect2(point - handle_size * 0.5, handle_size),
  332. false);
  333. }
  334. if (on_edge) {
  335. Ref<Texture> add_handle = get_icon("EditorHandleAdd", "EditorIcons");
  336. p_overlay->draw_texture(add_handle, edge_point - add_handle->get_size() * 0.5);
  337. }
  338. }
  339. void Path2DEditor::_node_visibility_changed() {
  340. if (!node)
  341. return;
  342. canvas_item_editor->update_viewport();
  343. }
  344. void Path2DEditor::edit(Node *p_path2d) {
  345. if (!canvas_item_editor) {
  346. canvas_item_editor = CanvasItemEditor::get_singleton();
  347. }
  348. if (p_path2d) {
  349. node = Object::cast_to<Path2D>(p_path2d);
  350. if (!node->is_connected("visibility_changed", this, "_node_visibility_changed"))
  351. node->connect("visibility_changed", this, "_node_visibility_changed");
  352. } else {
  353. // node may have been deleted at this point
  354. if (node && node->is_connected("visibility_changed", this, "_node_visibility_changed"))
  355. node->disconnect("visibility_changed", this, "_node_visibility_changed");
  356. node = NULL;
  357. }
  358. }
  359. void Path2DEditor::_bind_methods() {
  360. //ClassDB::bind_method(D_METHOD("_menu_option"),&Path2DEditor::_menu_option);
  361. ClassDB::bind_method(D_METHOD("_node_visibility_changed"), &Path2DEditor::_node_visibility_changed);
  362. ClassDB::bind_method(D_METHOD("_mode_selected"), &Path2DEditor::_mode_selected);
  363. ClassDB::bind_method(D_METHOD("_handle_option_pressed"), &Path2DEditor::_handle_option_pressed);
  364. }
  365. void Path2DEditor::_mode_selected(int p_mode) {
  366. if (p_mode == MODE_CREATE) {
  367. curve_create->set_pressed(true);
  368. curve_edit->set_pressed(false);
  369. curve_edit_curve->set_pressed(false);
  370. curve_del->set_pressed(false);
  371. } else if (p_mode == MODE_EDIT) {
  372. curve_create->set_pressed(false);
  373. curve_edit->set_pressed(true);
  374. curve_edit_curve->set_pressed(false);
  375. curve_del->set_pressed(false);
  376. } else if (p_mode == MODE_EDIT_CURVE) {
  377. curve_create->set_pressed(false);
  378. curve_edit->set_pressed(false);
  379. curve_edit_curve->set_pressed(true);
  380. curve_del->set_pressed(false);
  381. } else if (p_mode == MODE_DELETE) {
  382. curve_create->set_pressed(false);
  383. curve_edit->set_pressed(false);
  384. curve_edit_curve->set_pressed(false);
  385. curve_del->set_pressed(true);
  386. } else if (p_mode == ACTION_CLOSE) {
  387. //?
  388. if (!node->get_curve().is_valid())
  389. return;
  390. if (node->get_curve()->get_point_count() < 3)
  391. return;
  392. Vector2 begin = node->get_curve()->get_point_position(0);
  393. Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1);
  394. if (begin.distance_to(end) < CMP_EPSILON)
  395. return;
  396. undo_redo->create_action(TTR("Remove Point from Curve"));
  397. undo_redo->add_do_method(node->get_curve().ptr(), "add_point", begin);
  398. undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point", node->get_curve()->get_point_count());
  399. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  400. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  401. undo_redo->commit_action();
  402. return;
  403. }
  404. mode = Mode(p_mode);
  405. }
  406. void Path2DEditor::_handle_option_pressed(int p_option) {
  407. PopupMenu *pm;
  408. pm = handle_menu->get_popup();
  409. switch (p_option) {
  410. case HANDLE_OPTION_ANGLE: {
  411. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  412. mirror_handle_angle = !is_checked;
  413. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  414. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  415. } break;
  416. case HANDLE_OPTION_LENGTH: {
  417. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  418. mirror_handle_length = !is_checked;
  419. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  420. } break;
  421. }
  422. }
  423. Path2DEditor::Path2DEditor(EditorNode *p_editor) {
  424. canvas_item_editor = NULL;
  425. editor = p_editor;
  426. undo_redo = editor->get_undo_redo();
  427. mirror_handle_angle = true;
  428. mirror_handle_length = true;
  429. on_edge = false;
  430. mode = MODE_EDIT;
  431. action = ACTION_NONE;
  432. base_hb = memnew(HBoxContainer);
  433. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(base_hb);
  434. sep = memnew(VSeparator);
  435. base_hb->add_child(sep);
  436. curve_edit = memnew(ToolButton);
  437. curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
  438. curve_edit->set_toggle_mode(true);
  439. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  440. 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"));
  441. curve_edit->connect("pressed", this, "_mode_selected", varray(MODE_EDIT));
  442. base_hb->add_child(curve_edit);
  443. curve_edit_curve = memnew(ToolButton);
  444. curve_edit_curve->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCurve", "EditorIcons"));
  445. curve_edit_curve->set_toggle_mode(true);
  446. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  447. curve_edit_curve->set_tooltip(TTR("Select Control Points (Shift+Drag)"));
  448. curve_edit_curve->connect("pressed", this, "_mode_selected", varray(MODE_EDIT_CURVE));
  449. base_hb->add_child(curve_edit_curve);
  450. curve_create = memnew(ToolButton);
  451. curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
  452. curve_create->set_toggle_mode(true);
  453. curve_create->set_focus_mode(Control::FOCUS_NONE);
  454. curve_create->set_tooltip(TTR("Add Point (in empty space)"));
  455. curve_create->connect("pressed", this, "_mode_selected", varray(MODE_CREATE));
  456. base_hb->add_child(curve_create);
  457. curve_del = memnew(ToolButton);
  458. curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
  459. curve_del->set_toggle_mode(true);
  460. curve_del->set_focus_mode(Control::FOCUS_NONE);
  461. curve_del->set_tooltip(TTR("Delete Point"));
  462. curve_del->connect("pressed", this, "_mode_selected", varray(MODE_DELETE));
  463. base_hb->add_child(curve_del);
  464. curve_close = memnew(ToolButton);
  465. curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
  466. curve_close->set_focus_mode(Control::FOCUS_NONE);
  467. curve_close->set_tooltip(TTR("Close Curve"));
  468. curve_close->connect("pressed", this, "_mode_selected", varray(ACTION_CLOSE));
  469. base_hb->add_child(curve_close);
  470. PopupMenu *menu;
  471. handle_menu = memnew(MenuButton);
  472. handle_menu->set_text(TTR("Options"));
  473. base_hb->add_child(handle_menu);
  474. menu = handle_menu->get_popup();
  475. menu->add_check_item(TTR("Mirror Handle Angles"));
  476. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  477. menu->add_check_item(TTR("Mirror Handle Lengths"));
  478. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  479. menu->connect("id_pressed", this, "_handle_option_pressed");
  480. base_hb->hide();
  481. curve_edit->set_pressed(true);
  482. }
  483. void Path2DEditorPlugin::edit(Object *p_object) {
  484. path2d_editor->edit(Object::cast_to<Node>(p_object));
  485. }
  486. bool Path2DEditorPlugin::handles(Object *p_object) const {
  487. return p_object->is_class("Path2D");
  488. }
  489. void Path2DEditorPlugin::make_visible(bool p_visible) {
  490. if (p_visible) {
  491. path2d_editor->show();
  492. path2d_editor->base_hb->show();
  493. } else {
  494. path2d_editor->hide();
  495. path2d_editor->base_hb->hide();
  496. path2d_editor->edit(NULL);
  497. }
  498. }
  499. Path2DEditorPlugin::Path2DEditorPlugin(EditorNode *p_node) {
  500. editor = p_node;
  501. path2d_editor = memnew(Path2DEditor(p_node));
  502. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor);
  503. path2d_editor->hide();
  504. }
  505. Path2DEditorPlugin::~Path2DEditorPlugin() {
  506. }