| 1 | /**************************************************************************/ |
| 2 | /* color_picker_shape.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_shape.h" |
| 32 | |
| 33 | #include "scene/gui/margin_container.h" |
| 34 | #include "scene/resources/material.h" |
| 35 | #include "thirdparty/misc/ok_color_shader.h" |
| 36 | |
| 37 | void ColorPickerShape::init_shaders() { |
| 38 | wheel_shader.instantiate(); |
| 39 | wheel_shader->set_code(R"( |
| 40 | // ColorPicker wheel shader. |
| 41 | |
| 42 | shader_type canvas_item; |
| 43 | |
| 44 | uniform float wheel_radius = 0.42; |
| 45 | |
| 46 | void fragment() { |
| 47 | float x = UV.x - 0.5; |
| 48 | float y = UV.y - 0.5; |
| 49 | float a = atan(y, x); |
| 50 | x += 0.001; |
| 51 | y += 0.001; |
| 52 | float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius); |
| 53 | x -= 0.002; |
| 54 | float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius); |
| 55 | y -= 0.002; |
| 56 | float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius); |
| 57 | x += 0.002; |
| 58 | float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius); |
| 59 | |
| 60 | COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00); |
| 61 | } |
| 62 | )"); |
| 63 | |
| 64 | circle_shader.instantiate(); |
| 65 | circle_shader->set_code(R"( |
| 66 | // ColorPicker circle shader. |
| 67 | |
| 68 | shader_type canvas_item; |
| 69 | |
| 70 | uniform float v = 1.0; |
| 71 | |
| 72 | void fragment() { |
| 73 | float x = UV.x - 0.5; |
| 74 | float y = UV.y - 0.5; |
| 75 | float a = atan(y, x); |
| 76 | x += 0.001; |
| 77 | y += 0.001; |
| 78 | float b = float(sqrt(x * x + y * y) < 0.5); |
| 79 | x -= 0.002; |
| 80 | float b2 = float(sqrt(x * x + y * y) < 0.5); |
| 81 | y -= 0.002; |
| 82 | float b3 = float(sqrt(x * x + y * y) < 0.5); |
| 83 | x += 0.002; |
| 84 | float b4 = float(sqrt(x * x + y * y) < 0.5); |
| 85 | |
| 86 | COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00); |
| 87 | })"); |
| 88 | |
| 89 | circle_ok_color_shader.instantiate(); |
| 90 | circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"( |
| 91 | // ColorPicker ok color hsl circle shader. |
| 92 | |
| 93 | uniform float ok_hsl_l = 1.0; |
| 94 | |
| 95 | void fragment() { |
| 96 | float x = UV.x - 0.5; |
| 97 | float y = UV.y - 0.5; |
| 98 | float h = atan(y, x) / (2.0 * M_PI); |
| 99 | float s = sqrt(x * x + y * y) * 2.0; |
| 100 | vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l)); |
| 101 | x += 0.001; |
| 102 | y += 0.001; |
| 103 | float b = float(sqrt(x * x + y * y) < 0.5); |
| 104 | x -= 0.002; |
| 105 | float b2 = float(sqrt(x * x + y * y) < 0.5); |
| 106 | y -= 0.002; |
| 107 | float b3 = float(sqrt(x * x + y * y) < 0.5); |
| 108 | x += 0.002; |
| 109 | float b4 = float(sqrt(x * x + y * y) < 0.5); |
| 110 | COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00); |
| 111 | })"); |
| 112 | |
| 113 | rectangle_ok_color_hs_shader.instantiate(); |
| 114 | rectangle_ok_color_hs_shader->set_code(OK_COLOR_SHADER + R"( |
| 115 | // ColorPicker ok color hs rectangle shader. |
| 116 | |
| 117 | uniform float ok_hsl_l = 0.0; |
| 118 | |
| 119 | void fragment() { |
| 120 | float h = UV.x; |
| 121 | float s = 1.0 - UV.y; |
| 122 | vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l)); |
| 123 | COLOR = vec4(col, 1.0); |
| 124 | })"); |
| 125 | |
| 126 | rectangle_ok_color_hl_shader.instantiate(); |
| 127 | rectangle_ok_color_hl_shader->set_code(OK_COLOR_SHADER + R"( |
| 128 | // ColorPicker ok color hl rectangle shader. |
| 129 | |
| 130 | uniform float ok_hsl_s = 0.0; |
| 131 | |
| 132 | void fragment() { |
| 133 | float h = UV.x; |
| 134 | float l = 1.0 - UV.y; |
| 135 | vec3 col = okhsl_to_srgb(vec3(h, ok_hsl_s, l)); |
| 136 | COLOR = vec4(col, 1.0); |
| 137 | })"); |
| 138 | } |
| 139 | |
| 140 | void ColorPickerShape::finish_shaders() { |
| 141 | wheel_shader.unref(); |
| 142 | circle_shader.unref(); |
| 143 | circle_ok_color_shader.unref(); |
| 144 | rectangle_ok_color_hs_shader.unref(); |
| 145 | rectangle_ok_color_hl_shader.unref(); |
| 146 | } |
| 147 | |
| 148 | void ColorPickerShape::_emit_color_changed() { |
| 149 | color_picker->emit_signal(SNAME("color_changed"), color_picker->color); |
| 150 | } |
| 151 | |
| 152 | bool ColorPickerShape::can_handle(const Ref<InputEvent> &p_event, Vector2 &r_position, bool *r_is_click) { |
| 153 | Ref<InputEventMouseButton> mb = p_event; |
| 154 | if (mb.is_valid()) { |
| 155 | if (mb->get_button_index() != MouseButton::LEFT) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (r_is_click) { |
| 160 | *r_is_click = true; |
| 161 | } |
| 162 | |
| 163 | if (mb->is_pressed()) { |
| 164 | is_dragging = true; |
| 165 | r_position = mb->get_position(); |
| 166 | return true; |
| 167 | } else { |
| 168 | _emit_color_changed(); |
| 169 | color_picker->add_recent_preset(color_picker->color); |
| 170 | is_dragging = false; |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | Ref<InputEventMouseMotion> mm = p_event; |
| 176 | if (is_dragging && mm.is_valid()) { |
| 177 | r_position = mm->get_position(); |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void ColorPickerShape::apply_color() { |
| 184 | color_picker->_copy_hsv_okhsl_to_normalized(); |
| 185 | color_picker->_normalized_apply_intensity_to_color(); |
| 186 | color_picker->hsv_cached = true; |
| 187 | color_picker->okhsl_cached = true; |
| 188 | color_picker->_set_pick_color(color_picker->color, true, false); |
| 189 | |
| 190 | if (!color_picker->deferred_mode_enabled) { |
| 191 | _emit_color_changed(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void ColorPickerShape::cancel_event() { |
| 196 | is_dragging = false; |
| 197 | } |
| 198 | |
| 199 | void ColorPickerShape::draw_focus_rect(Control *p_control, const Rect2 &p_rect) { |
| 200 | if (!p_control->has_focus(true)) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | Rect2 focus_rect; |
| 205 | if (p_rect.has_area()) { |
| 206 | focus_rect = p_rect; |
| 207 | } else { |
| 208 | focus_rect = Rect2(Vector2(), p_control->get_size()); |
| 209 | } |
| 210 | |
| 211 | const RID ci = p_control->get_canvas_item(); |
| 212 | if (!cursor_editing) { |
| 213 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, focus_rect, color_picker->theme_cache.focused_not_editing_cursor_color); |
| 214 | } |
| 215 | color_picker->theme_cache.picker_focus_rectangle->draw(ci, focus_rect); |
| 216 | } |
| 217 | |
| 218 | void ColorPickerShape::draw_focus_circle(Control *p_control) { |
| 219 | if (!p_control->has_focus(true)) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | const Rect2 focus_rect(Vector2(), p_control->get_size()); |
| 224 | const RID ci = p_control->get_canvas_item(); |
| 225 | if (!cursor_editing) { |
| 226 | RenderingServer::get_singleton()->canvas_item_add_circle(ci, focus_rect.get_center(), focus_rect.get_size().y * 0.5, color_picker->theme_cache.focused_not_editing_cursor_color); |
| 227 | } |
| 228 | color_picker->theme_cache.picker_focus_circle->draw(ci, focus_rect); |
| 229 | } |
| 230 | |
| 231 | void ColorPickerShape::draw_sv_square(Control *p_control, const Rect2 &p_square, bool p_draw_focus) { |
| 232 | const Vector2 end = p_square.get_end(); |
| 233 | PackedVector2Array points = { |
| 234 | p_square.position, |
| 235 | Vector2(end.x, p_square.position.y), |
| 236 | end, |
| 237 | Vector2(p_square.position.x, end.y), |
| 238 | }; |
| 239 | |
| 240 | Color color1 = Color::from_hsv(color_picker->h, 1, 1); |
| 241 | Color color2 = Color::from_hsv(color_picker->h, 1, 0); |
| 242 | |
| 243 | PackedColorArray colors = { |
| 244 | Color(1, 1, 1, 1), |
| 245 | Color(1, 1, 1, 1), |
| 246 | Color(0, 0, 0, 1), |
| 247 | Color(0, 0, 0, 1), |
| 248 | }; |
| 249 | p_control->draw_polygon(points, colors); |
| 250 | |
| 251 | colors = { |
| 252 | Color(color1, 0), |
| 253 | Color(color1, 1), |
| 254 | Color(color2, 1), |
| 255 | Color(color2, 0), |
| 256 | }; |
| 257 | p_control->draw_polygon(points, colors); |
| 258 | |
| 259 | Vector2 cursor_pos; |
| 260 | cursor_pos.x = CLAMP(p_square.position.x + p_square.size.x * color_picker->s, p_square.position.x, end.x); |
| 261 | cursor_pos.y = CLAMP(p_square.position.y + p_square.size.y * (1.0 - color_picker->v), p_square.position.y, end.y); |
| 262 | |
| 263 | if (p_draw_focus) { |
| 264 | draw_focus_rect(p_control, p_square); |
| 265 | } |
| 266 | draw_cursor(p_control, cursor_pos); |
| 267 | } |
| 268 | |
| 269 | void ColorPickerShape::draw_cursor(Control *p_control, const Vector2 &p_center, bool p_draw_bg) { |
| 270 | const Vector2 position = p_center - color_picker->theme_cache.picker_cursor->get_size() * 0.5; |
| 271 | if (p_draw_bg) { |
| 272 | p_control->draw_texture(color_picker->theme_cache.picker_cursor_bg, position, Color(color_picker->color, 1.0)); |
| 273 | } |
| 274 | p_control->draw_texture(color_picker->theme_cache.picker_cursor, position); |
| 275 | } |
| 276 | |
| 277 | void ColorPickerShape::draw_circle_cursor(Control *p_control, float p_hue, float p_saturation) { |
| 278 | const Vector2 center = p_control->get_size() * 0.5; |
| 279 | const Vector2 cursor_pos( |
| 280 | center.x + (center.x * Math::cos(p_hue * Math::TAU) * p_saturation), |
| 281 | center.y + (center.y * Math::sin(p_hue * Math::TAU) * p_saturation)); |
| 282 | |
| 283 | draw_cursor(p_control, cursor_pos); |
| 284 | } |
| 285 | |
| 286 | void ColorPickerShape::connect_shape_focus(Control *p_shape) { |
| 287 | p_shape->set_focus_mode(Control::FOCUS_ALL); |
| 288 | p_shape->connect(SceneStringName(focus_entered), callable_mp(this, &ColorPickerShape::shape_focus_entered)); |
| 289 | p_shape->connect(SceneStringName(focus_exited), callable_mp(this, &ColorPickerShape::shape_focus_exited)); |
| 290 | } |
| 291 | |
| 292 | void ColorPickerShape::shape_focus_entered() { |
| 293 | Input *input = Input::get_singleton(); |
| 294 | if (!(input->is_action_pressed("ui_up") || input->is_action_pressed("ui_down") || input->is_action_pressed("ui_left") || input->is_action_pressed("ui_right"))) { |
| 295 | cursor_editing = true; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void ColorPickerShape::shape_focus_exited() { |
| 300 | cursor_editing = false; |
| 301 | } |
| 302 | |
| 303 | void ColorPickerShape::handle_cursor_editing(const Ref<InputEvent> &p_event, Control *p_control) { |
| 304 | if (p_event->is_action_pressed("ui_accept", false, true)) { |
| 305 | cursor_editing = !cursor_editing; |
| 306 | p_control->queue_redraw(); |
| 307 | color_picker->accept_event(); |
| 308 | } |
| 309 | |
| 310 | if (cursor_editing && p_event->is_action_pressed("ui_cancel", false, true)) { |
| 311 | cursor_editing = false; |
| 312 | p_control->queue_redraw(); |
| 313 | color_picker->accept_event(); |
| 314 | } |
| 315 | |
| 316 | if (!cursor_editing) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | Input *input = Input::get_singleton(); |
| 321 | bool is_joypad_event = Object::cast_to<InputEventJoypadMotion>(p_event.ptr()) || Object::cast_to<InputEventJoypadButton>(p_event.ptr()); |
| 322 | |
| 323 | if (p_event->is_action_pressed("ui_left", true) || p_event->is_action_pressed("ui_right", true) || p_event->is_action_pressed("ui_up", true) || p_event->is_action_pressed("ui_down", true)) { |
| 324 | if (is_joypad_event) { |
| 325 | if (color_picker->is_processing_internal()) { |
| 326 | color_picker->accept_event(); |
| 327 | return; |
| 328 | } |
| 329 | color_picker->set_process_internal(true); |
| 330 | } |
| 331 | |
| 332 | Vector2 color_change_vector = Vector2( |
| 333 | input->is_action_pressed("ui_right") - input->is_action_pressed("ui_left"), |
| 334 | input->is_action_pressed("ui_down") - input->is_action_pressed("ui_up")); |
| 335 | update_cursor(color_change_vector, p_event->is_echo()); |
| 336 | color_picker->accept_event(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | int ColorPickerShape::get_edge_h_change(const Vector2 &p_color_change_vector) { |
| 341 | int h_change = 0; |
| 342 | |
| 343 | if (color_picker->h > 0 && color_picker->h < 0.5) { |
| 344 | h_change -= p_color_change_vector.x; |
| 345 | } else if (color_picker->h > 0.5 && color_picker->h < 1) { |
| 346 | h_change += p_color_change_vector.x; |
| 347 | } |
| 348 | |
| 349 | if (color_picker->h > 0.25 && color_picker->h < 0.75) { |
| 350 | h_change -= p_color_change_vector.y; |
| 351 | } else if (color_picker->h < 0.25 || color_picker->h > 0.75) { |
| 352 | h_change += p_color_change_vector.y; |
| 353 | } |
| 354 | return h_change; |
| 355 | } |
| 356 | |
| 357 | float ColorPickerShape::get_h_on_circle_edge(const Vector2 &p_color_change_vector) { |
| 358 | int h_change = get_edge_h_change(p_color_change_vector); |
| 359 | |
| 360 | float target_h = Math::wrapf(color_picker->h + h_change / 360.0, 0, 1); |
| 361 | int current_quarter = color_picker->h * 4; |
| 362 | int future_quarter = target_h * 4; |
| 363 | if (p_color_change_vector.y > 0 && ((future_quarter == 0 && current_quarter == 1) || (future_quarter == 1 && current_quarter == 0))) { |
| 364 | target_h = 0.25f; |
| 365 | } else if (p_color_change_vector.y < 0 && ((future_quarter == 2 && current_quarter == 3) || (future_quarter == 3 && current_quarter == 2))) { |
| 366 | target_h = 0.75f; |
| 367 | } else if (p_color_change_vector.x < 0 && ((future_quarter == 1 && current_quarter == 2) || (future_quarter == 2 && current_quarter == 1))) { |
| 368 | target_h = 0.5f; |
| 369 | } else if (p_color_change_vector.x > 0 && ((future_quarter == 3 && current_quarter == 0) || (future_quarter == 0 && current_quarter == 3))) { |
| 370 | target_h = 0; |
| 371 | } |
| 372 | return target_h; |
| 373 | } |
| 374 | |
| 375 | void ColorPickerShape::initialize_controls() { |
| 376 | _initialize_controls(); |
| 377 | update_theme(); |
| 378 | is_initialized = true; |
| 379 | } |
| 380 | |
| 381 | void ColorPickerShape::update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 382 | if (p_color_change_vector.is_zero_approx()) { |
| 383 | echo_multiplier = 1.0; |
| 384 | } else { |
| 385 | echo_multiplier = p_is_echo ? CLAMP(echo_multiplier * 1.1, 1, 25) : 1; |
| 386 | _update_cursor(p_color_change_vector * echo_multiplier, p_is_echo); |
| 387 | apply_color(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | ColorPickerShape::ColorPickerShape(ColorPicker *p_color_picker) { |
| 392 | color_picker = p_color_picker; |
| 393 | } |
| 394 | |
| 395 | void ColorPickerShapeRectangle::_sv_square_input(const Ref<InputEvent> &p_event) { |
| 396 | handle_cursor_editing(p_event, sv_square); |
| 397 | |
| 398 | Vector2 event_position; |
| 399 | if (!can_handle(p_event, event_position)) { |
| 400 | return; |
| 401 | } |
| 402 | event_position = (event_position / sv_square->get_size()).clampf(0.0, 1.0); |
| 403 | |
| 404 | color_picker->s = event_position.x; |
| 405 | color_picker->v = 1.0 - event_position.y; |
| 406 | |
| 407 | apply_color(); |
| 408 | } |
| 409 | |
| 410 | void ColorPickerShapeRectangle::_hue_slider_input(const Ref<InputEvent> &p_event) { |
| 411 | handle_cursor_editing(p_event, hue_slider); |
| 412 | |
| 413 | Vector2 event_position; |
| 414 | if (!can_handle(p_event, event_position)) { |
| 415 | return; |
| 416 | } |
| 417 | color_picker->h = CLAMP(event_position.y / hue_slider->get_size().y, 0.0, 1.0); |
| 418 | apply_color(); |
| 419 | } |
| 420 | |
| 421 | void ColorPickerShapeRectangle::_sv_square_draw() { |
| 422 | draw_sv_square(sv_square, Rect2(Vector2(), sv_square->get_size())); |
| 423 | } |
| 424 | |
| 425 | void ColorPickerShapeRectangle::_hue_slider_draw() { |
| 426 | const Vector2 size = hue_slider->get_size(); |
| 427 | hue_slider->draw_texture_rect(color_picker->theme_cache.color_hue, Rect2(0, 0, -size.y, size.x), false, Color(1, 1, 1), true); |
| 428 | |
| 429 | draw_focus_rect(hue_slider); |
| 430 | |
| 431 | int y = size.y * color_picker->h; |
| 432 | const Color color = Color::from_hsv(color_picker->h, 1, 1); |
| 433 | hue_slider->draw_line(Vector2(0, y), Vector2(size.x, y), color.inverted()); |
| 434 | } |
| 435 | |
| 436 | void ColorPickerShapeRectangle::_initialize_controls() { |
| 437 | sv_square = memnew(Control); |
| 438 | sv_square->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 439 | color_picker->shape_container->add_child(sv_square); |
| 440 | sv_square->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeRectangle::_sv_square_input)); |
| 441 | sv_square->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeRectangle::_sv_square_draw)); |
| 442 | connect_shape_focus(sv_square); |
| 443 | |
| 444 | hue_slider = memnew(Control); |
| 445 | color_picker->shape_container->add_child(hue_slider); |
| 446 | hue_slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeRectangle::_hue_slider_input)); |
| 447 | hue_slider->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeRectangle::_hue_slider_draw)); |
| 448 | connect_shape_focus(hue_slider); |
| 449 | |
| 450 | controls.append(sv_square); |
| 451 | controls.append(hue_slider); |
| 452 | } |
| 453 | |
| 454 | void ColorPickerShapeRectangle::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 455 | if (sv_square->has_focus()) { |
| 456 | color_picker->s = CLAMP(color_picker->s + p_color_change_vector.x / 100.0, 0, 1); |
| 457 | color_picker->v = CLAMP(color_picker->v - p_color_change_vector.y / 100.0, 0, 1); |
| 458 | } else if (hue_slider->has_focus()) { |
| 459 | color_picker->h = CLAMP(color_picker->h + p_color_change_vector.y * echo_multiplier / 360.0, 0, 1); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void ColorPickerShapeRectangle::update_theme() { |
| 464 | const ColorPicker::ThemeCache &theme_cache = color_picker->theme_cache; |
| 465 | sv_square->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height)); |
| 466 | hue_slider->set_custom_minimum_size(Size2(theme_cache.h_width, 0)); |
| 467 | } |
| 468 | |
| 469 | void ColorPickerShapeRectangle::grab_focus() { |
| 470 | hue_slider->grab_focus(); |
| 471 | } |
| 472 | |
| 473 | void ColorPickerShapeOKHSRectangle::_initialize_controls() { |
| 474 | rectangle_margin = memnew(MarginContainer); |
| 475 | rectangle_margin->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 476 | color_picker->shape_container->add_child(rectangle_margin); |
| 477 | |
| 478 | Ref<ShaderMaterial> material; |
| 479 | material.instantiate(); |
| 480 | material->set_shader(_get_shader()); |
| 481 | |
| 482 | square = memnew(Control); |
| 483 | rectangle_margin->add_child(square); |
| 484 | square->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeOKHSRectangle::_square_draw)); |
| 485 | square->set_material(material); |
| 486 | |
| 487 | square_overlay = memnew(Control); |
| 488 | rectangle_margin->add_child(square_overlay); |
| 489 | square_overlay->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 490 | square_overlay->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeOKHSRectangle::_square_overlay_input)); |
| 491 | square_overlay->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeOKHSRectangle::_square_overlay_draw)); |
| 492 | connect_shape_focus(square_overlay); |
| 493 | |
| 494 | value_slider = memnew(Control); |
| 495 | color_picker->shape_container->add_child(value_slider); |
| 496 | value_slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeOKHSRectangle::_value_slider_input)); |
| 497 | value_slider->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeOKHSRectangle::_value_slider_draw)); |
| 498 | connect_shape_focus(value_slider); |
| 499 | |
| 500 | controls.append(rectangle_margin); |
| 501 | controls.append(square); |
| 502 | controls.append(square_overlay); |
| 503 | controls.append(value_slider); |
| 504 | } |
| 505 | |
| 506 | void ColorPickerShapeOKHSRectangle::update_theme() { |
| 507 | const ColorPicker::ThemeCache &theme_cache = color_picker->theme_cache; |
| 508 | rectangle_margin->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height)); |
| 509 | value_slider->set_custom_minimum_size(Size2(theme_cache.h_width, 0)); |
| 510 | } |
| 511 | |
| 512 | void ColorPickerShapeOKHSRectangle::grab_focus() { |
| 513 | square_overlay->grab_focus(); |
| 514 | } |
| 515 | |
| 516 | void ColorPickerShapeOKHSRectangle::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 517 | if (square_overlay->has_focus()) { |
| 518 | color_picker->ok_hsl_h = CLAMP(color_picker->ok_hsl_h + p_color_change_vector.x / 100.0, 0, 1); |
| 519 | color_picker->ok_hsl_s = CLAMP(color_picker->ok_hsl_s - p_color_change_vector.y / 100.0, 0, 1); |
| 520 | } else if (value_slider->has_focus()) { |
| 521 | color_picker->ok_hsl_l = CLAMP(color_picker->ok_hsl_l + p_color_change_vector.y * echo_multiplier / 360.0, 0, 1); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void ColorPickerShapeOKHSRectangle::_square_draw() { |
| 526 | Ref<ShaderMaterial> material = square->get_material(); |
| 527 | material->set_shader_parameter(SNAME("ok_hsl_l"), color_picker->ok_hsl_l); |
| 528 | square->draw_rect(Rect2(Point2(), square->get_size()), Color(1, 1, 1)); |
| 529 | } |
| 530 | |
| 531 | void ColorPickerShapeOKHSRectangle::_square_overlay_input(const Ref<InputEvent> &p_event) { |
| 532 | handle_cursor_editing(p_event, square_overlay); |
| 533 | |
| 534 | Vector2 event_position; |
| 535 | if (!can_handle(p_event, event_position)) { |
| 536 | return; |
| 537 | } |
| 538 | event_position = (event_position / square_overlay->get_size()).clampf(0.0, 1.0); |
| 539 | |
| 540 | color_picker->ok_hsl_h = event_position.x; |
| 541 | color_picker->ok_hsl_s = 1.0 - event_position.y; |
| 542 | |
| 543 | apply_color(); |
| 544 | } |
| 545 | |
| 546 | void ColorPickerShapeOKHSRectangle::_square_overlay_draw() { |
| 547 | const Rect2 rect = Rect2(Vector2(), square_overlay->get_size()); |
| 548 | const Vector2 end = rect.get_end(); |
| 549 | Vector2 cursor_pos; |
| 550 | cursor_pos.x = CLAMP(rect.position.x + rect.size.x * color_picker->ok_hsl_h, rect.position.x, end.x); |
| 551 | cursor_pos.y = CLAMP(rect.position.y + rect.size.y * (1.0 - color_picker->ok_hsl_s), rect.position.y, end.y); |
| 552 | |
| 553 | draw_focus_rect(square_overlay); |
| 554 | draw_cursor(square_overlay, cursor_pos); |
| 555 | } |
| 556 | |
| 557 | void ColorPickerShapeOKHSRectangle::_value_slider_input(const Ref<InputEvent> &p_event) { |
| 558 | handle_cursor_editing(p_event, value_slider); |
| 559 | |
| 560 | Vector2 event_position; |
| 561 | if (!can_handle(p_event, event_position)) { |
| 562 | return; |
| 563 | } |
| 564 | color_picker->ok_hsl_l = 1 - CLAMP(event_position.y / value_slider->get_size().y, 0.0, 1.0); |
| 565 | apply_color(); |
| 566 | } |
| 567 | |
| 568 | void ColorPickerShapeOKHSRectangle::_value_slider_draw() { |
| 569 | const float ok_hsl_h = color_picker->ok_hsl_h; |
| 570 | const float ok_hsl_s = color_picker->ok_hsl_s; |
| 571 | |
| 572 | const Vector2 size = value_slider->get_size(); |
| 573 | PackedVector2Array points{ |
| 574 | Vector2(size.x, 0), |
| 575 | Vector2(size.x, size.y * 0.5), |
| 576 | size, |
| 577 | Vector2(0, size.y), |
| 578 | Vector2(0, size.y * 0.5), |
| 579 | Vector2(), |
| 580 | }; |
| 581 | |
| 582 | Color color1 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 1); |
| 583 | Color color2 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 0.5); |
| 584 | Color color3 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 0); |
| 585 | PackedColorArray colors = { |
| 586 | color1, |
| 587 | color2, |
| 588 | color3, |
| 589 | color3, |
| 590 | color2, |
| 591 | color1, |
| 592 | }; |
| 593 | value_slider->draw_polygon(points, colors); |
| 594 | |
| 595 | draw_focus_rect(value_slider); |
| 596 | |
| 597 | int y = size.y * (1 - CLAMP(color_picker->ok_hsl_l, 0, 1)); |
| 598 | const Color color = Color::from_ok_hsl(ok_hsl_h, 1, color_picker->ok_hsl_l); |
| 599 | value_slider->draw_line(Vector2(0, y), Vector2(size.x, y), color.inverted()); |
| 600 | } |
| 601 | |
| 602 | void ColorPickerShapeOKHLRectangle::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 603 | if (square_overlay->has_focus()) { |
| 604 | color_picker->ok_hsl_h = CLAMP(color_picker->ok_hsl_h + p_color_change_vector.x / 100.0, 0, 1); |
| 605 | color_picker->ok_hsl_l = CLAMP(color_picker->ok_hsl_l - p_color_change_vector.y / 100.0, 0, 1); |
| 606 | } else if (value_slider->has_focus()) { |
| 607 | color_picker->ok_hsl_s = CLAMP(color_picker->ok_hsl_s + p_color_change_vector.y * echo_multiplier / 360.0, 0, 1); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void ColorPickerShapeOKHLRectangle::_square_overlay_input(const Ref<InputEvent> &p_event) { |
| 612 | handle_cursor_editing(p_event, square_overlay); |
| 613 | |
| 614 | Vector2 event_position; |
| 615 | if (!can_handle(p_event, event_position)) { |
| 616 | return; |
| 617 | } |
| 618 | event_position = (event_position / square_overlay->get_size()).clampf(0.0, 1.0); |
| 619 | |
| 620 | color_picker->ok_hsl_h = event_position.x; |
| 621 | color_picker->ok_hsl_l = 1.0 - event_position.y; |
| 622 | |
| 623 | apply_color(); |
| 624 | } |
| 625 | |
| 626 | void ColorPickerShapeOKHLRectangle::_square_overlay_draw() { |
| 627 | const Rect2 rect = Rect2(Vector2(), square_overlay->get_size()); |
| 628 | const Vector2 end = rect.get_end(); |
| 629 | Vector2 cursor_pos; |
| 630 | cursor_pos.x = CLAMP(rect.position.x + rect.size.x * color_picker->ok_hsl_h, rect.position.x, end.x); |
| 631 | cursor_pos.y = CLAMP(rect.position.y + rect.size.y * (1.0 - color_picker->ok_hsl_l), rect.position.y, end.y); |
| 632 | |
| 633 | draw_focus_rect(square_overlay); |
| 634 | draw_cursor(square_overlay, cursor_pos); |
| 635 | } |
| 636 | |
| 637 | void ColorPickerShapeOKHLRectangle::_square_draw() { |
| 638 | Ref<ShaderMaterial> material = square->get_material(); |
| 639 | material->set_shader_parameter(SNAME("ok_hsl_s"), color_picker->ok_hsl_s); |
| 640 | square->draw_rect(Rect2(Point2(), square->get_size()), Color(1, 1, 1)); |
| 641 | } |
| 642 | |
| 643 | void ColorPickerShapeOKHLRectangle::_value_slider_input(const Ref<InputEvent> &p_event) { |
| 644 | handle_cursor_editing(p_event, value_slider); |
| 645 | |
| 646 | Vector2 event_position; |
| 647 | if (!can_handle(p_event, event_position)) { |
| 648 | return; |
| 649 | } |
| 650 | color_picker->ok_hsl_s = 1 - CLAMP(event_position.y / value_slider->get_size().y, 0.0, 1.0); |
| 651 | apply_color(); |
| 652 | } |
| 653 | |
| 654 | void ColorPickerShapeOKHLRectangle::_value_slider_draw() { |
| 655 | const float ok_hsl_h = color_picker->ok_hsl_h; |
| 656 | const float ok_hsl_l = color_picker->ok_hsl_l; |
| 657 | |
| 658 | const Vector2 size = value_slider->get_size(); |
| 659 | PackedVector2Array points{ |
| 660 | Vector2(size.x, 0), |
| 661 | Vector2(size.x, size.y * 0.5), |
| 662 | size, |
| 663 | Vector2(0, size.y), |
| 664 | Vector2(0, size.y * 0.5), |
| 665 | Vector2(), |
| 666 | }; |
| 667 | |
| 668 | Color color1 = Color::from_ok_hsl(ok_hsl_h, 1, ok_hsl_l); |
| 669 | Color color2 = Color::from_ok_hsl(ok_hsl_h, 0.5, ok_hsl_l); |
| 670 | Color color3 = Color::from_ok_hsl(ok_hsl_h, 0, ok_hsl_l); |
| 671 | PackedColorArray colors = { |
| 672 | color1, |
| 673 | color2, |
| 674 | color3, |
| 675 | color3, |
| 676 | color2, |
| 677 | color1, |
| 678 | }; |
| 679 | value_slider->draw_polygon(points, colors); |
| 680 | |
| 681 | draw_focus_rect(value_slider); |
| 682 | |
| 683 | int y = size.y * (1 - CLAMP(color_picker->ok_hsl_s, 0, 1)); |
| 684 | const Color color = Color::from_ok_hsl(ok_hsl_h, 1, ok_hsl_l); |
| 685 | value_slider->draw_line(Vector2(0, y), Vector2(size.x, y), color.inverted()); |
| 686 | } |
| 687 | |
| 688 | float ColorPickerShapeWheel::_get_h_on_wheel(const Vector2 &p_color_change_vector) { |
| 689 | int h_change = get_edge_h_change(p_color_change_vector); |
| 690 | |
| 691 | float target_h = Math::wrapf(color_picker->h + h_change / 360.0, 0, 1); |
| 692 | int current_quarter = color_picker->h * 4; |
| 693 | int future_quarter = target_h * 4; |
| 694 | |
| 695 | if (p_color_change_vector.y > 0 && ((future_quarter == 0 && current_quarter == 1) || (future_quarter == 1 && current_quarter == 0))) { |
| 696 | rotate_next_echo_event = !rotate_next_echo_event; |
| 697 | } else if (p_color_change_vector.y < 0 && ((future_quarter == 2 && current_quarter == 3) || (future_quarter == 3 && current_quarter == 2))) { |
| 698 | rotate_next_echo_event = !rotate_next_echo_event; |
| 699 | } else if (p_color_change_vector.x < 0 && ((future_quarter == 1 && current_quarter == 2) || (future_quarter == 2 && current_quarter == 1))) { |
| 700 | rotate_next_echo_event = !rotate_next_echo_event; |
| 701 | } else if (p_color_change_vector.x > 0 && ((future_quarter == 3 && current_quarter == 0) || (future_quarter == 0 && current_quarter == 3))) { |
| 702 | rotate_next_echo_event = !rotate_next_echo_event; |
| 703 | } |
| 704 | |
| 705 | return target_h; |
| 706 | } |
| 707 | |
| 708 | void ColorPickerShapeWheel::_reset_wheel_focus() { |
| 709 | wheel_focused = true; |
| 710 | } |
| 711 | |
| 712 | void ColorPickerShapeWheel::_wheel_input(const Ref<InputEvent> &p_event) { |
| 713 | handle_cursor_editing(p_event, wheel_uv); |
| 714 | if (!cursor_editing) { |
| 715 | // Wheel and inner square are the same control, so focus has to be moved manually. |
| 716 | if (!wheel_focused && p_event->is_action_pressed("ui_down", true)) { |
| 717 | wheel_focused = true; |
| 718 | wheel_uv->queue_redraw(); |
| 719 | color_picker->accept_event(); |
| 720 | return; |
| 721 | } else if (wheel_focused && p_event->is_action_pressed("ui_up", true)) { |
| 722 | wheel_focused = false; |
| 723 | wheel_uv->queue_redraw(); |
| 724 | color_picker->accept_event(); |
| 725 | return; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | Vector2 event_position; |
| 730 | bool is_click = false; |
| 731 | if (!can_handle(p_event, event_position, &is_click)) { |
| 732 | if (is_click) { |
| 733 | // Released mouse button while dragging wheel. |
| 734 | spinning = false; |
| 735 | } |
| 736 | return; |
| 737 | } |
| 738 | const Vector2 uv_size = wheel_uv->get_size(); |
| 739 | const Vector2 ring_radius = uv_size * Math::SQRT12 * WHEEL_RADIUS; |
| 740 | const Vector2 center = uv_size * 0.5; |
| 741 | |
| 742 | if (is_click && !spinning) { |
| 743 | real_t dist = center.distance_to(event_position); |
| 744 | if (dist >= center.x * WHEEL_RADIUS * 2.0 && dist <= center.x) { |
| 745 | spinning = true; |
| 746 | if (!wheel_focused) { |
| 747 | cursor_editing = true; |
| 748 | wheel_focused = true; |
| 749 | } |
| 750 | } else if (dist > center.x) { |
| 751 | // Clicked outside the wheel. |
| 752 | cancel_event(); |
| 753 | return; |
| 754 | } |
| 755 | }; |
| 756 | |
| 757 | if (spinning) { |
| 758 | real_t rad = center.angle_to_point(event_position); |
| 759 | color_picker->h = ((rad >= 0) ? rad : (Math::TAU + rad)) / Math::TAU; |
| 760 | apply_color(); |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | const Rect2 uv_rect(center - ring_radius, ring_radius * 2.0); |
| 765 | event_position -= uv_rect.position; |
| 766 | event_position /= uv_rect.size; |
| 767 | |
| 768 | if (is_click && (event_position.x < 0 || event_position.x > 1 || event_position.y < 0 || event_position.y > 1)) { |
| 769 | // Clicked inside the wheel, but outside the square. |
| 770 | cancel_event(); |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | event_position = event_position.clampf(0.0, 1.0); |
| 775 | |
| 776 | color_picker->s = event_position.x; |
| 777 | color_picker->v = 1.0 - event_position.y; |
| 778 | if (wheel_focused) { |
| 779 | cursor_editing = true; |
| 780 | wheel_focused = false; |
| 781 | } |
| 782 | |
| 783 | apply_color(); |
| 784 | } |
| 785 | |
| 786 | void ColorPickerShapeWheel::_wheel_draw() { |
| 787 | wheel->draw_rect(Rect2(Point2(), wheel->get_size()), Color(1, 1, 1)); |
| 788 | } |
| 789 | |
| 790 | void ColorPickerShapeWheel::_wheel_uv_draw() { |
| 791 | const Vector2 uv_size = wheel_uv->get_size(); |
| 792 | const Vector2 ring_radius = uv_size * Math::SQRT12 * WHEEL_RADIUS; |
| 793 | const Vector2 center = uv_size * 0.5; |
| 794 | |
| 795 | const Rect2 uv_rect(center - ring_radius, ring_radius * 2.0); |
| 796 | draw_sv_square(wheel_uv, uv_rect, !wheel_focused); |
| 797 | if (wheel_focused) { |
| 798 | draw_focus_circle(wheel_uv); |
| 799 | } |
| 800 | |
| 801 | float radius = WHEEL_RADIUS * 2.0; |
| 802 | radius += (1.0 - radius) * 0.5; |
| 803 | const Vector2 cursor_pos = center + |
| 804 | Vector2(center.x * Math::cos(color_picker->h * Math::TAU) * radius, |
| 805 | center.y * Math::sin(color_picker->h * Math::TAU) * radius); |
| 806 | draw_cursor(wheel_uv, cursor_pos, false); |
| 807 | } |
| 808 | |
| 809 | void ColorPickerShapeWheel::_initialize_controls() { |
| 810 | Ref<ShaderMaterial> material; |
| 811 | material.instantiate(); |
| 812 | material->set_shader(ColorPickerShape::wheel_shader); |
| 813 | material->set_shader_parameter("wheel_radius", WHEEL_RADIUS); |
| 814 | |
| 815 | wheel = memnew(Control); |
| 816 | wheel->set_material(material); |
| 817 | color_picker->shape_container->add_child(wheel); |
| 818 | wheel->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeWheel::_wheel_draw)); |
| 819 | |
| 820 | wheel_uv = memnew(Control); |
| 821 | wheel_uv->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 822 | wheel->add_child(wheel_uv); |
| 823 | wheel_uv->connect(SceneStringName(focus_entered), callable_mp(this, &ColorPickerShapeWheel::_reset_wheel_focus)); |
| 824 | wheel_uv->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeWheel::_wheel_input)); |
| 825 | wheel_uv->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeWheel::_wheel_uv_draw)); |
| 826 | connect_shape_focus(wheel_uv); |
| 827 | |
| 828 | controls.append(wheel); |
| 829 | controls.append(wheel_uv); |
| 830 | } |
| 831 | |
| 832 | void ColorPickerShapeWheel::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 833 | if (wheel_focused) { |
| 834 | if (p_is_echo && rotate_next_echo_event) { |
| 835 | color_picker->h = _get_h_on_wheel(-p_color_change_vector); |
| 836 | } else { |
| 837 | rotate_next_echo_event = false; |
| 838 | color_picker->h = _get_h_on_wheel(p_color_change_vector); |
| 839 | } |
| 840 | } else { |
| 841 | color_picker->s = CLAMP(color_picker->s + p_color_change_vector.x / 100.0, 0, 1); |
| 842 | color_picker->v = CLAMP(color_picker->v - p_color_change_vector.y / 100.0, 0, 1); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | void ColorPickerShapeWheel::update_theme() { |
| 847 | const ColorPicker::ThemeCache &theme_cache = color_picker->theme_cache; |
| 848 | wheel->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height)); |
| 849 | } |
| 850 | |
| 851 | void ColorPickerShapeWheel::grab_focus() { |
| 852 | wheel_uv->grab_focus(); |
| 853 | } |
| 854 | |
| 855 | void ColorPickerShapeCircle::update_circle_cursor(const Vector2 &p_color_change_vector, const Vector2 &p_center, const Vector2 &p_hue_offset) { |
| 856 | if (circle_keyboard_joypad_picker_cursor_position == Vector2i()) { |
| 857 | circle_keyboard_joypad_picker_cursor_position = p_center + p_hue_offset; |
| 858 | } |
| 859 | |
| 860 | Vector2i potential_cursor_position = circle_keyboard_joypad_picker_cursor_position + p_color_change_vector; |
| 861 | real_t potential_new_cursor_distance = p_center.distance_to(potential_cursor_position); |
| 862 | real_t dist_pre = p_center.distance_to(circle_keyboard_joypad_picker_cursor_position); |
| 863 | if (color_picker->s < 1 || potential_new_cursor_distance < dist_pre) { |
| 864 | circle_keyboard_joypad_picker_cursor_position += p_color_change_vector; |
| 865 | real_t dist = p_center.distance_to(circle_keyboard_joypad_picker_cursor_position); |
| 866 | real_t rad = p_center.angle_to_point(circle_keyboard_joypad_picker_cursor_position); |
| 867 | color_picker->h = ((rad >= 0) ? rad : (Math::TAU + rad)) / Math::TAU; |
| 868 | color_picker->s = CLAMP(dist / p_center.x, 0, 1); |
| 869 | } else { |
| 870 | color_picker->h = get_h_on_circle_edge(p_color_change_vector); |
| 871 | circle_keyboard_joypad_picker_cursor_position = Vector2i(); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | void ColorPickerShapeCircle::_initialize_controls() { |
| 876 | Ref<ShaderMaterial> material; |
| 877 | material.instantiate(); |
| 878 | material->set_shader(_get_shader()); |
| 879 | |
| 880 | circle = memnew(Control); |
| 881 | circle->set_material(material); |
| 882 | color_picker->shape_container->add_child(circle); |
| 883 | circle->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeCircle::_circle_draw)); |
| 884 | |
| 885 | circle_overlay = memnew(Control); |
| 886 | circle_overlay->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 887 | circle->add_child(circle_overlay); |
| 888 | circle_overlay->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeCircle::_circle_input)); |
| 889 | circle_overlay->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeCircle::_circle_overlay_draw)); |
| 890 | connect_shape_focus(circle_overlay); |
| 891 | |
| 892 | value_slider = memnew(Control); |
| 893 | color_picker->shape_container->add_child(value_slider); |
| 894 | value_slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPickerShapeCircle::_value_slider_input)); |
| 895 | value_slider->connect(SceneStringName(draw), callable_mp(this, &ColorPickerShapeCircle::_value_slider_draw)); |
| 896 | connect_shape_focus(value_slider); |
| 897 | |
| 898 | controls.append(circle); |
| 899 | controls.append(circle_overlay); |
| 900 | controls.append(value_slider); |
| 901 | } |
| 902 | |
| 903 | void ColorPickerShapeCircle::update_theme() { |
| 904 | const ColorPicker::ThemeCache &theme_cache = color_picker->theme_cache; |
| 905 | circle->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height)); |
| 906 | value_slider->set_custom_minimum_size(Size2(theme_cache.h_width, 0)); |
| 907 | } |
| 908 | |
| 909 | void ColorPickerShapeCircle::grab_focus() { |
| 910 | circle_overlay->grab_focus(); |
| 911 | } |
| 912 | |
| 913 | void ColorPickerShapeVHSCircle::_circle_input(const Ref<InputEvent> &p_event) { |
| 914 | handle_cursor_editing(p_event, circle_overlay); |
| 915 | |
| 916 | Vector2 event_position; |
| 917 | bool is_click = false; |
| 918 | if (!can_handle(p_event, event_position, &is_click)) { |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | Vector2 center = circle->get_size() * 0.5; |
| 923 | real_t dist = center.distance_to(event_position); |
| 924 | if (is_click && dist > center.x) { |
| 925 | // Clicked outside the circle. |
| 926 | cancel_event(); |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | real_t rad = center.angle_to_point(event_position); |
| 931 | color_picker->h = ((rad >= 0) ? rad : (Math::TAU + rad)) / Math::TAU; |
| 932 | color_picker->s = CLAMP(dist / center.x, 0, 1); |
| 933 | color_picker->ok_hsl_h = color_picker->h; |
| 934 | color_picker->ok_hsl_s = color_picker->s; |
| 935 | circle_keyboard_joypad_picker_cursor_position = Vector2i(); |
| 936 | |
| 937 | apply_color(); |
| 938 | } |
| 939 | |
| 940 | void ColorPickerShapeVHSCircle::_value_slider_input(const Ref<InputEvent> &p_event) { |
| 941 | handle_cursor_editing(p_event, value_slider); |
| 942 | |
| 943 | Vector2 event_position; |
| 944 | if (!can_handle(p_event, event_position)) { |
| 945 | return; |
| 946 | } |
| 947 | color_picker->v = 1.0 - CLAMP(event_position.y / value_slider->get_size().y, 0.0, 1.0); |
| 948 | apply_color(); |
| 949 | } |
| 950 | |
| 951 | void ColorPickerShapeVHSCircle::_circle_draw() { |
| 952 | Ref<ShaderMaterial> material = circle->get_material(); |
| 953 | material->set_shader_parameter(SNAME("v"), color_picker->v); |
| 954 | circle->draw_rect(Rect2(Point2(), circle->get_size()), Color(1, 1, 1)); |
| 955 | } |
| 956 | |
| 957 | void ColorPickerShapeVHSCircle::_circle_overlay_draw() { |
| 958 | draw_focus_circle(circle_overlay); |
| 959 | draw_circle_cursor(circle_overlay, color_picker->h, color_picker->s); |
| 960 | } |
| 961 | |
| 962 | void ColorPickerShapeVHSCircle::_value_slider_draw() { |
| 963 | const Vector2 size = value_slider->get_size(); |
| 964 | PackedVector2Array points{ |
| 965 | Vector2(), |
| 966 | Vector2(size.x, 0), |
| 967 | size, |
| 968 | Vector2(0, size.y), |
| 969 | }; |
| 970 | |
| 971 | Color color = Color::from_hsv(color_picker->h, color_picker->s, 1); |
| 972 | PackedColorArray colors = { |
| 973 | color, |
| 974 | color, |
| 975 | Color(), |
| 976 | Color(), |
| 977 | }; |
| 978 | |
| 979 | value_slider->draw_polygon(points, colors); |
| 980 | |
| 981 | draw_focus_rect(value_slider); |
| 982 | |
| 983 | int y = size.y * (1 - CLAMP(color_picker->v, 0, 1)); |
| 984 | color.set_hsv(color_picker->h, 1, color_picker->v); |
| 985 | value_slider->draw_line(Vector2(0, y), Vector2(size.x, y), color.inverted()); |
| 986 | } |
| 987 | |
| 988 | void ColorPickerShapeVHSCircle::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 989 | if (circle_overlay->has_focus()) { |
| 990 | const Vector2 center = circle_overlay->get_size() / 2.0; |
| 991 | const Vector2 hue_offset = center * Vector2(Math::cos(color_picker->h * Math::TAU), Math::sin(color_picker->h * Math::TAU)) * color_picker->s; |
| 992 | update_circle_cursor(p_color_change_vector, center, hue_offset); |
| 993 | } else if (value_slider->has_focus()) { |
| 994 | color_picker->v = CLAMP(color_picker->v - p_color_change_vector.y * echo_multiplier / 100.0, 0, 1); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | void ColorPickerShapeOKHSLCircle::_circle_input(const Ref<InputEvent> &p_event) { |
| 999 | handle_cursor_editing(p_event, circle_overlay); |
| 1000 | |
| 1001 | Vector2 event_position; |
| 1002 | bool is_click = false; |
| 1003 | if (!can_handle(p_event, event_position, &is_click)) { |
| 1004 | return; |
| 1005 | } |
| 1006 | |
| 1007 | const Vector2 center = circle->get_size() * 0.5; |
| 1008 | real_t dist = center.distance_to(event_position); |
| 1009 | if (is_click && dist > center.x) { |
| 1010 | // Clicked outside the circle. |
| 1011 | cancel_event(); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | real_t rad = center.angle_to_point(event_position); |
| 1016 | color_picker->h = ((rad >= 0) ? rad : (Math::TAU + rad)) / Math::TAU; |
| 1017 | color_picker->s = CLAMP(dist / center.x, 0, 1); |
| 1018 | color_picker->ok_hsl_h = color_picker->h; |
| 1019 | color_picker->ok_hsl_s = color_picker->s; |
| 1020 | circle_keyboard_joypad_picker_cursor_position = Vector2i(); |
| 1021 | |
| 1022 | apply_color(); |
| 1023 | } |
| 1024 | |
| 1025 | void ColorPickerShapeOKHSLCircle::_value_slider_input(const Ref<InputEvent> &p_event) { |
| 1026 | handle_cursor_editing(p_event, value_slider); |
| 1027 | |
| 1028 | Vector2 event_position; |
| 1029 | if (!can_handle(p_event, event_position)) { |
| 1030 | return; |
| 1031 | } |
| 1032 | color_picker->ok_hsl_l = 1.0 - CLAMP(event_position.y / value_slider->get_size().y, 0.0, 1.0); |
| 1033 | apply_color(); |
| 1034 | } |
| 1035 | |
| 1036 | void ColorPickerShapeOKHSLCircle::_circle_draw() { |
| 1037 | Ref<ShaderMaterial> material = circle->get_material(); |
| 1038 | material->set_shader_parameter(SNAME("ok_hsl_l"), color_picker->ok_hsl_l); |
| 1039 | circle->draw_rect(Rect2(Point2(), circle->get_size()), Color(1, 1, 1)); |
| 1040 | } |
| 1041 | |
| 1042 | void ColorPickerShapeOKHSLCircle::_circle_overlay_draw() { |
| 1043 | draw_focus_circle(circle_overlay); |
| 1044 | draw_circle_cursor(circle_overlay, color_picker->ok_hsl_h, color_picker->ok_hsl_s); |
| 1045 | } |
| 1046 | |
| 1047 | void ColorPickerShapeOKHSLCircle::_value_slider_draw() { |
| 1048 | const float ok_hsl_h = color_picker->ok_hsl_h; |
| 1049 | const float ok_hsl_s = color_picker->ok_hsl_s; |
| 1050 | const float ok_hsl_l = color_picker->ok_hsl_l; |
| 1051 | |
| 1052 | const Vector2 size = value_slider->get_size(); |
| 1053 | PackedVector2Array points{ |
| 1054 | Vector2(size.x, 0), |
| 1055 | Vector2(size.x, size.y * 0.5), |
| 1056 | size, |
| 1057 | Vector2(0, size.y), |
| 1058 | Vector2(0, size.y * 0.5), |
| 1059 | Vector2(), |
| 1060 | }; |
| 1061 | |
| 1062 | Color color1 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 1); |
| 1063 | Color color2 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 0.5); |
| 1064 | Color color3 = Color::from_ok_hsl(ok_hsl_h, ok_hsl_s, 0); |
| 1065 | PackedColorArray colors = { |
| 1066 | color1, |
| 1067 | color2, |
| 1068 | color3, |
| 1069 | color3, |
| 1070 | color2, |
| 1071 | color1, |
| 1072 | }; |
| 1073 | value_slider->draw_polygon(points, colors); |
| 1074 | |
| 1075 | draw_focus_rect(value_slider); |
| 1076 | |
| 1077 | int y = size.y * (1 - CLAMP(ok_hsl_l, 0, 1)); |
| 1078 | value_slider->draw_line(Vector2(0, y), Vector2(size.x, y), Color::from_ok_hsl(ok_hsl_h, 1, ok_hsl_l).inverted()); |
| 1079 | } |
| 1080 | |
| 1081 | void ColorPickerShapeOKHSLCircle::_update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) { |
| 1082 | if (circle_overlay->has_focus()) { |
| 1083 | const Vector2 center = circle_overlay->get_size() / 2.0; |
| 1084 | const Vector2 hue_offset = center * Vector2(Math::cos(color_picker->ok_hsl_h * Math::TAU), Math::sin(color_picker->ok_hsl_h * Math::TAU)) * color_picker->ok_hsl_s; |
| 1085 | update_circle_cursor(p_color_change_vector, center, hue_offset); |
| 1086 | color_picker->ok_hsl_h = color_picker->h; |
| 1087 | color_picker->ok_hsl_s = color_picker->s; |
| 1088 | } else if (value_slider->has_focus()) { |
| 1089 | color_picker->ok_hsl_l = CLAMP(color_picker->ok_hsl_l - p_color_change_vector.y * echo_multiplier / 100.0, 0, 1); |
| 1090 | } |
| 1091 | } |