path_2d_editor_plugin.cpp 22 KB

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