@@ -109,6 +109,13 @@ void ColorPicker::_notification(int p_what) {
109
109
picker_window->hide ();
110
110
}
111
111
} break ;
112
+
113
+ case NOTIFICATION_INTERNAL_PROCESS: {
114
+ if (!is_picking_color) {
115
+ return ;
116
+ }
117
+ set_pick_color (DisplayServer::get_singleton ()->screen_get_pixel (DisplayServer::get_singleton ()->mouse_get_position ()));
118
+ }
112
119
}
113
120
}
114
121
@@ -1421,30 +1428,6 @@ void ColorPicker::_recent_preset_pressed(const bool p_pressed, ColorPresetButton
1421
1428
emit_signal (SNAME (" color_changed" ), p_preset->get_preset_color ());
1422
1429
}
1423
1430
1424
- void ColorPicker::_picker_texture_input (const Ref<InputEvent> &p_event) {
1425
- if (!is_inside_tree ()) {
1426
- return ;
1427
- }
1428
-
1429
- Ref<InputEventMouseButton> bev = p_event;
1430
- if (bev.is_valid () && bev->get_button_index () == MouseButton::LEFT && !bev->is_pressed ()) {
1431
- set_pick_color (picker_color);
1432
- emit_signal (SNAME (" color_changed" ), color);
1433
- picker_window->hide ();
1434
- }
1435
-
1436
- Ref<InputEventMouseMotion> mev = p_event;
1437
- if (mev.is_valid ()) {
1438
- Ref<Image> img = picker_texture_rect->get_texture ()->get_image ();
1439
- if (img.is_valid () && !img->is_empty ()) {
1440
- Vector2 ofs = mev->get_position ();
1441
- picker_color = img->get_pixel (ofs.x , ofs.y );
1442
- picker_preview_style_box->set_bg_color (picker_color);
1443
- picker_preview_label->set_self_modulate (picker_color.get_luminance () < 0.5 ? Color (1 .0f , 1 .0f , 1 .0f ) : Color (0 .0f , 0 .0f , 0 .0f ));
1444
- }
1445
- }
1446
- }
1447
-
1448
1431
void ColorPicker::_text_changed (const String &) {
1449
1432
text_changed = true ;
1450
1433
}
@@ -1455,6 +1438,34 @@ void ColorPicker::_add_preset_pressed() {
1455
1438
}
1456
1439
1457
1440
void ColorPicker::_pick_button_pressed () {
1441
+ is_picking_color = true ;
1442
+ set_process_internal (true );
1443
+
1444
+ if (!picker_window) {
1445
+ picker_window = memnew (Popup);
1446
+ picker_window->set_size (Vector2i (1 , 1 ));
1447
+ picker_window->connect (" visibility_changed" , callable_mp (this , &ColorPicker::_pick_finished));
1448
+ add_child (picker_window);
1449
+ }
1450
+ picker_window->popup ();
1451
+ }
1452
+
1453
+ void ColorPicker::_pick_finished () {
1454
+ if (picker_window->is_visible ()) {
1455
+ return ;
1456
+ }
1457
+
1458
+ if (Input::get_singleton ()->is_key_pressed (Key::ESCAPE)) {
1459
+ set_pick_color (old_color);
1460
+ } else {
1461
+ emit_signal (SNAME (" color_changed" ), color);
1462
+ }
1463
+ is_picking_color = false ;
1464
+ set_process_internal (false );
1465
+ picker_window->hide ();
1466
+ }
1467
+
1468
+ void ColorPicker::_pick_button_pressed_legacy () {
1458
1469
if (!is_inside_tree ()) {
1459
1470
return ;
1460
1471
}
@@ -1469,7 +1480,7 @@ void ColorPicker::_pick_button_pressed() {
1469
1480
picker_texture_rect->set_anchors_preset (Control::PRESET_FULL_RECT);
1470
1481
picker_window->add_child (picker_texture_rect);
1471
1482
picker_texture_rect->set_default_cursor_shape (CURSOR_POINTING_HAND);
1472
- picker_texture_rect->connect (SNAME ( " gui_input" ) , callable_mp (this , &ColorPicker::_picker_texture_input));
1483
+ picker_texture_rect->connect (" gui_input" , callable_mp (this , &ColorPicker::_picker_texture_input));
1473
1484
1474
1485
picker_preview = memnew (Panel);
1475
1486
picker_preview->set_anchors_preset (Control::PRESET_CENTER_TOP);
@@ -1529,6 +1540,30 @@ void ColorPicker::_pick_button_pressed() {
1529
1540
picker_window->popup ();
1530
1541
}
1531
1542
1543
+ void ColorPicker::_picker_texture_input (const Ref<InputEvent> &p_event) {
1544
+ if (!is_inside_tree ()) {
1545
+ return ;
1546
+ }
1547
+
1548
+ Ref<InputEventMouseButton> bev = p_event;
1549
+ if (bev.is_valid () && bev->get_button_index () == MouseButton::LEFT && !bev->is_pressed ()) {
1550
+ set_pick_color (picker_color);
1551
+ emit_signal (SNAME (" color_changed" ), color);
1552
+ picker_window->hide ();
1553
+ }
1554
+
1555
+ Ref<InputEventMouseMotion> mev = p_event;
1556
+ if (mev.is_valid ()) {
1557
+ Ref<Image> img = picker_texture_rect->get_texture ()->get_image ();
1558
+ if (img.is_valid () && !img->is_empty ()) {
1559
+ Vector2 ofs = mev->get_position ();
1560
+ picker_color = img->get_pixel (ofs.x , ofs.y );
1561
+ picker_preview_style_box->set_bg_color (picker_color);
1562
+ picker_preview_label->set_self_modulate (picker_color.get_luminance () < 0.5 ? Color (1 .0f , 1 .0f , 1 .0f ) : Color (0 .0f , 0 .0f , 0 .0f ));
1563
+ }
1564
+ }
1565
+ }
1566
+
1532
1567
void ColorPicker::_html_focus_exit () {
1533
1568
if (c_text->is_menu_visible ()) {
1534
1569
return ;
@@ -1692,8 +1727,14 @@ ColorPicker::ColorPicker() {
1692
1727
1693
1728
btn_pick = memnew (Button );
1694
1729
sample_hbc->add_child (btn_pick);
1695
- btn_pick->set_tooltip_text (RTR (" Pick a color from the application window." ));
1696
- btn_pick->connect (SNAME (" pressed" ), callable_mp (this , &ColorPicker::_pick_button_pressed));
1730
+ if (DisplayServer::get_singleton ()->has_feature (DisplayServer::FEATURE_SCREEN_CAPTURE)) {
1731
+ btn_pick->set_tooltip_text (RTR (" Pick a color from the screen." ));
1732
+ btn_pick->connect (SNAME (" pressed" ), callable_mp (this , &ColorPicker::_pick_button_pressed));
1733
+ } else {
1734
+ // On unsupported platforms, use a legacy method for color picking.
1735
+ btn_pick->set_tooltip_text (RTR (" Pick a color from the application window." ));
1736
+ btn_pick->connect (SNAME (" pressed" ), callable_mp (this , &ColorPicker::_pick_button_pressed_legacy));
1737
+ }
1697
1738
1698
1739
sample = memnew (TextureRect);
1699
1740
sample_hbc->add_child (sample);
0 commit comments