polygon_2d_editor_plugin.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**************************************************************************/
  2. /* polygon_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 POLYGON_2D_EDITOR_PLUGIN_H
  31. #define POLYGON_2D_EDITOR_PLUGIN_H
  32. #include "editor/plugins/abstract_polygon_2d_editor.h"
  33. #include "scene/2d/polygon_2d.h"
  34. class AcceptDialog;
  35. class ButtonGroup;
  36. class EditorZoomWidget;
  37. class HScrollBar;
  38. class HSlider;
  39. class Label;
  40. class MenuButton;
  41. class Panel;
  42. class ScrollContainer;
  43. class SpinBox;
  44. class TextureRect;
  45. class ViewPanner;
  46. class VScrollBar;
  47. class Polygon2DEditor : public AbstractPolygon2DEditor {
  48. GDCLASS(Polygon2DEditor, AbstractPolygon2DEditor);
  49. enum {
  50. MENU_POLYGON_TO_UV,
  51. MENU_UV_TO_POLYGON,
  52. MENU_UV_CLEAR,
  53. MENU_GRID_SETTINGS,
  54. };
  55. enum Mode {
  56. MODE_POINTS,
  57. MODE_POLYGONS,
  58. MODE_UV,
  59. MODE_BONES,
  60. MODE_MAX
  61. };
  62. enum Action {
  63. ACTION_CREATE,
  64. ACTION_CREATE_INTERNAL,
  65. ACTION_REMOVE_INTERNAL,
  66. ACTION_EDIT_POINT,
  67. ACTION_MOVE,
  68. ACTION_ROTATE,
  69. ACTION_SCALE,
  70. ACTION_ADD_POLYGON,
  71. ACTION_REMOVE_POLYGON,
  72. ACTION_PAINT_WEIGHT,
  73. ACTION_CLEAR_WEIGHT,
  74. ACTION_MAX
  75. };
  76. Polygon2D *node = nullptr;
  77. Polygon2D *previous_node = nullptr;
  78. Button *dock_button = nullptr;
  79. VBoxContainer *polygon_edit = nullptr;
  80. Mode current_mode = MODE_MAX; // Uninitialized.
  81. Button *mode_buttons[MODE_MAX];
  82. Action selected_action = ACTION_CREATE;
  83. Button *action_buttons[ACTION_MAX];
  84. Button *b_snap_enable = nullptr;
  85. Button *b_snap_grid = nullptr;
  86. MenuButton *edit_menu = nullptr;
  87. Control *canvas = nullptr;
  88. Panel *canvas_background = nullptr;
  89. Polygon2D *preview_polygon = nullptr;
  90. EditorZoomWidget *zoom_widget = nullptr;
  91. HScrollBar *hscroll = nullptr;
  92. VScrollBar *vscroll = nullptr;
  93. bool center_view_on_draw = false;
  94. Ref<ViewPanner> panner;
  95. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  96. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  97. Vector2 draw_offset;
  98. real_t draw_zoom = 1.0;
  99. VBoxContainer *bone_scroll_main_vb = nullptr;
  100. ScrollContainer *bone_scroll = nullptr;
  101. VBoxContainer *bone_scroll_vb = nullptr;
  102. Button *sync_bones = nullptr;
  103. HSlider *bone_paint_strength = nullptr;
  104. SpinBox *bone_paint_radius = nullptr;
  105. Label *bone_paint_radius_label = nullptr;
  106. bool bone_painting = false;
  107. int bone_painting_bone = 0;
  108. Vector<float> prev_weights;
  109. Vector2 bone_paint_pos;
  110. AcceptDialog *grid_settings = nullptr;
  111. void _sync_bones();
  112. void _update_bone_list();
  113. Vector<Vector2> editing_points;
  114. Vector<Vector2> previous_uv;
  115. Vector<Vector2> previous_polygon;
  116. Vector<Color> previous_colors;
  117. int previous_internal_vertices = 0;
  118. Array previous_bones;
  119. Array previous_polygons;
  120. Vector2 create_to;
  121. int point_drag_index = -1;
  122. bool is_dragging = false;
  123. bool is_creating = false;
  124. Vector<int> polygon_create;
  125. Action current_action = ACTION_CREATE;
  126. Vector2 drag_from;
  127. AcceptDialog *error = nullptr;
  128. bool use_snap = false;
  129. bool snap_show_grid = false;
  130. Vector2 snap_offset;
  131. Vector2 snap_step;
  132. void _edit_menu_option(int p_option);
  133. void _cancel_editing();
  134. void _update_polygon_editing_state();
  135. void _update_available_modes();
  136. void _center_view();
  137. void _update_zoom_and_pan(bool p_zoom_at_center);
  138. void _canvas_input(const Ref<InputEvent> &p_input);
  139. void _center_view_on_draw(bool p_enabled = true);
  140. void _canvas_draw();
  141. void _set_action(int p_mode);
  142. void _set_use_snap(bool p_use);
  143. void _set_show_grid(bool p_show);
  144. void _set_snap_off_x(real_t p_val);
  145. void _set_snap_off_y(real_t p_val);
  146. void _set_snap_step_x(real_t p_val);
  147. void _set_snap_step_y(real_t p_val);
  148. void _select_mode(int p_mode);
  149. void _bone_paint_selected(int p_index);
  150. int _get_polygon_count() const override;
  151. protected:
  152. virtual Node2D *_get_node() const override;
  153. virtual void _set_node(Node *p_polygon) override;
  154. virtual Vector2 _get_offset(int p_idx) const override;
  155. virtual bool _has_uv() const override { return true; }
  156. virtual void _commit_action() override;
  157. void _notification(int p_what);
  158. static void _bind_methods();
  159. Vector2 snap_point(Vector2 p_target) const;
  160. public:
  161. Polygon2DEditor();
  162. };
  163. class Polygon2DEditorPlugin : public AbstractPolygon2DEditorPlugin {
  164. GDCLASS(Polygon2DEditorPlugin, AbstractPolygon2DEditorPlugin);
  165. public:
  166. Polygon2DEditorPlugin();
  167. };
  168. #endif // POLYGON_2D_EDITOR_PLUGIN_H