Browse Source

Fixed undo/redo behaviour of color picker and added ability to cancel/confirm color selection.

Zi Ye 1 year ago
parent
commit
d827b34ea8

+ 13 - 3
editor/editor_properties.cpp

@@ -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);
 	}
 }
@@ -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);

+ 2 - 1
editor/editor_properties.h

@@ -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;
@@ -633,6 +633,7 @@ protected:
 public:
 	virtual void update_property() override;
 	void setup(bool p_show_alpha);
+	void set_live_changes_enabled(bool p_enabled);
 	EditorPropertyColor();
 };
 

+ 2 - 0
editor/plugins/visual_shader_editor_plugin.cpp

@@ -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);

+ 25 - 3
scene/gui/color_picker.cpp

@@ -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) {
@@ -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);
@@ -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) {
@@ -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) {
@@ -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);
 }
@@ -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);

+ 5 - 2
scene/gui/color_picker.h

@@ -317,12 +317,11 @@ public:
 	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;
@@ -375,6 +374,10 @@ public:
 	~ColorPicker();
 };
 
+class ColorPickerPopupPanel : public PopupPanel {
+	virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
+};
+
 class ColorPickerButton : public Button {
 	GDCLASS(ColorPickerButton, Button);