Path: blob/master/editor/scene/gradient_editor_plugin.cpp
20843 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/input/input.h"33#include "core/os/keyboard.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/gui/editor_spin_slider.h"38#include "editor/scene/3d/node_3d_editor_plugin.h"39#include "editor/scene/canvas_item_editor_plugin.h"40#include "editor/settings/editor_settings.h"41#include "editor/themes/editor_scale.h"42#include "scene/gui/color_picker.h"43#include "scene/gui/flow_container.h"44#include "scene/gui/popup.h"45#include "scene/gui/separator.h"46#include "scene/resources/gradient_texture.h"4748int GradientEdit::_get_point_at(int p_xpos) const {49int result = -1;50int total_w = _get_gradient_rect_width();51float min_distance = handle_width * 0.8; // Allow the cursor to be more than half a handle width away for ease of use.52for (int i = 0; i < gradient->get_point_count(); i++) {53// Ignore points outside of [0, 1].54if (gradient->get_offset(i) < 0) {55continue;56} else if (gradient->get_offset(i) > 1) {57break;58}59// Check if we clicked at point.60float distance = Math::abs(p_xpos - gradient->get_offset(i) * total_w);61if (distance < min_distance) {62result = i;63min_distance = distance;64}65}66return result;67}6869int GradientEdit::_predict_insertion_index(float p_offset) {70int result = 0;71while (result < gradient->get_point_count() && gradient->get_offset(result) < p_offset) {72result++;73}74return result;75}7677int GradientEdit::_get_gradient_rect_width() const {78return get_size().width - get_size().height - draw_spacing - handle_width;79}8081void GradientEdit::_show_color_picker() {82if (selected_index == -1) {83return;84}8586picker->set_pick_color(gradient->get_color(selected_index));87Size2 minsize = popup->get_contents_minimum_size();88float viewport_height = get_viewport_rect().size.y;8990// Determine in which direction to show the popup. By default popup below.91// But if the popup doesn't fit below and the Gradient Editor is in the bottom half of the viewport, show above.92bool show_above = get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height;9394float v_offset = show_above ? -minsize.y : get_size().y;95popup->set_position(get_screen_position() + Vector2(0, v_offset));96popup->popup();97}9899void GradientEdit::_color_changed(const Color &p_color) {100set_color(selected_index, p_color);101}102103void GradientEdit::set_gradient(const Ref<Gradient> &p_gradient) {104gradient = p_gradient;105gradient->connect(CoreStringName(changed), callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));106}107108const Ref<Gradient> &GradientEdit::get_gradient() const {109return gradient;110}111112void GradientEdit::add_point(float p_offset, const Color &p_color) {113int new_idx = _predict_insertion_index(p_offset);114115EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();116undo_redo->create_action(TTR("Add Gradient Point"));117undo_redo->add_do_method(*gradient, "add_point", p_offset, p_color);118undo_redo->add_do_method(this, "set_selected_index", new_idx);119undo_redo->add_undo_method(*gradient, "remove_point", new_idx);120undo_redo->add_undo_method(this, "set_selected_index", -1);121undo_redo->commit_action();122}123124void GradientEdit::remove_point(int p_index) {125ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");126127if (gradient->get_point_count() <= 1) {128return;129}130131// If the point is removed while it's being moved, remember its old offset.132float old_offset = (grabbing == GRAB_MOVE) ? pre_grab_offset : gradient->get_offset(p_index);133Color old_color = gradient->get_color(p_index);134135int new_selected_index = selected_index;136// Reselect the old selected point if it's not the deleted one.137if (new_selected_index > p_index) {138new_selected_index -= 1;139} else if (new_selected_index == p_index) {140new_selected_index = -1;141}142143EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();144undo_redo->create_action(TTR("Remove Gradient Point"));145undo_redo->add_do_method(*gradient, "remove_point", p_index);146undo_redo->add_do_method(this, "set_selected_index", new_selected_index);147undo_redo->add_undo_method(*gradient, "add_point", old_offset, old_color);148undo_redo->add_undo_method(this, "set_selected_index", selected_index);149undo_redo->commit_action();150}151152void GradientEdit::set_offset(int p_index, float p_offset) {153ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");154155// Use pre_grab_offset to determine things for the undo/redo.156if (Math::is_equal_approx(pre_grab_offset, p_offset)) {157return;158}159160int new_idx = _predict_insertion_index(p_offset);161162gradient->set_offset(p_index, pre_grab_offset); // Pretend the point started from its old place.163EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();164undo_redo->create_action(TTR("Move Gradient Point"));165undo_redo->add_do_method(*gradient, "set_offset", pre_grab_index, p_offset);166undo_redo->add_do_method(this, "set_selected_index", new_idx);167undo_redo->add_undo_method(*gradient, "set_offset", new_idx, pre_grab_offset);168undo_redo->add_undo_method(this, "set_selected_index", pre_grab_index);169undo_redo->commit_action();170queue_redraw();171}172173void GradientEdit::set_color(int p_index, const Color &p_color) {174ERR_FAIL_INDEX_MSG(p_index, gradient->get_point_count(), "Gradient point is out of bounds.");175176Color old_color = gradient->get_color(p_index);177if (old_color == p_color) {178return;179}180181EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();182undo_redo->create_action(TTR("Recolor Gradient Point"), UndoRedo::MERGE_ENDS);183undo_redo->add_do_method(*gradient, "set_color", p_index, p_color);184undo_redo->add_undo_method(*gradient, "set_color", p_index, old_color);185undo_redo->commit_action();186queue_redraw();187}188189void GradientEdit::reverse_gradient() {190int new_selected_idx = (selected_index == -1) ? -1 : (gradient->get_point_count() - selected_index - 1);191EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();192undo_redo->create_action(TTR("Reverse Gradient"), UndoRedo::MERGE_DISABLE, *gradient);193undo_redo->add_do_method(*gradient, "reverse");194undo_redo->add_do_method(this, "set_selected_index", new_selected_idx);195undo_redo->add_undo_method(*gradient, "reverse");196undo_redo->add_undo_method(this, "set_selected_index", selected_index);197undo_redo->commit_action();198}199200void GradientEdit::set_selected_index(int p_index) {201selected_index = p_index;202queue_redraw();203}204205void GradientEdit::set_snap_enabled(bool p_enabled) {206snap_enabled = p_enabled;207queue_redraw();208if (gradient.is_valid()) {209if (snap_enabled) {210gradient->set_meta(SNAME("_snap_enabled"), true);211} else {212gradient->remove_meta(SNAME("_snap_enabled"));213}214}215}216217void GradientEdit::set_snap_count(int p_count) {218snap_count = p_count;219queue_redraw();220if (gradient.is_valid()) {221if (snap_count != GradientEditor::DEFAULT_SNAP) {222gradient->set_meta(SNAME("_snap_count"), snap_count);223} else {224gradient->remove_meta(SNAME("_snap_count"));225}226}227}228229ColorPicker *GradientEdit::get_picker() const {230return picker;231}232233PopupPanel *GradientEdit::get_popup() const {234return popup;235}236237void GradientEdit::gui_input(const Ref<InputEvent> &p_event) {238ERR_FAIL_COND(p_event.is_null());239240Ref<InputEventKey> k = p_event;241242if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && selected_index != -1) {243if (grabbing == GRAB_ADD) {244gradient->remove_point(selected_index); // Point is temporary, so remove directly from gradient.245set_selected_index(-1);246} else {247remove_point(selected_index);248}249grabbing = GRAB_NONE;250hovered_index = -1;251accept_event();252}253254Ref<InputEventMouseButton> mb = p_event;255256if (mb.is_valid() && mb->is_pressed()) {257float adjusted_mb_x = mb->get_position().x - handle_width / 2;258bool should_snap = snap_enabled || mb->is_ctrl_pressed();259260// Delete point or move it to old position on middle or right click.261if (mb->get_button_index() == MouseButton::RIGHT || mb->get_button_index() == MouseButton::MIDDLE) {262if (grabbing == GRAB_MOVE && mb->get_button_index() == MouseButton::RIGHT) {263gradient->set_offset(selected_index, pre_grab_offset);264set_selected_index(pre_grab_index);265} else {266int point_to_remove = _get_point_at(adjusted_mb_x);267if (point_to_remove == -1) {268set_selected_index(-1); // Nothing on the place of the click, just deselect any handle.269} else {270if (grabbing == GRAB_ADD) {271gradient->remove_point(point_to_remove); // Point is temporary, so remove directly from gradient.272set_selected_index(-1);273} else {274remove_point(point_to_remove);275}276hovered_index = -1;277}278}279grabbing = GRAB_NONE;280accept_event();281}282283// Select point.284if (mb->get_button_index() == MouseButton::LEFT) {285int total_w = _get_gradient_rect_width();286287// Check if color picker was clicked or gradient was double-clicked.288if (adjusted_mb_x > total_w + draw_spacing) {289if (!mb->is_double_click()) {290_show_color_picker();291}292accept_event();293return;294} else if (mb->is_double_click()) {295set_selected_index(_get_point_at(adjusted_mb_x));296_show_color_picker();297accept_event();298return;299}300301if (grabbing == GRAB_NONE) {302set_selected_index(_get_point_at(adjusted_mb_x));303}304305if (selected_index != -1 && !mb->is_alt_pressed()) {306// An existing point was grabbed.307grabbing = GRAB_MOVE;308pre_grab_offset = gradient->get_offset(selected_index);309pre_grab_index = selected_index;310} else if (grabbing == GRAB_NONE) {311// Adding a new point. Insert a temporary point for the user to adjust, so it's not in the undo/redo.312float new_offset = CLAMP(adjusted_mb_x / float(total_w), 0, 1);313if (should_snap) {314new_offset = Math::snapped(new_offset, 1.0 / snap_count);315}316317for (int i = 0; i < gradient->get_point_count(); i++) {318if (gradient->get_offset(i) == new_offset) {319// If another point with the same offset is found, then320// tweak it if Alt was pressed, otherwise something has gone wrong, so stop the operation.321if (mb->is_alt_pressed()) {322new_offset = MIN(gradient->get_offset(i) + 0.00001, 1);323} else {324return;325}326}327}328329Color new_color = gradient->get_color_at_offset(new_offset);330if (mb->is_alt_pressed()) {331// Alt + Click on a point duplicates it. So copy its color.332int point_to_copy = _get_point_at(adjusted_mb_x);333if (point_to_copy != -1) {334new_color = gradient->get_color(point_to_copy);335}336}337// Add a temporary point for the user to adjust before adding it permanently.338gradient->add_point(new_offset, new_color);339set_selected_index(_predict_insertion_index(new_offset));340grabbing = GRAB_ADD;341}342}343}344345if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {346if (grabbing == GRAB_MOVE) {347// Finish moving a point.348set_offset(selected_index, gradient->get_offset(selected_index));349grabbing = GRAB_NONE;350} else if (grabbing == GRAB_ADD) {351// Finish inserting a new point. Remove the temporary point and insert the permanent one in its place.352float new_offset = gradient->get_offset(selected_index);353Color new_color = gradient->get_color(selected_index);354gradient->remove_point(selected_index);355add_point(new_offset, new_color);356grabbing = GRAB_NONE;357}358}359360Ref<InputEventMouseMotion> mm = p_event;361362if (mm.is_valid()) {363int total_w = _get_gradient_rect_width();364float adjusted_mm_x = mm->get_position().x - handle_width / 2;365bool should_snap = snap_enabled || mm->is_ctrl_pressed();366367// Hovering logic.368if (grabbing == GRAB_NONE) {369int nearest_point = _get_point_at(adjusted_mm_x);370if (hovered_index != nearest_point) {371hovered_index = nearest_point;372queue_redraw();373}374return;375} else {376hovered_index = -1;377}378379// Grabbing logic.380float new_offset = CLAMP(adjusted_mm_x / float(total_w), 0, 1);381382// Give the ability to snap right next to a point when using Shift.383if (mm->is_shift_pressed()) {384float smallest_offset = should_snap ? (0.5 / snap_count) : 0.01;385int nearest_idx = -1;386// Only check the two adjacent points to find which one is the nearest.387if (selected_index > 0) {388float temp_offset = Math::abs(gradient->get_offset(selected_index - 1) - new_offset);389if (temp_offset < smallest_offset) {390smallest_offset = temp_offset;391nearest_idx = selected_index - 1;392}393}394if (selected_index < gradient->get_point_count() - 1) {395float temp_offset = Math::abs(gradient->get_offset(selected_index + 1) - new_offset);396if (temp_offset < smallest_offset) {397smallest_offset = temp_offset;398nearest_idx = selected_index + 1;399}400}401if (nearest_idx != -1) {402// Snap to the point with a slight adjustment to the left or right.403float adjustment = gradient->get_offset(nearest_idx) < new_offset ? 0.00001 : -0.00001;404new_offset = CLAMP(gradient->get_offset(nearest_idx) + adjustment, 0, 1);405} else if (should_snap) {406new_offset = Math::snapped(new_offset, 1.0 / snap_count);407}408} else if (should_snap) {409// Shift is not pressed, so snap fully without adjustments.410new_offset = Math::snapped(new_offset, 1.0 / snap_count);411}412413// Don't move the point if its new offset would be the same as another point's.414for (int i = 0; i < gradient->get_point_count(); i++) {415if (gradient->get_offset(i) == new_offset && i != selected_index) {416return;417}418}419420if (selected_index == -1) {421return;422}423424// We want to only save this action for undo/redo when released, so don't use set_offset() yet.425gradient->set_offset(selected_index, new_offset);426427// Update selected_index after the gradient updates its indices, so you keep holding the same color.428for (int i = 0; i < gradient->get_point_count(); i++) {429if (gradient->get_offset(i) == new_offset) {430set_selected_index(i);431break;432}433}434}435}436437void GradientEdit::_redraw() {438int w = get_size().x;439int h = get_size().y - draw_spacing; // A bit of spacing below the gradient too.440441if (w == 0 || h == 0) {442return; // Safety check as there is nothing to draw with such size.443}444445int total_w = _get_gradient_rect_width();446int half_handle_width = handle_width * 0.5;447448// Draw gradient.449draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(half_handle_width, 0, total_w, h), true);450preview_texture->set_gradient(gradient);451draw_texture_rect(preview_texture, Rect2(half_handle_width, 0, total_w, h));452453// Draw vertical snap lines.454if (snap_enabled || (Input::get_singleton()->is_key_pressed(Key::CTRL) && grabbing != GRAB_NONE)) {455const Color line_color = Color(0.5, 0.5, 0.5, 0.5);456for (int idx = 1; idx < snap_count; idx++) {457float offset_x = idx * total_w / (float)snap_count + half_handle_width;458draw_line(Point2(offset_x, 0), Point2(offset_x, h), line_color);459}460}461462// Draw handles.463for (int i = 0; i < gradient->get_point_count(); i++) {464// Only draw handles for points in [0, 1]. If there are points before or after, draw a little indicator.465if (gradient->get_offset(i) < 0.0) {466continue;467} else if (gradient->get_offset(i) > 1.0) {468break;469}470// White or black handle color, to contrast with the selected color's brightness.471// Also consider the fact that the color may be translucent.472// The checkerboard pattern in the background has an average luminance of 0.75.473Color inside_col = gradient->get_color(i);474Color border_col = Math::lerp(0.75f, inside_col.get_luminance(), inside_col.a) > 0.455 ? Color(0, 0, 0) : Color(1, 1, 1);475476int handle_thickness = MAX(1, Math::round(EDSCALE));477float handle_x_pos = gradient->get_offset(i) * total_w + half_handle_width;478float handle_start_x = handle_x_pos - half_handle_width;479Rect2 rect = Rect2(handle_start_x, h / 2, handle_width, h / 2);480481if (inside_col.a < 1) {482// If the color is translucent, draw a little opaque rectangle at the bottom to more easily see it.483draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), rect, true);484draw_rect(rect, inside_col, true);485Color inside_col_opaque = inside_col;486inside_col_opaque.a = 1.0;487draw_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);488} else {489draw_rect(rect, inside_col, true);490}491492if (selected_index == i) {493// Handle is selected.494draw_rect(rect, border_col, false, handle_thickness);495draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);496if (inside_col.a < 1) {497draw_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);498}499rect = rect.grow(-handle_thickness);500const Color focus_col = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));501draw_rect(rect, has_focus() ? focus_col : focus_col.darkened(0.4), false, handle_thickness);502rect = rect.grow(-handle_thickness);503draw_rect(rect, border_col, false, handle_thickness);504} else {505// Handle isn't selected.506border_col.a = 0.9;507draw_rect(rect, border_col, false, handle_thickness);508draw_line(Vector2(handle_x_pos, 0), Vector2(handle_x_pos, h / 2 - handle_thickness), border_col, handle_thickness);509if (inside_col.a < 1) {510draw_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);511}512if (hovered_index == i) {513// Draw a subtle translucent rect inside the handle if it's being hovered.514rect = rect.grow(-handle_thickness);515border_col.a = 0.54;516draw_rect(rect, border_col, false, handle_thickness);517}518}519}520521// Draw "button" for color selector.522int button_offset = total_w + handle_width + draw_spacing;523if (selected_index != -1) {524Color grabbed_col = gradient->get_color(selected_index);525if (grabbed_col.a < 1) {526draw_texture_rect(get_editor_theme_icon(SNAME("GuiMiniCheckerboard")), Rect2(button_offset, 0, h, h), true);527}528draw_rect(Rect2(button_offset, 0, h, h), grabbed_col);529if (grabbed_col.r > 1 || grabbed_col.g > 1 || grabbed_col.b > 1) {530// Draw an indicator to denote that the currently selected color is "overbright".531draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(button_offset, 0));532}533} else {534// If no color is selected, draw gray color with 'X' on top.535draw_rect(Rect2(button_offset, 0, h, h), Color(0.5, 0.5, 0.5, 1));536draw_line(Vector2(button_offset, 0), Vector2(button_offset + h, h), Color(0.8, 0.8, 0.8));537draw_line(Vector2(button_offset, h), Vector2(button_offset + h, 0), Color(0.8, 0.8, 0.8));538}539}540541void GradientEdit::_notification(int p_what) {542switch (p_what) {543case NOTIFICATION_THEME_CHANGED: {544draw_spacing = BASE_SPACING * get_theme_default_base_scale();545handle_width = BASE_HANDLE_WIDTH * get_theme_default_base_scale();546} break;547case NOTIFICATION_DRAW: {548_redraw();549} break;550case NOTIFICATION_MOUSE_EXIT: {551if (hovered_index != -1) {552hovered_index = -1;553queue_redraw();554}555} break;556case NOTIFICATION_VISIBILITY_CHANGED: {557if (!is_visible()) {558grabbing = GRAB_NONE;559}560} break;561}562}563564void GradientEdit::_bind_methods() {565ClassDB::bind_method(D_METHOD("set_selected_index", "index"), &GradientEdit::set_selected_index);566}567568GradientEdit::GradientEdit() {569set_focus_mode(FOCUS_ALL);570set_custom_minimum_size(Size2(0, 60) * EDSCALE);571572picker = memnew(ColorPicker);573int picker_shape = EDITOR_GET("interface/inspector/default_color_picker_shape");574picker->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);575picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed));576577popup = memnew(PopupPanel);578popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(picker));579580add_child(popup, false, INTERNAL_MODE_FRONT);581popup->add_child(picker);582583preview_texture.instantiate();584preview_texture->set_width(1024);585}586587///////////////////////588589const int GradientEditor::DEFAULT_SNAP = 10;590591void GradientEditor::_set_snap_enabled(bool p_enabled) {592gradient_editor_rect->set_snap_enabled(p_enabled);593snap_count_edit->set_visible(p_enabled);594}595596void GradientEditor::_set_snap_count(int p_count) {597gradient_editor_rect->set_snap_count(CLAMP(p_count, 2, 100));598}599600void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {601gradient_editor_rect->set_gradient(p_gradient);602}603604void GradientEditor::_notification(int p_what) {605switch (p_what) {606case NOTIFICATION_THEME_CHANGED: {607reverse_button->set_button_icon(get_editor_theme_icon(SNAME("ReverseGradient")));608snap_button->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));609} break;610case NOTIFICATION_READY: {611Ref<Gradient> gradient = gradient_editor_rect->get_gradient();612if (gradient.is_valid()) {613// Set snapping settings based on the gradient's meta.614snap_button->set_pressed(gradient->get_meta("_snap_enabled", false));615snap_count_edit->set_value(gradient->get_meta("_snap_count", DEFAULT_SNAP));616}617} break;618}619}620621GradientEditor::GradientEditor() {622HFlowContainer *toolbar = memnew(HFlowContainer);623add_child(toolbar);624625reverse_button = memnew(Button);626reverse_button->set_tooltip_text(TTR("Reverse/Mirror Gradient"));627toolbar->add_child(reverse_button);628629toolbar->add_child(memnew(VSeparator));630631snap_button = memnew(Button);632snap_button->set_tooltip_text(TTR("Toggle Grid Snap"));633snap_button->set_toggle_mode(true);634toolbar->add_child(snap_button);635snap_button->connect(SceneStringName(toggled), callable_mp(this, &GradientEditor::_set_snap_enabled));636637snap_count_edit = memnew(EditorSpinSlider);638snap_count_edit->set_min(2);639snap_count_edit->set_max(100);640snap_count_edit->set_accessibility_name(TTRC("Grid Step"));641snap_count_edit->set_value(DEFAULT_SNAP);642snap_count_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));643toolbar->add_child(snap_count_edit);644snap_count_edit->connect(SceneStringName(value_changed), callable_mp(this, &GradientEditor::_set_snap_count));645646gradient_editor_rect = memnew(GradientEdit);647add_child(gradient_editor_rect);648reverse_button->connect(SceneStringName(pressed), callable_mp(gradient_editor_rect, &GradientEdit::reverse_gradient));649650set_mouse_filter(MOUSE_FILTER_STOP);651_set_snap_enabled(snap_button->is_pressed());652_set_snap_count(snap_count_edit->get_value());653}654655///////////////////////656657bool EditorInspectorPluginGradient::can_handle(Object *p_object) {658return Object::cast_to<Gradient>(p_object) != nullptr;659}660661void EditorInspectorPluginGradient::parse_begin(Object *p_object) {662Gradient *gradient = Object::cast_to<Gradient>(p_object);663ERR_FAIL_NULL(gradient);664Ref<Gradient> g(gradient);665666GradientEditor *editor = memnew(GradientEditor);667editor->set_gradient(g);668add_custom_control(editor);669}670671///////////////////////672673GradientEditorPlugin::GradientEditorPlugin() {674Ref<EditorInspectorPluginGradient> plugin;675plugin.instantiate();676add_inspector_plugin(plugin);677}678679680