Merge pull request #101266 from WhalesState/color-picking-fix

Fix color picking on Linux/X11.
This commit is contained in:
Rémi Verschelde 2025-01-09 00:42:22 +01:00
commit 8e0f498400
3 changed files with 54 additions and 23 deletions

View File

@ -788,6 +788,7 @@ bool DisplayServerMacOS::has_feature(Feature p_feature) const {
case FEATURE_STATUS_INDICATOR:
case FEATURE_NATIVE_HELP:
case FEATURE_WINDOW_DRAG:
case FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE:
return true;
default: {
}

View File

@ -138,6 +138,7 @@ bool DisplayServerWindows::has_feature(Feature p_feature) const {
case FEATURE_SCREEN_CAPTURE:
case FEATURE_STATUS_INDICATOR:
case FEATURE_WINDOW_EMBEDDING:
case FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE:
return true;
default:
return false;

View File

@ -161,13 +161,19 @@ void ColorPicker::_notification(int p_what) {
Vector2 ofs = ds->mouse_get_position();
picker_window->set_position(ofs - Vector2(28, 28));
Color c = DisplayServer::get_singleton()->screen_get_pixel(DisplayServer::get_singleton()->mouse_get_position());
Color c = DisplayServer::get_singleton()->screen_get_pixel(ofs);
picker_preview_style_box_color->set_bg_color(c);
picker_preview_style_box->set_bg_color(c.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f));
Ref<Image> zoom_preview_img = ds->screen_get_image_rect(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
picker_texture_zoom->set_texture(ImageTexture::create_from_image(zoom_preview_img));
if (ds->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE)) {
Ref<Image> zoom_preview_img = ds->screen_get_image_rect(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17));
picker_window->set_position(ofs - Vector2(28, 28));
picker_texture_zoom->set_texture(ImageTexture::create_from_image(zoom_preview_img));
} else {
Size2i screen_size = ds->screen_get_size();
picker_window->set_position(ofs + Vector2(ofs.x < screen_size.width / 2 ? 8 : -36, ofs.y < screen_size.height / 2 ? 8 : -36));
}
set_pick_color(c);
}
@ -1708,12 +1714,16 @@ void ColorPicker::_pick_button_pressed() {
if (!picker_window) {
picker_window = memnew(Popup);
picker_window->set_size(Vector2i(55, 72));
bool has_feature_exclude_from_capture = DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE);
if (!has_feature_exclude_from_capture) {
picker_window->set_size(Vector2i(28, 28));
} else {
picker_window->set_size(Vector2i(55, 72));
picker_window->set_flag(Window::FLAG_EXCLUDE_FROM_CAPTURE, true); // Only supported on MacOS and Windows.
}
picker_window->connect(SceneStringName(visibility_changed), callable_mp(this, &ColorPicker::_pick_finished));
picker_window->connect(SceneStringName(window_input), callable_mp(this, &ColorPicker::_target_gui_input));
picker_window->set_flag(Window::FLAG_EXCLUDE_FROM_CAPTURE, true);
picker_preview = memnew(Panel);
picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_preview->set_size(Vector2i(55, 72));
@ -1721,16 +1731,23 @@ void ColorPicker::_pick_button_pressed() {
picker_preview_color = memnew(Panel);
picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_preview_color->set_size(Vector2i(51, 15));
picker_preview_color->set_position(Vector2i(2, 55));
if (!has_feature_exclude_from_capture) {
picker_preview_color->set_size(Vector2i(24, 24));
picker_preview_color->set_position(Vector2i(2, 2));
} else {
picker_preview_color->set_size(Vector2i(51, 15));
picker_preview_color->set_position(Vector2i(2, 55));
}
picker_preview->add_child(picker_preview_color);
picker_texture_zoom = memnew(TextureRect);
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
picker_texture_zoom->set_position(Vector2i(2, 2));
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
picker_preview->add_child(picker_texture_zoom);
if (has_feature_exclude_from_capture) {
picker_texture_zoom = memnew(TextureRect);
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
picker_texture_zoom->set_position(Vector2i(2, 2));
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
picker_preview->add_child(picker_texture_zoom);
}
picker_preview_style_box.instantiate();
picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box);
@ -1886,23 +1903,35 @@ void ColorPicker::_pick_button_pressed_legacy() {
picker_window->add_child(picker_texture_rect);
picker_texture_rect->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_picker_texture_input));
bool has_feature_exclude_from_capture = DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE);
picker_preview = memnew(Panel);
picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_preview->set_size(Vector2i(55, 72));
if (!has_feature_exclude_from_capture) {
picker_preview->set_size(Vector2i(28, 28));
} else {
picker_preview->set_size(Vector2i(55, 72));
}
picker_window->add_child(picker_preview);
picker_preview_color = memnew(Panel);
picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_preview_color->set_size(Vector2i(51, 15));
picker_preview_color->set_position(Vector2i(2, 55));
if (!has_feature_exclude_from_capture) {
picker_preview_color->set_size(Vector2i(24, 24));
picker_preview_color->set_position(Vector2i(2, 2));
} else {
picker_preview_color->set_size(Vector2i(51, 15));
picker_preview_color->set_position(Vector2i(2, 55));
}
picker_preview->add_child(picker_preview_color);
picker_texture_zoom = memnew(TextureRect);
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
picker_texture_zoom->set_position(Vector2i(2, 2));
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
picker_preview->add_child(picker_texture_zoom);
if (has_feature_exclude_from_capture) {
picker_texture_zoom = memnew(TextureRect);
picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE);
picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51));
picker_texture_zoom->set_position(Vector2i(2, 2));
picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST);
picker_preview->add_child(picker_texture_zoom);
}
picker_preview_style_box.instantiate();
picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box);