path_2d_editor_plugin.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**************************************************************************/
  2. /* path_2d_editor_plugin.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PATH_2D_EDITOR_PLUGIN_H
  31. #define PATH_2D_EDITOR_PLUGIN_H
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "scene/2d/path_2d.h"
  34. #include "scene/gui/box_container.h"
  35. class CanvasItemEditor;
  36. class ConfirmationDialog;
  37. class MenuButton;
  38. class Path2DEditor : public HBoxContainer {
  39. GDCLASS(Path2DEditor, HBoxContainer);
  40. friend class Path2DEditorPlugin;
  41. CanvasItemEditor *canvas_item_editor = nullptr;
  42. Panel *panel = nullptr;
  43. Path2D *node = nullptr;
  44. enum Mode {
  45. MODE_CREATE,
  46. MODE_EDIT,
  47. MODE_EDIT_CURVE,
  48. MODE_DELETE,
  49. MODE_CLOSE,
  50. MODE_CLEAR_POINTS,
  51. };
  52. Mode mode = MODE_EDIT;
  53. HBoxContainer *toolbar = nullptr;
  54. Button *curve_clear_points = nullptr;
  55. Button *curve_close = nullptr;
  56. Button *curve_create = nullptr;
  57. Button *curve_del = nullptr;
  58. Button *curve_edit = nullptr;
  59. Button *curve_edit_curve = nullptr;
  60. MenuButton *handle_menu = nullptr;
  61. Button *create_curve_button = nullptr;
  62. ConfirmationDialog *clear_points_dialog = nullptr;
  63. bool mirror_handle_angle = true;
  64. bool mirror_handle_length = true;
  65. bool on_edge = false;
  66. enum HandleOption {
  67. HANDLE_OPTION_ANGLE,
  68. HANDLE_OPTION_LENGTH,
  69. };
  70. enum Action {
  71. ACTION_NONE,
  72. ACTION_MOVING_POINT,
  73. ACTION_MOVING_NEW_POINT,
  74. ACTION_MOVING_NEW_POINT_FROM_SPLIT,
  75. ACTION_MOVING_IN,
  76. ACTION_MOVING_OUT,
  77. };
  78. Action action = ACTION_NONE;
  79. int action_point = 0;
  80. Point2 moving_from;
  81. Point2 moving_screen_from;
  82. float orig_in_length = 0.0f;
  83. float orig_out_length = 0.0f;
  84. Vector2 edge_point;
  85. Vector2 original_mouse_pos;
  86. void _mode_selected(int p_mode);
  87. void _handle_option_pressed(int p_option);
  88. void _cancel_current_action();
  89. void _node_visibility_changed();
  90. void _update_toolbar();
  91. void _create_curve();
  92. void _confirm_clear_points();
  93. void _clear_curve_points(Path2D *p_path2d);
  94. void _restore_curve_points(Path2D *p_path2d, const PackedVector2Array &p_points);
  95. protected:
  96. void _notification(int p_what);
  97. void _node_removed(Node *p_node);
  98. static void _bind_methods();
  99. public:
  100. bool forward_gui_input(const Ref<InputEvent> &p_event);
  101. void forward_canvas_draw_over_viewport(Control *p_overlay);
  102. void edit(Node *p_path2d);
  103. Path2DEditor();
  104. };
  105. class Path2DEditorPlugin : public EditorPlugin {
  106. GDCLASS(Path2DEditorPlugin, EditorPlugin);
  107. Path2DEditor *path2d_editor = nullptr;
  108. public:
  109. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return path2d_editor->forward_gui_input(p_event); }
  110. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { path2d_editor->forward_canvas_draw_over_viewport(p_overlay); }
  111. virtual String get_plugin_name() const override { return "Path2D"; }
  112. bool has_main_screen() const override { return false; }
  113. virtual void edit(Object *p_object) override;
  114. virtual bool handles(Object *p_object) const override;
  115. virtual void make_visible(bool p_visible) override;
  116. Path2DEditorPlugin();
  117. ~Path2DEditorPlugin();
  118. };
  119. #endif // PATH_2D_EDITOR_PLUGIN_H