color_picker_shape.cpp 38 KB

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