texture_region_editor_plugin.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef TEXTURE_REGION_EDITOR_PLUGIN_H
  31. #define TEXTURE_REGION_EDITOR_PLUGIN_H
  32. #include "canvas_item_editor_plugin.h"
  33. #include "editor/editor_plugin.h"
  34. #include "scene/2d/sprite_2d.h"
  35. #include "scene/3d/sprite_3d.h"
  36. #include "scene/gui/nine_patch_rect.h"
  37. #include "scene/resources/style_box.h"
  38. #include "scene/resources/texture.h"
  39. class ViewPanner;
  40. class EditorUndoRedoManager;
  41. class TextureRegionEditor : public AcceptDialog {
  42. GDCLASS(TextureRegionEditor, AcceptDialog);
  43. enum SnapMode {
  44. SNAP_NONE,
  45. SNAP_PIXEL,
  46. SNAP_GRID,
  47. SNAP_AUTOSLICE
  48. };
  49. friend class TextureRegionEditorPlugin;
  50. OptionButton *snap_mode_button = nullptr;
  51. Button *zoom_in = nullptr;
  52. Button *zoom_reset = nullptr;
  53. Button *zoom_out = nullptr;
  54. HBoxContainer *hb_grid = nullptr; //For showing/hiding the grid controls when changing the SnapMode
  55. SpinBox *sb_step_y = nullptr;
  56. SpinBox *sb_step_x = nullptr;
  57. SpinBox *sb_off_y = nullptr;
  58. SpinBox *sb_off_x = nullptr;
  59. SpinBox *sb_sep_y = nullptr;
  60. SpinBox *sb_sep_x = nullptr;
  61. Panel *edit_draw = nullptr;
  62. VScrollBar *vscroll = nullptr;
  63. HScrollBar *hscroll = nullptr;
  64. Ref<EditorUndoRedoManager> undo_redo;
  65. Vector2 draw_ofs;
  66. float draw_zoom = 0.0;
  67. bool updating_scroll = false;
  68. int snap_mode = 0;
  69. Vector2 snap_offset;
  70. Vector2 snap_step;
  71. Vector2 snap_separation;
  72. Sprite2D *node_sprite_2d = nullptr;
  73. Sprite3D *node_sprite_3d = nullptr;
  74. NinePatchRect *node_ninepatch = nullptr;
  75. Ref<StyleBoxTexture> obj_styleBox;
  76. Ref<AtlasTexture> atlas_tex;
  77. Rect2 rect;
  78. Rect2 rect_prev;
  79. float prev_margin = 0.0f;
  80. int edited_margin = 0;
  81. HashMap<RID, List<Rect2>> cache_map;
  82. List<Rect2> autoslice_cache;
  83. bool autoslice_is_dirty = false;
  84. bool drag = false;
  85. bool creating = false;
  86. Vector2 drag_from;
  87. int drag_index = 0;
  88. bool request_center = false;
  89. Ref<ViewPanner> panner;
  90. void _scroll_callback(Vector2 p_scroll_vec, bool p_alt);
  91. void _pan_callback(Vector2 p_scroll_vec);
  92. void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt);
  93. void _set_snap_mode(int p_mode);
  94. void _set_snap_off_x(float p_val);
  95. void _set_snap_off_y(float p_val);
  96. void _set_snap_step_x(float p_val);
  97. void _set_snap_step_y(float p_val);
  98. void _set_snap_sep_x(float p_val);
  99. void _set_snap_sep_y(float p_val);
  100. void _zoom_on_position(float p_zoom, Point2 p_position = Point2());
  101. void _zoom_in();
  102. void _zoom_reset();
  103. void _zoom_out();
  104. void apply_rect(const Rect2 &p_rect);
  105. void _update_rect();
  106. void _update_autoslice();
  107. void _texture_changed();
  108. protected:
  109. void _notification(int p_what);
  110. void _node_removed(Object *p_obj);
  111. static void _bind_methods();
  112. Vector2 snap_point(Vector2 p_target) const;
  113. public:
  114. void _edit_region();
  115. void _region_draw();
  116. void _region_input(const Ref<InputEvent> &p_input);
  117. void _scroll_changed(float);
  118. bool is_stylebox();
  119. bool is_atlas_texture();
  120. bool is_ninepatch();
  121. Sprite2D *get_sprite_2d();
  122. Sprite3D *get_sprite_3d();
  123. void edit(Object *p_obj);
  124. TextureRegionEditor();
  125. };
  126. //
  127. class EditorInspectorPluginTextureRegion : public EditorInspectorPlugin {
  128. GDCLASS(EditorInspectorPluginTextureRegion, EditorInspectorPlugin);
  129. TextureRegionEditor *texture_region_editor = nullptr;
  130. void _region_edit(Object *p_object);
  131. public:
  132. virtual bool can_handle(Object *p_object) override;
  133. 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 uint32_t p_usage, const bool p_wide) override;
  134. EditorInspectorPluginTextureRegion();
  135. };
  136. class TextureRegionEditorPlugin : public EditorPlugin {
  137. GDCLASS(TextureRegionEditorPlugin, EditorPlugin);
  138. public:
  139. virtual String get_name() const override { return "TextureRegion"; }
  140. TextureRegionEditorPlugin();
  141. };
  142. #endif // TEXTURE_REGION_EDITOR_PLUGIN_H