| 1 | /**************************************************************************/ |
| 2 | /* color_picker.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "color_picker.h" |
| 32 | |
| 33 | #include "core/io/image.h" |
| 34 | #include "core/math/expression.h" |
| 35 | #include "scene/gui/color_mode.h" |
| 36 | #include "scene/gui/color_picker_shape.h" |
| 37 | #include "scene/gui/file_dialog.h" |
| 38 | #include "scene/gui/grid_container.h" |
| 39 | #include "scene/gui/label.h" |
| 40 | #include "scene/gui/line_edit.h" |
| 41 | #include "scene/gui/link_button.h" |
| 42 | #include "scene/gui/margin_container.h" |
| 43 | #include "scene/gui/menu_button.h" |
| 44 | #include "scene/gui/panel.h" |
| 45 | #include "scene/gui/popup_menu.h" |
| 46 | #include "scene/gui/slider.h" |
| 47 | #include "scene/gui/spin_box.h" |
| 48 | #include "scene/gui/texture_rect.h" |
| 49 | #include "scene/resources/atlas_texture.h" |
| 50 | #include "scene/resources/color_palette.h" |
| 51 | #include "scene/resources/image_texture.h" |
| 52 | #include "scene/resources/style_box_flat.h" |
| 53 | #include "scene/resources/style_box_texture.h" |
| 54 | #include "scene/theme/theme_db.h" |
| 55 | |
| 56 | static inline bool is_color_overbright(const Color &color) { |
| 57 | return (color.r > 1.0) || (color.g > 1.0) || (color.b > 1.0); |
| 58 | } |
| 59 | |
| 60 | static inline bool is_color_valid_hex(const Color &color) { |
| 61 | return !is_color_overbright(color) && color.r >= 0 && color.g >= 0 && color.b >= 0; |
| 62 | } |
| 63 | |
| 64 | static inline String color_to_string(const Color &color, bool show_alpha = true, bool force_value_format = false) { |
| 65 | if (!force_value_format && !is_color_overbright(color)) { |
| 66 | return "#" + color.to_html(show_alpha); |
| 67 | } |
| 68 | String t = "(" + String::num(color.r, 3) + ", " + String::num(color.g, 3) + ", " + String::num(color.b, 3); |
| 69 | if (show_alpha) { |
| 70 | t += ", " + String::num(color.a, 3) + ")"; |
| 71 | } else { |
| 72 | t += ")"; |
| 73 | } |
| 74 | return t; |
| 75 | } |
| 76 | |
| 77 | void ColorPicker::_notification(int p_what) { |
| 78 | switch (p_what) { |
| 79 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 80 | RID ae = get_accessibility_element(); |
| 81 | ERR_FAIL_COND(ae.is_null()); |
| 82 | |
| 83 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_COLOR_PICKER); |
| 84 | DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, color); |
| 85 | } break; |
| 86 | |
| 87 | case NOTIFICATION_ENTER_TREE: { |
| 88 | _update_color(); |
| 89 | } break; |
| 90 | |
| 91 | #ifdef MACOS_ENABLED |
| 92 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 93 | if (is_visible_in_tree()) { |
| 94 | perm_hb->set_visible(!OS::get_singleton()->get_granted_permissions().has("macos.permission.RECORD_SCREEN")); |
| 95 | } |
| 96 | } break; |
| 97 | #endif |
| 98 | |
| 99 | case NOTIFICATION_READY: { |
| 100 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_COLOR_PICKER)) { |
| 101 | btn_pick->set_tooltip_text(ETR("Pick a color from the screen.")); |
| 102 | btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed_native)); |
| 103 | } else if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_CAPTURE) && !get_tree()->get_root()->is_embedding_subwindows()) { |
| 104 | // FIXME: The embedding check is needed to fix a bug in single-window mode (GH-93718). |
| 105 | btn_pick->set_tooltip_text(ETR("Pick a color from the screen.")); |
| 106 | btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed)); |
| 107 | } else { |
| 108 | // On unsupported platforms, use a legacy method for color picking. |
| 109 | btn_pick->set_tooltip_text(ETR("Pick a color from the application window.")); |
| 110 | btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed_legacy)); |
| 111 | } |
| 112 | } break; |
| 113 | |
| 114 | case NOTIFICATION_THEME_CHANGED: { |
| 115 | btn_pick->set_button_icon(theme_cache.screen_picker); |
| 116 | _update_drop_down_arrow(btn_preset->is_pressed(), btn_preset); |
| 117 | _update_drop_down_arrow(btn_recent_preset->is_pressed(), btn_recent_preset); |
| 118 | btn_add_preset->set_button_icon(theme_cache.add_preset); |
| 119 | menu_btn->set_button_icon(theme_cache.menu_option); |
| 120 | btn_mode->set_button_icon(theme_cache.menu_option); |
| 121 | |
| 122 | btn_pick->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0)); |
| 123 | btn_shape->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0)); |
| 124 | btn_mode->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0)); |
| 125 | |
| 126 | { |
| 127 | int i = 0; |
| 128 | for (ColorPickerShape *shape : shapes) { |
| 129 | if (shape->is_initialized) { |
| 130 | shape->update_theme(); |
| 131 | for (Control *c : shape->controls) { |
| 132 | c->queue_redraw(); |
| 133 | } |
| 134 | } |
| 135 | shape_popup->set_item_icon(i, shape->get_icon()); |
| 136 | i++; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (current_shape != SHAPE_NONE) { |
| 141 | btn_shape->set_button_icon(shape_popup->get_item_icon(get_current_shape_index())); |
| 142 | } |
| 143 | |
| 144 | for (int i = 0; i < MODE_SLIDER_COUNT; i++) { |
| 145 | labels[i]->set_custom_minimum_size(Size2(theme_cache.label_width, 0)); |
| 146 | sliders[i]->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers); |
| 147 | } |
| 148 | alpha_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0)); |
| 149 | alpha_slider->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers); |
| 150 | intensity_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0)); |
| 151 | |
| 152 | for (int i = 0; i < MODE_BUTTON_COUNT; i++) { |
| 153 | mode_btns[i]->begin_bulk_theme_override(); |
| 154 | mode_btns[i]->add_theme_style_override(SceneStringName(pressed), theme_cache.mode_button_pressed); |
| 155 | mode_btns[i]->add_theme_style_override("hover_pressed", theme_cache.mode_button_hover_pressed); |
| 156 | mode_btns[i]->add_theme_style_override(CoreStringName(normal), theme_cache.mode_button_normal); |
| 157 | mode_btns[i]->add_theme_style_override(SceneStringName(hover), theme_cache.mode_button_hover); |
| 158 | mode_btns[i]->end_bulk_theme_override(); |
| 159 | } |
| 160 | |
| 161 | internal_margin->begin_bulk_theme_override(); |
| 162 | internal_margin->add_theme_constant_override(SNAME("margin_bottom"), theme_cache.content_margin); |
| 163 | internal_margin->add_theme_constant_override(SNAME("margin_left"), theme_cache.content_margin); |
| 164 | internal_margin->add_theme_constant_override(SNAME("margin_right"), theme_cache.content_margin); |
| 165 | internal_margin->add_theme_constant_override(SNAME("margin_top"), theme_cache.content_margin); |
| 166 | internal_margin->end_bulk_theme_override(); |
| 167 | |
| 168 | _reset_sliders_theme(); |
| 169 | |
| 170 | hex_label->set_custom_minimum_size(Size2(38 * theme_cache.base_scale, 0)); |
| 171 | // Adjust for the width of the "script" icon. |
| 172 | text_type->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0)); |
| 173 | |
| 174 | _update_controls(); |
| 175 | // HACK: Deferring updating presets to ensure their size is correct when creating ColorPicker at runtime. |
| 176 | callable_mp(this, &ColorPicker::_update_presets).call_deferred(); |
| 177 | callable_mp(this, &ColorPicker::_update_recent_presets).call_deferred(); |
| 178 | } break; |
| 179 | |
| 180 | case NOTIFICATION_WM_CLOSE_REQUEST: { |
| 181 | if (picker_window != nullptr && picker_window->is_visible()) { |
| 182 | picker_window->hide(); |
| 183 | } |
| 184 | } break; |
| 185 | |
| 186 | case NOTIFICATION_FOCUS_ENTER: |
| 187 | case NOTIFICATION_FOCUS_EXIT: { |
| 188 | if (current_shape != SHAPE_NONE) { |
| 189 | shapes[get_current_shape_index()]->cursor_editing = false; |
| 190 | } |
| 191 | } break; |
| 192 | |
| 193 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 194 | if (!is_picking_color) { |
| 195 | Input *input = Input::get_singleton(); |
| 196 | if (input->is_action_just_released("ui_left") || |
| 197 | input->is_action_just_released("ui_right") || |
| 198 | input->is_action_just_released("ui_up") || |
| 199 | input->is_action_just_released("ui_down")) { |
| 200 | gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS; |
| 201 | if (current_shape == SHAPE_NONE) { |
| 202 | shapes[get_current_shape_index()]->echo_multiplier = 1; |
| 203 | } |
| 204 | accept_event(); |
| 205 | set_process_internal(false); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | if (current_shape == SHAPE_NONE) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | gamepad_event_delay_ms -= get_process_delta_time(); |
| 214 | if (gamepad_event_delay_ms <= 0) { |
| 215 | gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms; |
| 216 | // Treat any input from joypad axis as -1, 0, or 1, as the value is added to Vector2i and would be lost. |
| 217 | Vector2 color_change_vector = Vector2( |
| 218 | input->is_action_pressed("ui_right") - input->is_action_pressed("ui_left"), |
| 219 | input->is_action_pressed("ui_down") - input->is_action_pressed("ui_up")); |
| 220 | |
| 221 | shapes[get_current_shape_index()]->update_cursor(color_change_vector, true); |
| 222 | accept_event(); |
| 223 | } |
| 224 | return; |
| 225 | } |
| 226 | DisplayServer *ds = DisplayServer::get_singleton(); |
| 227 | Vector2 ofs = ds->mouse_get_position(); |
| 228 | |
| 229 | Color c = DisplayServer::get_singleton()->screen_get_pixel(ofs); |
| 230 | |
| 231 | picker_preview_style_box_color->set_bg_color(c); |
| 232 | 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)); |
| 233 | |
| 234 | if (ds->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE)) { |
| 235 | Ref<Image> zoom_preview_img = ds->screen_get_image_rect(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17)); |
| 236 | picker_window->set_position(ofs - Vector2(28, 28)); |
| 237 | picker_texture_zoom->set_texture(ImageTexture::create_from_image(zoom_preview_img)); |
| 238 | } else { |
| 239 | Size2i screen_size = ds->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS); |
| 240 | Vector2i screen_position = ds->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS); |
| 241 | |
| 242 | float ofs_decal_x = (ofs.x < screen_position.x + screen_size.width - 51) ? 8 : -36; |
| 243 | float ofs_decal_y = (ofs.y < screen_position.y + screen_size.height - 51) ? 8 : -36; |
| 244 | |
| 245 | picker_window->set_position(ofs + Vector2(ofs_decal_x, ofs_decal_y)); |
| 246 | } |
| 247 | |
| 248 | set_pick_color(c); |
| 249 | } break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void ColorPicker::_update_theme_item_cache() { |
| 254 | VBoxContainer::_update_theme_item_cache(); |
| 255 | |
| 256 | theme_cache.base_scale = get_theme_default_base_scale(); |
| 257 | } |
| 258 | |
| 259 | void ColorPicker::set_focus_on_line_edit() { |
| 260 | callable_mp((Control *)c_text, &Control::grab_focus).call_deferred(false); |
| 261 | } |
| 262 | |
| 263 | void ColorPicker::set_focus_on_picker_shape() { |
| 264 | shapes[get_current_shape_index()]->grab_focus(); |
| 265 | } |
| 266 | |
| 267 | void ColorPicker::_update_controls() { |
| 268 | int mode_sliders_count = modes[current_mode]->get_slider_count(); |
| 269 | |
| 270 | for (int i = current_slider_count; i < mode_sliders_count; i++) { |
| 271 | sliders[i]->show(); |
| 272 | labels[i]->show(); |
| 273 | values[i]->show(); |
| 274 | } |
| 275 | for (int i = mode_sliders_count; i < current_slider_count; i++) { |
| 276 | sliders[i]->hide(); |
| 277 | labels[i]->hide(); |
| 278 | values[i]->hide(); |
| 279 | } |
| 280 | current_slider_count = mode_sliders_count; |
| 281 | |
| 282 | for (int i = 0; i < current_slider_count; i++) { |
| 283 | labels[i]->set_text(modes[current_mode]->get_slider_label(i)); |
| 284 | sliders[i]->set_accessibility_name(modes[current_mode]->get_slider_label(i)); |
| 285 | values[i]->set_accessibility_name(modes[current_mode]->get_slider_label(i)); |
| 286 | } |
| 287 | alpha_label->set_text("A"); |
| 288 | alpha_slider->set_accessibility_name(ETR("Alpha")); |
| 289 | alpha_value->set_accessibility_name(ETR("Alpha")); |
| 290 | |
| 291 | intensity_label->set_text("I"); |
| 292 | intensity_slider->set_accessibility_name(ETR("Intensity")); |
| 293 | intensity_value->set_accessibility_name(ETR("Intensity")); |
| 294 | |
| 295 | alpha_value->set_visible(edit_alpha); |
| 296 | alpha_slider->set_visible(edit_alpha); |
| 297 | alpha_label->set_visible(edit_alpha); |
| 298 | |
| 299 | intensity_value->set_visible(edit_intensity); |
| 300 | intensity_slider->set_visible(edit_intensity); |
| 301 | intensity_label->set_visible(edit_intensity); |
| 302 | |
| 303 | int i = 0; |
| 304 | for (ColorPickerShape *shape : shapes) { |
| 305 | bool is_active = get_current_shape_index() == i; |
| 306 | i++; |
| 307 | |
| 308 | if (!shape->is_initialized) { |
| 309 | if (is_active) { |
| 310 | // Controls are initialized on demand, because ColorPicker does not need them all at once. |
| 311 | shape->initialize_controls(); |
| 312 | } else { |
| 313 | continue; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | for (Control *control : shape->controls) { |
| 318 | control->set_visible(is_active); |
| 319 | } |
| 320 | } |
| 321 | btn_shape->set_visible(current_shape != SHAPE_NONE); |
| 322 | } |
| 323 | |
| 324 | void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders, bool p_calc_intensity) { |
| 325 | if (text_changed) { |
| 326 | add_recent_preset(color); |
| 327 | text_changed = false; |
| 328 | } |
| 329 | |
| 330 | color = p_color; |
| 331 | if (p_calc_intensity) { |
| 332 | _copy_color_to_normalized_and_intensity(); |
| 333 | } |
| 334 | _copy_normalized_to_hsv_okhsl(); |
| 335 | |
| 336 | if (!is_inside_tree()) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | _update_color(p_update_sliders); |
| 341 | } |
| 342 | |
| 343 | void ColorPicker::set_pick_color(const Color &p_color) { |
| 344 | _set_pick_color(p_color, true, true); // Because setters can't have more arguments. |
| 345 | } |
| 346 | |
| 347 | void ColorPicker::set_old_color(const Color &p_color) { |
| 348 | old_color = p_color; |
| 349 | } |
| 350 | |
| 351 | void ColorPicker::set_display_old_color(bool p_enabled) { |
| 352 | display_old_color = p_enabled; |
| 353 | } |
| 354 | |
| 355 | bool ColorPicker::is_displaying_old_color() const { |
| 356 | return display_old_color; |
| 357 | } |
| 358 | |
| 359 | void ColorPicker::set_edit_alpha(bool p_show) { |
| 360 | if (edit_alpha == p_show) { |
| 361 | return; |
| 362 | } |
| 363 | edit_alpha = p_show; |
| 364 | _update_controls(); |
| 365 | |
| 366 | if (!is_inside_tree()) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | _update_color(); |
| 371 | sample->queue_redraw(); |
| 372 | } |
| 373 | |
| 374 | bool ColorPicker::is_editing_alpha() const { |
| 375 | return edit_alpha; |
| 376 | } |
| 377 | |
| 378 | void ColorPicker::set_edit_intensity(bool p_show) { |
| 379 | if (edit_intensity == p_show) { |
| 380 | return; |
| 381 | } |
| 382 | if (p_show) { |
| 383 | set_pick_color(color); |
| 384 | } else { |
| 385 | _normalized_apply_intensity_to_color(); |
| 386 | color_normalized = color; |
| 387 | intensity = 0; |
| 388 | } |
| 389 | edit_intensity = p_show; |
| 390 | _update_controls(); |
| 391 | |
| 392 | if (!is_inside_tree()) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | _update_color(); |
| 397 | sample->queue_redraw(); |
| 398 | } |
| 399 | |
| 400 | bool ColorPicker::is_editing_intensity() const { |
| 401 | return edit_intensity; |
| 402 | } |
| 403 | |
| 404 | void ColorPicker::_slider_drag_started() { |
| 405 | currently_dragging = true; |
| 406 | } |
| 407 | |
| 408 | void ColorPicker::_slider_value_changed() { |
| 409 | if (updating) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | intensity = intensity_value->get_value(); |
| 414 | color_normalized = modes[current_mode]->get_color(); |
| 415 | if (edit_intensity && is_color_overbright(color_normalized)) { |
| 416 | modes[current_mode]->_greater_value_inputted(); |
| 417 | color_normalized = modes[current_mode]->get_color(); |
| 418 | } |
| 419 | _normalized_apply_intensity_to_color(); |
| 420 | intensity_value->set_prefix(intensity < 0 ? "" : "+"); |
| 421 | |
| 422 | modes[current_mode]->_value_changed(); |
| 423 | |
| 424 | _set_pick_color(color, false, false); |
| 425 | if (!deferred_mode_enabled || !currently_dragging) { |
| 426 | emit_signal(SNAME("color_changed"), color); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void ColorPicker::_slider_drag_ended() { |
| 431 | currently_dragging = false; |
| 432 | if (deferred_mode_enabled) { |
| 433 | emit_signal(SNAME("color_changed"), color); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void ColorPicker::add_mode(ColorMode *p_mode) { |
| 438 | modes.push_back(p_mode); |
| 439 | } |
| 440 | |
| 441 | void ColorPicker::add_shape(ColorPickerShape *p_shape) { |
| 442 | shapes.push_back(p_shape); |
| 443 | } |
| 444 | |
| 445 | void ColorPicker::create_slider(GridContainer *gc, int idx) { |
| 446 | Label *lbl = memnew(Label); |
| 447 | lbl->set_v_size_flags(SIZE_SHRINK_CENTER); |
| 448 | lbl->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 449 | gc->add_child(lbl); |
| 450 | |
| 451 | HSlider *slider = memnew(HSlider); |
| 452 | slider->set_v_size_flags(SIZE_SHRINK_CENTER); |
| 453 | slider->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 454 | gc->add_child(slider); |
| 455 | |
| 456 | SpinBox *val = memnew(SpinBox); |
| 457 | slider->share(val); |
| 458 | val->set_select_all_on_focus(true); |
| 459 | gc->add_child(val); |
| 460 | |
| 461 | LineEdit *vle = val->get_line_edit(); |
| 462 | vle->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed)); |
| 463 | vle->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_line_edit_input)); |
| 464 | vle->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
| 465 | |
| 466 | val->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input)); |
| 467 | |
| 468 | slider->set_h_size_flags(SIZE_EXPAND_FILL); |
| 469 | |
| 470 | slider->connect("drag_started", callable_mp(this, &ColorPicker::_slider_drag_started)); |
| 471 | slider->connect(SceneStringName(value_changed), callable_mp(this, &ColorPicker::_slider_value_changed).unbind(1)); |
| 472 | slider->connect("drag_ended", callable_mp(this, &ColorPicker::_slider_drag_ended).unbind(1)); |
| 473 | if (idx < MODE_SLIDER_COUNT) { |
| 474 | slider->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_slider_draw).bind(idx)); |
| 475 | } else if (idx == SLIDER_ALPHA) { |
| 476 | slider->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_alpha_slider_draw)); |
| 477 | } |
| 478 | slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input)); |
| 479 | |
| 480 | if (idx < MODE_SLIDER_COUNT) { |
| 481 | sliders[idx] = slider; |
| 482 | values[idx] = val; |
| 483 | labels[idx] = lbl; |
| 484 | } else if (idx == SLIDER_INTENSITY) { |
| 485 | intensity_slider = slider; |
| 486 | intensity_value = val; |
| 487 | intensity_label = lbl; |
| 488 | } else if (idx == SLIDER_ALPHA) { |
| 489 | alpha_slider = slider; |
| 490 | alpha_value = val; |
| 491 | alpha_label = lbl; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | #ifdef TOOLS_ENABLED |
| 496 | void ColorPicker::set_editor_settings(Object *p_editor_settings) { |
| 497 | if (editor_settings) { |
| 498 | return; |
| 499 | } |
| 500 | editor_settings = p_editor_settings; |
| 501 | |
| 502 | if (preset_cache.is_empty()) { |
| 503 | PackedColorArray saved_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "presets", PackedColorArray()); |
| 504 | for (int i = 0; i < saved_presets.size(); i++) { |
| 505 | preset_cache.push_back(saved_presets[i]); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | for (const Color &preset : preset_cache) { |
| 510 | presets.push_back(preset); |
| 511 | } |
| 512 | |
| 513 | if (recent_preset_cache.is_empty()) { |
| 514 | PackedColorArray saved_recent_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "recent_presets", PackedColorArray()); |
| 515 | for (int i = 0; i < saved_recent_presets.size(); i++) { |
| 516 | recent_preset_cache.push_back(saved_recent_presets[i]); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | for (const Color &preset : recent_preset_cache) { |
| 521 | recent_presets.push_back(preset); |
| 522 | } |
| 523 | |
| 524 | _update_presets(); |
| 525 | _update_recent_presets(); |
| 526 | } |
| 527 | |
| 528 | void ColorPicker::set_quick_open_callback(const Callable &p_file_selected) { |
| 529 | quick_open_callback = p_file_selected; |
| 530 | } |
| 531 | |
| 532 | void ColorPicker::set_palette_saved_callback(const Callable &p_palette_saved) { |
| 533 | palette_saved_callback = p_palette_saved; |
| 534 | } |
| 535 | |
| 536 | #endif |
| 537 | |
| 538 | HSlider *ColorPicker::get_slider(int p_idx) { |
| 539 | ERR_FAIL_INDEX_V(p_idx, MODE_MAX, nullptr); |
| 540 | return sliders[p_idx]; |
| 541 | } |
| 542 | |
| 543 | Vector<float> ColorPicker::get_active_slider_values() { |
| 544 | Vector<float> cur_values; |
| 545 | for (int i = 0; i < current_slider_count; i++) { |
| 546 | cur_values.push_back(sliders[i]->get_value()); |
| 547 | } |
| 548 | cur_values.push_back(alpha_slider->get_value()); |
| 549 | return cur_values; |
| 550 | } |
| 551 | |
| 552 | void ColorPicker::_copy_normalized_to_hsv_okhsl() { |
| 553 | if (!okhsl_cached) { |
| 554 | ok_hsl_h = color_normalized.get_ok_hsl_h(); |
| 555 | ok_hsl_s = color_normalized.get_ok_hsl_s(); |
| 556 | ok_hsl_l = color_normalized.get_ok_hsl_l(); |
| 557 | } |
| 558 | if (!hsv_cached) { |
| 559 | h = color_normalized.get_h(); |
| 560 | s = color_normalized.get_s(); |
| 561 | v = color_normalized.get_v(); |
| 562 | } |
| 563 | hsv_cached = false; |
| 564 | okhsl_cached = false; |
| 565 | } |
| 566 | |
| 567 | void ColorPicker::_copy_hsv_okhsl_to_normalized() { |
| 568 | if (current_shape != SHAPE_NONE && shapes[get_current_shape_index()]->is_ok_hsl()) { |
| 569 | color_normalized.set_ok_hsl(ok_hsl_h, ok_hsl_s, ok_hsl_l, color_normalized.a); |
| 570 | } else { |
| 571 | color_normalized.set_hsv(h, s, v, color_normalized.a); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | Color ColorPicker::_color_apply_intensity(const Color &col) const { |
| 576 | if (intensity == 0.0f) { |
| 577 | return col; |
| 578 | } |
| 579 | Color linear_color = col.srgb_to_linear(); |
| 580 | Color result; |
| 581 | float multiplier = Math::pow(2, intensity); |
| 582 | for (int i = 0; i < 3; i++) { |
| 583 | result.components[i] = linear_color.components[i] * multiplier; |
| 584 | } |
| 585 | result.a = col.a; |
| 586 | return result.linear_to_srgb(); |
| 587 | } |
| 588 | |
| 589 | void ColorPicker::_normalized_apply_intensity_to_color() { |
| 590 | color = _color_apply_intensity(color_normalized); |
| 591 | } |
| 592 | |
| 593 | void ColorPicker::_copy_color_to_normalized_and_intensity() { |
| 594 | Color linear_color = color.srgb_to_linear(); |
| 595 | float multiplier = MAX(1, MAX(MAX(linear_color.r, linear_color.g), linear_color.b)); |
| 596 | for (int i = 0; i < 3; i++) { |
| 597 | color_normalized.components[i] = linear_color.components[i] / multiplier; |
| 598 | } |
| 599 | color_normalized.a = linear_color.a; |
| 600 | color_normalized = color_normalized.linear_to_srgb(); |
| 601 | intensity = Math::log2(multiplier); |
| 602 | } |
| 603 | |
| 604 | void ColorPicker::_select_from_preset_container(const Color &p_color) { |
| 605 | if (preset_group->get_pressed_button()) { |
| 606 | preset_group->get_pressed_button()->set_pressed(false); |
| 607 | } |
| 608 | |
| 609 | for (int i = 1; i < preset_container->get_child_count(); i++) { |
| 610 | ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i)); |
| 611 | if (current_btn && p_color == current_btn->get_preset_color()) { |
| 612 | current_btn->set_pressed(true); |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | bool ColorPicker::_select_from_recent_preset_hbc(const Color &p_color) { |
| 619 | for (int i = 0; i < recent_preset_hbc->get_child_count(); i++) { |
| 620 | ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i)); |
| 621 | if (current_btn && p_color == current_btn->get_preset_color()) { |
| 622 | current_btn->set_pressed(true); |
| 623 | return true; |
| 624 | } |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | void ColorPicker::_reset_sliders_theme() { |
| 630 | Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat)); |
| 631 | style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale); |
| 632 | style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp()); |
| 633 | |
| 634 | for (int i = 0; i < MODE_SLIDER_COUNT; i++) { |
| 635 | sliders[i]->begin_bulk_theme_override(); |
| 636 | sliders[i]->add_theme_icon_override(SNAME("grabber"), theme_cache.bar_arrow); |
| 637 | sliders[i]->add_theme_icon_override(SNAME("grabber_highlight"), theme_cache.bar_arrow); |
| 638 | sliders[i]->add_theme_constant_override(SNAME("grabber_offset"), 8 * theme_cache.base_scale); |
| 639 | if (!colorize_sliders) { |
| 640 | sliders[i]->add_theme_style_override(SNAME("slider"), style_box_flat); |
| 641 | } |
| 642 | sliders[i]->end_bulk_theme_override(); |
| 643 | } |
| 644 | |
| 645 | alpha_slider->begin_bulk_theme_override(); |
| 646 | alpha_slider->add_theme_icon_override(SNAME("grabber"), theme_cache.bar_arrow); |
| 647 | alpha_slider->add_theme_icon_override(SNAME("grabber_highlight"), theme_cache.bar_arrow); |
| 648 | alpha_slider->add_theme_constant_override(SNAME("grabber_offset"), 8 * theme_cache.base_scale); |
| 649 | if (!colorize_sliders) { |
| 650 | alpha_slider->add_theme_style_override(SNAME("slider"), style_box_flat); |
| 651 | } |
| 652 | alpha_slider->end_bulk_theme_override(); |
| 653 | } |
| 654 | |
| 655 | void ColorPicker::_html_submitted(const String &p_html) { |
| 656 | if (updating) { |
| 657 | return; |
| 658 | } |
| 659 | Color new_color = color; |
| 660 | if (text_is_constructor || !is_color_valid_hex(color)) { |
| 661 | Ref<Expression> expr; |
| 662 | expr.instantiate(); |
| 663 | Error err = expr->parse(p_html); |
| 664 | if (err == OK) { |
| 665 | Variant result = expr->execute(Array(), nullptr, false, true); |
| 666 | // This is basically the same as Variant::operator Color(), but remains original color if Color::from_string() fails |
| 667 | if (result.get_type() == Variant::COLOR) { |
| 668 | new_color = result; |
| 669 | } else if (result.get_type() == Variant::STRING) { |
| 670 | new_color = Color::from_string(result, color); |
| 671 | } else if (result.get_type() == Variant::INT) { |
| 672 | new_color = Color::hex(result); |
| 673 | } |
| 674 | } |
| 675 | } else { |
| 676 | new_color = Color::from_string(p_html.strip_edges(), color); |
| 677 | String html_no_prefix = p_html.strip_edges().trim_prefix("#"); |
| 678 | if (html_no_prefix.is_valid_hex_number(false)) { |
| 679 | // Convert invalid HTML color codes that software like Figma supports. |
| 680 | if (html_no_prefix.length() == 1) { |
| 681 | // Turn `#1` into `#111111`. |
| 682 | html_no_prefix = html_no_prefix.repeat(6); |
| 683 | } else if (html_no_prefix.length() == 2) { |
| 684 | // Turn `#12` into `#121212`. |
| 685 | html_no_prefix = html_no_prefix.repeat(3); |
| 686 | } else if (html_no_prefix.length() == 5) { |
| 687 | // Turn `#12345` into `#11223344`. |
| 688 | html_no_prefix = html_no_prefix.left(4); |
| 689 | } else if (html_no_prefix.length() == 7) { |
| 690 | // Turn `#1234567` into `#123456`. |
| 691 | html_no_prefix = html_no_prefix.left(6); |
| 692 | } |
| 693 | } |
| 694 | new_color = Color::from_string(html_no_prefix, new_color); |
| 695 | } |
| 696 | |
| 697 | if (!is_editing_alpha()) { |
| 698 | new_color.a = color.a; |
| 699 | } |
| 700 | |
| 701 | if (new_color == color) { |
| 702 | return; |
| 703 | } |
| 704 | color = new_color; |
| 705 | |
| 706 | if (!is_inside_tree()) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | set_pick_color(color); |
| 711 | emit_signal(SNAME("color_changed"), color); |
| 712 | } |
| 713 | |
| 714 | void ColorPicker::_update_color(bool p_update_sliders) { |
| 715 | updating = true; |
| 716 | |
| 717 | if (p_update_sliders) { |
| 718 | float step = modes[current_mode]->get_slider_step(); |
| 719 | float spinbox_arrow_step = modes[current_mode]->get_spinbox_arrow_step(); |
| 720 | for (int i = 0; i < current_slider_count; i++) { |
| 721 | sliders[i]->set_max(modes[current_mode]->get_slider_max(i)); |
| 722 | sliders[i]->set_step(step); |
| 723 | sliders[i]->set_value(modes[current_mode]->get_slider_value(i)); |
| 724 | values[i]->set_custom_arrow_step(spinbox_arrow_step); |
| 725 | values[i]->set_allow_greater(modes[current_mode]->get_allow_greater()); |
| 726 | } |
| 727 | alpha_slider->set_max(modes[current_mode]->get_alpha_slider_max()); |
| 728 | alpha_slider->set_step(step); |
| 729 | alpha_slider->set_value(modes[current_mode]->get_alpha_slider_value()); |
| 730 | intensity_slider->set_value(intensity); |
| 731 | intensity_value->set_prefix(intensity < 0 ? "" : "+"); |
| 732 | } |
| 733 | |
| 734 | _update_text_value(); |
| 735 | |
| 736 | if (current_shape != SHAPE_NONE) { |
| 737 | for (Control *control : shapes[get_current_shape_index()]->controls) { |
| 738 | control->queue_redraw(); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | sample->queue_redraw(); |
| 743 | |
| 744 | for (int i = 0; i < current_slider_count; i++) { |
| 745 | sliders[i]->queue_redraw(); |
| 746 | } |
| 747 | alpha_slider->queue_redraw(); |
| 748 | updating = false; |
| 749 | queue_accessibility_update(); |
| 750 | } |
| 751 | |
| 752 | void ColorPicker::_update_presets() { |
| 753 | int preset_size = _get_preset_size(); |
| 754 | btn_add_preset->set_custom_minimum_size(Size2(preset_size, preset_size)); |
| 755 | // Only update the preset button size if it has changed. |
| 756 | if (preset_size != prev_preset_size) { |
| 757 | prev_preset_size = preset_size; |
| 758 | for (int i = 1; i < preset_container->get_child_count(); i++) { |
| 759 | ColorPresetButton *cpb = Object::cast_to<ColorPresetButton>(preset_container->get_child(i)); |
| 760 | cpb->set_custom_minimum_size(Size2(preset_size, preset_size)); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | #ifdef TOOLS_ENABLED |
| 765 | if (editor_settings) { |
| 766 | String cached_name = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_name", String()); |
| 767 | palette_path = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_path", String()); |
| 768 | bool palette_edited = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "palette_edited", false); |
| 769 | if (cached_name.is_empty()) { |
| 770 | palette_path = String(); |
| 771 | palette_name->hide(); |
| 772 | } else { |
| 773 | palette_name->set_text(cached_name); |
| 774 | if (btn_preset->is_pressed() && !presets.is_empty()) { |
| 775 | palette_name->show(); |
| 776 | } |
| 777 | |
| 778 | if (palette_edited) { |
| 779 | palette_name->set_text(vformat("%s*", palette_name->get_text().remove_char('*'))); |
| 780 | palette_name->set_tooltip_text(TTRC("The changes to this palette have not been saved to a file.")); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | if (presets_just_loaded || presets.is_empty() || Engine::get_singleton()->is_editor_hint()) { |
| 787 | // Rebuild swatch color buttons, keeping the add-preset button in the first position. |
| 788 | for (int i = 1; i < preset_container->get_child_count(); i++) { |
| 789 | preset_container->get_child(i)->queue_free(); |
| 790 | } |
| 791 | |
| 792 | presets = preset_cache; |
| 793 | for (const Color &preset : presets) { |
| 794 | _add_preset_button(preset_size, preset); |
| 795 | } |
| 796 | presets_just_loaded = false; |
| 797 | } |
| 798 | |
| 799 | _notification(NOTIFICATION_VISIBILITY_CHANGED); |
| 800 | } |
| 801 | |
| 802 | void ColorPicker::_update_recent_presets() { |
| 803 | #ifdef TOOLS_ENABLED |
| 804 | if (editor_settings) { |
| 805 | int recent_preset_count = recent_preset_hbc->get_child_count(); |
| 806 | for (int i = 0; i < recent_preset_count; i++) { |
| 807 | memdelete(recent_preset_hbc->get_child(0)); |
| 808 | } |
| 809 | |
| 810 | recent_presets.clear(); |
| 811 | for (const Color &preset : recent_preset_cache) { |
| 812 | recent_presets.push_back(preset); |
| 813 | } |
| 814 | |
| 815 | int preset_size = _get_preset_size(); |
| 816 | for (const Color &preset : recent_presets) { |
| 817 | _add_recent_preset_button(preset_size, preset); |
| 818 | } |
| 819 | |
| 820 | _notification(NOTIFICATION_VISIBILITY_CHANGED); |
| 821 | } |
| 822 | #endif |
| 823 | } |
| 824 | |
| 825 | #ifdef TOOLS_ENABLED |
| 826 | void ColorPicker::_text_type_toggled() { |
| 827 | text_is_constructor = !text_is_constructor; |
| 828 | if (text_is_constructor) { |
| 829 | hex_label->set_text(ETR("Expr")); |
| 830 | text_type->set_text(""); |
| 831 | text_type->set_button_icon(theme_cache.color_script); |
| 832 | |
| 833 | c_text->set_tooltip_text(RTR("Execute an expression as a color.")); |
| 834 | } else { |
| 835 | hex_label->set_text(ETR("Hex")); |
| 836 | text_type->set_text("#"); |
| 837 | text_type->set_button_icon(nullptr); |
| 838 | |
| 839 | c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\").")); |
| 840 | } |
| 841 | _update_color(); |
| 842 | } |
| 843 | #endif // TOOLS_ENABLED |
| 844 | |
| 845 | Color ColorPicker::get_pick_color() const { |
| 846 | return color; |
| 847 | } |
| 848 | |
| 849 | Color ColorPicker::get_old_color() const { |
| 850 | return old_color; |
| 851 | } |
| 852 | |
| 853 | void ColorPicker::set_picker_shape(PickerShapeType p_shape) { |
| 854 | ERR_FAIL_INDEX(p_shape, SHAPE_MAX); |
| 855 | if (p_shape == current_shape) { |
| 856 | return; |
| 857 | } |
| 858 | if (current_shape != SHAPE_NONE) { |
| 859 | shape_popup->set_item_checked(get_current_shape_index(), false); |
| 860 | } |
| 861 | if (p_shape != SHAPE_NONE) { |
| 862 | shape_popup->set_item_checked(shape_to_index(p_shape), true); |
| 863 | btn_shape->set_button_icon(shape_popup->get_item_icon(shape_to_index(p_shape))); |
| 864 | } |
| 865 | |
| 866 | current_shape = p_shape; |
| 867 | |
| 868 | #ifdef TOOLS_ENABLED |
| 869 | if (editor_settings) { |
| 870 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "picker_shape", current_shape); |
| 871 | } |
| 872 | #endif |
| 873 | |
| 874 | _copy_normalized_to_hsv_okhsl(); |
| 875 | _update_controls(); |
| 876 | _update_color(); |
| 877 | } |
| 878 | |
| 879 | ColorPicker::PickerShapeType ColorPicker::get_picker_shape() const { |
| 880 | return current_shape; |
| 881 | } |
| 882 | |
| 883 | inline int ColorPicker::_get_preset_size() { |
| 884 | return (int(get_minimum_size().width) - (preset_container->get_h_separation() * (PRESET_COLUMN_COUNT - 1))) / PRESET_COLUMN_COUNT; |
| 885 | } |
| 886 | |
| 887 | void ColorPicker::_add_preset_button(int p_size, const Color &p_color) { |
| 888 | ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, false)); |
| 889 | SET_DRAG_FORWARDING_GCDU(btn_preset_new, ColorPicker); |
| 890 | btn_preset_new->set_button_group(preset_group); |
| 891 | preset_container->add_child(btn_preset_new); |
| 892 | btn_preset_new->set_pressed(true); |
| 893 | btn_preset_new->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_preset_input).bind(p_color)); |
| 894 | } |
| 895 | |
| 896 | void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) { |
| 897 | ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, true)); |
| 898 | btn_preset_new->set_button_group(recent_preset_group); |
| 899 | recent_preset_hbc->add_child(btn_preset_new); |
| 900 | recent_preset_hbc->move_child(btn_preset_new, 0); |
| 901 | btn_preset_new->set_pressed(true); |
| 902 | btn_preset_new->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_recent_preset_pressed).bind(btn_preset_new)); |
| 903 | } |
| 904 | |
| 905 | void ColorPicker::_load_palette() { |
| 906 | List<String> extensions; |
| 907 | ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions); |
| 908 | |
| 909 | file_dialog->set_title(ETR("Load Color Palette")); |
| 910 | file_dialog->clear_filters(); |
| 911 | for (const String &K : extensions) { |
| 912 | file_dialog->add_filter("*." + K); |
| 913 | } |
| 914 | |
| 915 | file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE); |
| 916 | file_dialog->set_current_file(""); |
| 917 | file_dialog->popup_centered_ratio(); |
| 918 | } |
| 919 | |
| 920 | void ColorPicker::_save_palette(bool p_is_save_as) { |
| 921 | if (!p_is_save_as && !palette_path.is_empty()) { |
| 922 | file_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE); |
| 923 | _palette_file_selected(palette_path); |
| 924 | return; |
| 925 | } else { |
| 926 | List<String> extensions; |
| 927 | ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions); |
| 928 | |
| 929 | file_dialog->set_title(ETR("Save Color Palette")); |
| 930 | file_dialog->clear_filters(); |
| 931 | for (const String &K : extensions) { |
| 932 | file_dialog->add_filter("*." + K); |
| 933 | } |
| 934 | |
| 935 | file_dialog->set_file_mode(FileDialog::FILE_MODE_SAVE_FILE); |
| 936 | file_dialog->set_current_file("new_palette.tres"); |
| 937 | file_dialog->popup_centered_ratio(); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | #ifdef TOOLS_ENABLED |
| 942 | void ColorPicker::_quick_open_palette_file_selected(const String &p_path) { |
| 943 | _ensure_file_dialog(); |
| 944 | file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE); |
| 945 | _palette_file_selected(p_path); |
| 946 | } |
| 947 | |
| 948 | GridContainer *ColorPicker::get_slider_container() { |
| 949 | return slider_gc; |
| 950 | } |
| 951 | |
| 952 | #endif // ifdef TOOLS_ENABLED |
| 953 | |
| 954 | void ColorPicker::_palette_file_selected(const String &p_path) { |
| 955 | switch (file_dialog->get_file_mode()) { |
| 956 | case FileDialog::FileMode::FILE_MODE_OPEN_FILE: { |
| 957 | Ref<ColorPalette> palette = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE); |
| 958 | ERR_FAIL_COND_MSG(palette.is_null(), vformat("Cannot open color palette file for reading at: %s", p_path)); |
| 959 | preset_cache.clear(); |
| 960 | presets.clear(); |
| 961 | |
| 962 | PackedColorArray saved_presets = palette->get_colors(); |
| 963 | for (const Color &saved_preset : saved_presets) { |
| 964 | preset_cache.push_back(saved_preset); |
| 965 | presets.push_back(saved_preset); |
| 966 | } |
| 967 | presets_just_loaded = true; |
| 968 | |
| 969 | #ifdef TOOLS_ENABLED |
| 970 | if (editor_settings) { |
| 971 | const StringName set_project_metadata = SNAME("set_project_metadata"); |
| 972 | editor_settings->call(set_project_metadata, "color_picker", "presets", saved_presets); |
| 973 | editor_settings->call(set_project_metadata, "color_picker", "palette_edited", false); |
| 974 | } |
| 975 | #endif |
| 976 | } break; |
| 977 | case FileDialog::FileMode::FILE_MODE_SAVE_FILE: { |
| 978 | Ref<ColorPalette> palette; |
| 979 | palette.instantiate(); |
| 980 | palette->set_colors(get_presets()); |
| 981 | Error error = ResourceSaver::save(palette, p_path); |
| 982 | ERR_FAIL_COND_MSG(error != Error::OK, vformat("Cannot open color palette file for writing at: %s", p_path)); |
| 983 | #ifdef TOOLS_ENABLED |
| 984 | if (palette_saved_callback.is_valid()) { |
| 985 | palette_saved_callback.call_deferred(p_path); |
| 986 | } |
| 987 | #endif // TOOLS_ENABLED |
| 988 | } break; |
| 989 | default: |
| 990 | break; |
| 991 | } |
| 992 | |
| 993 | palette_name->set_text(p_path.get_file().get_basename()); |
| 994 | palette_name->set_tooltip_text(""); |
| 995 | palette_name->show(); |
| 996 | palette_path = p_path; |
| 997 | btn_preset->set_pressed(true); |
| 998 | #ifdef TOOLS_ENABLED |
| 999 | if (editor_settings) { |
| 1000 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_name", palette_name->get_text()); |
| 1001 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_path", palette_path); |
| 1002 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_edited", false); |
| 1003 | } |
| 1004 | #endif |
| 1005 | if (file_dialog->get_file_mode() == FileDialog::FileMode::FILE_MODE_OPEN_FILE) { |
| 1006 | _update_presets(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | void ColorPicker::_show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container) { |
| 1011 | if (p_is_btn_pressed) { |
| 1012 | p_preset_container->show(); |
| 1013 | } else { |
| 1014 | p_preset_container->hide(); |
| 1015 | } |
| 1016 | _update_drop_down_arrow(p_is_btn_pressed, p_btn_preset); |
| 1017 | |
| 1018 | palette_name->hide(); |
| 1019 | if (btn_preset->is_pressed() && !palette_name->get_text().is_empty()) { |
| 1020 | palette_name->show(); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | void ColorPicker::_update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset) { |
| 1025 | if (p_is_btn_pressed) { |
| 1026 | p_btn_preset->set_button_icon(theme_cache.expanded_arrow); |
| 1027 | } else { |
| 1028 | p_btn_preset->set_button_icon(theme_cache.folded_arrow); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | void ColorPicker::_set_mode_popup_value(ColorModeType p_mode) { |
| 1033 | ERR_FAIL_INDEX(p_mode, MODE_MAX + 1); |
| 1034 | |
| 1035 | if (p_mode == MODE_MAX) { |
| 1036 | set_colorize_sliders(!colorize_sliders); |
| 1037 | } else { |
| 1038 | set_color_mode(p_mode); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | Variant ColorPicker::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) { |
| 1043 | ColorPresetButton *dragged_preset_button = Object::cast_to<ColorPresetButton>(p_from_control); |
| 1044 | |
| 1045 | if (!dragged_preset_button) { |
| 1046 | return Variant(); |
| 1047 | } |
| 1048 | |
| 1049 | ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size(), false)); |
| 1050 | set_drag_preview(drag_preview); |
| 1051 | |
| 1052 | Dictionary drag_data; |
| 1053 | drag_data["type"] = "color_preset"; |
| 1054 | drag_data["color_preset"] = dragged_preset_button->get_index(); |
| 1055 | |
| 1056 | return drag_data; |
| 1057 | } |
| 1058 | |
| 1059 | bool ColorPicker::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const { |
| 1060 | Dictionary d = p_data; |
| 1061 | if (!d.has("type") || String(d["type"]) != "color_preset") { |
| 1062 | return false; |
| 1063 | } |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
| 1067 | void ColorPicker::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) { |
| 1068 | Dictionary d = p_data; |
| 1069 | if (!d.has("type")) { |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | if (String(d["type"]) == "color_preset") { |
| 1074 | int preset_from_id = d["color_preset"]; |
| 1075 | int hover_now = p_from_control->get_index(); |
| 1076 | |
| 1077 | if (preset_from_id == hover_now || hover_now == -1) { |
| 1078 | return; |
| 1079 | } |
| 1080 | preset_container->move_child(preset_container->get_child(preset_from_id), hover_now); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | void ColorPicker::_ensure_file_dialog() { |
| 1085 | if (file_dialog) { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | file_dialog = memnew(FileDialog); |
| 1090 | file_dialog->set_mode_overrides_title(false); |
| 1091 | file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM); |
| 1092 | file_dialog->set_current_dir(Engine::get_singleton()->is_editor_hint() ? "res://" : "user://"); |
| 1093 | add_child(file_dialog, false, INTERNAL_MODE_FRONT); |
| 1094 | file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_palette_file_selected)); |
| 1095 | } |
| 1096 | |
| 1097 | void ColorPicker::add_preset(const Color &p_color) { |
| 1098 | List<Color>::Element *e = presets.find(p_color); |
| 1099 | if (e) { |
| 1100 | presets.move_to_back(e); |
| 1101 | |
| 1102 | for (int i = 1; i < preset_container->get_child_count(); i++) { |
| 1103 | ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i)); |
| 1104 | if (current_btn && p_color == current_btn->get_preset_color()) { |
| 1105 | preset_container->move_child(current_btn, preset_container->get_child_count() - 1); |
| 1106 | current_btn->set_pressed(true); |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | } else { |
| 1111 | presets.push_back(p_color); |
| 1112 | |
| 1113 | _add_preset_button(_get_preset_size(), p_color); |
| 1114 | } |
| 1115 | |
| 1116 | List<Color>::Element *cache_e = preset_cache.find(p_color); |
| 1117 | if (cache_e) { |
| 1118 | preset_cache.move_to_back(cache_e); |
| 1119 | } else { |
| 1120 | preset_cache.push_back(p_color); |
| 1121 | } |
| 1122 | |
| 1123 | if (!palette_name->get_text().is_empty()) { |
| 1124 | palette_name->set_text(vformat("%s*", palette_name->get_text().trim_suffix("*"))); |
| 1125 | palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file.")); |
| 1126 | } |
| 1127 | |
| 1128 | #ifdef TOOLS_ENABLED |
| 1129 | if (editor_settings) { |
| 1130 | PackedColorArray arr_to_save = get_presets(); |
| 1131 | const StringName set_project_metadata = SNAME("set_project_metadata"); |
| 1132 | editor_settings->call(set_project_metadata, "color_picker", "presets", arr_to_save); |
| 1133 | editor_settings->call(set_project_metadata, "color_picker", "palette_edited", true); |
| 1134 | } |
| 1135 | #endif |
| 1136 | } |
| 1137 | |
| 1138 | void ColorPicker::add_recent_preset(const Color &p_color) { |
| 1139 | if (!_select_from_recent_preset_hbc(p_color)) { |
| 1140 | if (recent_preset_hbc->get_child_count() >= PRESET_COLUMN_COUNT) { |
| 1141 | recent_preset_cache.pop_front(); |
| 1142 | recent_presets.pop_front(); |
| 1143 | recent_preset_hbc->get_child(PRESET_COLUMN_COUNT - 1)->queue_free(); |
| 1144 | } |
| 1145 | recent_presets.push_back(p_color); |
| 1146 | recent_preset_cache.push_back(p_color); |
| 1147 | _add_recent_preset_button(_get_preset_size(), p_color); |
| 1148 | } |
| 1149 | _select_from_preset_container(p_color); |
| 1150 | |
| 1151 | #ifdef TOOLS_ENABLED |
| 1152 | if (editor_settings) { |
| 1153 | PackedColorArray arr_to_save = get_recent_presets(); |
| 1154 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save); |
| 1155 | } |
| 1156 | #endif |
| 1157 | } |
| 1158 | |
| 1159 | void ColorPicker::erase_preset(const Color &p_color) { |
| 1160 | List<Color>::Element *e = presets.find(p_color); |
| 1161 | if (e) { |
| 1162 | presets.erase(e); |
| 1163 | preset_cache.erase(preset_cache.find(p_color)); |
| 1164 | |
| 1165 | // Find preset button to remove. |
| 1166 | for (int i = 1; i < preset_container->get_child_count(); i++) { |
| 1167 | ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i)); |
| 1168 | if (current_btn && p_color == current_btn->get_preset_color()) { |
| 1169 | current_btn->queue_free(); |
| 1170 | // Removing focused control loose the focus totally. We focus on previous button to keep it possible to navigate with keyboard/joypad. |
| 1171 | Control *focus_target = Object::cast_to<Control>(preset_container->get_child(i - 1)); |
| 1172 | focus_target->grab_focus(); |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | palette_name->set_text(vformat("%s*", palette_name->get_text().remove_char('*'))); |
| 1178 | palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file.")); |
| 1179 | if (presets.is_empty()) { |
| 1180 | palette_name->set_text(""); |
| 1181 | palette_path = String(); |
| 1182 | palette_name->hide(); |
| 1183 | } |
| 1184 | |
| 1185 | #ifdef TOOLS_ENABLED |
| 1186 | if (editor_settings) { |
| 1187 | PackedColorArray arr_to_save = get_presets(); |
| 1188 | const StringName set_project_metadata = SNAME("set_project_metadata"); |
| 1189 | editor_settings->call(set_project_metadata, "color_picker", "presets", arr_to_save); |
| 1190 | editor_settings->call(set_project_metadata, "color_picker", "palette_edited", true); |
| 1191 | editor_settings->call(set_project_metadata, "color_picker", "palette_name", palette_name->get_text()); |
| 1192 | editor_settings->call(set_project_metadata, "color_picker", "palette_path", palette_path); |
| 1193 | } |
| 1194 | #endif |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | void ColorPicker::erase_recent_preset(const Color &p_color) { |
| 1199 | List<Color>::Element *e = recent_presets.find(p_color); |
| 1200 | if (e) { |
| 1201 | recent_presets.erase(e); |
| 1202 | recent_preset_cache.erase(recent_preset_cache.find(p_color)); |
| 1203 | |
| 1204 | // Find recent preset button to remove. |
| 1205 | for (int i = 1; i < recent_preset_hbc->get_child_count(); i++) { |
| 1206 | ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i)); |
| 1207 | if (current_btn && p_color == current_btn->get_preset_color()) { |
| 1208 | current_btn->queue_free(); |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | #ifdef TOOLS_ENABLED |
| 1214 | if (editor_settings) { |
| 1215 | PackedColorArray arr_to_save = get_recent_presets(); |
| 1216 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save); |
| 1217 | } |
| 1218 | #endif |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | PackedColorArray ColorPicker::get_presets() const { |
| 1223 | PackedColorArray arr; |
| 1224 | arr.resize(presets.size()); |
| 1225 | int i = 0; |
| 1226 | for (List<Color>::ConstIterator itr = presets.begin(); itr != presets.end(); ++itr, ++i) { |
| 1227 | arr.set(i, *itr); |
| 1228 | } |
| 1229 | return arr; |
| 1230 | } |
| 1231 | |
| 1232 | PackedColorArray ColorPicker::get_recent_presets() const { |
| 1233 | PackedColorArray arr; |
| 1234 | arr.resize(recent_presets.size()); |
| 1235 | int i = 0; |
| 1236 | for (List<Color>::ConstIterator itr = recent_presets.begin(); itr != recent_presets.end(); ++itr, ++i) { |
| 1237 | arr.set(i, *itr); |
| 1238 | } |
| 1239 | return arr; |
| 1240 | } |
| 1241 | |
| 1242 | void ColorPicker::set_color_mode(ColorModeType p_mode) { |
| 1243 | ERR_FAIL_INDEX(p_mode, MODE_MAX); |
| 1244 | |
| 1245 | if (current_mode == p_mode) { |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | mode_popup->set_item_checked(current_mode, false); |
| 1250 | mode_popup->set_item_checked(p_mode, true); |
| 1251 | |
| 1252 | if (p_mode < MODE_BUTTON_COUNT) { |
| 1253 | mode_btns[p_mode]->set_pressed(true); |
| 1254 | } else if (current_mode < MODE_BUTTON_COUNT) { |
| 1255 | mode_btns[current_mode]->set_pressed(false); |
| 1256 | } |
| 1257 | |
| 1258 | current_mode = p_mode; |
| 1259 | |
| 1260 | #ifdef TOOLS_ENABLED |
| 1261 | if (editor_settings) { |
| 1262 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "color_mode", current_mode); |
| 1263 | } |
| 1264 | #endif |
| 1265 | |
| 1266 | if (!is_inside_tree()) { |
| 1267 | return; |
| 1268 | } |
| 1269 | |
| 1270 | _update_controls(); |
| 1271 | _update_color(); |
| 1272 | } |
| 1273 | |
| 1274 | ColorPicker::ColorModeType ColorPicker::get_color_mode() const { |
| 1275 | return current_mode; |
| 1276 | } |
| 1277 | |
| 1278 | void ColorPicker::set_colorize_sliders(bool p_colorize_sliders) { |
| 1279 | if (colorize_sliders == p_colorize_sliders) { |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | colorize_sliders = p_colorize_sliders; |
| 1284 | mode_popup->set_item_checked(MODE_MAX + 1, colorize_sliders); |
| 1285 | |
| 1286 | if (colorize_sliders) { |
| 1287 | Ref<StyleBoxEmpty> style_box_empty(memnew(StyleBoxEmpty)); |
| 1288 | |
| 1289 | for (int i = 0; i < MODE_SLIDER_COUNT; i++) { |
| 1290 | sliders[i]->add_theme_style_override("slider", style_box_empty); |
| 1291 | } |
| 1292 | |
| 1293 | alpha_slider->add_theme_style_override("slider", style_box_empty); |
| 1294 | } else { |
| 1295 | Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat)); |
| 1296 | style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale); |
| 1297 | style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp()); |
| 1298 | |
| 1299 | for (int i = 0; i < MODE_SLIDER_COUNT; i++) { |
| 1300 | sliders[i]->add_theme_style_override("slider", style_box_flat); |
| 1301 | } |
| 1302 | |
| 1303 | alpha_slider->add_theme_style_override("slider", style_box_flat); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | bool ColorPicker::is_colorizing_sliders() const { |
| 1308 | return colorize_sliders; |
| 1309 | } |
| 1310 | |
| 1311 | void ColorPicker::set_deferred_mode(bool p_enabled) { |
| 1312 | deferred_mode_enabled = p_enabled; |
| 1313 | } |
| 1314 | |
| 1315 | bool ColorPicker::is_deferred_mode() const { |
| 1316 | return deferred_mode_enabled; |
| 1317 | } |
| 1318 | |
| 1319 | void ColorPicker::_update_text_value() { |
| 1320 | if (text_is_constructor || !is_color_valid_hex(color)) { |
| 1321 | String t = "Color" + color_to_string(color, edit_alpha && color.a < 1, true); |
| 1322 | |
| 1323 | text_type->set_text(""); |
| 1324 | text_type->set_button_icon(theme_cache.color_script); |
| 1325 | text_type->set_disabled(!is_color_valid_hex(color)); |
| 1326 | hex_label->set_text(ETR("Expr")); |
| 1327 | c_text->set_text(t); |
| 1328 | c_text->set_tooltip_text(RTR("Execute an expression as a color.")); |
| 1329 | } else { |
| 1330 | text_type->set_text("#"); |
| 1331 | text_type->set_button_icon(nullptr); |
| 1332 | text_type->set_disabled(false); |
| 1333 | hex_label->set_text(ETR("Hex")); |
| 1334 | c_text->set_text(color.to_html(edit_alpha && color.a < 1)); |
| 1335 | c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\").")); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | void ColorPicker::_sample_input(const Ref<InputEvent> &p_event) { |
| 1340 | if (!display_old_color) { |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | const Ref<InputEventMouseButton> mb = p_event; |
| 1345 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
| 1346 | const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95)); |
| 1347 | if (rect_old.has_point(mb->get_position())) { |
| 1348 | // Revert to the old color when left-clicking the old color sample. |
| 1349 | set_pick_color(old_color); |
| 1350 | |
| 1351 | sample->set_focus_mode(FOCUS_NONE); |
| 1352 | emit_signal(SNAME("color_changed"), color); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) { |
| 1357 | set_pick_color(old_color); |
| 1358 | emit_signal(SNAME("color_changed"), color); |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | void ColorPicker::_sample_draw() { |
| 1363 | // Covers the right half of the sample if the old color is being displayed, |
| 1364 | // or the whole sample if it's not being displayed. |
| 1365 | Rect2 rect_new; |
| 1366 | Rect2 rect_old; |
| 1367 | |
| 1368 | if (display_old_color) { |
| 1369 | rect_new = Rect2(Point2(sample->get_size().width * 0.5, 0), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95)); |
| 1370 | |
| 1371 | // Draw both old and new colors for easier comparison (only if spawned from a ColorPickerButton). |
| 1372 | rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95)); |
| 1373 | |
| 1374 | if (old_color.a < 1.0) { |
| 1375 | sample->draw_texture_rect(theme_cache.sample_bg, rect_old, true); |
| 1376 | } |
| 1377 | |
| 1378 | sample->draw_rect(rect_old, old_color); |
| 1379 | |
| 1380 | if (!old_color.is_equal_approx(color)) { |
| 1381 | // Draw a revert indicator to indicate that the old sample can be clicked to revert to this old color. |
| 1382 | // Adapt icon color to the background color (taking alpha checkerboard into account) so that it's always visible. |
| 1383 | sample->draw_texture(theme_cache.sample_revert, |
| 1384 | rect_old.size * 0.5 - theme_cache.sample_revert->get_size() * 0.5, |
| 1385 | Math::lerp(0.75f, old_color.get_luminance(), old_color.a) < 0.455 ? Color(1, 1, 1) : (Color(0.01, 0.01, 0.01))); |
| 1386 | |
| 1387 | sample->set_focus_mode(FOCUS_ALL); |
| 1388 | } else { |
| 1389 | sample->set_focus_mode(FOCUS_NONE); |
| 1390 | } |
| 1391 | |
| 1392 | if (is_color_overbright(color)) { |
| 1393 | // Draw an indicator to denote that the old color is "overbright" and can't be displayed accurately in the preview. |
| 1394 | sample->draw_texture(theme_cache.overbright_indicator, Point2()); |
| 1395 | } |
| 1396 | } else { |
| 1397 | rect_new = Rect2(Point2(), Size2(sample->get_size().width, sample->get_size().height * 0.95)); |
| 1398 | } |
| 1399 | |
| 1400 | if (color.a < 1.0) { |
| 1401 | sample->draw_texture_rect(theme_cache.sample_bg, rect_new, true); |
| 1402 | } |
| 1403 | |
| 1404 | sample->draw_rect(rect_new, color); |
| 1405 | |
| 1406 | if (display_old_color && !old_color.is_equal_approx(color) && sample->has_focus(true)) { |
| 1407 | RID ci = sample->get_canvas_item(); |
| 1408 | theme_cache.sample_focus->draw(ci, rect_old); |
| 1409 | } |
| 1410 | |
| 1411 | if (is_color_overbright(color)) { |
| 1412 | // Draw an indicator to denote that the new color is "overbright" and can't be displayed accurately in the preview. |
| 1413 | sample->draw_texture(theme_cache.overbright_indicator, Point2(sample->get_size().width * 0.5, 0)); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | void ColorPicker::_slider_draw(int p_which) { |
| 1418 | if (colorize_sliders) { |
| 1419 | modes[current_mode]->slider_draw(p_which); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | void ColorPicker::_alpha_slider_draw() { |
| 1424 | if (!colorize_sliders) { |
| 1425 | return; |
| 1426 | } |
| 1427 | Vector<Vector2> pos; |
| 1428 | pos.resize(4); |
| 1429 | Vector<Color> col; |
| 1430 | col.resize(4); |
| 1431 | Size2 size = alpha_slider->get_size(); |
| 1432 | Color left_color; |
| 1433 | Color right_color; |
| 1434 | const real_t margin = 16 * theme_cache.base_scale; |
| 1435 | alpha_slider->draw_texture_rect(theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true); |
| 1436 | |
| 1437 | left_color = color_normalized; |
| 1438 | left_color.a = 0; |
| 1439 | right_color = color_normalized; |
| 1440 | right_color.a = 1; |
| 1441 | |
| 1442 | col.set(0, left_color); |
| 1443 | col.set(1, right_color); |
| 1444 | col.set(2, right_color); |
| 1445 | col.set(3, left_color); |
| 1446 | pos.set(0, Vector2(0, 0)); |
| 1447 | pos.set(1, Vector2(size.x, 0)); |
| 1448 | pos.set(2, Vector2(size.x, margin)); |
| 1449 | pos.set(3, Vector2(0, margin)); |
| 1450 | |
| 1451 | alpha_slider->draw_polygon(pos, col); |
| 1452 | } |
| 1453 | |
| 1454 | void ColorPicker::_slider_or_spin_input(const Ref<InputEvent> &p_event) { |
| 1455 | if (line_edit_mouse_release) { |
| 1456 | line_edit_mouse_release = false; |
| 1457 | return; |
| 1458 | } |
| 1459 | Ref<InputEventMouseButton> bev = p_event; |
| 1460 | if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) { |
| 1461 | add_recent_preset(color); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | void ColorPicker::_line_edit_input(const Ref<InputEvent> &p_event) { |
| 1466 | Ref<InputEventMouseButton> bev = p_event; |
| 1467 | if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) { |
| 1468 | line_edit_mouse_release = true; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | void ColorPicker::_preset_input(const Ref<InputEvent> &p_event, const Color &p_color) { |
| 1473 | Ref<InputEventMouseButton> bev = p_event; |
| 1474 | |
| 1475 | if (bev.is_valid()) { |
| 1476 | if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) { |
| 1477 | set_pick_color(p_color); |
| 1478 | add_recent_preset(color); |
| 1479 | emit_signal(SNAME("color_changed"), p_color); |
| 1480 | } else if (bev->is_pressed() && bev->get_button_index() == MouseButton::RIGHT && can_add_swatches) { |
| 1481 | erase_preset(p_color); |
| 1482 | emit_signal(SNAME("preset_removed"), p_color); |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) { |
| 1487 | set_pick_color(p_color); |
| 1488 | add_recent_preset(color); |
| 1489 | emit_signal(SNAME("color_changed"), p_color); |
| 1490 | } else if (p_event->is_action_pressed(SNAME("ui_colorpicker_delete_preset"), false, true) && can_add_swatches) { |
| 1491 | erase_preset(p_color); |
| 1492 | emit_signal(SNAME("preset_removed"), p_color); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | void ColorPicker::_recent_preset_pressed(const bool p_pressed, ColorPresetButton *p_preset) { |
| 1497 | if (!p_pressed) { |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | // Avoid applying and recalculating the intensity for non-overbright color if it doesn't change. |
| 1502 | if (color != p_preset->get_preset_color()) { |
| 1503 | set_pick_color(p_preset->get_preset_color()); |
| 1504 | } |
| 1505 | |
| 1506 | recent_presets.move_to_back(recent_presets.find(p_preset->get_preset_color())); |
| 1507 | List<Color>::Element *e = recent_preset_cache.find(p_preset->get_preset_color()); |
| 1508 | if (e) { |
| 1509 | recent_preset_cache.move_to_back(e); |
| 1510 | } |
| 1511 | |
| 1512 | recent_preset_hbc->move_child(p_preset, 0); |
| 1513 | emit_signal(SNAME("color_changed"), p_preset->get_preset_color()); |
| 1514 | } |
| 1515 | |
| 1516 | void ColorPicker::_text_changed(const String &) { |
| 1517 | text_changed = true; |
| 1518 | } |
| 1519 | |
| 1520 | void ColorPicker::_add_preset_pressed() { |
| 1521 | add_preset(color); |
| 1522 | emit_signal(SNAME("preset_added"), color); |
| 1523 | } |
| 1524 | |
| 1525 | void ColorPicker::_pick_button_pressed_native() { |
| 1526 | if (!DisplayServer::get_singleton()->color_picker(callable_mp(this, &ColorPicker::_native_cb))) { |
| 1527 | // Fallback to default/legacy picker. |
| 1528 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_CAPTURE) && !get_tree()->get_root()->is_embedding_subwindows()) { |
| 1529 | _pick_button_pressed(); |
| 1530 | } else { |
| 1531 | _pick_button_pressed_legacy(); |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | void ColorPicker::_native_cb(bool p_status, const Color &p_color) { |
| 1537 | if (p_status) { |
| 1538 | set_pick_color(p_color); |
| 1539 | if (!deferred_mode_enabled) { |
| 1540 | emit_signal(SNAME("color_changed"), color); |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | void ColorPicker::_pick_button_pressed() { |
| 1546 | is_picking_color = true; |
| 1547 | pre_picking_color = color; |
| 1548 | |
| 1549 | if (!picker_window) { |
| 1550 | picker_window = memnew(Popup); |
| 1551 | bool has_feature_exclude_from_capture = DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_EXCLUDE_FROM_CAPTURE); |
| 1552 | if (!has_feature_exclude_from_capture) { |
| 1553 | picker_window->set_size(Vector2i(28, 28)); |
| 1554 | } else { |
| 1555 | picker_window->set_size(Vector2i(55, 72)); |
| 1556 | picker_window->set_flag(Window::FLAG_EXCLUDE_FROM_CAPTURE, true); // Only supported on MacOS and Windows. |
| 1557 | } |
| 1558 | picker_window->connect(SceneStringName(visibility_changed), callable_mp(this, &ColorPicker::_pick_finished)); |
| 1559 | picker_window->connect(SceneStringName(window_input), callable_mp(this, &ColorPicker::_target_gui_input)); |
| 1560 | |
| 1561 | picker_preview = memnew(Panel); |
| 1562 | picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1563 | picker_preview->set_size(Vector2i(55, 72)); |
| 1564 | picker_window->add_child(picker_preview); |
| 1565 | |
| 1566 | picker_preview_color = memnew(Panel); |
| 1567 | picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1568 | if (!has_feature_exclude_from_capture) { |
| 1569 | picker_preview_color->set_size(Vector2i(24, 24)); |
| 1570 | picker_preview_color->set_position(Vector2i(2, 2)); |
| 1571 | } else { |
| 1572 | picker_preview_color->set_size(Vector2i(51, 15)); |
| 1573 | picker_preview_color->set_position(Vector2i(2, 55)); |
| 1574 | } |
| 1575 | picker_preview->add_child(picker_preview_color); |
| 1576 | |
| 1577 | if (has_feature_exclude_from_capture) { |
| 1578 | picker_texture_zoom = memnew(TextureRect); |
| 1579 | picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1580 | picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51)); |
| 1581 | picker_texture_zoom->set_position(Vector2i(2, 2)); |
| 1582 | picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 1583 | picker_preview->add_child(picker_texture_zoom); |
| 1584 | } |
| 1585 | |
| 1586 | picker_preview_style_box.instantiate(); |
| 1587 | picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box); |
| 1588 | |
| 1589 | picker_preview_style_box_color.instantiate(); |
| 1590 | picker_preview_color->add_theme_style_override(SceneStringName(panel), picker_preview_style_box_color); |
| 1591 | |
| 1592 | add_child(picker_window, false, INTERNAL_MODE_FRONT); |
| 1593 | } |
| 1594 | set_process_internal(true); |
| 1595 | |
| 1596 | picker_window->popup(); |
| 1597 | } |
| 1598 | |
| 1599 | void ColorPicker::_target_gui_input(const Ref<InputEvent> &p_event) { |
| 1600 | const Ref<InputEventMouseButton> mouse_event = p_event; |
| 1601 | if (mouse_event.is_null()) { |
| 1602 | return; |
| 1603 | } |
| 1604 | if (mouse_event->get_button_index() == MouseButton::LEFT) { |
| 1605 | if (mouse_event->is_pressed()) { |
| 1606 | picker_window->hide(); |
| 1607 | _pick_finished(); |
| 1608 | } |
| 1609 | } else if (mouse_event->get_button_index() == MouseButton::RIGHT) { |
| 1610 | set_pick_color(pre_picking_color); // Cancel. |
| 1611 | is_picking_color = false; |
| 1612 | set_process_internal(false); |
| 1613 | picker_window->hide(); |
| 1614 | } else { |
| 1615 | Window *w = picker_window->get_parent_visible_window(); |
| 1616 | while (w) { |
| 1617 | Point2i win_mpos = w->get_mouse_position(); // Mouse position local to the window. |
| 1618 | Size2i win_size = w->get_size(); |
| 1619 | if (win_mpos.x >= 0 && win_mpos.y >= 0 && win_mpos.x <= win_size.x && win_mpos.y <= win_size.y) { |
| 1620 | // Mouse event inside window bounds, forward this event to the window. |
| 1621 | Ref<InputEventMouseButton> new_ev = p_event->duplicate(); |
| 1622 | new_ev->set_position(win_mpos); |
| 1623 | new_ev->set_global_position(win_mpos); |
| 1624 | w->push_input(new_ev, true); |
| 1625 | return; |
| 1626 | } |
| 1627 | w = w->get_parent_visible_window(); |
| 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | void ColorPicker::_pick_finished() { |
| 1633 | if (picker_window->is_visible()) { |
| 1634 | return; |
| 1635 | } |
| 1636 | |
| 1637 | if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) { |
| 1638 | set_pick_color(pre_picking_color); |
| 1639 | } else { |
| 1640 | emit_signal(SNAME("color_changed"), color); |
| 1641 | } |
| 1642 | is_picking_color = false; |
| 1643 | set_process_internal(false); |
| 1644 | picker_window->hide(); |
| 1645 | } |
| 1646 | |
| 1647 | void ColorPicker::_update_menu_items() { |
| 1648 | options_menu->clear(); |
| 1649 | options_menu->reset_size(); |
| 1650 | |
| 1651 | options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save"), static_cast<int>(MenuOption::MENU_SAVE)); |
| 1652 | options_menu->set_item_tooltip(-1, ETR("Save the current color palette to reuse later.")); |
| 1653 | options_menu->set_item_disabled(-1, presets.is_empty()); |
| 1654 | |
| 1655 | options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save As"), static_cast<int>(MenuOption::MENU_SAVE_AS)); |
| 1656 | options_menu->set_item_tooltip(-1, ETR("Save the current color palette as a new to reuse later.")); |
| 1657 | options_menu->set_item_disabled(-1, palette_path.is_empty()); |
| 1658 | |
| 1659 | options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), ETR("Load"), static_cast<int>(MenuOption::MENU_LOAD)); |
| 1660 | options_menu->set_item_tooltip(-1, ETR("Load existing color palette.")); |
| 1661 | |
| 1662 | #ifdef TOOLS_ENABLED |
| 1663 | if (Engine::get_singleton()->is_editor_hint()) { |
| 1664 | options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), TTRC("Quick Load"), static_cast<int>(MenuOption::MENU_QUICKLOAD)); |
| 1665 | options_menu->set_item_tooltip(-1, TTRC("Load existing color palette.")); |
| 1666 | } |
| 1667 | #endif // TOOLS_ENABLED |
| 1668 | |
| 1669 | options_menu->add_icon_item(get_theme_icon(SNAME("clear"), SNAME("FileDialog")), ETR("Clear"), static_cast<int>(MenuOption::MENU_CLEAR)); |
| 1670 | options_menu->set_item_tooltip(-1, ETR("Clear the currently loaded color palettes in the picker.")); |
| 1671 | options_menu->set_item_disabled(-1, presets.is_empty()); |
| 1672 | } |
| 1673 | |
| 1674 | void ColorPicker::_options_menu_cbk(int p_which) { |
| 1675 | _ensure_file_dialog(); |
| 1676 | |
| 1677 | MenuOption option = static_cast<MenuOption>(p_which); |
| 1678 | switch (option) { |
| 1679 | case MenuOption::MENU_SAVE: |
| 1680 | _save_palette(false); |
| 1681 | break; |
| 1682 | case MenuOption::MENU_SAVE_AS: |
| 1683 | _save_palette(true); |
| 1684 | break; |
| 1685 | case MenuOption::MENU_LOAD: |
| 1686 | _load_palette(); |
| 1687 | break; |
| 1688 | |
| 1689 | #ifdef TOOLS_ENABLED |
| 1690 | case MenuOption::MENU_QUICKLOAD: |
| 1691 | if (quick_open_callback.is_valid()) { |
| 1692 | file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE); |
| 1693 | quick_open_callback.call_deferred(); |
| 1694 | } |
| 1695 | break; |
| 1696 | #endif // TOOLS_ENABLED |
| 1697 | case MenuOption::MENU_CLEAR: { |
| 1698 | PackedColorArray colors = get_presets(); |
| 1699 | for (Color c : colors) { |
| 1700 | erase_preset(c); |
| 1701 | } |
| 1702 | |
| 1703 | palette_name->set_text(""); |
| 1704 | palette_name->set_tooltip_text(""); |
| 1705 | palette_path = String(); |
| 1706 | btn_preset->set_pressed(false); |
| 1707 | |
| 1708 | #ifdef TOOLS_ENABLED |
| 1709 | if (editor_settings) { |
| 1710 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_name", palette_name->get_text()); |
| 1711 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_path", palette_path); |
| 1712 | editor_settings->call(SNAME("set_project_metadata"), "color_picker", "palette_edited", false); |
| 1713 | } |
| 1714 | #endif // TOOLS_ENABLED |
| 1715 | |
| 1716 | } |
| 1717 | |
| 1718 | break; |
| 1719 | default: |
| 1720 | break; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | void ColorPicker::_block_input_on_popup_show() { |
| 1725 | if (!get_tree()->get_root()->is_embedding_subwindows()) { |
| 1726 | get_viewport()->set_disable_input(true); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | void ColorPicker::_enable_input_on_popup_hide() { |
| 1731 | if (!get_tree()->get_root()->is_embedding_subwindows()) { |
| 1732 | get_viewport()->set_disable_input(false); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | void ColorPicker::_pick_button_pressed_legacy() { |
| 1737 | if (!is_inside_tree()) { |
| 1738 | return; |
| 1739 | } |
| 1740 | pre_picking_color = color; |
| 1741 | |
| 1742 | if (!picker_window) { |
| 1743 | picker_window = memnew(Popup); |
| 1744 | picker_window->hide(); |
| 1745 | picker_window->set_transient(true); |
| 1746 | add_child(picker_window, false, INTERNAL_MODE_FRONT); |
| 1747 | |
| 1748 | picker_texture_rect = memnew(TextureRect); |
| 1749 | picker_texture_rect->set_anchors_preset(Control::PRESET_FULL_RECT); |
| 1750 | picker_texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 1751 | picker_texture_rect->set_default_cursor_shape(Control::CURSOR_CROSS); |
| 1752 | picker_window->add_child(picker_texture_rect); |
| 1753 | picker_texture_rect->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_picker_texture_input)); |
| 1754 | |
| 1755 | picker_preview = memnew(Panel); |
| 1756 | picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1757 | picker_preview->set_size(Vector2i(55, 72)); |
| 1758 | picker_window->add_child(picker_preview); |
| 1759 | |
| 1760 | picker_preview_color = memnew(Panel); |
| 1761 | picker_preview_color->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1762 | picker_preview_color->set_size(Vector2i(51, 15)); |
| 1763 | picker_preview_color->set_position(Vector2i(2, 55)); |
| 1764 | picker_preview->add_child(picker_preview_color); |
| 1765 | |
| 1766 | picker_texture_zoom = memnew(TextureRect); |
| 1767 | picker_texture_zoom->set_mouse_filter(MOUSE_FILTER_IGNORE); |
| 1768 | picker_texture_zoom->set_custom_minimum_size(Vector2i(51, 51)); |
| 1769 | picker_texture_zoom->set_position(Vector2i(2, 2)); |
| 1770 | picker_texture_zoom->set_texture_filter(CanvasItem::TEXTURE_FILTER_NEAREST); |
| 1771 | picker_preview->add_child(picker_texture_zoom); |
| 1772 | |
| 1773 | picker_preview_style_box.instantiate(); |
| 1774 | picker_preview->add_theme_style_override(SceneStringName(panel), picker_preview_style_box); |
| 1775 | |
| 1776 | picker_preview_style_box_color.instantiate(); |
| 1777 | picker_preview_color->add_theme_style_override(SceneStringName(panel), picker_preview_style_box_color); |
| 1778 | } |
| 1779 | |
| 1780 | Rect2i screen_rect; |
| 1781 | if (picker_window->is_embedded()) { |
| 1782 | Ref<ImageTexture> tx = ImageTexture::create_from_image(picker_window->get_embedder()->get_texture()->get_image()); |
| 1783 | screen_rect = picker_window->get_embedder()->get_visible_rect(); |
| 1784 | picker_window->set_position(Point2i()); |
| 1785 | picker_texture_rect->set_texture(tx); |
| 1786 | |
| 1787 | Vector2 ofs = picker_window->get_mouse_position(); |
| 1788 | picker_preview->set_position(ofs - Vector2(28, 28)); |
| 1789 | |
| 1790 | Vector2 scale = screen_rect.size / tx->get_image()->get_size(); |
| 1791 | ofs /= scale; |
| 1792 | |
| 1793 | Ref<AtlasTexture> atlas; |
| 1794 | atlas.instantiate(); |
| 1795 | atlas->set_atlas(tx); |
| 1796 | atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17)); |
| 1797 | picker_texture_zoom->set_texture(atlas); |
| 1798 | } else { |
| 1799 | screen_rect = picker_window->get_parent_rect(); |
| 1800 | picker_window->set_position(screen_rect.position); |
| 1801 | |
| 1802 | Ref<Image> target_image = Image::create_empty(screen_rect.size.x, screen_rect.size.y, false, Image::FORMAT_RGB8); |
| 1803 | DisplayServer *ds = DisplayServer::get_singleton(); |
| 1804 | |
| 1805 | // Add the Texture of each Window to the Image. |
| 1806 | Vector<DisplayServer::WindowID> wl = ds->get_window_list(); |
| 1807 | // FIXME: sort windows by visibility. |
| 1808 | for (const DisplayServer::WindowID &window_id : wl) { |
| 1809 | Window *w = Window::get_from_id(window_id); |
| 1810 | if (!w) { |
| 1811 | continue; |
| 1812 | } |
| 1813 | |
| 1814 | Ref<Image> img = w->get_texture()->get_image(); |
| 1815 | if (img.is_null() || img->is_empty()) { |
| 1816 | continue; |
| 1817 | } |
| 1818 | img->convert(Image::FORMAT_RGB8); |
| 1819 | target_image->blit_rect(img, Rect2i(Point2i(0, 0), img->get_size()), w->get_position()); |
| 1820 | } |
| 1821 | |
| 1822 | Ref<ImageTexture> tx = ImageTexture::create_from_image(target_image); |
| 1823 | picker_texture_rect->set_texture(tx); |
| 1824 | |
| 1825 | Vector2 ofs = screen_rect.position - DisplayServer::get_singleton()->mouse_get_position(); |
| 1826 | picker_preview->set_position(ofs - Vector2(28, 28)); |
| 1827 | |
| 1828 | Ref<AtlasTexture> atlas; |
| 1829 | atlas.instantiate(); |
| 1830 | atlas->set_atlas(tx); |
| 1831 | atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17)); |
| 1832 | picker_texture_zoom->set_texture(atlas); |
| 1833 | } |
| 1834 | |
| 1835 | picker_window->set_size(screen_rect.size); |
| 1836 | picker_window->popup(); |
| 1837 | } |
| 1838 | |
| 1839 | void ColorPicker::_picker_texture_input(const Ref<InputEvent> &p_event) { |
| 1840 | if (!is_inside_tree()) { |
| 1841 | return; |
| 1842 | } |
| 1843 | |
| 1844 | Ref<InputEventMouseButton> bev = p_event; |
| 1845 | if (bev.is_valid() && bev->get_button_index() == MouseButton::LEFT && !bev->is_pressed()) { |
| 1846 | set_pick_color(picker_color); |
| 1847 | emit_signal(SNAME("color_changed"), color); |
| 1848 | picker_window->hide(); |
| 1849 | } |
| 1850 | |
| 1851 | Ref<InputEventMouseMotion> mev = p_event; |
| 1852 | if (mev.is_valid()) { |
| 1853 | Ref<Image> img = picker_texture_rect->get_texture()->get_image(); |
| 1854 | if (img.is_valid() && !img->is_empty()) { |
| 1855 | Vector2 ofs = mev->get_position(); |
| 1856 | picker_preview->set_position(ofs - Vector2(28, 28)); |
| 1857 | Vector2 scale = picker_texture_rect->get_size() / img->get_size(); |
| 1858 | ofs /= scale; |
| 1859 | picker_color = img->get_pixel(ofs.x, ofs.y); |
| 1860 | picker_preview_style_box_color->set_bg_color(picker_color); |
| 1861 | picker_preview_style_box->set_bg_color(picker_color.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f)); |
| 1862 | |
| 1863 | Ref<AtlasTexture> atlas = picker_texture_zoom->get_texture(); |
| 1864 | if (atlas.is_valid()) { |
| 1865 | atlas->set_region(Rect2i(ofs.x - 8, ofs.y - 8, 17, 17)); |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | void ColorPicker::_html_focus_exit() { |
| 1872 | if (c_text->is_menu_visible()) { |
| 1873 | return; |
| 1874 | } |
| 1875 | |
| 1876 | if (is_visible_in_tree()) { |
| 1877 | _html_submitted(c_text->get_text()); |
| 1878 | } else { |
| 1879 | _update_text_value(); |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | void ColorPicker::set_can_add_swatches(bool p_enabled) { |
| 1884 | if (can_add_swatches == p_enabled) { |
| 1885 | return; |
| 1886 | } |
| 1887 | can_add_swatches = p_enabled; |
| 1888 | if (!p_enabled) { |
| 1889 | btn_add_preset->set_disabled(true); |
| 1890 | btn_add_preset->set_focus_mode(FOCUS_NONE); |
| 1891 | } else { |
| 1892 | btn_add_preset->set_disabled(false); |
| 1893 | btn_add_preset->set_focus_mode(FOCUS_ALL); |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | bool ColorPicker::are_swatches_enabled() const { |
| 1898 | return can_add_swatches; |
| 1899 | } |
| 1900 | |
| 1901 | void ColorPicker::set_presets_visible(bool p_visible) { |
| 1902 | if (presets_visible == p_visible) { |
| 1903 | return; |
| 1904 | } |
| 1905 | presets_visible = p_visible; |
| 1906 | swatches_vbc->set_visible(p_visible); |
| 1907 | } |
| 1908 | |
| 1909 | bool ColorPicker::are_presets_visible() const { |
| 1910 | return presets_visible; |
| 1911 | } |
| 1912 | |
| 1913 | void ColorPicker::set_modes_visible(bool p_visible) { |
| 1914 | if (color_modes_visible == p_visible) { |
| 1915 | return; |
| 1916 | } |
| 1917 | color_modes_visible = p_visible; |
| 1918 | mode_hbc->set_visible(p_visible); |
| 1919 | } |
| 1920 | |
| 1921 | bool ColorPicker::are_modes_visible() const { |
| 1922 | return color_modes_visible; |
| 1923 | } |
| 1924 | |
| 1925 | void ColorPicker::set_sampler_visible(bool p_visible) { |
| 1926 | if (sampler_visible == p_visible) { |
| 1927 | return; |
| 1928 | } |
| 1929 | sampler_visible = p_visible; |
| 1930 | sample_hbc->set_visible(p_visible); |
| 1931 | } |
| 1932 | |
| 1933 | bool ColorPicker::is_sampler_visible() const { |
| 1934 | return sampler_visible; |
| 1935 | } |
| 1936 | |
| 1937 | void ColorPicker::set_sliders_visible(bool p_visible) { |
| 1938 | if (sliders_visible == p_visible) { |
| 1939 | return; |
| 1940 | } |
| 1941 | sliders_visible = p_visible; |
| 1942 | slider_gc->set_visible(p_visible); |
| 1943 | } |
| 1944 | |
| 1945 | bool ColorPicker::are_sliders_visible() const { |
| 1946 | return sliders_visible; |
| 1947 | } |
| 1948 | |
| 1949 | void ColorPicker::set_hex_visible(bool p_visible) { |
| 1950 | if (hex_visible == p_visible) { |
| 1951 | return; |
| 1952 | } |
| 1953 | hex_visible = p_visible; |
| 1954 | hex_hbc->set_visible(p_visible); |
| 1955 | } |
| 1956 | |
| 1957 | bool ColorPicker::is_hex_visible() const { |
| 1958 | return hex_visible; |
| 1959 | } |
| 1960 | |
| 1961 | void ColorPicker::_bind_methods() { |
| 1962 | ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color); |
| 1963 | ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color); |
| 1964 | ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode); |
| 1965 | ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode); |
| 1966 | ClassDB::bind_method(D_METHOD("set_color_mode", "color_mode"), &ColorPicker::set_color_mode); |
| 1967 | ClassDB::bind_method(D_METHOD("get_color_mode"), &ColorPicker::get_color_mode); |
| 1968 | ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha); |
| 1969 | ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha); |
| 1970 | ClassDB::bind_method(D_METHOD("set_edit_intensity", "show"), &ColorPicker::set_edit_intensity); |
| 1971 | ClassDB::bind_method(D_METHOD("is_editing_intensity"), &ColorPicker::is_editing_intensity); |
| 1972 | ClassDB::bind_method(D_METHOD("set_can_add_swatches", "enabled"), &ColorPicker::set_can_add_swatches); |
| 1973 | ClassDB::bind_method(D_METHOD("are_swatches_enabled"), &ColorPicker::are_swatches_enabled); |
| 1974 | ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible); |
| 1975 | ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible); |
| 1976 | ClassDB::bind_method(D_METHOD("set_modes_visible", "visible"), &ColorPicker::set_modes_visible); |
| 1977 | ClassDB::bind_method(D_METHOD("are_modes_visible"), &ColorPicker::are_modes_visible); |
| 1978 | ClassDB::bind_method(D_METHOD("set_sampler_visible", "visible"), &ColorPicker::set_sampler_visible); |
| 1979 | ClassDB::bind_method(D_METHOD("is_sampler_visible"), &ColorPicker::is_sampler_visible); |
| 1980 | ClassDB::bind_method(D_METHOD("set_sliders_visible", "visible"), &ColorPicker::set_sliders_visible); |
| 1981 | ClassDB::bind_method(D_METHOD("are_sliders_visible"), &ColorPicker::are_sliders_visible); |
| 1982 | ClassDB::bind_method(D_METHOD("set_hex_visible", "visible"), &ColorPicker::set_hex_visible); |
| 1983 | ClassDB::bind_method(D_METHOD("is_hex_visible"), &ColorPicker::is_hex_visible); |
| 1984 | ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset); |
| 1985 | ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset); |
| 1986 | ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets); |
| 1987 | ClassDB::bind_method(D_METHOD("add_recent_preset", "color"), &ColorPicker::add_recent_preset); |
| 1988 | ClassDB::bind_method(D_METHOD("erase_recent_preset", "color"), &ColorPicker::erase_recent_preset); |
| 1989 | ClassDB::bind_method(D_METHOD("get_recent_presets"), &ColorPicker::get_recent_presets); |
| 1990 | ClassDB::bind_method(D_METHOD("set_picker_shape", "shape"), &ColorPicker::set_picker_shape); |
| 1991 | ClassDB::bind_method(D_METHOD("get_picker_shape"), &ColorPicker::get_picker_shape); |
| 1992 | |
| 1993 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color"); |
| 1994 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha"); |
| 1995 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_intensity"), "set_edit_intensity", "is_editing_intensity"); |
| 1996 | ADD_PROPERTY(PropertyInfo(Variant::INT, "color_mode", PROPERTY_HINT_ENUM, "RGB,HSV,LINEAR,OKHSL"), "set_color_mode", "get_color_mode"); |
| 1997 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode"); |
| 1998 | ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle,OK HS Rectangle:5,OK HL Rectangle,None:4"), "set_picker_shape", "get_picker_shape"); |
| 1999 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_add_swatches"), "set_can_add_swatches", "are_swatches_enabled"); |
| 2000 | ADD_GROUP("Customization", ""); |
| 2001 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sampler_visible"), "set_sampler_visible", "is_sampler_visible"); |
| 2002 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "color_modes_visible"), "set_modes_visible", "are_modes_visible"); |
| 2003 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sliders_visible"), "set_sliders_visible", "are_sliders_visible"); |
| 2004 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hex_visible"), "set_hex_visible", "is_hex_visible"); |
| 2005 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible"); |
| 2006 | |
| 2007 | ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color"))); |
| 2008 | ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color"))); |
| 2009 | ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color"))); |
| 2010 | |
| 2011 | BIND_ENUM_CONSTANT(MODE_RGB); |
| 2012 | BIND_ENUM_CONSTANT(MODE_HSV); |
| 2013 | #ifndef DISABLE_DEPRECATED |
| 2014 | BIND_ENUM_CONSTANT(MODE_RAW); |
| 2015 | #endif |
| 2016 | BIND_ENUM_CONSTANT(MODE_LINEAR); |
| 2017 | BIND_ENUM_CONSTANT(MODE_OKHSL); |
| 2018 | |
| 2019 | BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE); |
| 2020 | BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL); |
| 2021 | BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE); |
| 2022 | BIND_ENUM_CONSTANT(SHAPE_OKHSL_CIRCLE); |
| 2023 | BIND_ENUM_CONSTANT(SHAPE_NONE); |
| 2024 | BIND_ENUM_CONSTANT(SHAPE_OK_HS_RECTANGLE); |
| 2025 | BIND_ENUM_CONSTANT(SHAPE_OK_HL_RECTANGLE); |
| 2026 | |
| 2027 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ColorPicker, content_margin, "margin"); |
| 2028 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, label_width); |
| 2029 | |
| 2030 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_width); |
| 2031 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_height); |
| 2032 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, h_width); |
| 2033 | |
| 2034 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, center_slider_grabbers); |
| 2035 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, sample_focus); |
| 2036 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, picker_focus_rectangle); |
| 2037 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, ColorPicker, picker_focus_circle); |
| 2038 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ColorPicker, focused_not_editing_cursor_color); |
| 2039 | |
| 2040 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, menu_option); |
| 2041 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, screen_picker); |
| 2042 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, expanded_arrow); |
| 2043 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, folded_arrow); |
| 2044 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, add_preset); |
| 2045 | |
| 2046 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect); |
| 2047 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect_wheel); |
| 2048 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_circle); |
| 2049 | |
| 2050 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, bar_arrow); |
| 2051 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_bg); |
| 2052 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_revert); |
| 2053 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, overbright_indicator); |
| 2054 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor); |
| 2055 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor_bg); |
| 2056 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_hue); |
| 2057 | |
| 2058 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_script); |
| 2059 | |
| 2060 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_normal, "tab_unselected", "TabContainer"); |
| 2061 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_pressed, "tab_selected", "TabContainer"); |
| 2062 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_hover, "tab_selected", "TabContainer"); |
| 2063 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_hover_pressed, "tab_selected", "TabContainer"); |
| 2064 | |
| 2065 | ADD_CLASS_DEPENDENCY("LineEdit"); |
| 2066 | ADD_CLASS_DEPENDENCY("MenuButton"); |
| 2067 | ADD_CLASS_DEPENDENCY("PopupMenu"); |
| 2068 | } |
| 2069 | |
| 2070 | ColorPicker::ColorPicker() { |
| 2071 | internal_margin = memnew(MarginContainer); |
| 2072 | add_child(internal_margin, false, INTERNAL_MODE_FRONT); |
| 2073 | |
| 2074 | VBoxContainer *real_vbox = memnew(VBoxContainer); |
| 2075 | internal_margin->add_child(real_vbox); |
| 2076 | |
| 2077 | shape_container = memnew(HBoxContainer); |
| 2078 | shape_container->set_alignment(ALIGNMENT_CENTER); |
| 2079 | real_vbox->add_child(shape_container); |
| 2080 | |
| 2081 | sample_hbc = memnew(HBoxContainer); |
| 2082 | real_vbox->add_child(sample_hbc); |
| 2083 | |
| 2084 | btn_pick = memnew(Button); |
| 2085 | btn_pick->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2086 | btn_pick->set_accessibility_name(ETR("Pick")); |
| 2087 | sample_hbc->add_child(btn_pick); |
| 2088 | |
| 2089 | sample = memnew(TextureRect); |
| 2090 | sample_hbc->add_child(sample); |
| 2091 | sample->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2092 | sample->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_sample_input)); |
| 2093 | sample->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_sample_draw)); |
| 2094 | |
| 2095 | btn_shape = memnew(MenuButton); |
| 2096 | btn_shape->set_flat(false); |
| 2097 | btn_shape->set_theme_type_variation("FlatMenuButton"); |
| 2098 | sample_hbc->add_child(btn_shape); |
| 2099 | btn_shape->set_toggle_mode(true); |
| 2100 | btn_shape->set_tooltip_text(ETR("Select a picker shape.")); |
| 2101 | btn_shape->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2102 | btn_shape->set_focus_mode(FOCUS_ALL); |
| 2103 | |
| 2104 | add_shape(memnew(ColorPickerShapeRectangle(this))); |
| 2105 | add_shape(memnew(ColorPickerShapeWheel(this))); |
| 2106 | add_shape(memnew(ColorPickerShapeVHSCircle(this))); |
| 2107 | add_shape(memnew(ColorPickerShapeOKHSLCircle(this))); |
| 2108 | add_shape(memnew(ColorPickerShapeOKHSRectangle(this))); |
| 2109 | add_shape(memnew(ColorPickerShapeOKHLRectangle(this))); |
| 2110 | |
| 2111 | shape_popup = btn_shape->get_popup(); |
| 2112 | { |
| 2113 | int i = 0; |
| 2114 | for (const ColorPickerShape *shape : shapes) { |
| 2115 | shape_popup->add_radio_check_item(shape->get_name(), index_to_shape(i)); |
| 2116 | i++; |
| 2117 | } |
| 2118 | } |
| 2119 | shape_popup->set_item_checked(get_current_shape_index(), true); |
| 2120 | shape_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::set_picker_shape)); |
| 2121 | shape_popup->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show)); |
| 2122 | shape_popup->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide)); |
| 2123 | |
| 2124 | add_mode(memnew(ColorModeRGB(this))); |
| 2125 | add_mode(memnew(ColorModeHSV(this))); |
| 2126 | add_mode(memnew(ColorModeLinear(this))); |
| 2127 | add_mode(memnew(ColorModeOKHSL(this))); |
| 2128 | |
| 2129 | mode_hbc = memnew(HBoxContainer); |
| 2130 | real_vbox->add_child(mode_hbc); |
| 2131 | |
| 2132 | mode_group.instantiate(); |
| 2133 | |
| 2134 | for (int i = 0; i < MODE_BUTTON_COUNT; i++) { |
| 2135 | mode_btns[i] = memnew(Button); |
| 2136 | mode_hbc->add_child(mode_btns[i]); |
| 2137 | mode_btns[i]->set_focus_mode(FOCUS_ALL); |
| 2138 | mode_btns[i]->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2139 | mode_btns[i]->set_toggle_mode(true); |
| 2140 | mode_btns[i]->set_text(modes[i]->get_name()); |
| 2141 | mode_btns[i]->set_button_group(mode_group); |
| 2142 | mode_btns[i]->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::set_color_mode).bind((ColorModeType)i)); |
| 2143 | } |
| 2144 | mode_btns[0]->set_pressed(true); |
| 2145 | |
| 2146 | btn_mode = memnew(MenuButton); |
| 2147 | btn_mode->set_flat(false); |
| 2148 | btn_mode->set_theme_type_variation("FlatMenuButton"); |
| 2149 | mode_hbc->add_child(btn_mode); |
| 2150 | btn_mode->set_toggle_mode(true); |
| 2151 | btn_mode->set_accessibility_name(ETR("Select a picker mode.")); |
| 2152 | btn_mode->set_tooltip_text(ETR("Select a picker mode.")); |
| 2153 | btn_mode->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2154 | btn_mode->set_focus_mode(FOCUS_ALL); |
| 2155 | |
| 2156 | mode_popup = btn_mode->get_popup(); |
| 2157 | { |
| 2158 | int i = 0; |
| 2159 | for (const ColorMode *mode : modes) { |
| 2160 | mode_popup->add_radio_check_item(mode->get_name(), i); |
| 2161 | i++; |
| 2162 | } |
| 2163 | } |
| 2164 | mode_popup->add_separator(); |
| 2165 | mode_popup->add_check_item(ETR("Colorized Sliders"), MODE_MAX); |
| 2166 | mode_popup->set_item_checked(current_mode, true); |
| 2167 | mode_popup->set_item_checked(MODE_MAX + 1, true); |
| 2168 | mode_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_set_mode_popup_value)); |
| 2169 | mode_popup->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show)); |
| 2170 | mode_popup->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide)); |
| 2171 | |
| 2172 | slider_gc = memnew(GridContainer); |
| 2173 | |
| 2174 | real_vbox->add_child(slider_gc); |
| 2175 | slider_gc->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2176 | slider_gc->set_columns(3); |
| 2177 | |
| 2178 | for (int i = 0; i < SLIDER_MAX; i++) { |
| 2179 | create_slider(slider_gc, i); |
| 2180 | } |
| 2181 | alpha_label->set_text("A"); |
| 2182 | |
| 2183 | intensity_label->set_text("I"); |
| 2184 | intensity_slider->set_min(-10); |
| 2185 | intensity_slider->set_max(10); |
| 2186 | intensity_slider->set_step(0.001); |
| 2187 | intensity_value->set_allow_greater(true); |
| 2188 | intensity_value->set_custom_arrow_step(1); |
| 2189 | intensity_value->set_custom_arrow_round(true); |
| 2190 | |
| 2191 | hex_hbc = memnew(HBoxContainer); |
| 2192 | hex_hbc->set_alignment(ALIGNMENT_BEGIN); |
| 2193 | real_vbox->add_child(hex_hbc); |
| 2194 | hex_label = memnew(Label(ETR("Hex"))); |
| 2195 | hex_hbc->add_child(hex_label); |
| 2196 | |
| 2197 | text_type = memnew(Button); |
| 2198 | hex_hbc->add_child(text_type); |
| 2199 | text_type->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2200 | text_type->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS); |
| 2201 | text_type->set_text("#"); |
| 2202 | #ifdef TOOLS_ENABLED |
| 2203 | if (Engine::get_singleton()->is_editor_hint()) { |
| 2204 | text_type->set_tooltip_text(TTRC("Switch between hexadecimal and code values.")); |
| 2205 | text_type->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_text_type_toggled)); |
| 2206 | } else { |
| 2207 | text_type->set_accessibility_name(ETR("Hexadecimal Values")); |
| 2208 | #else |
| 2209 | { |
| 2210 | text_type->set_accessibility_name(ETR("Hexadecimal Values")); |
| 2211 | #endif // TOOLS_ENABLED |
| 2212 | text_type->set_flat(true); |
| 2213 | } |
| 2214 | |
| 2215 | c_text = memnew(LineEdit); |
| 2216 | hex_hbc->add_child(c_text); |
| 2217 | c_text->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2218 | c_text->set_select_all_on_focus(true); |
| 2219 | c_text->set_accessibility_name(ETR("Hex code or named color")); |
| 2220 | c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\").")); |
| 2221 | c_text->set_placeholder(ETR("Hex code or named color")); |
| 2222 | c_text->connect(SceneStringName(text_submitted), callable_mp(this, &ColorPicker::_html_submitted)); |
| 2223 | c_text->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed)); |
| 2224 | c_text->connect(SceneStringName(focus_exited), callable_mp(this, &ColorPicker::_html_focus_exit)); |
| 2225 | |
| 2226 | _update_controls(); |
| 2227 | updating = false; |
| 2228 | |
| 2229 | swatches_vbc = memnew(VBoxContainer); |
| 2230 | real_vbox->add_child(swatches_vbc); |
| 2231 | |
| 2232 | preset_container = memnew(GridContainer); |
| 2233 | preset_container->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2234 | preset_container->set_columns(PRESET_COLUMN_COUNT); |
| 2235 | preset_container->hide(); |
| 2236 | |
| 2237 | preset_group.instantiate(); |
| 2238 | |
| 2239 | HBoxContainer *palette_box = memnew(HBoxContainer); |
| 2240 | palette_box->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2241 | swatches_vbc->add_child(palette_box); |
| 2242 | |
| 2243 | btn_preset = memnew(Button); |
| 2244 | btn_preset->set_text(ETR("Swatches")); |
| 2245 | btn_preset->set_flat(true); |
| 2246 | btn_preset->set_toggle_mode(true); |
| 2247 | btn_preset->set_focus_mode(FOCUS_ALL); |
| 2248 | btn_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT); |
| 2249 | btn_preset->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2250 | btn_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_preset, preset_container)); |
| 2251 | palette_box->add_child(btn_preset); |
| 2252 | |
| 2253 | menu_btn = memnew(MenuButton); |
| 2254 | menu_btn->set_flat(false); |
| 2255 | menu_btn->set_theme_type_variation("FlatMenuButton"); |
| 2256 | menu_btn->set_focus_mode(FOCUS_ALL); |
| 2257 | menu_btn->set_tooltip_text(ETR("Show all options available.")); |
| 2258 | menu_btn->connect("about_to_popup", callable_mp(this, &ColorPicker::_update_menu_items)); |
| 2259 | palette_box->add_child(menu_btn); |
| 2260 | |
| 2261 | options_menu = menu_btn->get_popup(); |
| 2262 | options_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_options_menu_cbk)); |
| 2263 | options_menu->connect("about_to_popup", callable_mp(this, &ColorPicker::_block_input_on_popup_show)); |
| 2264 | options_menu->connect(SNAME("popup_hide"), callable_mp(this, &ColorPicker::_enable_input_on_popup_hide)); |
| 2265 | |
| 2266 | palette_name = memnew(Label); |
| 2267 | palette_name->hide(); |
| 2268 | palette_name->set_mouse_filter(MOUSE_FILTER_PASS); |
| 2269 | swatches_vbc->add_child(palette_name); |
| 2270 | |
| 2271 | swatches_vbc->add_child(preset_container); |
| 2272 | |
| 2273 | recent_preset_hbc = memnew(HBoxContainer); |
| 2274 | recent_preset_hbc->set_v_size_flags(SIZE_SHRINK_BEGIN); |
| 2275 | recent_preset_hbc->hide(); |
| 2276 | |
| 2277 | recent_preset_group.instantiate(); |
| 2278 | |
| 2279 | btn_recent_preset = memnew(Button(ETR("Recent Colors"))); |
| 2280 | btn_recent_preset->set_flat(true); |
| 2281 | btn_recent_preset->set_toggle_mode(true); |
| 2282 | btn_recent_preset->set_focus_mode(FOCUS_ALL); |
| 2283 | btn_recent_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT); |
| 2284 | btn_recent_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_recent_preset, recent_preset_hbc)); |
| 2285 | swatches_vbc->add_child(btn_recent_preset); |
| 2286 | |
| 2287 | swatches_vbc->add_child(recent_preset_hbc); |
| 2288 | |
| 2289 | set_pick_color(Color(1, 1, 1)); |
| 2290 | |
| 2291 | btn_add_preset = memnew(Button); |
| 2292 | btn_add_preset->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2293 | btn_add_preset->set_tooltip_text(ETR("Add current color as a preset.")); |
| 2294 | btn_add_preset->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_add_preset_pressed)); |
| 2295 | preset_container->add_child(btn_add_preset); |
| 2296 | |
| 2297 | perm_hb = memnew(HBoxContainer); |
| 2298 | perm_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER); |
| 2299 | |
| 2300 | LinkButton *perm_link = memnew(LinkButton); |
| 2301 | perm_link->set_text(ETR("Screen Recording permission missing!")); |
| 2302 | perm_link->set_tooltip_text(ETR("Screen Recording permission is required to pick colors from the other application windows.\nClick here to request access...")); |
| 2303 | perm_link->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_req_permission)); |
| 2304 | perm_hb->add_child(perm_link); |
| 2305 | real_vbox->add_child(perm_hb); |
| 2306 | perm_hb->set_visible(false); |
| 2307 | } |
| 2308 | |
| 2309 | void ColorPicker::_req_permission() { |
| 2310 | #ifdef MACOS_ENABLED |
| 2311 | OS::get_singleton()->request_permission("macos.permission.RECORD_SCREEN"); |
| 2312 | #endif |
| 2313 | } |
| 2314 | |
| 2315 | ColorPicker::~ColorPicker() { |
| 2316 | for (ColorMode *mode : modes) { |
| 2317 | memdelete(mode); |
| 2318 | } |
| 2319 | for (ColorPickerShape *shape : shapes) { |
| 2320 | memdelete(shape); |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | ///////////////// |
| 2325 | |
| 2326 | void ColorPickerPopupPanel::_input_from_window(const Ref<InputEvent> &p_event) { |
| 2327 | if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) { |
| 2328 | _close_pressed(); |
| 2329 | } |
| 2330 | PopupPanel::_input_from_window(p_event); |
| 2331 | } |
| 2332 | |
| 2333 | ///////////////// |
| 2334 | |
| 2335 | void ColorPickerButton::_about_to_popup() { |
| 2336 | if (!get_tree()->get_root()->is_embedding_subwindows()) { |
| 2337 | get_viewport()->set_disable_input(true); |
| 2338 | } |
| 2339 | set_pressed(true); |
| 2340 | if (picker) { |
| 2341 | picker->set_old_color(color); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | void ColorPickerButton::_color_changed(const Color &p_color) { |
| 2346 | color = p_color; |
| 2347 | queue_accessibility_update(); |
| 2348 | queue_redraw(); |
| 2349 | emit_signal(SNAME("color_changed"), color); |
| 2350 | } |
| 2351 | |
| 2352 | void ColorPickerButton::_modal_closed() { |
| 2353 | if (picker->is_visible_in_tree()) { |
| 2354 | if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) { |
| 2355 | set_pick_color(picker->get_old_color()); |
| 2356 | emit_signal(SNAME("color_changed"), color); |
| 2357 | } |
| 2358 | emit_signal(SNAME("popup_closed")); |
| 2359 | set_pressed(false); |
| 2360 | } |
| 2361 | if (!get_tree()->get_root()->is_embedding_subwindows()) { |
| 2362 | get_viewport()->set_disable_input(false); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | void ColorPickerButton::pressed() { |
| 2367 | _update_picker(); |
| 2368 | |
| 2369 | // Checking if the popup was open before, so we can keep it closed instead of reopening it. |
| 2370 | // Popups get closed when it's clicked outside of them. |
| 2371 | if (popup_was_open) { |
| 2372 | // Reset popup_was_open value. |
| 2373 | popup_was_open = popup->is_visible(); |
| 2374 | return; |
| 2375 | } |
| 2376 | |
| 2377 | Size2 minsize = popup->get_contents_minimum_size(); |
| 2378 | float viewport_height = get_viewport_rect().size.y; |
| 2379 | |
| 2380 | popup->reset_size(); |
| 2381 | picker->_update_presets(); |
| 2382 | picker->_update_recent_presets(); |
| 2383 | |
| 2384 | // Determine in which direction to show the popup. By default popup horizontally centered below the button. |
| 2385 | // But if the popup doesn't fit below and the button is in the bottom half of the viewport, show above. |
| 2386 | bool show_above = false; |
| 2387 | if (get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height) { |
| 2388 | show_above = true; |
| 2389 | } |
| 2390 | |
| 2391 | float h_offset = (get_size().x - minsize.x) / 2; |
| 2392 | float v_offset = show_above ? -minsize.y : get_size().y; |
| 2393 | popup->set_position(get_screen_position() + Vector2(h_offset, v_offset)); |
| 2394 | popup->popup(); |
| 2395 | if (!picker->is_hex_visible() && picker->get_picker_shape() != ColorPicker::SHAPE_NONE) { |
| 2396 | callable_mp(picker, &ColorPicker::set_focus_on_picker_shape).call_deferred(); |
| 2397 | } else if (DisplayServer::get_singleton()->has_hardware_keyboard()) { |
| 2398 | picker->set_focus_on_line_edit(); |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | void ColorPickerButton::gui_input(const Ref<InputEvent> &p_event) { |
| 2403 | ERR_FAIL_COND(p_event.is_null()); |
| 2404 | |
| 2405 | Ref<InputEventMouseButton> mouse_button = p_event; |
| 2406 | bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo(); |
| 2407 | bool mouse_left_pressed = mouse_button.is_valid() && mouse_button->get_button_index() == MouseButton::LEFT && mouse_button->is_pressed(); |
| 2408 | if (mouse_left_pressed || ui_accept) { |
| 2409 | popup_was_open = popup && popup->is_visible(); |
| 2410 | } |
| 2411 | |
| 2412 | BaseButton::gui_input(p_event); |
| 2413 | } |
| 2414 | |
| 2415 | void ColorPickerButton::_notification(int p_what) { |
| 2416 | switch (p_what) { |
| 2417 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 2418 | RID ae = get_accessibility_element(); |
| 2419 | ERR_FAIL_COND(ae.is_null()); |
| 2420 | |
| 2421 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON); |
| 2422 | DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_DIALOG); |
| 2423 | DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, color); |
| 2424 | } break; |
| 2425 | |
| 2426 | case NOTIFICATION_DRAW: { |
| 2427 | const Rect2 r = Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size()); |
| 2428 | draw_texture_rect(theme_cache.background_icon, r, true); |
| 2429 | draw_rect(r, color); |
| 2430 | |
| 2431 | if (color.r > 1 || color.g > 1 || color.b > 1) { |
| 2432 | // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview |
| 2433 | draw_texture(theme_cache.overbright_indicator, theme_cache.normal_style->get_offset()); |
| 2434 | } |
| 2435 | } break; |
| 2436 | |
| 2437 | case NOTIFICATION_WM_CLOSE_REQUEST: { |
| 2438 | if (popup) { |
| 2439 | popup->hide(); |
| 2440 | } |
| 2441 | } break; |
| 2442 | |
| 2443 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 2444 | if (popup && !is_visible_in_tree()) { |
| 2445 | popup->hide(); |
| 2446 | } |
| 2447 | } break; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | void ColorPickerButton::set_pick_color(const Color &p_color) { |
| 2452 | if (color == p_color) { |
| 2453 | return; |
| 2454 | } |
| 2455 | color = p_color; |
| 2456 | if (picker) { |
| 2457 | picker->set_pick_color(p_color); |
| 2458 | } |
| 2459 | queue_accessibility_update(); |
| 2460 | queue_redraw(); |
| 2461 | } |
| 2462 | |
| 2463 | Color ColorPickerButton::get_pick_color() const { |
| 2464 | return color; |
| 2465 | } |
| 2466 | |
| 2467 | void ColorPickerButton::set_edit_alpha(bool p_show) { |
| 2468 | if (edit_alpha == p_show) { |
| 2469 | return; |
| 2470 | } |
| 2471 | edit_alpha = p_show; |
| 2472 | if (picker) { |
| 2473 | picker->set_edit_alpha(p_show); |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | bool ColorPickerButton::is_editing_alpha() const { |
| 2478 | return edit_alpha; |
| 2479 | } |
| 2480 | |
| 2481 | void ColorPickerButton::set_edit_intensity(bool p_show) { |
| 2482 | if (edit_intensity == p_show) { |
| 2483 | return; |
| 2484 | } |
| 2485 | edit_intensity = p_show; |
| 2486 | if (picker) { |
| 2487 | picker->set_edit_intensity(p_show); |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | bool ColorPickerButton::is_editing_intensity() const { |
| 2492 | return edit_intensity; |
| 2493 | } |
| 2494 | |
| 2495 | ColorPicker *ColorPickerButton::get_picker() { |
| 2496 | _update_picker(); |
| 2497 | return picker; |
| 2498 | } |
| 2499 | |
| 2500 | PopupPanel *ColorPickerButton::get_popup() { |
| 2501 | _update_picker(); |
| 2502 | return popup; |
| 2503 | } |
| 2504 | |
| 2505 | void ColorPickerButton::_update_picker() { |
| 2506 | if (!picker) { |
| 2507 | popup = memnew(ColorPickerPopupPanel); |
| 2508 | popup->set_wrap_controls(true); |
| 2509 | picker = memnew(ColorPicker); |
| 2510 | picker->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
| 2511 | popup->add_child(picker); |
| 2512 | add_child(popup, false, INTERNAL_MODE_FRONT); |
| 2513 | picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed)); |
| 2514 | popup->connect("about_to_popup", callable_mp(this, &ColorPickerButton::_about_to_popup)); |
| 2515 | popup->connect("popup_hide", callable_mp(this, &ColorPickerButton::_modal_closed)); |
| 2516 | popup->connect("tree_exiting", callable_mp(this, &ColorPickerButton::_modal_closed)); |
| 2517 | picker->connect(SceneStringName(minimum_size_changed), callable_mp((Window *)popup, &Window::reset_size)); |
| 2518 | picker->set_pick_color(color); |
| 2519 | picker->set_edit_alpha(edit_alpha); |
| 2520 | picker->set_edit_intensity(edit_intensity); |
| 2521 | picker->set_display_old_color(true); |
| 2522 | emit_signal(SNAME("picker_created")); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | void ColorPickerButton::_bind_methods() { |
| 2527 | ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color); |
| 2528 | ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color); |
| 2529 | ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker); |
| 2530 | ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup); |
| 2531 | ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha); |
| 2532 | ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha); |
| 2533 | ClassDB::bind_method(D_METHOD("set_edit_intensity", "show"), &ColorPickerButton::set_edit_intensity); |
| 2534 | ClassDB::bind_method(D_METHOD("is_editing_intensity"), &ColorPickerButton::is_editing_intensity); |
| 2535 | ClassDB::bind_method(D_METHOD("_about_to_popup"), &ColorPickerButton::_about_to_popup); |
| 2536 | |
| 2537 | ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color"))); |
| 2538 | ADD_SIGNAL(MethodInfo("popup_closed")); |
| 2539 | ADD_SIGNAL(MethodInfo("picker_created")); |
| 2540 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color"); |
| 2541 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha"); |
| 2542 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_intensity"), "set_edit_intensity", "is_editing_intensity"); |
| 2543 | |
| 2544 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPickerButton, normal_style, "normal"); |
| 2545 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPickerButton, background_icon, "bg"); |
| 2546 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, ColorPickerButton, overbright_indicator, "overbright_indicator", "ColorPicker"); |
| 2547 | } |
| 2548 | |
| 2549 | ColorPickerButton::ColorPickerButton(const String &p_text) : |
| 2550 | Button(p_text) { |
| 2551 | set_toggle_mode(true); |
| 2552 | } |
| 2553 | |
| 2554 | ///////////////// |
| 2555 | |
| 2556 | void ColorPresetButton::_notification(int p_what) { |
| 2557 | switch (p_what) { |
| 2558 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 2559 | RID ae = get_accessibility_element(); |
| 2560 | ERR_FAIL_COND(ae.is_null()); |
| 2561 | |
| 2562 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON); |
| 2563 | DisplayServer::get_singleton()->accessibility_update_set_color_value(ae, preset_color); |
| 2564 | } break; |
| 2565 | |
| 2566 | case NOTIFICATION_DRAW: { |
| 2567 | const Rect2 r = Rect2(Point2(0, 0), get_size()); |
| 2568 | Ref<StyleBox> sb_raw = theme_cache.foreground_style->duplicate(); |
| 2569 | Ref<StyleBoxFlat> sb_flat = sb_raw; |
| 2570 | Ref<StyleBoxTexture> sb_texture = sb_raw; |
| 2571 | |
| 2572 | if (sb_flat.is_valid()) { |
| 2573 | sb_flat->set_border_width(SIDE_BOTTOM, 2); |
| 2574 | if (get_draw_mode() == DRAW_PRESSED || get_draw_mode() == DRAW_HOVER_PRESSED) { |
| 2575 | sb_flat->set_border_color(Color(1, 1, 1, 1)); |
| 2576 | } else { |
| 2577 | sb_flat->set_border_color(Color(0, 0, 0, 1)); |
| 2578 | } |
| 2579 | |
| 2580 | if (preset_color.a < 1) { |
| 2581 | // Draw a background pattern when the color is transparent. |
| 2582 | sb_flat->set_bg_color(Color(1, 1, 1)); |
| 2583 | sb_flat->draw(get_canvas_item(), r); |
| 2584 | |
| 2585 | Rect2 bg_texture_rect = r.grow_side(SIDE_LEFT, -sb_flat->get_margin(SIDE_LEFT)); |
| 2586 | bg_texture_rect = bg_texture_rect.grow_side(SIDE_RIGHT, -sb_flat->get_margin(SIDE_RIGHT)); |
| 2587 | bg_texture_rect = bg_texture_rect.grow_side(SIDE_TOP, -sb_flat->get_margin(SIDE_TOP)); |
| 2588 | bg_texture_rect = bg_texture_rect.grow_side(SIDE_BOTTOM, -sb_flat->get_margin(SIDE_BOTTOM)); |
| 2589 | |
| 2590 | draw_texture_rect(theme_cache.background_icon, bg_texture_rect, true); |
| 2591 | sb_flat->set_bg_color(preset_color); |
| 2592 | } |
| 2593 | sb_flat->set_bg_color(preset_color); |
| 2594 | sb_flat->draw(get_canvas_item(), r); |
| 2595 | } else if (sb_texture.is_valid()) { |
| 2596 | if (preset_color.a < 1) { |
| 2597 | // Draw a background pattern when the color is transparent. |
| 2598 | bool use_tile_texture = (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE) || (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE_FIT); |
| 2599 | draw_texture_rect(theme_cache.background_icon, r, use_tile_texture); |
| 2600 | } |
| 2601 | sb_texture->set_modulate(preset_color); |
| 2602 | sb_texture->draw(get_canvas_item(), r); |
| 2603 | } else { |
| 2604 | WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead."); |
| 2605 | } |
| 2606 | |
| 2607 | if (has_focus(true)) { |
| 2608 | RID ci = get_canvas_item(); |
| 2609 | theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size())); |
| 2610 | } |
| 2611 | |
| 2612 | if (is_color_overbright(preset_color)) { |
| 2613 | // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview |
| 2614 | draw_texture(theme_cache.overbright_indicator, Vector2(0, 0)); |
| 2615 | } |
| 2616 | |
| 2617 | } break; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | void ColorPresetButton::set_preset_color(const Color &p_color) { |
| 2622 | preset_color = p_color; |
| 2623 | queue_accessibility_update(); |
| 2624 | } |
| 2625 | |
| 2626 | Color ColorPresetButton::get_preset_color() const { |
| 2627 | return preset_color; |
| 2628 | } |
| 2629 | |
| 2630 | String ColorPresetButton::get_tooltip(const Point2 &p_pos) const { |
| 2631 | Color color = get_preset_color(); |
| 2632 | if (recent) { |
| 2633 | return vformat(atr(ETR("Color: %s\nLMB: Apply color")), color_to_string(color, color.a < 1)); |
| 2634 | } |
| 2635 | return vformat(atr(ETR("Color: %s\nLMB: Apply color\nRMB: Remove preset")), color_to_string(color, color.a < 1)); |
| 2636 | } |
| 2637 | |
| 2638 | void ColorPresetButton::_bind_methods() { |
| 2639 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, foreground_style, "preset_fg"); |
| 2640 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, focus_style, "preset_focus"); |
| 2641 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPresetButton, background_icon, "preset_bg"); |
| 2642 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPresetButton, overbright_indicator); |
| 2643 | } |
| 2644 | |
| 2645 | ColorPresetButton::ColorPresetButton(Color p_color, int p_size, bool p_recent) { |
| 2646 | preset_color = p_color; |
| 2647 | recent = p_recent; |
| 2648 | set_toggle_mode(true); |
| 2649 | set_custom_minimum_size(Size2(p_size, p_size)); |
| 2650 | set_accessibility_name(vformat(atr(ETR("Color: %s")), color_to_string(p_color, p_color.a < 1))); |
| 2651 | set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2652 | } |
| 2653 | |
| 2654 | ColorPresetButton::~ColorPresetButton() { |
| 2655 | } |