texture_region_editor_plugin.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* texture_region_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 TEXTURE_REGION_EDITOR_PLUGIN_H
  31. #define TEXTURE_REGION_EDITOR_PLUGIN_H
  32. #include "editor/editor_inspector.h"
  33. #include "editor/plugins/editor_plugin.h"
  34. #include "scene/gui/dialogs.h"
  35. class AtlasTexture;
  36. class NinePatchRect;
  37. class OptionButton;
  38. class PanelContainer;
  39. class Sprite2D;
  40. class Sprite3D;
  41. class StyleBoxTexture;
  42. class ViewPanner;
  43. class TextureRegionEditor : public AcceptDialog {
  44. GDCLASS(TextureRegionEditor, AcceptDialog);
  45. enum SnapMode {
  46. SNAP_NONE,
  47. SNAP_PIXEL,
  48. SNAP_GRID,
  49. SNAP_AUTOSLICE
  50. };
  51. friend class TextureRegionEditorPlugin;
  52. OptionButton *snap_mode_button = nullptr;
  53. Button *zoom_in = nullptr;
  54. Button *zoom_reset = nullptr;
  55. Button *zoom_out = nullptr;
  56. HBoxContainer *hb_grid = nullptr; //For showing/hiding the grid controls when changing the SnapMode
  57. SpinBox *sb_step_y = nullptr;
  58. SpinBox *sb_step_x = nullptr;
  59. SpinBox *sb_off_y = nullptr;
  60. SpinBox *sb_off_x = nullptr;
  61. SpinBox *sb_sep_y = nullptr;
  62. SpinBox *sb_sep_x = nullptr;
  63. PanelContainer *texture_preview = nullptr;
  64. Panel *texture_overlay = nullptr;
  65. VScrollBar *vscroll = nullptr;
  66. HScrollBar *hscroll = nullptr;
  67. Vector2 draw_ofs;
  68. float draw_zoom = 1.0;
  69. float min_draw_zoom = 1.0;
  70. float max_draw_zoom = 1.0;
  71. bool updating_scroll = false;
  72. SnapMode snap_mode = SNAP_NONE;
  73. Vector2 snap_offset;
  74. Vector2 snap_step;
  75. Vector2 snap_separation;
  76. Sprite2D *node_sprite_2d = nullptr;
  77. Sprite3D *node_sprite_3d = nullptr;
  78. NinePatchRect *node_ninepatch = nullptr;
  79. Ref<StyleBoxTexture> res_stylebox;
  80. Ref<AtlasTexture> res_atlas_texture;
  81. Rect2 rect;
  82. Rect2 rect_prev;
  83. float prev_margin = 0.0f;
  84. int edited_margin = -1;
  85. HashMap<RID, List<Rect2>> cache_map;
  86. List<Rect2> autoslice_cache;
  87. bool autoslice_is_dirty = true;
  88. bool drag = false;
  89. bool creating = false;
  90. Vector2 drag_from;
  91. int drag_index = -1;
  92. bool request_center = false;
  93. Ref<ViewPanner> panner;
  94. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  95. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  96. void _scroll_changed(float);
  97. Transform2D _get_offset_transform() const;
  98. void _set_snap_mode(int p_mode);
  99. void _set_snap_off_x(float p_val);
  100. void _set_snap_off_y(float p_val);
  101. void _set_snap_step_x(float p_val);
  102. void _set_snap_step_y(float p_val);
  103. void _set_snap_sep_x(float p_val);
  104. void _set_snap_sep_y(float p_val);
  105. void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
  106. void _zoom_in();
  107. void _zoom_reset();
  108. void _zoom_out();
  109. void _apply_rect(const Rect2 &p_rect);
  110. void _update_rect();
  111. void _update_autoslice();
  112. Ref<Texture2D> _get_edited_object_texture() const;
  113. Rect2 _get_edited_object_region() const;
  114. void _texture_changed();
  115. void _node_removed(Node *p_node);
  116. void _edit_region();
  117. void _clear_edited_object();
  118. void _draw_margin_line(Vector2 p_from, Vector2 p_to);
  119. void _set_grid_parameters_clamping(bool p_enabled);
  120. protected:
  121. void _notification(int p_what);
  122. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  123. static void _bind_methods();
  124. void _texture_preview_draw();
  125. void _texture_overlay_draw();
  126. void _texture_overlay_input(const Ref<InputEvent> &p_input);
  127. Vector2 snap_point(Vector2 p_target) const;
  128. public:
  129. void edit(Object *p_obj);
  130. TextureRegionEditor();
  131. };
  132. //
  133. class EditorInspectorPluginTextureRegion : public EditorInspectorPlugin {
  134. GDCLASS(EditorInspectorPluginTextureRegion, EditorInspectorPlugin);
  135. TextureRegionEditor *texture_region_editor = nullptr;
  136. void _region_edit(Object *p_object);
  137. public:
  138. virtual bool can_handle(Object *p_object) override;
  139. virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) override;
  140. EditorInspectorPluginTextureRegion();
  141. };
  142. class TextureRegionEditorPlugin : public EditorPlugin {
  143. GDCLASS(TextureRegionEditorPlugin, EditorPlugin);
  144. public:
  145. virtual String get_plugin_name() const override { return "TextureRegion"; }
  146. TextureRegionEditorPlugin();
  147. };
  148. #endif // TEXTURE_REGION_EDITOR_PLUGIN_H