path_2d_editor_plugin.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/editor_settings.h"
  33. #include "os/file_access.h"
  34. #include "os/keyboard.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. Ref<InputEventMouseButton> mb = p_event;
  61. if (mb.is_valid()) {
  62. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  63. Vector2 gpoint = mb->get_position();
  64. 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())));
  65. real_t grab_threshold = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8);
  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. return true;
  89. } else if (dist_to_p_in < grab_threshold && i > 0) {
  90. action = ACTION_MOVING_IN;
  91. action_point = i;
  92. moving_from = curve->get_point_in(i);
  93. moving_screen_from = gpoint;
  94. return true;
  95. }
  96. }
  97. }
  98. // Check for point deletion.
  99. if ((mb->get_button_index() == BUTTON_RIGHT && mode == MODE_EDIT) || (mb->get_button_index() == BUTTON_LEFT && mode == MODE_DELETE)) {
  100. if (dist_to_p < grab_threshold) {
  101. undo_redo->create_action(TTR("Remove Point from Curve"));
  102. undo_redo->add_do_method(curve.ptr(), "remove_point", i);
  103. 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);
  104. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  105. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  106. undo_redo->commit_action();
  107. return true;
  108. } else if (dist_to_p_out < grab_threshold) {
  109. undo_redo->create_action(TTR("Remove Out-Control from Curve"));
  110. undo_redo->add_do_method(curve.ptr(), "set_point_out", i, Vector2());
  111. undo_redo->add_undo_method(curve.ptr(), "set_point_out", i, curve->get_point_out(i));
  112. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  113. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  114. undo_redo->commit_action();
  115. return true;
  116. } else if (dist_to_p_in < grab_threshold) {
  117. undo_redo->create_action(TTR("Remove In-Control from Curve"));
  118. undo_redo->add_do_method(curve.ptr(), "set_point_in", i, Vector2());
  119. undo_redo->add_undo_method(curve.ptr(), "set_point_in", i, curve->get_point_in(i));
  120. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  121. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  122. undo_redo->commit_action();
  123. return true;
  124. }
  125. }
  126. }
  127. }
  128. // Check for point creation.
  129. if (mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && ((mb->get_command() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
  130. Ref<Curve2D> curve = node->get_curve();
  131. undo_redo->create_action(TTR("Add Point to Curve"));
  132. undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
  133. undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count());
  134. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  135. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  136. undo_redo->commit_action();
  137. action = ACTION_MOVING_POINT;
  138. action_point = curve->get_point_count() - 1;
  139. moving_from = curve->get_point_position(action_point);
  140. moving_screen_from = gpoint;
  141. canvas_item_editor->get_viewport_control()->update();
  142. return true;
  143. }
  144. // Check for point movement completion.
  145. if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && action != ACTION_NONE) {
  146. Ref<Curve2D> curve = node->get_curve();
  147. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  148. switch (action) {
  149. case ACTION_NONE:
  150. // N/A, handled in above condition.
  151. break;
  152. case ACTION_MOVING_POINT: {
  153. undo_redo->create_action(TTR("Move Point in Curve"));
  154. undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
  155. undo_redo->add_undo_method(curve.ptr(), "set_point_position", action_point, moving_from);
  156. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  157. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  158. undo_redo->commit_action();
  159. } break;
  160. case ACTION_MOVING_IN: {
  161. undo_redo->create_action(TTR("Move In-Control in Curve"));
  162. undo_redo->add_do_method(curve.ptr(), "set_point_in", action_point, new_pos);
  163. undo_redo->add_undo_method(curve.ptr(), "set_point_in", action_point, moving_from);
  164. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  165. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  166. undo_redo->commit_action();
  167. } break;
  168. case ACTION_MOVING_OUT: {
  169. undo_redo->create_action(TTR("Move Out-Control in Curve"));
  170. undo_redo->add_do_method(curve.ptr(), "set_point_out", action_point, new_pos);
  171. undo_redo->add_undo_method(curve.ptr(), "set_point_out", action_point, moving_from);
  172. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  173. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  174. undo_redo->commit_action();
  175. } break;
  176. }
  177. action = ACTION_NONE;
  178. return true;
  179. }
  180. }
  181. Ref<InputEventMouseMotion> mm = p_event;
  182. if (mm.is_valid()) {
  183. if (action != ACTION_NONE) {
  184. // Handle point/control movement.
  185. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  186. Vector2 gpoint = mm->get_position();
  187. 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())));
  188. Ref<Curve2D> curve = node->get_curve();
  189. Vector2 new_pos = moving_from + xform.affine_inverse().basis_xform(gpoint - moving_screen_from);
  190. switch (action) {
  191. case ACTION_NONE:
  192. // N/A, handled in above condition.
  193. break;
  194. case ACTION_MOVING_POINT: {
  195. curve->set_point_position(action_point, cpoint);
  196. } break;
  197. case ACTION_MOVING_IN: {
  198. curve->set_point_in(action_point, new_pos);
  199. } break;
  200. case ACTION_MOVING_OUT: {
  201. curve->set_point_out(action_point, new_pos);
  202. } break;
  203. }
  204. canvas_item_editor->get_viewport_control()->update();
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. void Path2DEditor::forward_draw_over_canvas(Control *p_canvas) {
  211. if (!node)
  212. return;
  213. if (!node->is_visible_in_tree())
  214. return;
  215. if (!node->get_curve().is_valid())
  216. return;
  217. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  218. Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  219. Size2 handle_size = handle->get_size();
  220. Ref<Curve2D> curve = node->get_curve();
  221. int len = curve->get_point_count();
  222. Control *vpc = canvas_item_editor->get_viewport_control();
  223. for (int i = 0; i < len; i++) {
  224. Vector2 point = xform.xform(curve->get_point_position(i));
  225. vpc->draw_texture_rect(handle, Rect2(point - handle_size * 0.5, handle_size), false, Color(1, 1, 1, 1));
  226. if (i < len - 1) {
  227. Vector2 pointout = xform.xform(curve->get_point_position(i) + curve->get_point_out(i));
  228. vpc->draw_line(point, pointout, Color(0.5, 0.5, 1.0, 0.8), 1.0);
  229. vpc->draw_texture_rect(handle, Rect2(pointout - handle_size * 0.5, handle_size), false, Color(1, 0.5, 1, 0.3));
  230. }
  231. if (i > 0) {
  232. Vector2 pointin = xform.xform(curve->get_point_position(i) + curve->get_point_in(i));
  233. vpc->draw_line(point, pointin, Color(0.5, 0.5, 1.0, 0.8), 1.0);
  234. vpc->draw_texture_rect(handle, Rect2(pointin - handle_size * 0.5, handle_size), false, Color(1, 0.5, 1, 0.3));
  235. }
  236. }
  237. }
  238. void Path2DEditor::_node_visibility_changed() {
  239. if (!node)
  240. return;
  241. canvas_item_editor->get_viewport_control()->update();
  242. }
  243. void Path2DEditor::edit(Node *p_path2d) {
  244. if (!canvas_item_editor) {
  245. canvas_item_editor = CanvasItemEditor::get_singleton();
  246. }
  247. if (p_path2d) {
  248. node = Object::cast_to<Path2D>(p_path2d);
  249. if (!node->is_connected("visibility_changed", this, "_node_visibility_changed"))
  250. node->connect("visibility_changed", this, "_node_visibility_changed");
  251. } else {
  252. // node may have been deleted at this point
  253. if (node && node->is_connected("visibility_changed", this, "_node_visibility_changed"))
  254. node->disconnect("visibility_changed", this, "_node_visibility_changed");
  255. node = NULL;
  256. }
  257. }
  258. void Path2DEditor::_bind_methods() {
  259. //ClassDB::bind_method(D_METHOD("_menu_option"),&Path2DEditor::_menu_option);
  260. ClassDB::bind_method(D_METHOD("_node_visibility_changed"), &Path2DEditor::_node_visibility_changed);
  261. ClassDB::bind_method(D_METHOD("_mode_selected"), &Path2DEditor::_mode_selected);
  262. }
  263. void Path2DEditor::_mode_selected(int p_mode) {
  264. if (p_mode == MODE_CREATE) {
  265. curve_create->set_pressed(true);
  266. curve_edit->set_pressed(false);
  267. curve_edit_curve->set_pressed(false);
  268. curve_del->set_pressed(false);
  269. } else if (p_mode == MODE_EDIT) {
  270. curve_create->set_pressed(false);
  271. curve_edit->set_pressed(true);
  272. curve_edit_curve->set_pressed(false);
  273. curve_del->set_pressed(false);
  274. } else if (p_mode == MODE_EDIT_CURVE) {
  275. curve_create->set_pressed(false);
  276. curve_edit->set_pressed(false);
  277. curve_edit_curve->set_pressed(true);
  278. curve_del->set_pressed(false);
  279. } else if (p_mode == MODE_DELETE) {
  280. curve_create->set_pressed(false);
  281. curve_edit->set_pressed(false);
  282. curve_edit_curve->set_pressed(false);
  283. curve_del->set_pressed(true);
  284. } else if (p_mode == ACTION_CLOSE) {
  285. //?
  286. if (!node->get_curve().is_valid())
  287. return;
  288. if (node->get_curve()->get_point_count() < 3)
  289. return;
  290. Vector2 begin = node->get_curve()->get_point_position(0);
  291. Vector2 end = node->get_curve()->get_point_position(node->get_curve()->get_point_count() - 1);
  292. if (begin.distance_to(end) < CMP_EPSILON)
  293. return;
  294. undo_redo->create_action(TTR("Remove Point from Curve"));
  295. undo_redo->add_do_method(node->get_curve().ptr(), "add_point", begin);
  296. undo_redo->add_undo_method(node->get_curve().ptr(), "remove_point", node->get_curve()->get_point_count());
  297. undo_redo->add_do_method(canvas_item_editor->get_viewport_control(), "update");
  298. undo_redo->add_undo_method(canvas_item_editor->get_viewport_control(), "update");
  299. undo_redo->commit_action();
  300. return;
  301. }
  302. mode = Mode(p_mode);
  303. }
  304. Path2DEditor::Path2DEditor(EditorNode *p_editor) {
  305. canvas_item_editor = NULL;
  306. editor = p_editor;
  307. undo_redo = editor->get_undo_redo();
  308. mode = MODE_EDIT;
  309. action = ACTION_NONE;
  310. base_hb = memnew(HBoxContainer);
  311. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(base_hb);
  312. sep = memnew(VSeparator);
  313. base_hb->add_child(sep);
  314. curve_edit = memnew(ToolButton);
  315. curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
  316. curve_edit->set_toggle_mode(true);
  317. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  318. 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("Right Click: Delete Point"));
  319. curve_edit->connect("pressed", this, "_mode_selected", varray(MODE_EDIT));
  320. base_hb->add_child(curve_edit);
  321. curve_edit_curve = memnew(ToolButton);
  322. curve_edit_curve->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCurve", "EditorIcons"));
  323. curve_edit_curve->set_toggle_mode(true);
  324. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  325. curve_edit_curve->set_tooltip(TTR("Select Control Points (Shift+Drag)"));
  326. curve_edit_curve->connect("pressed", this, "_mode_selected", varray(MODE_EDIT_CURVE));
  327. base_hb->add_child(curve_edit_curve);
  328. curve_create = memnew(ToolButton);
  329. curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
  330. curve_create->set_toggle_mode(true);
  331. curve_create->set_focus_mode(Control::FOCUS_NONE);
  332. curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  333. curve_create->connect("pressed", this, "_mode_selected", varray(MODE_CREATE));
  334. base_hb->add_child(curve_create);
  335. curve_del = memnew(ToolButton);
  336. curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
  337. curve_del->set_toggle_mode(true);
  338. curve_del->set_focus_mode(Control::FOCUS_NONE);
  339. curve_del->set_tooltip(TTR("Delete Point"));
  340. curve_del->connect("pressed", this, "_mode_selected", varray(MODE_DELETE));
  341. base_hb->add_child(curve_del);
  342. curve_close = memnew(ToolButton);
  343. curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
  344. curve_close->set_focus_mode(Control::FOCUS_NONE);
  345. curve_close->set_tooltip(TTR("Close Curve"));
  346. curve_close->connect("pressed", this, "_mode_selected", varray(ACTION_CLOSE));
  347. base_hb->add_child(curve_close);
  348. base_hb->hide();
  349. curve_edit->set_pressed(true);
  350. }
  351. void Path2DEditorPlugin::edit(Object *p_object) {
  352. path2d_editor->edit(Object::cast_to<Node>(p_object));
  353. }
  354. bool Path2DEditorPlugin::handles(Object *p_object) const {
  355. return p_object->is_class("Path2D");
  356. }
  357. void Path2DEditorPlugin::make_visible(bool p_visible) {
  358. if (p_visible) {
  359. path2d_editor->show();
  360. path2d_editor->base_hb->show();
  361. } else {
  362. path2d_editor->hide();
  363. path2d_editor->base_hb->hide();
  364. path2d_editor->edit(NULL);
  365. }
  366. }
  367. Path2DEditorPlugin::Path2DEditorPlugin(EditorNode *p_node) {
  368. editor = p_node;
  369. path2d_editor = memnew(Path2DEditor(p_node));
  370. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(path2d_editor);
  371. path2d_editor->hide();
  372. }
  373. Path2DEditorPlugin::~Path2DEditorPlugin() {
  374. }