1 | /**************************************************************************/ |
2 | /* gradient_editor.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_editor.h" |
32 | |
33 | #include "core/os/keyboard.h" |
34 | #include "editor/editor_node.h" |
35 | #include "editor/editor_scale.h" |
36 | #include "editor/editor_string_names.h" |
37 | #include "editor/editor_undo_redo_manager.h" |
38 | #include "scene/resources/gradient_texture.h" |
39 | |
40 | void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) { |
41 | gradient = p_gradient; |
42 | connect("ramp_changed" , callable_mp(this, &GradientEditor::_ramp_changed)); |
43 | gradient->connect_changed(callable_mp(this, &GradientEditor::_gradient_changed)); |
44 | set_points(gradient->get_points()); |
45 | set_interpolation_mode(gradient->get_interpolation_mode()); |
46 | set_interpolation_color_space(gradient->get_interpolation_color_space()); |
47 | } |
48 | |
49 | void GradientEditor::reverse_gradient() { |
50 | gradient->reverse(); |
51 | set_points(gradient->get_points()); |
52 | emit_signal(SNAME("ramp_changed" )); |
53 | queue_redraw(); |
54 | } |
55 | |
56 | int GradientEditor::_get_point_from_pos(int x) { |
57 | int result = -1; |
58 | int total_w = get_size().width - get_size().height - draw_spacing - handle_width; |
59 | float min_distance = 1e20; |
60 | for (int i = 0; i < points.size(); i++) { |
61 | // Check if we clicked at point. |
62 | float distance = ABS(x - points[i].offset * total_w); |
63 | float min = handle_width * 0.85; // Allow the mouse to be more than half a handle width away for ease of grabbing. |
64 | if (distance <= min && distance < min_distance) { |
65 | result = i; |
66 | min_distance = distance; |
67 | } |
68 | } |
69 | return result; |
70 | } |
71 | |
72 | void GradientEditor::_show_color_picker() { |
73 | if (grabbed == -1) { |
74 | return; |
75 | } |
76 | picker->set_pick_color(points[grabbed].color); |
77 | Size2 minsize = popup->get_contents_minimum_size(); |
78 | bool show_above = false; |
79 | if (get_global_position().y + get_size().y + minsize.y > get_viewport_rect().size.y) { |
80 | show_above = true; |
81 | } |
82 | if (show_above) { |
83 | popup->set_position(get_screen_position() - Vector2(0, minsize.y)); |
84 | } else { |
85 | popup->set_position(get_screen_position() + Vector2(0, get_size().y)); |
86 | } |
87 | popup->popup(); |
88 | } |
89 | |
90 | void GradientEditor::_gradient_changed() { |
91 | if (editing) { |
92 | return; |
93 | } |
94 | |
95 | editing = true; |
96 | Vector<Gradient::Point> grad_points = gradient->get_points(); |
97 | set_points(grad_points); |
98 | set_interpolation_mode(gradient->get_interpolation_mode()); |
99 | set_interpolation_color_space(gradient->get_interpolation_color_space()); |
100 | queue_redraw(); |
101 | editing = false; |
102 | } |
103 | |
104 | void GradientEditor::_ramp_changed() { |
105 | editing = true; |
106 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
107 | undo_redo->create_action(TTR("Gradient Edited" ), UndoRedo::MERGE_ENDS); |
108 | undo_redo->add_do_method(gradient.ptr(), "set_offsets" , get_offsets()); |
109 | undo_redo->add_do_method(gradient.ptr(), "set_colors" , get_colors()); |
110 | undo_redo->add_do_method(gradient.ptr(), "set_interpolation_mode" , get_interpolation_mode()); |
111 | undo_redo->add_do_method(gradient.ptr(), "set_interpolation_color_space" , get_interpolation_color_space()); |
112 | undo_redo->add_undo_method(gradient.ptr(), "set_offsets" , gradient->get_offsets()); |
113 | undo_redo->add_undo_method(gradient.ptr(), "set_colors" , gradient->get_colors()); |
114 | undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_mode" , gradient->get_interpolation_mode()); |
115 | undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_color_space" , gradient->get_interpolation_color_space()); |
116 | undo_redo->commit_action(); |
117 | editing = false; |
118 | } |
119 | |
120 | void GradientEditor::_color_changed(const Color &p_color) { |
121 | if (grabbed == -1) { |
122 | return; |
123 | } |
124 | points.write[grabbed].color = p_color; |
125 | queue_redraw(); |
126 | emit_signal(SNAME("ramp_changed" )); |
127 | } |
128 | |
129 | void GradientEditor::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) { |
130 | ERR_FAIL_COND(p_offsets.size() != p_colors.size()); |
131 | points.clear(); |
132 | for (int i = 0; i < p_offsets.size(); i++) { |
133 | Gradient::Point p; |
134 | p.offset = p_offsets[i]; |
135 | p.color = p_colors[i]; |
136 | points.push_back(p); |
137 | } |
138 | |
139 | points.sort(); |
140 | queue_redraw(); |
141 | } |
142 | |
143 | Vector<float> GradientEditor::get_offsets() const { |
144 | Vector<float> ret; |
145 | for (int i = 0; i < points.size(); i++) { |
146 | ret.push_back(points[i].offset); |
147 | } |
148 | return ret; |
149 | } |
150 | |
151 | Vector<Color> GradientEditor::get_colors() const { |
152 | Vector<Color> ret; |
153 | for (int i = 0; i < points.size(); i++) { |
154 | ret.push_back(points[i].color); |
155 | } |
156 | return ret; |
157 | } |
158 | |
159 | void GradientEditor::set_points(Vector<Gradient::Point> &p_points) { |
160 | if (points.size() != p_points.size()) { |
161 | grabbed = -1; |
162 | } |
163 | points.clear(); |
164 | points = p_points; |
165 | points.sort(); |
166 | } |
167 | |
168 | Vector<Gradient::Point> &GradientEditor::get_points() { |
169 | return points; |
170 | } |
171 | |
172 | void GradientEditor::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) { |
173 | interpolation_mode = p_interp_mode; |
174 | } |
175 | |
176 | Gradient::InterpolationMode GradientEditor::get_interpolation_mode() { |
177 | return interpolation_mode; |
178 | } |
179 | |
180 | void GradientEditor::set_interpolation_color_space(Gradient::ColorSpace p_color_space) { |
181 | interpolation_color_space = p_color_space; |
182 | } |
183 | |
184 | Gradient::ColorSpace GradientEditor::get_interpolation_color_space() { |
185 | return interpolation_color_space; |
186 | } |
187 | |
188 | ColorPicker *GradientEditor::get_picker() { |
189 | return picker; |
190 | } |
191 | |
192 | PopupPanel *GradientEditor::() { |
193 | return popup; |
194 | } |
195 | |
196 | Size2 GradientEditor::get_minimum_size() const { |
197 | return Size2(0, 60) * EDSCALE; |
198 | } |
199 | |
200 | void GradientEditor::gui_input(const Ref<InputEvent> &p_event) { |
201 | ERR_FAIL_COND(p_event.is_null()); |
202 | |
203 | Ref<InputEventKey> k = p_event; |
204 | |
205 | if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && grabbed != -1) { |
206 | points.remove_at(grabbed); |
207 | grabbed = -1; |
208 | grabbing = false; |
209 | queue_redraw(); |
210 | emit_signal(SNAME("ramp_changed" )); |
211 | accept_event(); |
212 | } |
213 | |
214 | Ref<InputEventMouseButton> mb = p_event; |
215 | |
216 | if (mb.is_valid() && mb->is_pressed()) { |
217 | float adjusted_mb_x = mb->get_position().x - handle_width / 2; |
218 | |
219 | // Delete point on right click. |
220 | if (mb->get_button_index() == MouseButton::RIGHT) { |
221 | grabbed = _get_point_from_pos(adjusted_mb_x); |
222 | if (grabbed != -1) { |
223 | points.remove_at(grabbed); |
224 | grabbed = -1; |
225 | grabbing = false; |
226 | queue_redraw(); |
227 | emit_signal(SNAME("ramp_changed" )); |
228 | accept_event(); |
229 | } |
230 | } |
231 | |
232 | // Hold Alt key to duplicate selected color. |
233 | if (mb->get_button_index() == MouseButton::LEFT && mb->is_alt_pressed()) { |
234 | grabbed = _get_point_from_pos(adjusted_mb_x); |
235 | |
236 | if (grabbed != -1) { |
237 | int total_w = get_size().width - get_size().height - draw_spacing - handle_width; |
238 | Gradient::Point new_point = points[grabbed]; |
239 | new_point.offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1); |
240 | points.push_back(new_point); |
241 | points.sort(); |
242 | for (int i = 0; i < points.size(); ++i) { |
243 | if (points[i].offset == new_point.offset) { |
244 | grabbed = i; |
245 | break; |
246 | } |
247 | } |
248 | |
249 | emit_signal(SNAME("ramp_changed" )); |
250 | queue_redraw(); |
251 | } |
252 | } |
253 | |
254 | // Select. |
255 | if (mb->get_button_index() == MouseButton::LEFT) { |
256 | queue_redraw(); |
257 | int total_w = get_size().width - get_size().height - draw_spacing - handle_width; |
258 | |
259 | // Check if color selector was clicked or ramp was double-clicked. |
260 | if (adjusted_mb_x > total_w + draw_spacing) { |
261 | if (!mb->is_double_click()) { |
262 | _show_color_picker(); |
263 | } |
264 | return; |
265 | } else if (mb->is_double_click()) { |
266 | grabbed = _get_point_from_pos(adjusted_mb_x); |
267 | _show_color_picker(); |
268 | accept_event(); |
269 | return; |
270 | } |
271 | |
272 | grabbing = true; |
273 | grabbed = _get_point_from_pos(adjusted_mb_x); |
274 | |
275 | // Grab or select. |
276 | if (grabbed != -1) { |
277 | return; |
278 | } |
279 | |
280 | // Insert point. |
281 | Gradient::Point new_point; |
282 | new_point.offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1); |
283 | new_point.color = gradient->get_color_at_offset(new_point.offset); |
284 | |
285 | points.push_back(new_point); |
286 | points.sort(); |
287 | for (int i = 0; i < points.size(); i++) { |
288 | if (points[i].offset == new_point.offset) { |
289 | grabbed = i; |
290 | break; |
291 | } |
292 | } |
293 | |
294 | emit_signal(SNAME("ramp_changed" )); |
295 | } |
296 | } |
297 | |
298 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) { |
299 | if (grabbing) { |
300 | grabbing = false; |
301 | emit_signal(SNAME("ramp_changed" )); |
302 | } |
303 | queue_redraw(); |
304 | } |
305 | |
306 | Ref<InputEventMouseMotion> mm = p_event; |
307 | |
308 | if (mm.is_valid() && grabbing) { |
309 | float adjusted_mm_x = mm->get_position().x - handle_width / 2; |
310 | int total_w = get_size().width - get_size().height - draw_spacing - handle_width; |
311 | float newofs = CLAMP(adjusted_mm_x / float(total_w), 0, 1); |
312 | |
313 | // Snap to "round" coordinates if holding Ctrl. |
314 | // Be more precise if holding Shift as well. |
315 | if (mm->is_ctrl_pressed()) { |
316 | newofs = Math::snapped(newofs, mm->is_shift_pressed() ? 0.025 : 0.1); |
317 | } else if (mm->is_shift_pressed()) { |
318 | // Snap to nearest point if holding just Shift. |
319 | const float snap_threshold = 0.03; |
320 | float smallest_ofs = snap_threshold; |
321 | bool found = false; |
322 | int nearest_point = 0; |
323 | for (int i = 0; i < points.size(); ++i) { |
324 | if (i != grabbed) { |
325 | float temp_ofs = ABS(points[i].offset - newofs); |
326 | if (temp_ofs < smallest_ofs) { |
327 | smallest_ofs = temp_ofs; |
328 | nearest_point = i; |
329 | if (found) { |
330 | break; |
331 | } |
332 | found = true; |
333 | } |
334 | } |
335 | } |
336 | if (found) { |
337 | if (points[nearest_point].offset < newofs) { |
338 | newofs = points[nearest_point].offset + 0.00001; |
339 | } else { |
340 | newofs = points[nearest_point].offset - 0.00001; |
341 | } |
342 | newofs = CLAMP(newofs, 0, 1); |
343 | } |
344 | } |
345 | |
346 | bool valid = true; |
347 | for (int i = 0; i < points.size(); i++) { |
348 | if (points[i].offset == newofs && i != grabbed) { |
349 | valid = false; |
350 | break; |
351 | } |
352 | } |
353 | |
354 | if (!valid || grabbed == -1) { |
355 | return; |
356 | } |
357 | points.write[grabbed].offset = newofs; |
358 | |
359 | points.sort(); |
360 | for (int i = 0; i < points.size(); i++) { |
361 | if (points[i].offset == newofs) { |
362 | grabbed = i; |
363 | break; |
364 | } |
365 | } |
366 | |
367 | emit_signal(SNAME("ramp_changed" )); |
368 | |
369 | queue_redraw(); |
370 | } |
371 | } |
372 | |
373 | void GradientEditor::_notification(int p_what) { |
374 | switch (p_what) { |
375 | case NOTIFICATION_ENTER_TREE: { |
376 | if (!picker->is_connected("color_changed" , callable_mp(this, &GradientEditor::_color_changed))) { |
377 | picker->connect("color_changed" , callable_mp(this, &GradientEditor::_color_changed)); |
378 | } |
379 | [[fallthrough]]; |
380 | } |
381 | case NOTIFICATION_THEME_CHANGED: { |
382 | draw_spacing = BASE_SPACING * get_theme_default_base_scale(); |
383 | handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale(); |
384 | } break; |
385 | |
386 | case NOTIFICATION_DRAW: { |
387 | int w = get_size().x; |
388 | int h = get_size().y; |
389 | |
390 | if (w == 0 || h == 0) { |
391 | return; // Safety check. We have division by 'h'. And in any case there is nothing to draw with such size. |
392 | } |
393 | |
394 | int total_w = get_size().width - get_size().height - draw_spacing - handle_width; |
395 | |
396 | // Draw checker pattern for ramp. |
397 | draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard" )), Rect2(handle_width / 2, 0, total_w, h), true); |
398 | |
399 | // Draw color ramp. |
400 | gradient_cache->set_points(points); |
401 | gradient_cache->set_interpolation_mode(interpolation_mode); |
402 | gradient_cache->set_interpolation_color_space(interpolation_color_space); |
403 | preview_texture->set_gradient(gradient_cache); |
404 | draw_texture_rect(preview_texture, Rect2(handle_width / 2, 0, total_w, h)); |
405 | |
406 | // Draw borders around color ramp if in focus. |
407 | if (has_focus()) { |
408 | draw_rect(Rect2(handle_width / 2, 0, total_w, h), Color(1, 1, 1, 0.9), false, 1); |
409 | } |
410 | |
411 | // Draw point markers. |
412 | for (int i = 0; i < points.size(); i++) { |
413 | Color col = points[i].color.get_v() > 0.5 ? Color(0, 0, 0) : Color(1, 1, 1); |
414 | col.a = 0.9; |
415 | |
416 | draw_line(Vector2(points[i].offset * total_w + handle_width / 2, 0), Vector2(points[i].offset * total_w + handle_width / 2, h / 2), col); |
417 | Rect2 rect = Rect2(points[i].offset * total_w, h / 2, handle_width, h / 2); |
418 | draw_rect(rect, points[i].color, true); |
419 | draw_rect(rect, col, false, 1); |
420 | if (grabbed == i) { |
421 | const Color focus_color = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
422 | rect = rect.grow(-1); |
423 | if (has_focus()) { |
424 | draw_rect(rect, focus_color, false, 1); |
425 | } else { |
426 | draw_rect(rect, focus_color.darkened(0.4), false, 1); |
427 | } |
428 | |
429 | rect = rect.grow(-1); |
430 | draw_rect(rect, col, false, 1); |
431 | } |
432 | } |
433 | |
434 | // Draw "button" for color selector. |
435 | int button_offset = total_w + handle_width + draw_spacing; |
436 | draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard" )), Rect2(button_offset, 0, h, h), true); |
437 | if (grabbed != -1) { |
438 | // Draw with selection color. |
439 | draw_rect(Rect2(button_offset, 0, h, h), points[grabbed].color); |
440 | } else { |
441 | // If no color selected draw gray color with 'X' on top. |
442 | draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1)); |
443 | draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(1, 1, 1, 0.6)); |
444 | draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(1, 1, 1, 0.6)); |
445 | } |
446 | } break; |
447 | |
448 | case NOTIFICATION_VISIBILITY_CHANGED: { |
449 | if (!is_visible()) { |
450 | grabbing = false; |
451 | } |
452 | } break; |
453 | } |
454 | } |
455 | |
456 | void GradientEditor::_bind_methods() { |
457 | ADD_SIGNAL(MethodInfo("ramp_changed" )); |
458 | } |
459 | |
460 | GradientEditor::GradientEditor() { |
461 | set_focus_mode(FOCUS_ALL); |
462 | |
463 | popup = memnew(PopupPanel); |
464 | picker = memnew(ColorPicker); |
465 | popup->add_child(picker); |
466 | popup->connect("about_to_popup" , callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(GradientEditor::get_picker())); |
467 | |
468 | gradient_cache.instantiate(); |
469 | preview_texture.instantiate(); |
470 | |
471 | preview_texture->set_width(1024); |
472 | add_child(popup, false, INTERNAL_MODE_FRONT); |
473 | } |
474 | |
475 | GradientEditor::~GradientEditor() { |
476 | } |
477 | |