summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-05-08 12:20:07 +0200
committerRémi Verschelde <rverschelde@gmail.com>2023-05-08 12:20:07 +0200
commite758164ba65280dce57064afc1e4607f23a65cef (patch)
tree4a3dc3b5c7aa89d0344853207a354de89b9ef5cd
parente70777260dffd96a704c54876f9ff543f0f45eac (diff)
parent040e25816447709cb88fc415e2da6c2cbf175729 (diff)
downloadredot-engine-e758164ba65280dce57064afc1e4607f23a65cef.tar.gz
Merge pull request #72376 from MewPurPur/fix-color-button-popup
Fix popup position of color picker
-rw-r--r--scene/gui/color_picker.cpp33
1 files changed, 11 insertions, 22 deletions
diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp
index b7dc1c4fbe..5e861ba45d 100644
--- a/scene/gui/color_picker.cpp
+++ b/scene/gui/color_picker.cpp
@@ -1913,34 +1913,23 @@ void ColorPickerButton::_modal_closed() {
void ColorPickerButton::pressed() {
_update_picker();
- Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
+ Size2 minsize = popup->get_contents_minimum_size();
+ float viewport_height = get_viewport_rect().size.y;
popup->reset_size();
picker->_update_presets();
picker->_update_recent_presets();
- Rect2i usable_rect = popup->get_usable_parent_rect();
- //let's try different positions to see which one we can use
-
- Rect2i cp_rect(Point2i(), popup->get_size());
- for (int i = 0; i < 4; i++) {
- if (i > 1) {
- cp_rect.position.y = get_screen_position().y - cp_rect.size.y;
- } else {
- cp_rect.position.y = get_screen_position().y + size.height;
- }
-
- if (i & 1) {
- cp_rect.position.x = get_screen_position().x;
- } else {
- cp_rect.position.x = get_screen_position().x - MAX(0, (cp_rect.size.x - size.x));
- }
-
- if (usable_rect.encloses(cp_rect)) {
- break;
- }
+ // Determine in which direction to show the popup. By default popup horizontally centered below the button.
+ // But if the popup doesn't fit below and the button is in the bottom half of the viewport, show above.
+ bool show_above = false;
+ if (get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height) {
+ show_above = true;
}
- popup->set_position(cp_rect.position);
+
+ float h_offset = (get_size().x - minsize.x) / 2;
+ float v_offset = show_above ? -minsize.y : get_size().y;
+ popup->set_position(get_screen_position() + Vector2(h_offset, v_offset));
popup->popup();
picker->set_focus_on_line_edit();
}