Path: blob/master/editor/scene/gradient_editor_plugin.cpp
9902 views
/**************************************************************************/1/* gradient_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gradient_editor_plugin.h"3132#include "core/os/keyboard.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/gui/editor_spin_slider.h"37#include "editor/scene/3d/node_3d_editor_plugin.h"38#include "editor/scene/canvas_item_editor_plugin.h"39#include "editor/settings/editor_settings.h"40#include "editor/themes/editor_scale.h"41#include "scene/gui/color_picker.h"42#include "scene/gui/flow_container.h"43#include "scene/gui/popup.h"44#include "scene/gui/separator.h"45#include "scene/resources/gradient_texture.h"4647int GradientEdit::_get_point_at(int p_xpos) const {48int result = -1;49int total_w = _get_gradient_rect_width();50float min_distance = handle_width * 0.8; // Allow the cursor to be more than half a handle width away for ease of use.51for (int i = 0; i < gradient->get_point_count(); i++) {52// Ignore points outside of [0, 1].53if (gradient->get_offset(i) < 0) {54continue;55} else if (gradient->get_offset(i) > 1) {56break;57}58// Check if we clicked at point.59float distance = Math::abs(p_xpos - gradient->get_offset(i) * total_w);60if (distance < min_distance) {61result = i;62min_distance = distance;63}64}65return result;66}6768int GradientEdit::_predict_insertion_index(float p_offset) {69int result = 0;70while (result < gradient->get_point_count() && gradient->get_offset(result) < p_offset) {71result++;72}73return result;74}7576int GradientEdit::_get_gradient_rect_width() const {77return get_size().width - get_size().height - draw_spacing - handle_width;78}7980void GradientEdit::_show_color_picker() {81if (selected_index == -1) {82return;83}8485picker->set_pick_color(gradient->get_color(selected_index));86Size2 minsize = popup->get_contents_minimum_size();87float viewport_height = get_viewport_rect().size.y;8889// Determine in which direction to show the popup. By default popup below.90// But if the popup doesn't fit below and the Gradient Editor is in the bottom half of the viewport, show above.91bool show_above = get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height;9293float v_offset = show_above ? -minsize.y : get_size().y;94popup->set_position(get_screen_position() + Vector2(0, v_offset));95popup->popup();96}9798void GradientEdit::_color_changed(const Color &p_color) {99set_color(selected_index, p_color);100}101102void GradientEdit::set_gradient(const Ref<Gradient> &p_gradient) {103gradient = p_gradient;104gradient->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));105}106107const Ref<Gradient> &GradientEdit::get_gradient() const {108return gradient;109}110111void GradientEdit::add_point(float p_offset, const Color &p_color) {112int new_idx = _predict_insertion_index(p_offset);113114EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();115undo_redo->create_action(TTR("Add Gradient Point"));116undo_redo->add_do_method(*gradient, "add_point", p_offset, p_color);117undo_redo->add_do_method(this, "set_selected_index", new_idx);118undo_redo->add_undo_method(*gradient, "remove_point", new_idx);119undo_redo->add_undo_method(this, "set_selected_index", -1);120undo_redo->commit_action();121}122123void GradientEdit::remove_point(int p_index) {124ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");125126if (gradient->get_point_count() <= 1) {127return;128}129130// If the point is removed while it's being moved, remember its old offset.131float old_offset = (grabbing == GRAB_MOVE) ? pre_grab_offset : gradient->get_offset(p_index);132Color old_color = gradient->get_color(p_index);133134int new_selected_index = selected_index;135// Reselect the old selected point if it's not the deleted one.136if (new_selected_index > p_index) {137new_selected_index -= 1;138} else if (new_selected_index == p_index) {139new_selected_index = -1;140}141142EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();143undo_redo->create_action(TTR("Remove Gradient Point"));144undo_redo->add_do_method(*gradient, "remove_point", p_index);145undo_redo->add_do_method(this, "set_selected_index", new_selected_index);146undo_redo->add_undo_method(*gradient, "add_point", old_offset, old_color);147undo_redo->add_undo_method(this, "set_selected_index", selected_index);148undo_redo->commit_action();149}150151void GradientEdit::set_offset(int p_index, float p_offset) {152ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");153154// Use pre_grab_offset to determine things for the undo/redo.155if (Math::is_equal_approx(pre_grab_offset, p_offset)) {156return;157}158159int new_idx = _predict_insertion_index(p_offset);160161gradient->set_offset(p_index, pre_grab_offset); // Pretend the point started from its old place.162EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();163undo_redo->create_action(TTR("Move Gradient Point"));164undo_redo->add_do_method(*gradient, "set_offset", pre_grab_index, p_offset);165undo_redo->add_do_method(this, "set_selected_index", new_idx);166undo_redo->add_undo_method(*gradient, "set_offset", new_idx, pre_grab_offset);167undo_redo->add_undo_method(this, "set_selected_index", pre_grab_index);168undo_redo->commit_action();169queue_redraw();170}171172void GradientEdit::set_color(int p_index, const Color &p_color) {173ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");174175Color old_color = gradient->get_color(p_index);176if (old_color == p_color) {177return;178}179180EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();181undo_redo->create_action(TTR("Recolor Gradient Point"), UndoRedo::MERGE_ENDS);182undo_redo->add_do_method(*gradient, "set_color", p_index, p_color);183undo_redo->add_undo_method(*gradient, "set_color", p_index, old_color);184undo_redo->commit_action();185queue_redraw();186}187188void GradientEdit::reverse_gradient() {189int new_selected_idx = (selected_index == -1) ? -1 : (gradient->get_point_count() - selected_index - 1);190EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();191undo_redo->create_action(TTR("Reverse Gradient"), UndoRedo::MERGE_DISABLE, *gradient);192undo_redo->add_do_method(*gradient, "reverse");193undo_redo->add_do_method(this, "set_selected_index", new_selected_idx);194undo_redo->add_undo_method(*gradient, "reverse");195undo_redo->add_undo_method(this, "set_selected_index", selected_index);196undo_redo->commit_action();197}198199void GradientEdit::set_selected_index(int p_index) {200selected_index = p_index;201queue_redraw();202}203204void GradientEdit::set_snap_enabled(bool p_enabled) {205snap_enabled = p_enabled;206queue_redraw();207if (gradient.is_valid()) {208if (snap_enabled) {209gradient->set_meta(SNAME("_snap_enabled"), true);210} else {211gradient->remove_meta(SNAME("_snap_enabled"));212}213}214}215216void GradientEdit::set_snap_count(int p_count) {217snap_count = p_count;218queue_redraw();219if (gradient.is_valid()) {220if (snap_count != GradientEditor::DEFAULT_SNAP) {221gradient->set_meta(SNAME("_snap_count"), snap_count);222} else {223gradient->remove_meta(SNAME("_snap_count"));224}225}226}227228ColorPicker *GradientEdit::get_picker() const {229return picker;230}231232PopupPanel *GradientEdit::get_popup() const {233return popup;234}235236void GradientEdit::gui_input(const Ref<InputEvent> &p_event) {237ERR_FAIL_COND(p_event.is_null());238239Ref<InputEventKey> k = p_event;240241if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && selected_index != -1) {242if (grabbing == GRAB_ADD) {243gradient->remove_point(selected_index); // Point is temporary, so remove directly from gradient.244set_selected_index(-1);245} else {246remove_point(selected_index);247}248grabbing = GRAB_NONE;249hovered_index = -1;250accept_event();251}252253Ref<InputEventMouseButton> mb = p_event;254255if (mb.is_valid() && mb->is_pressed()) {256float adjusted_mb_x = mb->get_position().x - handle_width / 2;257bool should_snap = snap_enabled || mb->is_ctrl_pressed();258259// Delete point or move it to old position on middle or right click.260if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {261if (grabbing == GRAB_MOVE && mb->get_button_index() == MouseButton::RIGHT) {262gradient->set_offset(selected_index, pre_grab_offset);263set_selected_index(pre_grab_index);264} else {265int point_to_remove = _get_point_at(adjusted_mb_x);266if (point_to_remove == -1) {267set_selected_index(-1); // Nothing on the place of the click, just deselect any handle.268} else {269if (grabbing == GRAB_ADD) {270gradient->remove_point(point_to_remove); // Point is temporary, so remove directly from gradient.271set_selected_index(-1);272} else {273remove_point(point_to_remove);274}275hovered_index = -1;276}277}278grabbing = GRAB_NONE;279accept_event();280}281282// Select point.283if (mb->get_button_index() == MouseButton::LEFT) {284int total_w = _get_gradient_rect_width();285286// Check if color picker was clicked or gradient was double-clicked.287if (adjusted_mb_x > total_w + draw_spacing) {288if (!mb->is_double_click()) {289_show_color_picker();290}291accept_event();292return;293} else if (mb->is_double_click()) {294set_selected_index(_get_point_at(adjusted_mb_x));295_show_color_picker();296accept_event();297return;298}299300if (grabbing == GRAB_NONE) {301set_selected_index(_get_point_at(adjusted_mb_x));302}303304if (selected_index != -1 && !mb->is_alt_pressed()) {305// An existing point was grabbed.306grabbing = GRAB_MOVE;307pre_grab_offset = gradient->get_offset(selected_index);308pre_grab_index = selected_index;309} else if (grabbing == GRAB_NONE) {310// Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.311float new_offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);312if (should_snap) {313new_offset = Math::snapped(new_offset, 1.0 / snap_count);314}315316for (int i = 0; i < gradient->get_point_count(); i++) {317if (gradient->get_offset(i) == new_offset) {318// If another point with the same offset is found, then319// tweak it if Alt was pressed, otherwise something has gone wrong, so stop the operation.320if (mb->is_alt_pressed()) {321new_offset = MIN(gradient->get_offset(i) + 0.00001, 1);322} else {323return;324}325}326}327328Color new_color = gradient->get_color_at_offset(new_offset);329if (mb->is_alt_pressed()) {330// Alt + Click on a point duplicates it. So copy its color.331int point_to_copy = _get_point_at(adjusted_mb_x);332if (point_to_copy != -1) {333new_color = gradient->get_color(point_to_copy);334}335}336// Add a temporary point for the user to adjust before adding it permanently.337gradient->add_point(new_offset, new_color);338set_selected_index(_predict_insertion_index(new_offset));339grabbing = GRAB_ADD;340}341}342}343344if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {345if (grabbing == GRAB_MOVE) {346// Finish moving a point.347set_offset(selected_index, gradient->get_offset(selected_index));348grabbing = GRAB_NONE;349} else if (grabbing == GRAB_ADD) {350// Finish inserting a new point. Remove the temporary point and insert the permanent one in its place.351float new_offset = gradient->get_offset(selected_index);352Color new_color = gradient->get_color(selected_index);353gradient->remove_point(selected_index);354add_point(new_offset, new_color);355grabbing = GRAB_NONE;356}357}358359Ref<InputEventMouseMotion> mm = p_event;360361if (mm.is_valid()) {362int total_w = _get_gradient_rect_width();363float adjusted_mm_x = mm->get_position().x - handle_width / 2;364bool should_snap = snap_enabled || mm->is_ctrl_pressed();365366// Hovering logic.367if (grabbing == GRAB_NONE) {368int nearest_point = _get_point_at(adjusted_mm_x);369if (hovered_index != nearest_point) {370hovered_index = nearest_point;371queue_redraw();372}373return;374} else {375hovered_index = -1;376}377378// Grabbing logic.379float new_offset = CLAMP(adjusted_mm_x / float(total_w), 0, 1);380381// Give the ability to snap right next to a point when using Shift.382if (mm->is_shift_pressed()) {383float smallest_offset = should_snap ? (0.5 / snap_count) : 0.01;384int nearest_idx = -1;385// Only check the two adjacent points to find which one is the nearest.386if (selected_index > 0) {387float temp_offset = Math::abs(gradient->get_offset(selected_index - 1) - new_offset);388if (temp_offset < smallest_offset) {389smallest_offset = temp_offset;390nearest_idx = selected_index - 1;391}392}393if (selected_index < gradient->get_point_count() - 1) {394float temp_offset = Math::abs(gradient->get_offset(selected_index + 1) - new_offset);395if (temp_offset < smallest_offset) {396smallest_offset = temp_offset;397nearest_idx = selected_index + 1;398}399}400if (nearest_idx != -1) {401// Snap to the point with a slight adjustment to the left or right.402float adjustment = gradient->get_offset(nearest_idx) < new_offset ? 0.00001 : -0.00001;403new_offset = CLAMP(gradient->get_offset(nearest_idx) + adjustment, 0, 1);404} else if (should_snap) {405new_offset = Math::snapped(new_offset, 1.0 / snap_count);406}407} else if (should_snap) {408// Shift is not pressed, so snap fully without adjustments.409new_offset = Math::snapped(new_offset, 1.0 / snap_count);410}411412// Don't move the point if its new offset would be the same as another point's.413for (int i = 0; i < gradient->get_point_count(); i++) {414if (gradient->get_offset(i) == new_offset && i != selected_index) {415return;416}417}418419if (selected_index == -1) {420return;421}422423// We want to only save this action for undo/redo when released, so don't use set_offset() yet.424gradient->set_offset(selected_index, new_offset);425426// Update selected_index after the gradient updates its indices, so you keep holding the same color.427for (int i = 0; i < gradient->get_point_count(); i++) {428if (gradient->get_offset(i) == new_offset) {429set_selected_index(i);430break;431}432}433}434}435436void GradientEdit::_redraw() {437int w = get_size().x;438int h = get_size().y - draw_spacing; // A bit of spacing below the gradient too.439440if (w == 0 || h == 0) {441return; // Safety check as there is nothing to draw with such size.442}443444int total_w = _get_gradient_rect_width();445int half_handle_width = handle_width * 0.5;446447// Draw gradient.448draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(half_handle_width, 0, total_w, h), true);449preview_texture->set_gradient(gradient);450draw_texture_rect(preview_texture, Rect2(half_handle_width, 0, total_w, h));451452// Draw vertical snap lines.453if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CTRL) && grabbing != GRAB_NONE)) {454const Color line_color = Color(0.5, 0.5, 0.5, 0.5);455for (int idx = 1; idx < snap_count; idx++) {456float offset_x = idx * total_w / (float)snap_count + half_handle_width;457draw_line(Point2(offset_x, 0), Point2(offset_x, h), line_color);458}459}460461// Draw handles.462for (int i = 0; i < gradient->get_point_count(); i++) {463// Only draw handles for points in [0, 1]. If there are points before or after, draw a little indicator.464if (gradient->get_offset(i) < 0.0) {465continue;466} else if (gradient->get_offset(i) > 1.0) {467break;468}469// White or black handle color, to contrast with the selected color's brightness.470// Also consider the fact that the color may be translucent.471// The checkerboard pattern in the background has an average luminance of 0.75.472Color inside_col = gradient->get_color(i);473Color border_col = Math::lerp(0.75f, inside_col.get_luminance(), inside_col.a) > 0.455 ? Color(0, 0, 0) : Color(1, 1, 1);474475int handle_thickness = MAX(1, Math::round(EDSCALE));476float handle_x_pos = gradient->get_offset(i) * total_w + half_handle_width;477float handle_start_x = handle_x_pos - half_handle_width;478Rect2 rect = Rect2(handle_start_x, h / 2, handle_width, h / 2);479480if (inside_col.a < 1) {481// If the color is translucent, draw a little opaque rectangle at the bottom to more easily see it.482draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), rect, true);483draw_rect(rect, inside_col, true);484Color inside_col_opaque = inside_col;485inside_col_opaque.a = 1.0;486draw_rect(Rect2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness / 2.0, handle_width - handle_thickness, h * 0.1), inside_col_opaque, true);487} else {488draw_rect(rect, inside_col, true);489}490491if (selected_index == i) {492// Handle is selected.493draw_rect(rect, border_col, false, handle_thickness);494draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);495if (inside_col.a < 1) {496draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);497}498rect = rect.grow(-handle_thickness);499const Color focus_col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));500draw_rect(rect, has_focus() ? focus_col : focus_col.darkened(0.4), false, handle_thickness);501rect = rect.grow(-handle_thickness);502draw_rect(rect, border_col, false, handle_thickness);503} else {504// Handle isn't selected.505border_col.a = 0.9;506draw_rect(rect, border_col, false, handle_thickness);507draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);508if (inside_col.a < 1) {509draw_line(Vector2(handle_start_x + handle_thickness / 2.0, h * 0.9 - handle_thickness), Vector2(handle_start_x + handle_width - handle_thickness / 2.0, h * 0.9 - handle_thickness), border_col, handle_thickness);510}511if (hovered_index == i) {512// Draw a subtle translucent rect inside the handle if it's being hovered.513rect = rect.grow(-handle_thickness);514border_col.a = 0.54;515draw_rect(rect, border_col, false, handle_thickness);516}517}518}519520// Draw "button" for color selector.521int button_offset = total_w + handle_width + draw_spacing;522if (selected_index != -1) {523Color grabbed_col = gradient->get_color(selected_index);524if (grabbed_col.a < 1) {525draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(button_offset, 0, h, h), true);526}527draw_rect(Rect2(button_offset, 0, h, h), grabbed_col);528if (grabbed_col.r > 1 || grabbed_col.g > 1 || grabbed_col.b > 1) {529// Draw an indicator to denote that the currently selected color is "overbright".530draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(button_offset, 0));531}532} else {533// If no color is selected, draw gray color with 'X' on top.534draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));535draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(0.8, 0.8, 0.8));536draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(0.8, 0.8, 0.8));537}538}539540void GradientEdit::_notification(int p_what) {541switch (p_what) {542case NOTIFICATION_THEME_CHANGED: {543draw_spacing = BASE_SPACING * get_theme_default_base_scale();544handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();545} break;546case NOTIFICATION_ACCESSIBILITY_UPDATE: {547RID ae = get_accessibility_element();548ERR_FAIL_COND(ae.is_null());549550//TODO551DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);552DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Gradient editor")));553} break;554case NOTIFICATION_DRAW: {555_redraw();556} break;557case NOTIFICATION_MOUSE_EXIT: {558if (hovered_index != -1) {559hovered_index = -1;560queue_redraw();561}562} break;563case NOTIFICATION_VISIBILITY_CHANGED: {564if (!is_visible()) {565grabbing = GRAB_NONE;566}567} break;568}569}570571void GradientEdit::_bind_methods() {572ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &GradientEdit::set_selected_index);573}574575GradientEdit::GradientEdit() {576set_focus_mode(FOCUS_ALL);577set_custom_minimum_size(Size2(0, 60) * EDSCALE);578579picker = memnew(ColorPicker);580int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");581picker->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);582picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));583584popup = memnew(PopupPanel);585popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(picker));586587add_child(popup, false, INTERNAL_MODE_FRONT);588popup->add_child(picker);589590preview_texture.instantiate();591preview_texture->set_width(1024);592}593594///////////////////////595596const int GradientEditor::DEFAULT_SNAP = 10;597598void GradientEditor::_set_snap_enabled(bool p_enabled) {599gradient_editor_rect->set_snap_enabled(p_enabled);600snap_count_edit->set_visible(p_enabled);601}602603void GradientEditor::_set_snap_count(int p_count) {604gradient_editor_rect->set_snap_count(CLAMP(p_count, 2, 100));605}606607void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {608gradient_editor_rect->set_gradient(p_gradient);609}610611void GradientEditor::_notification(int p_what) {612switch (p_what) {613case NOTIFICATION_THEME_CHANGED: {614reverse_button->set_button_icon(get_editor_theme_icon(SNAME("ReverseGradient")));615snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));616} break;617case NOTIFICATION_READY: {618Ref<Gradient> gradient = gradient_editor_rect->get_gradient();619if (gradient.is_valid()) {620// Set snapping settings based on the gradient's meta.621snap_button->set_pressed(gradient->get_meta("_snap_enabled", false));622snap_count_edit->set_value(gradient->get_meta("_snap_count", DEFAULT_SNAP));623}624} break;625}626}627628GradientEditor::GradientEditor() {629HFlowContainer *toolbar = memnew(HFlowContainer);630add_child(toolbar);631632reverse_button = memnew(Button);633reverse_button->set_tooltip_text(TTR("Reverse/Mirror Gradient"));634toolbar->add_child(reverse_button);635636toolbar->add_child(memnew(VSeparator));637638snap_button = memnew(Button);639snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));640snap_button->set_toggle_mode(true);641toolbar->add_child(snap_button);642snap_button->connect(SceneStringName(toggled), callable_mp(this, &GradientEditor::_set_snap_enabled));643644snap_count_edit = memnew(EditorSpinSlider);645snap_count_edit->set_min(2);646snap_count_edit->set_max(100);647snap_count_edit->set_accessibility_name(TTRC("Grid Step"));648snap_count_edit->set_value(DEFAULT_SNAP);649snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));650toolbar->add_child(snap_count_edit);651snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &GradientEditor::_set_snap_count));652653gradient_editor_rect = memnew(GradientEdit);654add_child(gradient_editor_rect);655reverse_button->connect(SceneStringName(pressed), callable_mp(gradient_editor_rect, &GradientEdit::reverse_gradient));656657set_mouse_filter(MOUSE_FILTER_STOP);658_set_snap_enabled(snap_button->is_pressed());659_set_snap_count(snap_count_edit->get_value());660}661662///////////////////////663664bool EditorInspectorPluginGradient::can_handle(Object *p_object) {665return Object::cast_to<Gradient>(p_object) != nullptr;666}667668void EditorInspectorPluginGradient::parse_begin(Object *p_object) {669Gradient *gradient = Object::cast_to<Gradient>(p_object);670ERR_FAIL_NULL(gradient);671Ref<Gradient> g(gradient);672673GradientEditor *editor = memnew(GradientEditor);674editor->set_gradient(g);675add_custom_control(editor);676}677678///////////////////////679680GradientEditorPlugin::GradientEditorPlugin() {681Ref<EditorInspectorPluginGradient> plugin;682plugin.instantiate();683add_inspector_plugin(plugin);684}685686687