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