Browse Source

Forward color picker preview mouse button events to the window underneath.

Pāvels Nadtočajevs 7 months ago
parent
commit
09822ea1a9
2 changed files with 31 additions and 3 deletions
  1. 30 3
      scene/gui/color_picker.cpp
  2. 1 0
      scene/gui/color_picker.h

+ 30 - 3
scene/gui/color_picker.cpp

@@ -1695,6 +1695,7 @@ void ColorPicker::_add_preset_pressed() {
 
 void ColorPicker::_pick_button_pressed() {
 	is_picking_color = true;
+	pre_picking_color = color;
 
 	if (!picker_window) {
 		picker_window = memnew(Popup);
@@ -1748,9 +1749,34 @@ void ColorPicker::_pick_button_pressed() {
 
 void ColorPicker::_target_gui_input(const Ref<InputEvent> &p_event) {
 	const Ref<InputEventMouseButton> mouse_event = p_event;
-	if (mouse_event.is_valid() && mouse_event->is_pressed()) {
+	if (mouse_event.is_null()) {
+		return;
+	}
+	if (mouse_event->get_button_index() == MouseButton::LEFT) {
+		if (mouse_event->is_pressed()) {
+			picker_window->hide();
+			_pick_finished();
+		}
+	} else if (mouse_event->get_button_index() == MouseButton::RIGHT) {
+		set_pick_color(pre_picking_color); // Cancel.
+		is_picking_color = false;
+		set_process_internal(false);
 		picker_window->hide();
-		_pick_finished();
+	} else {
+		Window *w = picker_window->get_parent_visible_window();
+		while (w) {
+			Point2i win_mpos = w->get_mouse_position(); // Mouse position local to the window.
+			Size2i win_size = w->get_size();
+			if (win_mpos.x >= 0 && win_mpos.y >= 0 && win_mpos.x <= win_size.x && win_mpos.y <= win_size.y) {
+				// Mouse event inside window bounds, forward this event to the window.
+				Ref<InputEventMouseButton> new_ev = p_event->duplicate();
+				new_ev->set_position(win_mpos);
+				new_ev->set_global_position(win_mpos);
+				w->push_input(new_ev, true);
+				return;
+			}
+			w = w->get_parent_visible_window();
+		}
 	}
 }
 
@@ -1760,7 +1786,7 @@ void ColorPicker::_pick_finished() {
 	}
 
 	if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
-		set_pick_color(old_color);
+		set_pick_color(pre_picking_color);
 	} else {
 		emit_signal(SNAME("color_changed"), color);
 	}
@@ -1856,6 +1882,7 @@ void ColorPicker::_pick_button_pressed_legacy() {
 	if (!is_inside_tree()) {
 		return;
 	}
+	pre_picking_color = color;
 
 	if (!picker_window) {
 		picker_window = memnew(Popup);

+ 1 - 0
scene/gui/color_picker.h

@@ -211,6 +211,7 @@ private:
 
 	Color color;
 	Color old_color;
+	Color pre_picking_color;
 	bool is_picking_color = false;
 
 	bool display_old_color = false;