texture_region_editor_plugin.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "canvas_item_editor_plugin.h"
  33. #include "editor/editor_inspector.h"
  34. #include "editor/editor_plugin.h"
  35. #include "scene/2d/sprite_2d.h"
  36. #include "scene/3d/sprite_3d.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/nine_patch_rect.h"
  39. #include "scene/resources/style_box.h"
  40. class AtlasTexture;
  41. class OptionButton;
  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. Panel *edit_draw = nullptr;
  64. VScrollBar *vscroll = nullptr;
  65. HScrollBar *hscroll = nullptr;
  66. Vector2 draw_ofs;
  67. float draw_zoom = 0.0;
  68. bool updating_scroll = false;
  69. int snap_mode = 0;
  70. Vector2 snap_offset;
  71. Vector2 snap_step;
  72. Vector2 snap_separation;
  73. Sprite2D *node_sprite_2d = nullptr;
  74. Sprite3D *node_sprite_3d = nullptr;
  75. NinePatchRect *node_ninepatch = nullptr;
  76. Ref<StyleBoxTexture> obj_styleBox;
  77. Ref<AtlasTexture> atlas_tex;
  78. Ref<CanvasTexture> preview_tex;
  79. Rect2 rect;
  80. Rect2 rect_prev;
  81. float prev_margin = 0.0f;
  82. int edited_margin = 0;
  83. HashMap<RID, List<Rect2>> cache_map;
  84. List<Rect2> autoslice_cache;
  85. bool autoslice_is_dirty = false;
  86. bool drag = false;
  87. bool creating = false;
  88. Vector2 drag_from;
  89. int drag_index = 0;
  90. bool request_center = false;
  91. Ref<ViewPanner> panner;
  92. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  93. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  94. void _set_snap_mode(int p_mode);
  95. void _set_snap_off_x(float p_val);
  96. void _set_snap_off_y(float p_val);
  97. void _set_snap_step_x(float p_val);
  98. void _set_snap_step_y(float p_val);
  99. void _set_snap_sep_x(float p_val);
  100. void _set_snap_sep_y(float p_val);
  101. void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
  102. void _zoom_in();
  103. void _zoom_reset();
  104. void _zoom_out();
  105. void apply_rect(const Rect2 &p_rect);
  106. void _update_rect();
  107. void _update_autoslice();
  108. void _texture_changed();
  109. protected:
  110. void _notification(int p_what);
  111. void _node_removed(Object *p_obj);
  112. static void _bind_methods();
  113. Vector2 snap_point(Vector2 p_target) const;
  114. public:
  115. void _edit_region();
  116. void _region_draw();
  117. void _region_input(const Ref<InputEvent> &p_input);
  118. void _scroll_changed(float);
  119. bool is_stylebox();
  120. bool is_atlas_texture();
  121. bool is_ninepatch();
  122. Sprite2D *get_sprite_2d();
  123. Sprite3D *get_sprite_3d();
  124. void edit(Object *p_obj);
  125. TextureRegionEditor();
  126. };
  127. //
  128. class EditorInspectorPluginTextureRegion : public EditorInspectorPlugin {
  129. GDCLASS(EditorInspectorPluginTextureRegion, EditorInspectorPlugin);
  130. TextureRegionEditor *texture_region_editor = nullptr;
  131. void _region_edit(Object *p_object);
  132. public:
  133. virtual bool can_handle(Object *p_object) override;
  134. 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;
  135. EditorInspectorPluginTextureRegion();
  136. };
  137. class TextureRegionEditorPlugin : public EditorPlugin {
  138. GDCLASS(TextureRegionEditorPlugin, EditorPlugin);
  139. public:
  140. virtual String get_name() const override { return "TextureRegion"; }
  141. TextureRegionEditorPlugin();
  142. };
  143. #endif // TEXTURE_REGION_EDITOR_PLUGIN_H