Skip to content

Commit

Permalink
Fixed undo/redo behaviour of color picker and added ability to cancel…
Browse files Browse the repository at this point in the history
…/confirm color selection.
  • Loading branch information
MajorMcDoom committed Mar 24, 2024
1 parent 99ff024 commit d827b34
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
16 changes: 13 additions & 3 deletions editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2630,16 +2630,22 @@ void EditorPropertyColor::_set_read_only(bool p_read_only) {
}

void EditorPropertyColor::_color_changed(const Color &p_color) {
if (!live_changes_enabled) {
return;
}

// Cancel the color change if the current color is identical to the new one.
if (get_edited_property_value() == p_color) {
if (((Color)get_edited_property_value()).is_equal_approx(p_color)) {
return;
}

emit_changed(get_edited_property(), p_color, "", true);
// Preview color change, bypassing undo/redo.
get_edited_object()->set(get_edited_property(), p_color);
}

void EditorPropertyColor::_popup_closed() {
if (picker->get_pick_color() != last_color) {
get_edited_object()->set(get_edited_property(), last_color);
if (!picker->get_pick_color().is_equal_approx(last_color)) {
emit_changed(get_edited_property(), picker->get_pick_color(), "", false);
}
}
Expand Down Expand Up @@ -2682,6 +2688,10 @@ void EditorPropertyColor::setup(bool p_show_alpha) {
picker->set_edit_alpha(p_show_alpha);
}

void EditorPropertyColor::set_live_changes_enabled(bool p_enabled) {
live_changes_enabled = p_enabled;
}

EditorPropertyColor::EditorPropertyColor() {
picker = memnew(ColorPickerButton);
add_child(picker);
Expand Down
3 changes: 2 additions & 1 deletion editor/editor_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,10 @@ class EditorPropertyColor : public EditorProperty {
ColorPickerButton *picker = nullptr;
void _color_changed(const Color &p_color);
void _popup_closed();
void _picker_created();
void _picker_opening();

Color last_color;
bool live_changes_enabled = true;

protected:
virtual void _set_read_only(bool p_read_only) override;
Expand All @@ -633,6 +633,7 @@ class EditorPropertyColor : public EditorProperty {
public:
virtual void update_property() override;
void setup(bool p_show_alpha);
void set_live_changes_enabled(bool p_enabled);
EditorPropertyColor();
};

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/visual_shader_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6922,6 +6922,8 @@ Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_par
} else if (Object::cast_to<EditorPropertyEnum>(prop)) {
prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
} else if (Object::cast_to<EditorPropertyColor>(prop)) {
Object::cast_to<EditorPropertyColor>(prop)->set_live_changes_enabled(false);
}

editors.push_back(prop);
Expand Down
28 changes: 25 additions & 3 deletions scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ Color ColorPicker::get_pick_color() const {
return color;
}

Color ColorPicker::get_old_color() const {
return old_color;
}

void ColorPicker::set_picker_shape(PickerShapeType p_shape) {
ERR_FAIL_INDEX(p_shape, SHAPE_MAX);
if (p_shape == current_shape) {
Expand Down Expand Up @@ -1514,7 +1518,7 @@ void ColorPicker::_pick_finished() {
return;
}

if (Input::get_singleton()->is_key_pressed(Key::ESCAPE)) {
if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
set_pick_color(old_color);
} else {
emit_signal(SNAME("color_changed"), color);
Expand Down Expand Up @@ -1627,7 +1631,12 @@ void ColorPicker::_html_focus_exit() {
if (c_text->is_menu_visible()) {
return;
}
_html_submitted(c_text->get_text());

if (is_visible_in_tree()) {
_html_submitted(c_text->get_text());
} else {
_update_text_value();
}
}

void ColorPicker::set_can_add_swatches(bool p_enabled) {
Expand Down Expand Up @@ -2026,6 +2035,15 @@ ColorPicker::~ColorPicker() {

/////////////////

void ColorPickerPopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
_close_pressed();
}
PopupPanel::_input_from_window(p_event);
}

/////////////////

void ColorPickerButton::_about_to_popup() {
set_pressed(true);
if (picker) {
Expand All @@ -2040,6 +2058,10 @@ void ColorPickerButton::_color_changed(const Color &p_color) {
}

void ColorPickerButton::_modal_closed() {
if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
set_pick_color(picker->get_old_color());
emit_signal(SNAME("color_changed"), color);
}
emit_signal(SNAME("popup_closed"));
set_pressed(false);
}
Expand Down Expand Up @@ -2137,7 +2159,7 @@ PopupPanel *ColorPickerButton::get_popup() {

void ColorPickerButton::_update_picker() {
if (!picker) {
popup = memnew(PopupPanel);
popup = memnew(ColorPickerPopupPanel);
popup->set_wrap_controls(true);
picker = memnew(ColorPicker);
picker->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
Expand Down
7 changes: 5 additions & 2 deletions scene/gui/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,11 @@ class ColorPicker : public VBoxContainer {
void set_edit_alpha(bool p_show);
bool is_editing_alpha() const;

int get_preset_size();

void _set_pick_color(const Color &p_color, bool p_update_sliders);
void set_pick_color(const Color &p_color);
Color get_pick_color() const;
void set_old_color(const Color &p_color);
Color get_old_color() const;

void set_display_old_color(bool p_enabled);
bool is_displaying_old_color() const;
Expand Down Expand Up @@ -375,6 +374,10 @@ class ColorPicker : public VBoxContainer {
~ColorPicker();
};

class ColorPickerPopupPanel : public PopupPanel {
virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
};

class ColorPickerButton : public Button {
GDCLASS(ColorPickerButton, Button);

Expand Down

0 comments on commit d827b34

Please sign in to comment.