Fix ColorPicker deferred mode not working for sliders.

This commit is contained in:
ajreckof 2023-09-19 10:47:46 +02:00
parent 571cd0eb79
commit 3160add2b0
4 changed files with 26 additions and 8 deletions

View file

@ -341,7 +341,11 @@ bool ColorPicker::is_editing_alpha() const {
return edit_alpha;
}
void ColorPicker::_value_changed(double) {
void ColorPicker::_slider_drag_started() {
currently_dragging = true;
}
void ColorPicker::_slider_value_changed() {
if (updating) {
return;
}
@ -357,7 +361,16 @@ void ColorPicker::_value_changed(double) {
}
_set_pick_color(color, false);
emit_signal(SNAME("color_changed"), color);
if (!deferred_mode_enabled || !currently_dragging) {
emit_signal(SNAME("color_changed"), color);
}
}
void ColorPicker::_slider_drag_ended() {
currently_dragging = false;
if (deferred_mode_enabled) {
emit_signal(SNAME("color_changed"), color);
}
}
void ColorPicker::add_mode(ColorMode *p_mode) {
@ -388,7 +401,9 @@ void ColorPicker::create_slider(GridContainer *gc, int idx) {
slider->set_h_size_flags(SIZE_EXPAND_FILL);
slider->connect("value_changed", callable_mp(this, &ColorPicker::_value_changed));
slider->connect("drag_started", callable_mp(this, &ColorPicker::_slider_drag_started));
slider->connect("value_changed", callable_mp(this, &ColorPicker::_slider_value_changed).unbind(1));
slider->connect("drag_ended", callable_mp(this, &ColorPicker::_slider_drag_ended).unbind(1));
slider->connect("draw", callable_mp(this, &ColorPicker::_slider_draw).bind(idx));
slider->connect("gui_input", callable_mp(this, &ColorPicker::_slider_or_spin_input));
@ -1242,7 +1257,6 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
_copy_hsv_to_color();
last_color = color;
set_pick_color(color);
_update_color();
if (!deferred_mode_enabled) {
emit_signal(SNAME("color_changed"), color);
@ -1293,7 +1307,6 @@ void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
_copy_hsv_to_color();
last_color = color;
set_pick_color(color);
_update_color();
if (!deferred_mode_enabled) {
emit_signal(SNAME("color_changed"), color);
@ -1321,7 +1334,6 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
_copy_hsv_to_color();
last_color = color;
set_pick_color(color);
_update_color();
if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
add_recent_preset(color);
@ -1347,7 +1359,6 @@ void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
_copy_hsv_to_color();
last_color = color;
set_pick_color(color);
_update_color();
if (!deferred_mode_enabled) {
emit_signal(SNAME("color_changed"), color);

View file

@ -207,6 +207,7 @@ private:
bool hex_visible = true;
bool line_edit_mouse_release = false;
bool text_changed = false;
bool currently_dragging = false;
float h = 0.0;
float s = 0.0;
@ -254,7 +255,9 @@ private:
void create_slider(GridContainer *gc, int idx);
void _reset_sliders_theme();
void _html_submitted(const String &p_html);
void _value_changed(double);
void _slider_drag_started();
void _slider_value_changed();
void _slider_drag_ended();
void _update_controls();
void _update_color(bool p_update_sliders = true);
void _update_text_value();

View file

@ -64,6 +64,7 @@ class Range : public Control {
protected:
virtual void _value_changed(double p_value);
void _notify_shared_value_changed() { shared->emit_value_changed(); };
static void _bind_methods();

View file

@ -68,15 +68,18 @@ void Slider::gui_input(const Ref<InputEvent> &p_event) {
double grab_width = (double)grabber->get_width();
double grab_height = (double)grabber->get_height();
double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
set_block_signals(true);
if (orientation == VERTICAL) {
set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
} else {
set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
}
set_block_signals(false);
grab.active = true;
grab.uvalue = get_as_ratio();
emit_signal(SNAME("drag_started"));
_notify_shared_value_changed();
} else {
grab.active = false;