Debian Codegraph @

editor/scene/texture/gradient_texture_2d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gradient_texture_2d_editor_plugin.cpp */
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 #include "gradient_texture_2d_editor_plugin.h"
32
33 #include "editor/editor_undo_redo_manager.h"
34 #include "editor/gui/editor_spin_slider.h"
35 #include "editor/themes/editor_scale.h"
36 #include "scene/gui/button.h"
37 #include "scene/gui/flow_container.h"
38 #include "scene/gui/separator.h"
39 #include "scene/resources/gradient_texture.h"
40
41 Point2 GradientTexture2DEdit::_get_handle_pos(const Handle p_handle) {
42 // Get the handle's mouse position in pixels relative to offset.
43 return (p_handle == HANDLE_FROM ? texture->get_fill_from() : texture->get_fill_to()).clampf(0, 1) * size;
44 }
45
46 GradientTexture2DEdit::Handle GradientTexture2DEdit::get_handle_at(const Vector2 &p_pos) {
47 Point2 from_pos = _get_handle_pos(HANDLE_FROM);
48 Point2 to_pos = _get_handle_pos(HANDLE_TO);
49 // If both handles are at the position, grab the one that's closer.
50 if (p_pos.distance_squared_to(from_pos) < p_pos.distance_squared_to(to_pos)) {
51 return Rect2(from_pos.round() - handle_size / 2, handle_size).has_point(p_pos) ? HANDLE_FROM : HANDLE_NONE;
52 } else {
53 return Rect2(to_pos.round() - handle_size / 2, handle_size).has_point(p_pos) ? HANDLE_TO : HANDLE_NONE;
54 }
55 }
56
57 void GradientTexture2DEdit::set_fill_pos(const Vector2 &p_pos) {
58 if (p_pos.is_equal_approx(initial_grab_pos)) {
59 return;
60 }
61
62 const StringName property_name = (grabbed == HANDLE_FROM) ? "fill_from" : "fill_to";
63 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
64 undo_redo->create_action(TTR("Move GradientTexture2D Fill Point"));
65 undo_redo->add_do_property(texture.ptr(), property_name, p_pos);
66 undo_redo->add_undo_property(texture.ptr(), property_name, initial_grab_pos);
67 undo_redo->commit_action();
68 }
69
70 void GradientTexture2DEdit::gui_input(const Ref<InputEvent> &p_event) {
71 const Ref<InputEventMouseButton> mb = p_event;
72 if (mb.is_valid()) {
73 if (mb->get_button_index() == MouseButton::LEFT) {
74 if (mb->is_pressed()) {
75 grabbed = get_handle_at(mb->get_position() - offset);
76
77 if (grabbed != HANDLE_NONE) {
78 initial_grab_pos = _get_handle_pos(grabbed) / size;
79 queue_redraw();
80 }
81 } else {
82 // Release the handle.
83 if (grabbed != HANDLE_NONE) {
84 set_fill_pos(_get_handle_pos(grabbed) / size);
85 grabbed = HANDLE_NONE;
86 queue_redraw();
87 }
88 }
89 }
90
91 if (grabbed != HANDLE_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
92 texture->set((grabbed == HANDLE_FROM) ? SNAME("fill_from") : SNAME("fill_to"), initial_grab_pos);
93 grabbed = HANDLE_NONE;
94 queue_redraw();
95 }
96 }
97
98 // Move handle.
99 const Ref<InputEventMouseMotion> mm = p_event;
100 if (mm.is_valid()) {
101 Vector2 mpos = mm->get_position() - offset;
102
103 Handle handle_at_mpos = get_handle_at(mpos);
104 if (hovered != handle_at_mpos) {
105 hovered = handle_at_mpos;
106 queue_redraw();
107 }
108
109 if (grabbed == HANDLE_NONE) {
110 return;
111 }
112
113 Vector2 new_pos = (mpos / size).clampf(0, 1);
114 if (snap_enabled || mm->is_command_or_control_pressed()) {
115 new_pos = new_pos.snappedf(1.0 / snap_count);
116 }
117
118 // Allow to snap to an axis with Shift.
119 if (mm->is_shift_pressed()) {
120 Vector2 initial_mpos = initial_grab_pos * size;
121 if (Math::abs(mpos.x - initial_mpos.x) > Math::abs(mpos.y - initial_mpos.y)) {
122 new_pos.y = initial_grab_pos.y;
123 } else {
124 new_pos.x = initial_grab_pos.x;
125 }
126 }
127 // Do it directly from the texture so there's no undo/redo until the handle is released.
128 texture->set((grabbed == HANDLE_FROM) ? SNAME("fill_from") : SNAME("fill_to"), new_pos);
129 }
130 }
131
132 void GradientTexture2DEdit::set_texture(Ref<GradientTexture2D> &p_texture) {
133 texture = p_texture;
134 texture->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
135 }
136
137 void GradientTexture2DEdit::set_snap_enabled(bool p_snap_enabled) {
138 snap_enabled = p_snap_enabled;
139 queue_redraw();
140 if (texture.is_valid()) {
141 if (snap_enabled) {
142 texture->set_meta(SNAME("_snap_enabled"), true);
143 } else {
144 texture->remove_meta(SNAME("_snap_enabled"));
145 }
146 }
147 }
148
149 void GradientTexture2DEdit::set_snap_count(int p_snap_count) {
150 snap_count = p_snap_count;
151 queue_redraw();
152 if (texture.is_valid()) {
153 if (snap_count != GradientTexture2DEditor::DEFAULT_SNAP) {
154 texture->set_meta(SNAME("_snap_count"), snap_count);
155 } else {
156 texture->remove_meta(SNAME("_snap_count"));
157 }
158 }
159 }
160
161 void GradientTexture2DEdit::_notification(int p_what) {
162 switch (p_what) {
163 case NOTIFICATION_MOUSE_EXIT: {
164 if (hovered != HANDLE_NONE) {
165 hovered = HANDLE_NONE;
166 queue_redraw();
167 }
168 } break;
169 case NOTIFICATION_THEME_CHANGED: {
170 checkerboard->set_texture(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")));
171 } break;
172 case NOTIFICATION_DRAW: {
173 _draw();
174 } break;
175 }
176 }
177
178 void GradientTexture2DEdit::_draw() {
179 if (texture.is_null()) {
180 return;
181 }
182
183 const Ref<Texture2D> fill_from_icon = get_editor_theme_icon(SNAME("EditorPathSmoothHandle"));
184 const Ref<Texture2D> fill_to_icon = get_editor_theme_icon(SNAME("EditorPathSharpHandle"));
185 handle_size = fill_from_icon->get_size();
186
187 Size2 rect_size = get_size();
188
189 // Get the size and position to draw the texture and handles at.
190 // Subtract handle sizes so they stay inside the preview, but keep the texture's aspect ratio.
191 Size2 available_size = rect_size - handle_size;
192 Size2 ratio = available_size / texture->get_size();
193 size = MIN(ratio.x, ratio.y) * texture->get_size();
194 offset = ((rect_size - size) / 2).round();
195
196 checkerboard->set_rect(Rect2(offset, size));
197
198 draw_set_transform(offset);
199 draw_texture_rect(texture, Rect2(Point2(), size));
200
201 // Draw grid snap lines.
202 if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL) && grabbed != HANDLE_NONE)) {
203 const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
204
205 for (int idx = 0; idx < snap_count + 1; idx++) {
206 float x = float(idx * size.width) / snap_count;
207 float y = float(idx * size.height) / snap_count;
208 draw_line(Point2(x, 0), Point2(x, size.height), line_color);
209 draw_line(Point2(0, y), Point2(size.width, y), line_color);
210 }
211 }
212
213 // Draw handles.
214 const Color focus_modulate = Color(0.4, 1, 1);
215 bool modulate_handle_from = grabbed == HANDLE_FROM || hovered == HANDLE_FROM;
216 bool modulate_handle_to = grabbed == HANDLE_TO || hovered == HANDLE_TO;
217 draw_texture(fill_from_icon, (_get_handle_pos(HANDLE_FROM) - handle_size / 2).round(), modulate_handle_from ? focus_modulate : Color(1, 1, 1));
218 draw_texture(fill_to_icon, (_get_handle_pos(HANDLE_TO) - handle_size / 2).round(), modulate_handle_to ? focus_modulate : Color(1, 1, 1));
219 }
220
221 GradientTexture2DEdit::GradientTexture2DEdit() {
222 checkerboard = memnew(TextureRect);
223 checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
224 checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
225 checkerboard->set_draw_behind_parent(true);
226 add_child(checkerboard, false, INTERNAL_MODE_FRONT);
227
228 set_custom_minimum_size(Size2(0, 250 * EDSCALE));
229 }
230
231 ///////////////////////
232
233 const int GradientTexture2DEditor::DEFAULT_SNAP = 10;
234
235 void GradientTexture2DEditor::_reverse_button_pressed() {
236 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
237 undo_redo->create_action(TTR("Swap GradientTexture2D Fill Points"));
238 undo_redo->add_do_property(texture.ptr(), "fill_from", texture->get_fill_to());
239 undo_redo->add_do_property(texture.ptr(), "fill_to", texture->get_fill_from());
240 undo_redo->add_undo_property(texture.ptr(), "fill_from", texture->get_fill_from());
241 undo_redo->add_undo_property(texture.ptr(), "fill_to", texture->get_fill_to());
242 undo_redo->commit_action();
243 }
244
245 void GradientTexture2DEditor::_set_snap_enabled(bool p_enabled) {
246 texture_editor_rect->set_snap_enabled(p_enabled);
247 snap_count_edit->set_visible(p_enabled);
248 }
249
250 void GradientTexture2DEditor::_set_snap_count(int p_snap_count) {
251 texture_editor_rect->set_snap_count(p_snap_count);
252 }
253
254 void GradientTexture2DEditor::set_texture(Ref<GradientTexture2D> &p_texture) {
255 texture = p_texture;
256 texture_editor_rect->set_texture(p_texture);
257 }
258
259 void GradientTexture2DEditor::_notification(int p_what) {
260 switch (p_what) {
261 case NOTIFICATION_THEME_CHANGED: {
262 reverse_button->set_button_icon(get_editor_theme_icon(SNAME("ReverseGradient")));
263 snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
264 } break;
265 case NOTIFICATION_READY: {
266 if (texture.is_valid()) {
267 // Set snapping settings based on the texture's meta.
268 snap_button->set_pressed(texture->get_meta("_snap_enabled", false));
269 snap_count_edit->set_value(texture->get_meta("_snap_count", DEFAULT_SNAP));
270 }
271 } break;
272 }
273 }
274
275 GradientTexture2DEditor::GradientTexture2DEditor() {
276 HFlowContainer *toolbar = memnew(HFlowContainer);
277 add_child(toolbar);
278
279 reverse_button = memnew(Button);
280 reverse_button->set_tooltip_text(TTR("Swap Gradient Fill Points"));
281 toolbar->add_child(reverse_button);
282 reverse_button->connect(SceneStringName(pressed), callable_mp(this, &GradientTexture2DEditor::_reverse_button_pressed));
283
284 toolbar->add_child(memnew(VSeparator));
285
286 snap_button = memnew(Button);
287 snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));
288 snap_button->set_toggle_mode(true);
289 toolbar->add_child(snap_button);
290 snap_button->connect(SceneStringName(toggled), callable_mp(this, &GradientTexture2DEditor::_set_snap_enabled));
291
292 snap_count_edit = memnew(EditorSpinSlider);
293 snap_count_edit->set_min(2);
294 snap_count_edit->set_max(100);
295 snap_count_edit->set_value(DEFAULT_SNAP);
296 snap_count_edit->set_accessibility_name(TTRC("Grid Step"));
297 snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
298 toolbar->add_child(snap_count_edit);
299 snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &GradientTexture2DEditor::_set_snap_count));
300
301 texture_editor_rect = memnew(GradientTexture2DEdit);
302 add_child(texture_editor_rect);
303
304 set_mouse_filter(MOUSE_FILTER_STOP);
305 _set_snap_enabled(snap_button->is_pressed());
306 _set_snap_count(snap_count_edit->get_value());
307 }
308
309 ///////////////////////
310
311 bool EditorInspectorPluginGradientTexture2D::can_handle(Object *p_object) {
312 return Object::cast_to<GradientTexture2D>(p_object) != nullptr;
313 }
314
315 void EditorInspectorPluginGradientTexture2D::parse_begin(Object *p_object) {
316 GradientTexture2D *texture = Object::cast_to<GradientTexture2D>(p_object);
317 if (!texture) {
318 return;
319 }
320 Ref<GradientTexture2D> t(texture);
321
322 GradientTexture2DEditor *editor = memnew(GradientTexture2DEditor);
323 editor->set_texture(t);
324 add_custom_control(editor);
325 }
326
327 ///////////////////////
328
329 GradientTexture2DEditorPlugin::GradientTexture2DEditorPlugin() {
330 Ref<EditorInspectorPluginGradientTexture2D> plugin;
331 plugin.instantiate();
332 add_inspector_plugin(plugin);
333 }