color_picker.cpp 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /**************************************************************************/
  2. /* color_picker.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "color_picker.h"
  31. #include "core/io/image.h"
  32. #include "scene/gui/aspect_ratio_container.h"
  33. #include "scene/gui/color_mode.h"
  34. #include "scene/gui/grid_container.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/margin_container.h"
  38. #include "scene/gui/menu_button.h"
  39. #include "scene/gui/option_button.h"
  40. #include "scene/gui/popup_menu.h"
  41. #include "scene/gui/slider.h"
  42. #include "scene/gui/spin_box.h"
  43. #include "scene/gui/texture_rect.h"
  44. #include "scene/resources/image_texture.h"
  45. #include "scene/resources/style_box_flat.h"
  46. #include "scene/resources/style_box_texture.h"
  47. #include "scene/theme/theme_db.h"
  48. #include "thirdparty/misc/ok_color_shader.h"
  49. void ColorPicker::_notification(int p_what) {
  50. switch (p_what) {
  51. case NOTIFICATION_ENTER_TREE: {
  52. _update_color();
  53. } break;
  54. case NOTIFICATION_READY: {
  55. // FIXME: The embedding check is needed to fix a bug in single-window mode (GH-93718).
  56. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SCREEN_CAPTURE) && !get_tree()->get_root()->is_embedding_subwindows()) {
  57. btn_pick->set_tooltip_text(ETR("Pick a color from the screen."));
  58. btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed));
  59. } else {
  60. // On unsupported platforms, use a legacy method for color picking.
  61. btn_pick->set_tooltip_text(ETR("Pick a color from the application window."));
  62. btn_pick->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_pick_button_pressed_legacy));
  63. }
  64. } break;
  65. case NOTIFICATION_TRANSLATION_CHANGED: {
  66. List<BaseButton *> buttons;
  67. preset_group->get_buttons(&buttons);
  68. for (List<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  69. Color preset_color = ((ColorPresetButton *)E->get())->get_preset_color();
  70. E->get()->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color\nRMB: Remove preset")), preset_color.to_html(preset_color.a < 1)));
  71. }
  72. buttons.clear();
  73. recent_preset_group->get_buttons(&buttons);
  74. for (List<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
  75. Color preset_color = ((ColorPresetButton *)E->get())->get_preset_color();
  76. E->get()->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color")), preset_color.to_html(preset_color.a < 1)));
  77. }
  78. } break;
  79. case NOTIFICATION_THEME_CHANGED: {
  80. btn_pick->set_button_icon(theme_cache.screen_picker);
  81. _update_drop_down_arrow(btn_preset->is_pressed(), btn_preset);
  82. _update_drop_down_arrow(btn_recent_preset->is_pressed(), btn_recent_preset);
  83. btn_add_preset->set_button_icon(theme_cache.add_preset);
  84. btn_pick->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
  85. btn_shape->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
  86. btn_mode->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
  87. uv_edit->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height));
  88. w_edit->set_custom_minimum_size(Size2(theme_cache.h_width, 0));
  89. wheel_edit->set_custom_minimum_size(Size2(theme_cache.sv_width, theme_cache.sv_height));
  90. wheel_margin->add_theme_constant_override("margin_bottom", 8 * theme_cache.base_scale);
  91. for (int i = 0; i < SLIDER_COUNT; i++) {
  92. labels[i]->set_custom_minimum_size(Size2(theme_cache.label_width, 0));
  93. sliders[i]->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers);
  94. }
  95. alpha_label->set_custom_minimum_size(Size2(theme_cache.label_width, 0));
  96. alpha_slider->add_theme_constant_override(SNAME("center_grabber"), theme_cache.center_slider_grabbers);
  97. for (int i = 0; i < MODE_BUTTON_COUNT; i++) {
  98. mode_btns[i]->begin_bulk_theme_override();
  99. mode_btns[i]->add_theme_style_override(SceneStringName(pressed), theme_cache.mode_button_pressed);
  100. mode_btns[i]->add_theme_style_override(CoreStringName(normal), theme_cache.mode_button_normal);
  101. mode_btns[i]->add_theme_style_override(SNAME("hover"), theme_cache.mode_button_hover);
  102. mode_btns[i]->end_bulk_theme_override();
  103. }
  104. shape_popup->set_item_icon(shape_popup->get_item_index(SHAPE_HSV_RECTANGLE), theme_cache.shape_rect);
  105. shape_popup->set_item_icon(shape_popup->get_item_index(SHAPE_HSV_WHEEL), theme_cache.shape_rect_wheel);
  106. shape_popup->set_item_icon(shape_popup->get_item_index(SHAPE_VHS_CIRCLE), theme_cache.shape_circle);
  107. shape_popup->set_item_icon(shape_popup->get_item_index(SHAPE_OKHSL_CIRCLE), theme_cache.shape_circle);
  108. if (current_shape != SHAPE_NONE) {
  109. btn_shape->set_button_icon(shape_popup->get_item_icon(current_shape));
  110. }
  111. internal_margin->begin_bulk_theme_override();
  112. internal_margin->add_theme_constant_override(SNAME("margin_bottom"), theme_cache.content_margin);
  113. internal_margin->add_theme_constant_override(SNAME("margin_left"), theme_cache.content_margin);
  114. internal_margin->add_theme_constant_override(SNAME("margin_right"), theme_cache.content_margin);
  115. internal_margin->add_theme_constant_override(SNAME("margin_top"), theme_cache.content_margin);
  116. internal_margin->end_bulk_theme_override();
  117. _reset_sliders_theme();
  118. if (Engine::get_singleton()->is_editor_hint()) {
  119. // Adjust for the width of the "Script" icon.
  120. text_type->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
  121. }
  122. _update_presets();
  123. _update_recent_presets();
  124. _update_controls();
  125. } break;
  126. case NOTIFICATION_WM_CLOSE_REQUEST: {
  127. if (picker_window != nullptr && picker_window->is_visible()) {
  128. picker_window->hide();
  129. }
  130. } break;
  131. case NOTIFICATION_INTERNAL_PROCESS: {
  132. if (!is_picking_color) {
  133. return;
  134. }
  135. set_pick_color(DisplayServer::get_singleton()->screen_get_pixel(DisplayServer::get_singleton()->mouse_get_position()));
  136. }
  137. }
  138. }
  139. void ColorPicker::_update_theme_item_cache() {
  140. VBoxContainer::_update_theme_item_cache();
  141. theme_cache.base_scale = get_theme_default_base_scale();
  142. }
  143. void ColorPicker::init_shaders() {
  144. wheel_shader.instantiate();
  145. wheel_shader->set_code(R"(
  146. // ColorPicker wheel shader.
  147. shader_type canvas_item;
  148. void fragment() {
  149. float x = UV.x - 0.5;
  150. float y = UV.y - 0.5;
  151. float a = atan(y, x);
  152. x += 0.001;
  153. y += 0.001;
  154. float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  155. x -= 0.002;
  156. float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  157. y -= 0.002;
  158. float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  159. x += 0.002;
  160. float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  161. 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);
  162. }
  163. )");
  164. circle_shader.instantiate();
  165. circle_shader->set_code(R"(
  166. // ColorPicker circle shader.
  167. shader_type canvas_item;
  168. uniform float v = 1.0;
  169. void fragment() {
  170. float x = UV.x - 0.5;
  171. float y = UV.y - 0.5;
  172. float a = atan(y, x);
  173. x += 0.001;
  174. y += 0.001;
  175. float b = float(sqrt(x * x + y * y) < 0.5);
  176. x -= 0.002;
  177. float b2 = float(sqrt(x * x + y * y) < 0.5);
  178. y -= 0.002;
  179. float b3 = float(sqrt(x * x + y * y) < 0.5);
  180. x += 0.002;
  181. float b4 = float(sqrt(x * x + y * y) < 0.5);
  182. 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);
  183. })");
  184. circle_ok_color_shader.instantiate();
  185. circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
  186. // ColorPicker ok color hsv circle shader.
  187. uniform float ok_hsl_l = 1.0;
  188. void fragment() {
  189. float x = UV.x - 0.5;
  190. float y = UV.y - 0.5;
  191. float h = atan(y, x) / (2.0 * M_PI);
  192. float s = sqrt(x * x + y * y) * 2.0;
  193. vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
  194. x += 0.001;
  195. y += 0.001;
  196. float b = float(sqrt(x * x + y * y) < 0.5);
  197. x -= 0.002;
  198. float b2 = float(sqrt(x * x + y * y) < 0.5);
  199. y -= 0.002;
  200. float b3 = float(sqrt(x * x + y * y) < 0.5);
  201. x += 0.002;
  202. float b4 = float(sqrt(x * x + y * y) < 0.5);
  203. COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
  204. })");
  205. }
  206. void ColorPicker::finish_shaders() {
  207. wheel_shader.unref();
  208. circle_shader.unref();
  209. circle_ok_color_shader.unref();
  210. }
  211. void ColorPicker::set_focus_on_line_edit() {
  212. callable_mp((Control *)c_text, &Control::grab_focus).call_deferred();
  213. }
  214. void ColorPicker::_update_controls() {
  215. int mode_sliders_count = modes[current_mode]->get_slider_count();
  216. for (int i = current_slider_count; i < mode_sliders_count; i++) {
  217. sliders[i]->show();
  218. labels[i]->show();
  219. values[i]->show();
  220. }
  221. for (int i = mode_sliders_count; i < current_slider_count; i++) {
  222. sliders[i]->hide();
  223. labels[i]->hide();
  224. values[i]->hide();
  225. }
  226. current_slider_count = mode_sliders_count;
  227. for (int i = 0; i < current_slider_count; i++) {
  228. labels[i]->set_text(modes[current_mode]->get_slider_label(i));
  229. }
  230. alpha_label->set_text("A");
  231. slider_theme_modified = modes[current_mode]->apply_theme();
  232. if (edit_alpha) {
  233. alpha_value->show();
  234. alpha_slider->show();
  235. alpha_label->show();
  236. } else {
  237. alpha_value->hide();
  238. alpha_slider->hide();
  239. alpha_label->hide();
  240. }
  241. switch (_get_actual_shape()) {
  242. case SHAPE_HSV_RECTANGLE:
  243. wheel_edit->hide();
  244. w_edit->show();
  245. uv_edit->show();
  246. btn_shape->show();
  247. break;
  248. case SHAPE_HSV_WHEEL:
  249. wheel_edit->show();
  250. w_edit->hide();
  251. uv_edit->hide();
  252. btn_shape->show();
  253. wheel->set_material(wheel_mat);
  254. break;
  255. case SHAPE_VHS_CIRCLE:
  256. wheel_edit->show();
  257. w_edit->show();
  258. uv_edit->hide();
  259. btn_shape->show();
  260. wheel->set_material(circle_mat);
  261. circle_mat->set_shader(circle_shader);
  262. break;
  263. case SHAPE_OKHSL_CIRCLE:
  264. wheel_edit->show();
  265. w_edit->show();
  266. uv_edit->hide();
  267. btn_shape->show();
  268. wheel->set_material(circle_mat);
  269. circle_mat->set_shader(circle_ok_color_shader);
  270. break;
  271. case SHAPE_NONE:
  272. wheel_edit->hide();
  273. w_edit->hide();
  274. uv_edit->hide();
  275. btn_shape->hide();
  276. break;
  277. default: {
  278. }
  279. }
  280. }
  281. void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
  282. if (text_changed) {
  283. add_recent_preset(color);
  284. text_changed = false;
  285. }
  286. color = p_color;
  287. if (color != last_color) {
  288. _copy_color_to_hsv();
  289. last_color = color;
  290. }
  291. if (!is_inside_tree()) {
  292. return;
  293. }
  294. _update_color(p_update_sliders);
  295. }
  296. void ColorPicker::set_pick_color(const Color &p_color) {
  297. _set_pick_color(p_color, true); //because setters can't have more arguments
  298. }
  299. void ColorPicker::set_old_color(const Color &p_color) {
  300. old_color = p_color;
  301. }
  302. void ColorPicker::set_display_old_color(bool p_enabled) {
  303. display_old_color = p_enabled;
  304. }
  305. bool ColorPicker::is_displaying_old_color() const {
  306. return display_old_color;
  307. }
  308. void ColorPicker::set_edit_alpha(bool p_show) {
  309. if (edit_alpha == p_show) {
  310. return;
  311. }
  312. edit_alpha = p_show;
  313. _update_controls();
  314. if (!is_inside_tree()) {
  315. return;
  316. }
  317. _update_color();
  318. sample->queue_redraw();
  319. }
  320. bool ColorPicker::is_editing_alpha() const {
  321. return edit_alpha;
  322. }
  323. void ColorPicker::_slider_drag_started() {
  324. currently_dragging = true;
  325. }
  326. void ColorPicker::_slider_value_changed() {
  327. if (updating) {
  328. return;
  329. }
  330. color = modes[current_mode]->get_color();
  331. modes[current_mode]->_value_changed();
  332. if (current_mode == MODE_HSV) {
  333. h = sliders[0]->get_value() / 360.0;
  334. s = sliders[1]->get_value() / 100.0;
  335. v = sliders[2]->get_value() / 100.0;
  336. ok_hsl_h = color.get_ok_hsl_h();
  337. ok_hsl_s = color.get_ok_hsl_s();
  338. ok_hsl_l = color.get_ok_hsl_l();
  339. last_color = color;
  340. } else if (current_mode == MODE_OKHSL) {
  341. ok_hsl_h = sliders[0]->get_value() / 360.0;
  342. ok_hsl_s = sliders[1]->get_value() / 100.0;
  343. ok_hsl_l = sliders[2]->get_value() / 100.0;
  344. h = color.get_h();
  345. s = color.get_s();
  346. v = color.get_v();
  347. last_color = color;
  348. }
  349. _set_pick_color(color, false);
  350. if (!deferred_mode_enabled || !currently_dragging) {
  351. emit_signal(SNAME("color_changed"), color);
  352. }
  353. }
  354. void ColorPicker::_slider_drag_ended() {
  355. currently_dragging = false;
  356. if (deferred_mode_enabled) {
  357. emit_signal(SNAME("color_changed"), color);
  358. }
  359. }
  360. void ColorPicker::add_mode(ColorMode *p_mode) {
  361. modes.push_back(p_mode);
  362. }
  363. void ColorPicker::create_slider(GridContainer *gc, int idx) {
  364. Label *lbl = memnew(Label);
  365. lbl->set_v_size_flags(SIZE_SHRINK_CENTER);
  366. lbl->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  367. gc->add_child(lbl);
  368. HSlider *slider = memnew(HSlider);
  369. slider->set_v_size_flags(SIZE_SHRINK_CENTER);
  370. slider->set_focus_mode(FOCUS_NONE);
  371. gc->add_child(slider);
  372. SpinBox *val = memnew(SpinBox);
  373. slider->share(val);
  374. val->set_select_all_on_focus(true);
  375. gc->add_child(val);
  376. LineEdit *vle = val->get_line_edit();
  377. vle->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed));
  378. vle->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_line_edit_input));
  379. vle->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  380. val->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input));
  381. slider->set_h_size_flags(SIZE_EXPAND_FILL);
  382. slider->connect("drag_started", callable_mp(this, &ColorPicker::_slider_drag_started));
  383. slider->connect(SceneStringName(value_changed), callable_mp(this, &ColorPicker::_slider_value_changed).unbind(1));
  384. slider->connect("drag_ended", callable_mp(this, &ColorPicker::_slider_drag_ended).unbind(1));
  385. slider->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_slider_draw).bind(idx));
  386. slider->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_slider_or_spin_input));
  387. if (idx < SLIDER_COUNT) {
  388. sliders[idx] = slider;
  389. values[idx] = val;
  390. labels[idx] = lbl;
  391. } else {
  392. alpha_slider = slider;
  393. alpha_value = val;
  394. alpha_label = lbl;
  395. }
  396. }
  397. #ifdef TOOLS_ENABLED
  398. void ColorPicker::set_editor_settings(Object *p_editor_settings) {
  399. if (editor_settings) {
  400. return;
  401. }
  402. editor_settings = p_editor_settings;
  403. if (preset_cache.is_empty()) {
  404. PackedColorArray saved_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "presets", PackedColorArray());
  405. for (int i = 0; i < saved_presets.size(); i++) {
  406. preset_cache.push_back(saved_presets[i]);
  407. }
  408. }
  409. for (const Color &preset : preset_cache) {
  410. presets.push_back(preset);
  411. }
  412. if (recent_preset_cache.is_empty()) {
  413. PackedColorArray saved_recent_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "recent_presets", PackedColorArray());
  414. for (int i = 0; i < saved_recent_presets.size(); i++) {
  415. recent_preset_cache.push_back(saved_recent_presets[i]);
  416. }
  417. }
  418. for (const Color &preset : recent_preset_cache) {
  419. recent_presets.push_back(preset);
  420. }
  421. _update_presets();
  422. _update_recent_presets();
  423. }
  424. #endif
  425. HSlider *ColorPicker::get_slider(int p_idx) {
  426. if (p_idx < SLIDER_COUNT) {
  427. return sliders[p_idx];
  428. }
  429. return alpha_slider;
  430. }
  431. Vector<float> ColorPicker::get_active_slider_values() {
  432. Vector<float> cur_values;
  433. for (int i = 0; i < current_slider_count; i++) {
  434. cur_values.push_back(sliders[i]->get_value());
  435. }
  436. cur_values.push_back(alpha_slider->get_value());
  437. return cur_values;
  438. }
  439. void ColorPicker::_copy_color_to_hsv() {
  440. ok_hsl_h = color.get_ok_hsl_h();
  441. ok_hsl_s = color.get_ok_hsl_s();
  442. ok_hsl_l = color.get_ok_hsl_l();
  443. h = color.get_h();
  444. s = color.get_s();
  445. v = color.get_v();
  446. }
  447. void ColorPicker::_copy_hsv_to_color() {
  448. if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) {
  449. color.set_ok_hsl(ok_hsl_h, ok_hsl_s, ok_hsl_l, color.a);
  450. } else {
  451. color.set_hsv(h, s, v, color.a);
  452. }
  453. }
  454. void ColorPicker::_select_from_preset_container(const Color &p_color) {
  455. if (preset_group->get_pressed_button()) {
  456. preset_group->get_pressed_button()->set_pressed(false);
  457. }
  458. for (int i = 1; i < preset_container->get_child_count(); i++) {
  459. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  460. if (current_btn && p_color == current_btn->get_preset_color()) {
  461. current_btn->set_pressed(true);
  462. break;
  463. }
  464. }
  465. }
  466. bool ColorPicker::_select_from_recent_preset_hbc(const Color &p_color) {
  467. for (int i = 0; i < recent_preset_hbc->get_child_count(); i++) {
  468. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
  469. if (current_btn && p_color == current_btn->get_preset_color()) {
  470. current_btn->set_pressed(true);
  471. return true;
  472. }
  473. }
  474. return false;
  475. }
  476. ColorPicker::PickerShapeType ColorPicker::_get_actual_shape() const {
  477. return modes[current_mode]->get_shape_override() != SHAPE_MAX ? modes[current_mode]->get_shape_override() : current_shape;
  478. }
  479. void ColorPicker::_reset_sliders_theme() {
  480. Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
  481. style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale);
  482. style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
  483. for (int i = 0; i < SLIDER_COUNT; i++) {
  484. sliders[i]->begin_bulk_theme_override();
  485. sliders[i]->add_theme_icon_override("grabber", theme_cache.bar_arrow);
  486. sliders[i]->add_theme_icon_override("grabber_highlight", theme_cache.bar_arrow);
  487. sliders[i]->add_theme_constant_override("grabber_offset", 8 * theme_cache.base_scale);
  488. if (!colorize_sliders) {
  489. sliders[i]->add_theme_style_override("slider", style_box_flat);
  490. }
  491. sliders[i]->end_bulk_theme_override();
  492. }
  493. alpha_slider->begin_bulk_theme_override();
  494. alpha_slider->add_theme_icon_override("grabber", theme_cache.bar_arrow);
  495. alpha_slider->add_theme_icon_override("grabber_highlight", theme_cache.bar_arrow);
  496. alpha_slider->add_theme_constant_override("grabber_offset", 8 * theme_cache.base_scale);
  497. if (!colorize_sliders) {
  498. alpha_slider->add_theme_style_override("slider", style_box_flat);
  499. }
  500. alpha_slider->end_bulk_theme_override();
  501. }
  502. void ColorPicker::_html_submitted(const String &p_html) {
  503. if (updating || text_is_constructor || !c_text->is_visible()) {
  504. return;
  505. }
  506. Color new_color = Color::from_string(p_html.strip_edges(), color);
  507. String html_no_prefix = p_html.strip_edges().trim_prefix("#");
  508. if (html_no_prefix.is_valid_hex_number(false)) {
  509. // Convert invalid HTML color codes that software like Figma supports.
  510. if (html_no_prefix.length() == 1) {
  511. // Turn `#1` into `#111111`.
  512. html_no_prefix = html_no_prefix.repeat(6);
  513. } else if (html_no_prefix.length() == 2) {
  514. // Turn `#12` into `#121212`.
  515. html_no_prefix = html_no_prefix.repeat(3);
  516. } else if (html_no_prefix.length() == 5) {
  517. // Turn `#12345` into `#11223344`.
  518. html_no_prefix = html_no_prefix.left(4);
  519. } else if (html_no_prefix.length() == 7) {
  520. // Turn `#1234567` into `#123456`.
  521. html_no_prefix = html_no_prefix.left(6);
  522. }
  523. }
  524. new_color = Color::from_string(html_no_prefix, new_color);
  525. if (!is_editing_alpha()) {
  526. new_color.a = color.a;
  527. }
  528. if (new_color.to_argb32() == color.to_argb32()) {
  529. return;
  530. }
  531. color = new_color;
  532. if (!is_inside_tree()) {
  533. return;
  534. }
  535. set_pick_color(color);
  536. emit_signal(SNAME("color_changed"), color);
  537. }
  538. void ColorPicker::_update_color(bool p_update_sliders) {
  539. updating = true;
  540. if (p_update_sliders) {
  541. float step = modes[current_mode]->get_slider_step();
  542. float spinbox_arrow_step = modes[current_mode]->get_spinbox_arrow_step();
  543. for (int i = 0; i < current_slider_count; i++) {
  544. sliders[i]->set_max(modes[current_mode]->get_slider_max(i));
  545. sliders[i]->set_step(step);
  546. values[i]->set_custom_arrow_step(spinbox_arrow_step);
  547. sliders[i]->set_value(modes[current_mode]->get_slider_value(i));
  548. }
  549. alpha_slider->set_max(modes[current_mode]->get_slider_max(current_slider_count));
  550. alpha_slider->set_step(step);
  551. alpha_slider->set_value(modes[current_mode]->get_slider_value(current_slider_count));
  552. }
  553. _update_text_value();
  554. sample->queue_redraw();
  555. uv_edit->queue_redraw();
  556. w_edit->queue_redraw();
  557. for (int i = 0; i < current_slider_count; i++) {
  558. sliders[i]->queue_redraw();
  559. }
  560. alpha_slider->queue_redraw();
  561. wheel->queue_redraw();
  562. wheel_uv->queue_redraw();
  563. updating = false;
  564. }
  565. void ColorPicker::_update_presets() {
  566. int preset_size = _get_preset_size();
  567. // Only update the preset button size if it has changed.
  568. if (preset_size != prev_preset_size) {
  569. prev_preset_size = preset_size;
  570. btn_add_preset->set_custom_minimum_size(Size2(preset_size, preset_size));
  571. for (int i = 1; i < preset_container->get_child_count(); i++) {
  572. ColorPresetButton *cpb = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  573. cpb->set_custom_minimum_size(Size2(preset_size, preset_size));
  574. }
  575. }
  576. #ifdef TOOLS_ENABLED
  577. if (editor_settings) {
  578. // Rebuild swatch color buttons, keeping the add-preset button in the first position.
  579. for (int i = 1; i < preset_container->get_child_count(); i++) {
  580. preset_container->get_child(i)->queue_free();
  581. }
  582. for (const Color &preset : preset_cache) {
  583. _add_preset_button(preset_size, preset);
  584. }
  585. _notification(NOTIFICATION_VISIBILITY_CHANGED);
  586. }
  587. #endif
  588. }
  589. void ColorPicker::_update_recent_presets() {
  590. #ifdef TOOLS_ENABLED
  591. if (editor_settings) {
  592. int recent_preset_count = recent_preset_hbc->get_child_count();
  593. for (int i = 0; i < recent_preset_count; i++) {
  594. memdelete(recent_preset_hbc->get_child(0));
  595. }
  596. recent_presets.clear();
  597. for (const Color &preset : recent_preset_cache) {
  598. recent_presets.push_back(preset);
  599. }
  600. int preset_size = _get_preset_size();
  601. for (const Color &preset : recent_presets) {
  602. _add_recent_preset_button(preset_size, preset);
  603. }
  604. _notification(NOTIFICATION_VISIBILITY_CHANGED);
  605. }
  606. #endif
  607. }
  608. void ColorPicker::_text_type_toggled() {
  609. text_is_constructor = !text_is_constructor;
  610. if (text_is_constructor) {
  611. text_type->set_text("");
  612. #ifdef TOOLS_ENABLED
  613. text_type->set_button_icon(get_editor_theme_icon(SNAME("Script")));
  614. #endif
  615. c_text->set_editable(false);
  616. c_text->set_tooltip_text(RTR("Copy this constructor in a script."));
  617. } else {
  618. text_type->set_text("#");
  619. text_type->set_button_icon(nullptr);
  620. c_text->set_editable(true);
  621. c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\")."));
  622. }
  623. _update_color();
  624. }
  625. Color ColorPicker::get_pick_color() const {
  626. return color;
  627. }
  628. Color ColorPicker::get_old_color() const {
  629. return old_color;
  630. }
  631. void ColorPicker::set_picker_shape(PickerShapeType p_shape) {
  632. ERR_FAIL_INDEX(p_shape, SHAPE_MAX);
  633. if (p_shape == current_shape) {
  634. return;
  635. }
  636. if (current_shape != SHAPE_NONE) {
  637. shape_popup->set_item_checked(current_shape, false);
  638. }
  639. if (p_shape != SHAPE_NONE) {
  640. shape_popup->set_item_checked(p_shape, true);
  641. btn_shape->set_button_icon(shape_popup->get_item_icon(p_shape));
  642. }
  643. current_shape = p_shape;
  644. #ifdef TOOLS_ENABLED
  645. if (editor_settings) {
  646. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "picker_shape", current_shape);
  647. }
  648. #endif
  649. _copy_color_to_hsv();
  650. _update_controls();
  651. _update_color();
  652. }
  653. ColorPicker::PickerShapeType ColorPicker::get_picker_shape() const {
  654. return current_shape;
  655. }
  656. inline int ColorPicker::_get_preset_size() {
  657. return (int(get_minimum_size().width) - (preset_container->get_h_separation() * (PRESET_COLUMN_COUNT - 1))) / PRESET_COLUMN_COUNT;
  658. }
  659. void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
  660. ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
  661. btn_preset_new->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color\nRMB: Remove preset")), p_color.to_html(p_color.a < 1)));
  662. SET_DRAG_FORWARDING_GCDU(btn_preset_new, ColorPicker);
  663. btn_preset_new->set_button_group(preset_group);
  664. preset_container->add_child(btn_preset_new);
  665. btn_preset_new->set_pressed(true);
  666. btn_preset_new->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_preset_input).bind(p_color));
  667. }
  668. void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) {
  669. ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
  670. btn_preset_new->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color")), p_color.to_html(p_color.a < 1)));
  671. btn_preset_new->set_button_group(recent_preset_group);
  672. recent_preset_hbc->add_child(btn_preset_new);
  673. recent_preset_hbc->move_child(btn_preset_new, 0);
  674. btn_preset_new->set_pressed(true);
  675. btn_preset_new->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_recent_preset_pressed).bind(btn_preset_new));
  676. }
  677. void ColorPicker::_show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container) {
  678. if (p_is_btn_pressed) {
  679. p_preset_container->show();
  680. } else {
  681. p_preset_container->hide();
  682. }
  683. _update_drop_down_arrow(p_is_btn_pressed, p_btn_preset);
  684. }
  685. void ColorPicker::_update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset) {
  686. if (p_is_btn_pressed) {
  687. p_btn_preset->set_button_icon(theme_cache.expanded_arrow);
  688. } else {
  689. p_btn_preset->set_button_icon(theme_cache.folded_arrow);
  690. }
  691. }
  692. void ColorPicker::_set_mode_popup_value(ColorModeType p_mode) {
  693. ERR_FAIL_INDEX(p_mode, MODE_MAX + 1);
  694. if (p_mode == MODE_MAX) {
  695. set_colorize_sliders(!colorize_sliders);
  696. } else {
  697. set_color_mode(p_mode);
  698. }
  699. }
  700. Variant ColorPicker::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
  701. ColorPresetButton *dragged_preset_button = Object::cast_to<ColorPresetButton>(p_from_control);
  702. if (!dragged_preset_button) {
  703. return Variant();
  704. }
  705. ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size()));
  706. set_drag_preview(drag_preview);
  707. Dictionary drag_data;
  708. drag_data["type"] = "color_preset";
  709. drag_data["color_preset"] = dragged_preset_button->get_index();
  710. return drag_data;
  711. }
  712. bool ColorPicker::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
  713. Dictionary d = p_data;
  714. if (!d.has("type") || String(d["type"]) != "color_preset") {
  715. return false;
  716. }
  717. return true;
  718. }
  719. void ColorPicker::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
  720. Dictionary d = p_data;
  721. if (!d.has("type")) {
  722. return;
  723. }
  724. if (String(d["type"]) == "color_preset") {
  725. int preset_from_id = d["color_preset"];
  726. int hover_now = p_from_control->get_index();
  727. if (preset_from_id == hover_now || hover_now == -1) {
  728. return;
  729. }
  730. preset_container->move_child(preset_container->get_child(preset_from_id), hover_now);
  731. }
  732. }
  733. void ColorPicker::add_preset(const Color &p_color) {
  734. List<Color>::Element *e = presets.find(p_color);
  735. if (e) {
  736. presets.move_to_back(e);
  737. preset_cache.move_to_back(preset_cache.find(p_color));
  738. preset_container->move_child(preset_group->get_pressed_button(), preset_container->get_child_count() - 1);
  739. } else {
  740. presets.push_back(p_color);
  741. preset_cache.push_back(p_color);
  742. _add_preset_button(_get_preset_size(), p_color);
  743. }
  744. #ifdef TOOLS_ENABLED
  745. if (editor_settings) {
  746. PackedColorArray arr_to_save = get_presets();
  747. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "presets", arr_to_save);
  748. }
  749. #endif
  750. }
  751. void ColorPicker::add_recent_preset(const Color &p_color) {
  752. if (!_select_from_recent_preset_hbc(p_color)) {
  753. if (recent_preset_hbc->get_child_count() >= PRESET_COLUMN_COUNT) {
  754. recent_preset_cache.pop_front();
  755. recent_presets.pop_front();
  756. recent_preset_hbc->get_child(PRESET_COLUMN_COUNT - 1)->queue_free();
  757. }
  758. recent_presets.push_back(p_color);
  759. recent_preset_cache.push_back(p_color);
  760. _add_recent_preset_button(_get_preset_size(), p_color);
  761. }
  762. _select_from_preset_container(p_color);
  763. #ifdef TOOLS_ENABLED
  764. if (editor_settings) {
  765. PackedColorArray arr_to_save = get_recent_presets();
  766. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
  767. }
  768. #endif
  769. }
  770. void ColorPicker::erase_preset(const Color &p_color) {
  771. List<Color>::Element *e = presets.find(p_color);
  772. if (e) {
  773. presets.erase(e);
  774. preset_cache.erase(preset_cache.find(p_color));
  775. // Find preset button to remove.
  776. for (int i = 1; i < preset_container->get_child_count(); i++) {
  777. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  778. if (current_btn && p_color == current_btn->get_preset_color()) {
  779. current_btn->queue_free();
  780. break;
  781. }
  782. }
  783. #ifdef TOOLS_ENABLED
  784. if (editor_settings) {
  785. PackedColorArray arr_to_save = get_presets();
  786. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "presets", arr_to_save);
  787. }
  788. #endif
  789. }
  790. }
  791. void ColorPicker::erase_recent_preset(const Color &p_color) {
  792. List<Color>::Element *e = recent_presets.find(p_color);
  793. if (e) {
  794. recent_presets.erase(e);
  795. recent_preset_cache.erase(recent_preset_cache.find(p_color));
  796. // Find recent preset button to remove.
  797. for (int i = 1; i < recent_preset_hbc->get_child_count(); i++) {
  798. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
  799. if (current_btn && p_color == current_btn->get_preset_color()) {
  800. current_btn->queue_free();
  801. break;
  802. }
  803. }
  804. #ifdef TOOLS_ENABLED
  805. if (editor_settings) {
  806. PackedColorArray arr_to_save = get_recent_presets();
  807. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
  808. }
  809. #endif
  810. }
  811. }
  812. PackedColorArray ColorPicker::get_presets() const {
  813. PackedColorArray arr;
  814. arr.resize(presets.size());
  815. int i = 0;
  816. for (List<Color>::ConstIterator itr = presets.begin(); itr != presets.end(); ++itr, ++i) {
  817. arr.set(i, *itr);
  818. }
  819. return arr;
  820. }
  821. PackedColorArray ColorPicker::get_recent_presets() const {
  822. PackedColorArray arr;
  823. arr.resize(recent_presets.size());
  824. int i = 0;
  825. for (List<Color>::ConstIterator itr = recent_presets.begin(); itr != recent_presets.end(); ++itr, ++i) {
  826. arr.set(i, *itr);
  827. }
  828. return arr;
  829. }
  830. void ColorPicker::set_color_mode(ColorModeType p_mode) {
  831. ERR_FAIL_INDEX(p_mode, MODE_MAX);
  832. if (current_mode == p_mode) {
  833. return;
  834. }
  835. if (slider_theme_modified) {
  836. _reset_sliders_theme();
  837. }
  838. mode_popup->set_item_checked(current_mode, false);
  839. mode_popup->set_item_checked(p_mode, true);
  840. if (p_mode < MODE_BUTTON_COUNT) {
  841. mode_btns[p_mode]->set_pressed(true);
  842. } else if (current_mode < MODE_BUTTON_COUNT) {
  843. mode_btns[current_mode]->set_pressed(false);
  844. }
  845. current_mode = p_mode;
  846. #ifdef TOOLS_ENABLED
  847. if (editor_settings) {
  848. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "color_mode", current_mode);
  849. }
  850. #endif
  851. if (!is_inside_tree()) {
  852. return;
  853. }
  854. _update_controls();
  855. _update_color();
  856. }
  857. ColorPicker::ColorModeType ColorPicker::get_color_mode() const {
  858. return current_mode;
  859. }
  860. void ColorPicker::set_colorize_sliders(bool p_colorize_sliders) {
  861. if (colorize_sliders == p_colorize_sliders) {
  862. return;
  863. }
  864. colorize_sliders = p_colorize_sliders;
  865. mode_popup->set_item_checked(MODE_MAX + 1, colorize_sliders);
  866. if (colorize_sliders) {
  867. Ref<StyleBoxEmpty> style_box_empty(memnew(StyleBoxEmpty));
  868. if (!slider_theme_modified) {
  869. for (int i = 0; i < SLIDER_COUNT; i++) {
  870. sliders[i]->add_theme_style_override("slider", style_box_empty);
  871. }
  872. }
  873. alpha_slider->add_theme_style_override("slider", style_box_empty);
  874. } else {
  875. Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
  876. style_box_flat->set_content_margin(SIDE_TOP, 16 * theme_cache.base_scale);
  877. style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
  878. if (!slider_theme_modified) {
  879. for (int i = 0; i < SLIDER_COUNT; i++) {
  880. sliders[i]->add_theme_style_override("slider", style_box_flat);
  881. }
  882. }
  883. alpha_slider->add_theme_style_override("slider", style_box_flat);
  884. }
  885. }
  886. bool ColorPicker::is_colorizing_sliders() const {
  887. return colorize_sliders;
  888. }
  889. void ColorPicker::set_deferred_mode(bool p_enabled) {
  890. deferred_mode_enabled = p_enabled;
  891. }
  892. bool ColorPicker::is_deferred_mode() const {
  893. return deferred_mode_enabled;
  894. }
  895. void ColorPicker::_update_text_value() {
  896. bool text_visible = true;
  897. if (text_is_constructor) {
  898. String t = "Color(" + String::num(color.r, 3) + ", " + String::num(color.g, 3) + ", " + String::num(color.b, 3);
  899. if (edit_alpha && color.a < 1) {
  900. t += ", " + String::num(color.a, 3) + ")";
  901. } else {
  902. t += ")";
  903. }
  904. c_text->set_text(t);
  905. }
  906. if (color.r > 1 || color.g > 1 || color.b > 1 || color.r < 0 || color.g < 0 || color.b < 0) {
  907. text_visible = false;
  908. } else if (!text_is_constructor) {
  909. c_text->set_text(color.to_html(edit_alpha && color.a < 1));
  910. }
  911. text_type->set_visible(text_visible);
  912. c_text->set_visible(text_visible);
  913. }
  914. void ColorPicker::_sample_input(const Ref<InputEvent> &p_event) {
  915. const Ref<InputEventMouseButton> mb = p_event;
  916. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  917. const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  918. if (rect_old.has_point(mb->get_position())) {
  919. // Revert to the old color when left-clicking the old color sample.
  920. set_pick_color(old_color);
  921. emit_signal(SNAME("color_changed"), color);
  922. }
  923. }
  924. }
  925. void ColorPicker::_sample_draw() {
  926. // Covers the right half of the sample if the old color is being displayed,
  927. // or the whole sample if it's not being displayed.
  928. Rect2 rect_new;
  929. if (display_old_color) {
  930. rect_new = Rect2(Point2(sample->get_size().width * 0.5, 0), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  931. // Draw both old and new colors for easier comparison (only if spawned from a ColorPickerButton).
  932. const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  933. if (old_color.a < 1.0) {
  934. sample->draw_texture_rect(theme_cache.sample_bg, rect_old, true);
  935. }
  936. sample->draw_rect(rect_old, old_color);
  937. if (!old_color.is_equal_approx(color)) {
  938. // Draw a revert indicator to indicate that the old sample can be clicked to revert to this old color.
  939. // Adapt icon color to the background color (taking alpha checkerboard into account) so that it's always visible.
  940. sample->draw_texture(theme_cache.sample_revert,
  941. rect_old.size * 0.5 - theme_cache.sample_revert->get_size() * 0.5,
  942. Math::lerp(0.75f, old_color.get_luminance(), old_color.a) < 0.455 ? Color(1, 1, 1) : (Color(0.01, 0.01, 0.01)));
  943. }
  944. if (old_color.r > 1 || old_color.g > 1 || old_color.b > 1) {
  945. // Draw an indicator to denote that the old color is "overbright" and can't be displayed accurately in the preview.
  946. sample->draw_texture(theme_cache.overbright_indicator, Point2());
  947. }
  948. } else {
  949. rect_new = Rect2(Point2(), Size2(sample->get_size().width, sample->get_size().height * 0.95));
  950. }
  951. if (color.a < 1.0) {
  952. sample->draw_texture_rect(theme_cache.sample_bg, rect_new, true);
  953. }
  954. sample->draw_rect(rect_new, color);
  955. if (color.r > 1 || color.g > 1 || color.b > 1) {
  956. // Draw an indicator to denote that the new color is "overbright" and can't be displayed accurately in the preview.
  957. sample->draw_texture(theme_cache.overbright_indicator, Point2(uv_edit->get_size().width * 0.5, 0));
  958. }
  959. }
  960. void ColorPicker::_hsv_draw(int p_which, Control *c) {
  961. if (!c) {
  962. return;
  963. }
  964. PickerShapeType actual_shape = _get_actual_shape();
  965. if (p_which == 0) {
  966. Vector<Point2> points;
  967. Vector<Color> colors;
  968. Vector<Color> colors2;
  969. Color col = color;
  970. Vector2 center = c->get_size() / 2.0;
  971. switch (actual_shape) {
  972. case SHAPE_HSV_WHEEL: {
  973. points.resize(4);
  974. colors.resize(4);
  975. colors2.resize(4);
  976. real_t ring_radius_x = Math_SQRT12 * c->get_size().width * 0.42;
  977. real_t ring_radius_y = Math_SQRT12 * c->get_size().height * 0.42;
  978. points.set(0, center - Vector2(ring_radius_x, ring_radius_y));
  979. points.set(1, center + Vector2(ring_radius_x, -ring_radius_y));
  980. points.set(2, center + Vector2(ring_radius_x, ring_radius_y));
  981. points.set(3, center + Vector2(-ring_radius_x, ring_radius_y));
  982. colors.set(0, Color(1, 1, 1, 1));
  983. colors.set(1, Color(1, 1, 1, 1));
  984. colors.set(2, Color(0, 0, 0, 1));
  985. colors.set(3, Color(0, 0, 0, 1));
  986. c->draw_polygon(points, colors);
  987. col.set_hsv(h, 1, 1);
  988. col.a = 0;
  989. colors2.set(0, col);
  990. col.a = 1;
  991. colors2.set(1, col);
  992. col.set_hsv(h, 1, 0);
  993. colors2.set(2, col);
  994. col.a = 0;
  995. colors2.set(3, col);
  996. c->draw_polygon(points, colors2);
  997. break;
  998. }
  999. case SHAPE_HSV_RECTANGLE: {
  1000. points.resize(4);
  1001. colors.resize(4);
  1002. colors2.resize(4);
  1003. points.set(0, Vector2());
  1004. points.set(1, Vector2(c->get_size().x, 0));
  1005. points.set(2, c->get_size());
  1006. points.set(3, Vector2(0, c->get_size().y));
  1007. colors.set(0, Color(1, 1, 1, 1));
  1008. colors.set(1, Color(1, 1, 1, 1));
  1009. colors.set(2, Color(0, 0, 0, 1));
  1010. colors.set(3, Color(0, 0, 0, 1));
  1011. c->draw_polygon(points, colors);
  1012. col = color;
  1013. col.set_hsv(h, 1, 1);
  1014. col.a = 0;
  1015. colors2.set(0, col);
  1016. col.a = 1;
  1017. colors2.set(1, col);
  1018. col.set_hsv(h, 1, 0);
  1019. colors2.set(2, col);
  1020. col.a = 0;
  1021. colors2.set(3, col);
  1022. c->draw_polygon(points, colors2);
  1023. break;
  1024. }
  1025. default: {
  1026. }
  1027. }
  1028. int x;
  1029. int y;
  1030. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1031. x = center.x + (center.x * Math::cos((actual_shape == SHAPE_OKHSL_CIRCLE ? ok_hsl_h : h) * Math_TAU) * s) - (theme_cache.picker_cursor->get_width() / 2);
  1032. y = center.y + (center.y * Math::sin((actual_shape == SHAPE_OKHSL_CIRCLE ? ok_hsl_h : h) * Math_TAU) * s) - (theme_cache.picker_cursor->get_height() / 2);
  1033. } else {
  1034. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  1035. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  1036. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  1037. x = CLAMP(real_size.x * s, 0, real_size.x) + corner_x - (theme_cache.picker_cursor->get_width() / 2);
  1038. y = CLAMP(real_size.y - real_size.y * v, 0, real_size.y) + corner_y - (theme_cache.picker_cursor->get_height() / 2);
  1039. }
  1040. c->draw_texture(theme_cache.picker_cursor, Point2(x, y));
  1041. col.set_hsv(h, 1, 1);
  1042. if (actual_shape == SHAPE_HSV_WHEEL) {
  1043. points.resize(4);
  1044. double h1 = h - (0.5 / 360);
  1045. double h2 = h + (0.5 / 360);
  1046. points.set(0, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU)), center.y + (center.y * Math::sin(h1 * Math_TAU))));
  1047. points.set(1, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h1 * Math_TAU) * 0.84)));
  1048. points.set(2, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU)), center.y + (center.y * Math::sin(h2 * Math_TAU))));
  1049. points.set(3, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h2 * Math_TAU) * 0.84)));
  1050. c->draw_multiline(points, col.inverted());
  1051. }
  1052. } else if (p_which == 1) {
  1053. if (actual_shape == SHAPE_HSV_RECTANGLE) {
  1054. c->draw_set_transform(Point2(), -Math_PI / 2, Size2(c->get_size().x, -c->get_size().y));
  1055. c->draw_texture_rect(theme_cache.color_hue, Rect2(Point2(), Size2(1, 1)));
  1056. c->draw_set_transform(Point2(), 0, Size2(1, 1));
  1057. int y = c->get_size().y - c->get_size().y * (1.0 - h);
  1058. Color col;
  1059. col.set_hsv(h, 1, 1);
  1060. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  1061. } else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
  1062. Vector<Point2> points;
  1063. Vector<Color> colors;
  1064. Color col;
  1065. col.set_ok_hsl(ok_hsl_h, ok_hsl_s, 1);
  1066. Color col2;
  1067. col2.set_ok_hsl(ok_hsl_h, ok_hsl_s, 0.5);
  1068. Color col3;
  1069. col3.set_ok_hsl(ok_hsl_h, ok_hsl_s, 0);
  1070. points.resize(6);
  1071. colors.resize(6);
  1072. points.set(0, Vector2(c->get_size().x, 0));
  1073. points.set(1, Vector2(c->get_size().x, c->get_size().y * 0.5));
  1074. points.set(2, c->get_size());
  1075. points.set(3, Vector2(0, c->get_size().y));
  1076. points.set(4, Vector2(0, c->get_size().y * 0.5));
  1077. points.set(5, Vector2());
  1078. colors.set(0, col);
  1079. colors.set(1, col2);
  1080. colors.set(2, col3);
  1081. colors.set(3, col3);
  1082. colors.set(4, col2);
  1083. colors.set(5, col);
  1084. c->draw_polygon(points, colors);
  1085. int y = c->get_size().y - c->get_size().y * CLAMP(ok_hsl_l, 0, 1);
  1086. col.set_ok_hsl(ok_hsl_h, 1, ok_hsl_l);
  1087. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  1088. } else if (actual_shape == SHAPE_VHS_CIRCLE) {
  1089. Vector<Point2> points;
  1090. Vector<Color> colors;
  1091. Color col;
  1092. col.set_hsv(h, s, 1);
  1093. points.resize(4);
  1094. colors.resize(4);
  1095. points.set(0, Vector2());
  1096. points.set(1, Vector2(c->get_size().x, 0));
  1097. points.set(2, c->get_size());
  1098. points.set(3, Vector2(0, c->get_size().y));
  1099. colors.set(0, col);
  1100. colors.set(1, col);
  1101. colors.set(2, Color(0, 0, 0));
  1102. colors.set(3, Color(0, 0, 0));
  1103. c->draw_polygon(points, colors);
  1104. int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1);
  1105. col.set_hsv(h, 1, v);
  1106. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  1107. }
  1108. } else if (p_which == 2) {
  1109. c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
  1110. if (actual_shape == SHAPE_VHS_CIRCLE) {
  1111. circle_mat->set_shader_parameter("v", v);
  1112. } else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
  1113. circle_mat->set_shader_parameter("ok_hsl_l", ok_hsl_l);
  1114. }
  1115. }
  1116. }
  1117. void ColorPicker::_slider_draw(int p_which) {
  1118. if (colorize_sliders) {
  1119. modes[current_mode]->slider_draw(p_which);
  1120. }
  1121. }
  1122. void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
  1123. Ref<InputEventMouseButton> bev = p_event;
  1124. PickerShapeType actual_shape = _get_actual_shape();
  1125. if (bev.is_valid()) {
  1126. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1127. Vector2 center = c->get_size() / 2.0;
  1128. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1129. real_t dist = center.distance_to(bev->get_position());
  1130. if (dist <= center.x) {
  1131. real_t rad = center.angle_to_point(bev->get_position());
  1132. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1133. s = CLAMP(dist / center.x, 0, 1);
  1134. ok_hsl_h = h;
  1135. ok_hsl_s = s;
  1136. } else {
  1137. return;
  1138. }
  1139. } else {
  1140. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  1141. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  1142. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  1143. if (bev->get_position().x < corner_x || bev->get_position().x > c->get_size().x - corner_x ||
  1144. bev->get_position().y < corner_y || bev->get_position().y > c->get_size().y - corner_y) {
  1145. {
  1146. real_t dist = center.distance_to(bev->get_position());
  1147. if (dist >= center.x * 0.84 && dist <= center.x) {
  1148. real_t rad = center.angle_to_point(bev->get_position());
  1149. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1150. spinning = true;
  1151. } else {
  1152. return;
  1153. }
  1154. }
  1155. }
  1156. if (!spinning) {
  1157. real_t x = CLAMP(bev->get_position().x - corner_x, 0, real_size.x);
  1158. real_t y = CLAMP(bev->get_position().y - corner_y, 0, real_size.y);
  1159. s = x / real_size.x;
  1160. v = 1.0 - y / real_size.y;
  1161. }
  1162. }
  1163. changing_color = true;
  1164. _copy_hsv_to_color();
  1165. last_color = color;
  1166. set_pick_color(color);
  1167. if (!deferred_mode_enabled) {
  1168. emit_signal(SNAME("color_changed"), color);
  1169. }
  1170. } else if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1171. if (deferred_mode_enabled) {
  1172. emit_signal(SNAME("color_changed"), color);
  1173. }
  1174. add_recent_preset(color);
  1175. changing_color = false;
  1176. spinning = false;
  1177. } else {
  1178. changing_color = false;
  1179. spinning = false;
  1180. }
  1181. }
  1182. Ref<InputEventMouseMotion> mev = p_event;
  1183. if (mev.is_valid()) {
  1184. if (!changing_color) {
  1185. return;
  1186. }
  1187. Vector2 center = c->get_size() / 2.0;
  1188. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1189. real_t dist = center.distance_to(mev->get_position());
  1190. real_t rad = center.angle_to_point(mev->get_position());
  1191. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1192. s = CLAMP(dist / center.x, 0, 1);
  1193. ok_hsl_h = h;
  1194. ok_hsl_s = s;
  1195. } else {
  1196. if (spinning) {
  1197. real_t rad = center.angle_to_point(mev->get_position());
  1198. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1199. } else {
  1200. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  1201. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  1202. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  1203. real_t x = CLAMP(mev->get_position().x - corner_x, 0, real_size.x);
  1204. real_t y = CLAMP(mev->get_position().y - corner_y, 0, real_size.y);
  1205. s = x / real_size.x;
  1206. v = 1.0 - y / real_size.y;
  1207. }
  1208. }
  1209. _copy_hsv_to_color();
  1210. last_color = color;
  1211. set_pick_color(color);
  1212. if (!deferred_mode_enabled) {
  1213. emit_signal(SNAME("color_changed"), color);
  1214. }
  1215. }
  1216. }
  1217. void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
  1218. Ref<InputEventMouseButton> bev = p_event;
  1219. PickerShapeType actual_shape = _get_actual_shape();
  1220. if (bev.is_valid()) {
  1221. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1222. changing_color = true;
  1223. float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
  1224. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1225. v = 1.0 - (y / w_edit->get_size().height);
  1226. ok_hsl_l = v;
  1227. } else {
  1228. h = y / w_edit->get_size().height;
  1229. }
  1230. } else {
  1231. changing_color = false;
  1232. }
  1233. _copy_hsv_to_color();
  1234. last_color = color;
  1235. set_pick_color(color);
  1236. if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1237. add_recent_preset(color);
  1238. emit_signal(SNAME("color_changed"), color);
  1239. } else if (!deferred_mode_enabled) {
  1240. emit_signal(SNAME("color_changed"), color);
  1241. }
  1242. }
  1243. Ref<InputEventMouseMotion> mev = p_event;
  1244. if (mev.is_valid()) {
  1245. if (!changing_color) {
  1246. return;
  1247. }
  1248. float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
  1249. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1250. v = 1.0 - (y / w_edit->get_size().height);
  1251. ok_hsl_l = v;
  1252. } else {
  1253. h = y / w_edit->get_size().height;
  1254. }
  1255. _copy_hsv_to_color();
  1256. last_color = color;
  1257. set_pick_color(color);
  1258. if (!deferred_mode_enabled) {
  1259. emit_signal(SNAME("color_changed"), color);
  1260. }
  1261. }
  1262. }
  1263. void ColorPicker::_slider_or_spin_input(const Ref<InputEvent> &p_event) {
  1264. if (line_edit_mouse_release) {
  1265. line_edit_mouse_release = false;
  1266. return;
  1267. }
  1268. Ref<InputEventMouseButton> bev = p_event;
  1269. if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1270. add_recent_preset(color);
  1271. }
  1272. }
  1273. void ColorPicker::_line_edit_input(const Ref<InputEvent> &p_event) {
  1274. Ref<InputEventMouseButton> bev = p_event;
  1275. if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1276. line_edit_mouse_release = true;
  1277. }
  1278. }
  1279. void ColorPicker::_preset_input(const Ref<InputEvent> &p_event, const Color &p_color) {
  1280. Ref<InputEventMouseButton> bev = p_event;
  1281. if (bev.is_valid()) {
  1282. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1283. set_pick_color(p_color);
  1284. add_recent_preset(color);
  1285. emit_signal(SNAME("color_changed"), p_color);
  1286. } else if (bev->is_pressed() && bev->get_button_index() == MouseButton::RIGHT && can_add_swatches) {
  1287. erase_preset(p_color);
  1288. emit_signal(SNAME("preset_removed"), p_color);
  1289. }
  1290. }
  1291. }
  1292. void ColorPicker::_recent_preset_pressed(const bool p_pressed, ColorPresetButton *p_preset) {
  1293. if (!p_pressed) {
  1294. return;
  1295. }
  1296. set_pick_color(p_preset->get_preset_color());
  1297. recent_presets.move_to_back(recent_presets.find(p_preset->get_preset_color()));
  1298. List<Color>::Element *e = recent_preset_cache.find(p_preset->get_preset_color());
  1299. if (e) {
  1300. recent_preset_cache.move_to_back(e);
  1301. }
  1302. recent_preset_hbc->move_child(p_preset, 0);
  1303. emit_signal(SNAME("color_changed"), p_preset->get_preset_color());
  1304. }
  1305. void ColorPicker::_text_changed(const String &) {
  1306. text_changed = true;
  1307. }
  1308. void ColorPicker::_add_preset_pressed() {
  1309. add_preset(color);
  1310. emit_signal(SNAME("preset_added"), color);
  1311. }
  1312. void ColorPicker::_pick_button_pressed() {
  1313. is_picking_color = true;
  1314. set_process_internal(true);
  1315. if (!picker_window) {
  1316. picker_window = memnew(Popup);
  1317. picker_window->set_size(Vector2i(1, 1));
  1318. picker_window->connect(SceneStringName(visibility_changed), callable_mp(this, &ColorPicker::_pick_finished));
  1319. add_child(picker_window, false, INTERNAL_MODE_FRONT);
  1320. }
  1321. picker_window->popup();
  1322. }
  1323. void ColorPicker::_pick_finished() {
  1324. if (picker_window->is_visible()) {
  1325. return;
  1326. }
  1327. if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
  1328. set_pick_color(old_color);
  1329. } else {
  1330. emit_signal(SNAME("color_changed"), color);
  1331. }
  1332. is_picking_color = false;
  1333. set_process_internal(false);
  1334. picker_window->hide();
  1335. }
  1336. void ColorPicker::_pick_button_pressed_legacy() {
  1337. if (!is_inside_tree()) {
  1338. return;
  1339. }
  1340. if (!picker_window) {
  1341. picker_window = memnew(Popup);
  1342. picker_window->hide();
  1343. picker_window->set_transient(true);
  1344. add_child(picker_window, false, INTERNAL_MODE_FRONT);
  1345. picker_texture_rect = memnew(TextureRect);
  1346. picker_texture_rect->set_anchors_preset(Control::PRESET_FULL_RECT);
  1347. picker_texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1348. picker_window->add_child(picker_texture_rect);
  1349. picker_texture_rect->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1350. picker_texture_rect->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_picker_texture_input));
  1351. picker_preview_label = memnew(Label);
  1352. picker_preview_label->set_anchors_preset(Control::PRESET_CENTER_TOP);
  1353. picker_preview_label->set_text(ETR("Color Picking active"));
  1354. picker_preview_style_box.instantiate();
  1355. picker_preview_style_box->set_bg_color(Color(1.0, 1.0, 1.0));
  1356. picker_preview_style_box->set_content_margin_all(4.0);
  1357. picker_preview_label->add_theme_style_override(CoreStringName(normal), picker_preview_style_box);
  1358. picker_window->add_child(picker_preview_label);
  1359. }
  1360. Rect2i screen_rect;
  1361. if (picker_window->is_embedded()) {
  1362. screen_rect = picker_window->get_embedder()->get_visible_rect();
  1363. picker_window->set_position(Point2i());
  1364. picker_texture_rect->set_texture(ImageTexture::create_from_image(picker_window->get_embedder()->get_texture()->get_image()));
  1365. } else {
  1366. screen_rect = picker_window->get_parent_rect();
  1367. picker_window->set_position(screen_rect.position);
  1368. Ref<Image> target_image = Image::create_empty(screen_rect.size.x, screen_rect.size.y, false, Image::FORMAT_RGB8);
  1369. DisplayServer *ds = DisplayServer::get_singleton();
  1370. // Add the Texture of each Window to the Image.
  1371. Vector<DisplayServer::WindowID> wl = ds->get_window_list();
  1372. // FIXME: sort windows by visibility.
  1373. for (const DisplayServer::WindowID &window_id : wl) {
  1374. Window *w = Window::get_from_id(window_id);
  1375. if (!w) {
  1376. continue;
  1377. }
  1378. Ref<Image> img = w->get_texture()->get_image();
  1379. if (!img.is_valid() || img->is_empty()) {
  1380. continue;
  1381. }
  1382. img->convert(Image::FORMAT_RGB8);
  1383. target_image->blit_rect(img, Rect2i(Point2i(0, 0), img->get_size()), w->get_position());
  1384. }
  1385. picker_texture_rect->set_texture(ImageTexture::create_from_image(target_image));
  1386. }
  1387. picker_window->set_size(screen_rect.size);
  1388. picker_preview_label->set_custom_minimum_size(screen_rect.size / 10); // 10% of size in each axis.
  1389. picker_window->popup();
  1390. }
  1391. void ColorPicker::_picker_texture_input(const Ref<InputEvent> &p_event) {
  1392. if (!is_inside_tree()) {
  1393. return;
  1394. }
  1395. Ref<InputEventMouseButton> bev = p_event;
  1396. if (bev.is_valid() && bev->get_button_index() == MouseButton::LEFT && !bev->is_pressed()) {
  1397. set_pick_color(picker_color);
  1398. emit_signal(SNAME("color_changed"), color);
  1399. picker_window->hide();
  1400. }
  1401. Ref<InputEventMouseMotion> mev = p_event;
  1402. if (mev.is_valid()) {
  1403. Ref<Image> img = picker_texture_rect->get_texture()->get_image();
  1404. if (img.is_valid() && !img->is_empty()) {
  1405. Vector2 ofs = mev->get_position();
  1406. picker_color = img->get_pixel(ofs.x, ofs.y);
  1407. picker_preview_style_box->set_bg_color(picker_color);
  1408. picker_preview_label->add_theme_color_override(SceneStringName(font_color), picker_color.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f));
  1409. }
  1410. }
  1411. }
  1412. void ColorPicker::_html_focus_exit() {
  1413. if (c_text->is_menu_visible()) {
  1414. return;
  1415. }
  1416. if (is_visible_in_tree()) {
  1417. _html_submitted(c_text->get_text());
  1418. } else {
  1419. _update_text_value();
  1420. }
  1421. }
  1422. void ColorPicker::set_can_add_swatches(bool p_enabled) {
  1423. if (can_add_swatches == p_enabled) {
  1424. return;
  1425. }
  1426. can_add_swatches = p_enabled;
  1427. if (!p_enabled) {
  1428. btn_add_preset->set_disabled(true);
  1429. btn_add_preset->set_focus_mode(FOCUS_NONE);
  1430. } else {
  1431. btn_add_preset->set_disabled(false);
  1432. btn_add_preset->set_focus_mode(FOCUS_ALL);
  1433. }
  1434. }
  1435. bool ColorPicker::are_swatches_enabled() const {
  1436. return can_add_swatches;
  1437. }
  1438. void ColorPicker::set_presets_visible(bool p_visible) {
  1439. if (presets_visible == p_visible) {
  1440. return;
  1441. }
  1442. presets_visible = p_visible;
  1443. btn_preset->set_visible(p_visible);
  1444. btn_recent_preset->set_visible(p_visible);
  1445. }
  1446. bool ColorPicker::are_presets_visible() const {
  1447. return presets_visible;
  1448. }
  1449. void ColorPicker::set_modes_visible(bool p_visible) {
  1450. if (color_modes_visible == p_visible) {
  1451. return;
  1452. }
  1453. color_modes_visible = p_visible;
  1454. mode_hbc->set_visible(p_visible);
  1455. }
  1456. bool ColorPicker::are_modes_visible() const {
  1457. return color_modes_visible;
  1458. }
  1459. void ColorPicker::set_sampler_visible(bool p_visible) {
  1460. if (sampler_visible == p_visible) {
  1461. return;
  1462. }
  1463. sampler_visible = p_visible;
  1464. sample_hbc->set_visible(p_visible);
  1465. }
  1466. bool ColorPicker::is_sampler_visible() const {
  1467. return sampler_visible;
  1468. }
  1469. void ColorPicker::set_sliders_visible(bool p_visible) {
  1470. if (sliders_visible == p_visible) {
  1471. return;
  1472. }
  1473. sliders_visible = p_visible;
  1474. slider_gc->set_visible(p_visible);
  1475. }
  1476. bool ColorPicker::are_sliders_visible() const {
  1477. return sliders_visible;
  1478. }
  1479. void ColorPicker::set_hex_visible(bool p_visible) {
  1480. if (hex_visible == p_visible) {
  1481. return;
  1482. }
  1483. hex_visible = p_visible;
  1484. hex_hbc->set_visible(p_visible);
  1485. }
  1486. bool ColorPicker::is_hex_visible() const {
  1487. return hex_visible;
  1488. }
  1489. void ColorPicker::_bind_methods() {
  1490. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
  1491. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
  1492. ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
  1493. ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
  1494. ClassDB::bind_method(D_METHOD("set_color_mode", "color_mode"), &ColorPicker::set_color_mode);
  1495. ClassDB::bind_method(D_METHOD("get_color_mode"), &ColorPicker::get_color_mode);
  1496. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
  1497. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
  1498. ClassDB::bind_method(D_METHOD("set_can_add_swatches", "enabled"), &ColorPicker::set_can_add_swatches);
  1499. ClassDB::bind_method(D_METHOD("are_swatches_enabled"), &ColorPicker::are_swatches_enabled);
  1500. ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible);
  1501. ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible);
  1502. ClassDB::bind_method(D_METHOD("set_modes_visible", "visible"), &ColorPicker::set_modes_visible);
  1503. ClassDB::bind_method(D_METHOD("are_modes_visible"), &ColorPicker::are_modes_visible);
  1504. ClassDB::bind_method(D_METHOD("set_sampler_visible", "visible"), &ColorPicker::set_sampler_visible);
  1505. ClassDB::bind_method(D_METHOD("is_sampler_visible"), &ColorPicker::is_sampler_visible);
  1506. ClassDB::bind_method(D_METHOD("set_sliders_visible", "visible"), &ColorPicker::set_sliders_visible);
  1507. ClassDB::bind_method(D_METHOD("are_sliders_visible"), &ColorPicker::are_sliders_visible);
  1508. ClassDB::bind_method(D_METHOD("set_hex_visible", "visible"), &ColorPicker::set_hex_visible);
  1509. ClassDB::bind_method(D_METHOD("is_hex_visible"), &ColorPicker::is_hex_visible);
  1510. ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
  1511. ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
  1512. ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
  1513. ClassDB::bind_method(D_METHOD("add_recent_preset", "color"), &ColorPicker::add_recent_preset);
  1514. ClassDB::bind_method(D_METHOD("erase_recent_preset", "color"), &ColorPicker::erase_recent_preset);
  1515. ClassDB::bind_method(D_METHOD("get_recent_presets"), &ColorPicker::get_recent_presets);
  1516. ClassDB::bind_method(D_METHOD("set_picker_shape", "shape"), &ColorPicker::set_picker_shape);
  1517. ClassDB::bind_method(D_METHOD("get_picker_shape"), &ColorPicker::get_picker_shape);
  1518. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  1519. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  1520. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_mode", PROPERTY_HINT_ENUM, "RGB,HSV,RAW,OKHSL"), "set_color_mode", "get_color_mode");
  1521. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
  1522. ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle,None"), "set_picker_shape", "get_picker_shape");
  1523. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_add_swatches"), "set_can_add_swatches", "are_swatches_enabled");
  1524. ADD_GROUP("Customization", "");
  1525. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sampler_visible"), "set_sampler_visible", "is_sampler_visible");
  1526. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "color_modes_visible"), "set_modes_visible", "are_modes_visible");
  1527. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sliders_visible"), "set_sliders_visible", "are_sliders_visible");
  1528. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hex_visible"), "set_hex_visible", "is_hex_visible");
  1529. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
  1530. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  1531. ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
  1532. ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
  1533. BIND_ENUM_CONSTANT(MODE_RGB);
  1534. BIND_ENUM_CONSTANT(MODE_HSV);
  1535. BIND_ENUM_CONSTANT(MODE_RAW);
  1536. BIND_ENUM_CONSTANT(MODE_OKHSL);
  1537. BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE);
  1538. BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL);
  1539. BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE);
  1540. BIND_ENUM_CONSTANT(SHAPE_OKHSL_CIRCLE);
  1541. BIND_ENUM_CONSTANT(SHAPE_NONE);
  1542. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ColorPicker, content_margin, "margin");
  1543. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, label_width);
  1544. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_width);
  1545. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, sv_height);
  1546. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, h_width);
  1547. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ColorPicker, center_slider_grabbers);
  1548. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, screen_picker);
  1549. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, expanded_arrow);
  1550. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, folded_arrow);
  1551. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, add_preset);
  1552. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect);
  1553. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_rect_wheel);
  1554. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, shape_circle);
  1555. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, bar_arrow);
  1556. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_bg);
  1557. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, sample_revert);
  1558. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, overbright_indicator);
  1559. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, picker_cursor);
  1560. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_hue);
  1561. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPicker, color_okhsl_hue);
  1562. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_normal, "tab_unselected", "TabContainer");
  1563. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_pressed, "tab_selected", "TabContainer");
  1564. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, ColorPicker, mode_button_hover, "tab_selected", "TabContainer");
  1565. }
  1566. ColorPicker::ColorPicker() {
  1567. internal_margin = memnew(MarginContainer);
  1568. add_child(internal_margin, false, INTERNAL_MODE_FRONT);
  1569. VBoxContainer *real_vbox = memnew(VBoxContainer);
  1570. internal_margin->add_child(real_vbox);
  1571. HBoxContainer *hb_edit = memnew(HBoxContainer);
  1572. real_vbox->add_child(hb_edit);
  1573. hb_edit->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1574. uv_edit = memnew(Control);
  1575. hb_edit->add_child(uv_edit);
  1576. uv_edit->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_uv_input).bind(uv_edit));
  1577. uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  1578. uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1579. uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1580. uv_edit->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_hsv_draw).bind(0, uv_edit));
  1581. sample_hbc = memnew(HBoxContainer);
  1582. real_vbox->add_child(sample_hbc);
  1583. btn_pick = memnew(Button);
  1584. btn_pick->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1585. sample_hbc->add_child(btn_pick);
  1586. sample = memnew(TextureRect);
  1587. sample_hbc->add_child(sample);
  1588. sample->set_h_size_flags(SIZE_EXPAND_FILL);
  1589. sample->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_sample_input));
  1590. sample->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_sample_draw));
  1591. btn_shape = memnew(MenuButton);
  1592. btn_shape->set_flat(false);
  1593. sample_hbc->add_child(btn_shape);
  1594. btn_shape->set_toggle_mode(true);
  1595. btn_shape->set_tooltip_text(ETR("Select a picker shape."));
  1596. btn_shape->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1597. current_shape = SHAPE_HSV_RECTANGLE;
  1598. shape_popup = btn_shape->get_popup();
  1599. shape_popup->add_radio_check_item("HSV Rectangle", SHAPE_HSV_RECTANGLE);
  1600. shape_popup->add_radio_check_item("HSV Wheel", SHAPE_HSV_WHEEL);
  1601. shape_popup->add_radio_check_item("VHS Circle", SHAPE_VHS_CIRCLE);
  1602. shape_popup->add_radio_check_item("OKHSL Circle", SHAPE_OKHSL_CIRCLE);
  1603. shape_popup->set_item_checked(current_shape, true);
  1604. shape_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::set_picker_shape));
  1605. add_mode(new ColorModeRGB(this));
  1606. add_mode(new ColorModeHSV(this));
  1607. add_mode(new ColorModeRAW(this));
  1608. add_mode(new ColorModeOKHSL(this));
  1609. mode_hbc = memnew(HBoxContainer);
  1610. real_vbox->add_child(mode_hbc);
  1611. mode_group.instantiate();
  1612. for (int i = 0; i < MODE_BUTTON_COUNT; i++) {
  1613. mode_btns[i] = memnew(Button);
  1614. mode_hbc->add_child(mode_btns[i]);
  1615. mode_btns[i]->set_focus_mode(FOCUS_NONE);
  1616. mode_btns[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1617. mode_btns[i]->set_toggle_mode(true);
  1618. mode_btns[i]->set_text(modes[i]->get_name());
  1619. mode_btns[i]->set_button_group(mode_group);
  1620. mode_btns[i]->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::set_color_mode).bind((ColorModeType)i));
  1621. }
  1622. mode_btns[0]->set_pressed(true);
  1623. btn_mode = memnew(MenuButton);
  1624. btn_mode->set_text("...");
  1625. btn_mode->set_flat(false);
  1626. mode_hbc->add_child(btn_mode);
  1627. btn_mode->set_toggle_mode(true);
  1628. btn_mode->set_tooltip_text(ETR("Select a picker mode."));
  1629. current_mode = MODE_RGB;
  1630. mode_popup = btn_mode->get_popup();
  1631. for (int i = 0; i < modes.size(); i++) {
  1632. mode_popup->add_radio_check_item(modes[i]->get_name(), i);
  1633. }
  1634. mode_popup->add_separator();
  1635. mode_popup->add_check_item(ETR("Colorized Sliders"), MODE_MAX);
  1636. mode_popup->set_item_checked(current_mode, true);
  1637. mode_popup->set_item_checked(MODE_MAX + 1, true);
  1638. mode_popup->connect(SceneStringName(id_pressed), callable_mp(this, &ColorPicker::_set_mode_popup_value));
  1639. VBoxContainer *vbl = memnew(VBoxContainer);
  1640. real_vbox->add_child(vbl);
  1641. VBoxContainer *vbr = memnew(VBoxContainer);
  1642. real_vbox->add_child(vbr);
  1643. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  1644. slider_gc = memnew(GridContainer);
  1645. vbr->add_child(slider_gc);
  1646. slider_gc->set_h_size_flags(SIZE_EXPAND_FILL);
  1647. slider_gc->set_columns(3);
  1648. for (int i = 0; i < SLIDER_COUNT + 1; i++) {
  1649. create_slider(slider_gc, i);
  1650. }
  1651. alpha_label->set_text("A");
  1652. hex_hbc = memnew(HBoxContainer);
  1653. hex_hbc->set_alignment(ALIGNMENT_BEGIN);
  1654. vbr->add_child(hex_hbc);
  1655. hex_hbc->add_child(memnew(Label(ETR("Hex"))));
  1656. text_type = memnew(Button);
  1657. hex_hbc->add_child(text_type);
  1658. text_type->set_text("#");
  1659. text_type->set_tooltip_text(RTR("Switch between hexadecimal and code values."));
  1660. if (Engine::get_singleton()->is_editor_hint()) {
  1661. text_type->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_text_type_toggled));
  1662. } else {
  1663. text_type->set_flat(true);
  1664. text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1665. }
  1666. c_text = memnew(LineEdit);
  1667. hex_hbc->add_child(c_text);
  1668. c_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1669. c_text->set_select_all_on_focus(true);
  1670. c_text->set_tooltip_text(ETR("Enter a hex code (\"#ff0000\") or named color (\"red\")."));
  1671. c_text->set_placeholder(ETR("Hex code or named color"));
  1672. c_text->connect(SceneStringName(text_submitted), callable_mp(this, &ColorPicker::_html_submitted));
  1673. c_text->connect(SceneStringName(text_changed), callable_mp(this, &ColorPicker::_text_changed));
  1674. c_text->connect(SceneStringName(focus_exited), callable_mp(this, &ColorPicker::_html_focus_exit));
  1675. wheel_edit = memnew(AspectRatioContainer);
  1676. wheel_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1677. wheel_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1678. hb_edit->add_child(wheel_edit);
  1679. wheel_mat.instantiate();
  1680. wheel_mat->set_shader(wheel_shader);
  1681. circle_mat.instantiate();
  1682. circle_mat->set_shader(circle_shader);
  1683. wheel_margin = memnew(MarginContainer);
  1684. wheel_margin->add_theme_constant_override("margin_bottom", 8);
  1685. wheel_edit->add_child(wheel_margin);
  1686. wheel = memnew(Control);
  1687. wheel_margin->add_child(wheel);
  1688. wheel->set_mouse_filter(MOUSE_FILTER_PASS);
  1689. wheel->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_hsv_draw).bind(2, wheel));
  1690. wheel_uv = memnew(Control);
  1691. wheel_margin->add_child(wheel_uv);
  1692. wheel_uv->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_uv_input).bind(wheel_uv));
  1693. wheel_uv->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_hsv_draw).bind(0, wheel_uv));
  1694. w_edit = memnew(Control);
  1695. hb_edit->add_child(w_edit);
  1696. w_edit->set_h_size_flags(SIZE_FILL);
  1697. w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1698. w_edit->connect(SceneStringName(gui_input), callable_mp(this, &ColorPicker::_w_input));
  1699. w_edit->connect(SceneStringName(draw), callable_mp(this, &ColorPicker::_hsv_draw).bind(1, w_edit));
  1700. _update_controls();
  1701. updating = false;
  1702. preset_container = memnew(GridContainer);
  1703. preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1704. preset_container->set_columns(PRESET_COLUMN_COUNT);
  1705. preset_container->hide();
  1706. preset_group.instantiate();
  1707. btn_preset = memnew(Button(ETR("Swatches")));
  1708. btn_preset->set_flat(true);
  1709. btn_preset->set_toggle_mode(true);
  1710. btn_preset->set_focus_mode(FOCUS_NONE);
  1711. btn_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1712. btn_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_preset, preset_container));
  1713. real_vbox->add_child(btn_preset);
  1714. real_vbox->add_child(preset_container);
  1715. recent_preset_hbc = memnew(HBoxContainer);
  1716. recent_preset_hbc->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1717. recent_preset_hbc->hide();
  1718. recent_preset_group.instantiate();
  1719. btn_recent_preset = memnew(Button(ETR("Recent Colors")));
  1720. btn_recent_preset->set_flat(true);
  1721. btn_recent_preset->set_toggle_mode(true);
  1722. btn_recent_preset->set_focus_mode(FOCUS_NONE);
  1723. btn_recent_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1724. btn_recent_preset->connect(SceneStringName(toggled), callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_recent_preset, recent_preset_hbc));
  1725. real_vbox->add_child(btn_recent_preset);
  1726. real_vbox->add_child(recent_preset_hbc);
  1727. set_pick_color(Color(1, 1, 1));
  1728. btn_add_preset = memnew(Button);
  1729. btn_add_preset->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1730. btn_add_preset->set_tooltip_text(ETR("Add current color as a preset."));
  1731. btn_add_preset->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_add_preset_pressed));
  1732. preset_container->add_child(btn_add_preset);
  1733. }
  1734. ColorPicker::~ColorPicker() {
  1735. for (int i = 0; i < modes.size(); i++) {
  1736. delete modes[i];
  1737. }
  1738. }
  1739. /////////////////
  1740. void ColorPickerPopupPanel::_input_from_window(const Ref<InputEvent> &p_event) {
  1741. if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
  1742. _close_pressed();
  1743. }
  1744. PopupPanel::_input_from_window(p_event);
  1745. }
  1746. /////////////////
  1747. void ColorPickerButton::_about_to_popup() {
  1748. set_pressed(true);
  1749. if (picker) {
  1750. picker->set_old_color(color);
  1751. }
  1752. }
  1753. void ColorPickerButton::_color_changed(const Color &p_color) {
  1754. color = p_color;
  1755. queue_redraw();
  1756. emit_signal(SNAME("color_changed"), color);
  1757. }
  1758. void ColorPickerButton::_modal_closed() {
  1759. if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"))) {
  1760. set_pick_color(picker->get_old_color());
  1761. emit_signal(SNAME("color_changed"), color);
  1762. }
  1763. emit_signal(SNAME("popup_closed"));
  1764. set_pressed(false);
  1765. }
  1766. void ColorPickerButton::pressed() {
  1767. _update_picker();
  1768. Size2 minsize = popup->get_contents_minimum_size();
  1769. float viewport_height = get_viewport_rect().size.y;
  1770. popup->reset_size();
  1771. picker->_update_presets();
  1772. picker->_update_recent_presets();
  1773. // Determine in which direction to show the popup. By default popup horizontally centered below the button.
  1774. // But if the popup doesn't fit below and the button is in the bottom half of the viewport, show above.
  1775. bool show_above = false;
  1776. if (get_global_position().y + get_size().y + minsize.y > viewport_height && get_global_position().y * 2 + get_size().y > viewport_height) {
  1777. show_above = true;
  1778. }
  1779. float h_offset = (get_size().x - minsize.x) / 2;
  1780. float v_offset = show_above ? -minsize.y : get_size().y;
  1781. popup->set_position(get_screen_position() + Vector2(h_offset, v_offset));
  1782. popup->popup();
  1783. if (DisplayServer::get_singleton()->has_hardware_keyboard()) {
  1784. picker->set_focus_on_line_edit();
  1785. }
  1786. }
  1787. void ColorPickerButton::_notification(int p_what) {
  1788. switch (p_what) {
  1789. case NOTIFICATION_DRAW: {
  1790. const Rect2 r = Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size());
  1791. draw_texture_rect(theme_cache.background_icon, r, true);
  1792. draw_rect(r, color);
  1793. if (color.r > 1 || color.g > 1 || color.b > 1) {
  1794. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  1795. draw_texture(theme_cache.overbright_indicator, theme_cache.normal_style->get_offset());
  1796. }
  1797. } break;
  1798. case NOTIFICATION_WM_CLOSE_REQUEST: {
  1799. if (popup) {
  1800. popup->hide();
  1801. }
  1802. } break;
  1803. case NOTIFICATION_VISIBILITY_CHANGED: {
  1804. if (popup && !is_visible_in_tree()) {
  1805. popup->hide();
  1806. }
  1807. } break;
  1808. }
  1809. }
  1810. void ColorPickerButton::set_pick_color(const Color &p_color) {
  1811. if (color == p_color) {
  1812. return;
  1813. }
  1814. color = p_color;
  1815. if (picker) {
  1816. picker->set_pick_color(p_color);
  1817. }
  1818. queue_redraw();
  1819. }
  1820. Color ColorPickerButton::get_pick_color() const {
  1821. return color;
  1822. }
  1823. void ColorPickerButton::set_edit_alpha(bool p_show) {
  1824. if (edit_alpha == p_show) {
  1825. return;
  1826. }
  1827. edit_alpha = p_show;
  1828. if (picker) {
  1829. picker->set_edit_alpha(p_show);
  1830. }
  1831. }
  1832. bool ColorPickerButton::is_editing_alpha() const {
  1833. return edit_alpha;
  1834. }
  1835. ColorPicker *ColorPickerButton::get_picker() {
  1836. _update_picker();
  1837. return picker;
  1838. }
  1839. PopupPanel *ColorPickerButton::get_popup() {
  1840. _update_picker();
  1841. return popup;
  1842. }
  1843. void ColorPickerButton::_update_picker() {
  1844. if (!picker) {
  1845. popup = memnew(ColorPickerPopupPanel);
  1846. popup->set_wrap_controls(true);
  1847. picker = memnew(ColorPicker);
  1848. picker->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  1849. popup->add_child(picker);
  1850. add_child(popup, false, INTERNAL_MODE_FRONT);
  1851. picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed));
  1852. popup->connect("about_to_popup", callable_mp(this, &ColorPickerButton::_about_to_popup));
  1853. popup->connect("popup_hide", callable_mp(this, &ColorPickerButton::_modal_closed));
  1854. picker->connect(SceneStringName(minimum_size_changed), callable_mp((Window *)popup, &Window::reset_size));
  1855. picker->set_pick_color(color);
  1856. picker->set_edit_alpha(edit_alpha);
  1857. picker->set_display_old_color(true);
  1858. emit_signal(SNAME("picker_created"));
  1859. }
  1860. }
  1861. void ColorPickerButton::_bind_methods() {
  1862. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
  1863. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
  1864. ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
  1865. ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
  1866. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
  1867. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
  1868. ClassDB::bind_method(D_METHOD("_about_to_popup"), &ColorPickerButton::_about_to_popup);
  1869. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  1870. ADD_SIGNAL(MethodInfo("popup_closed"));
  1871. ADD_SIGNAL(MethodInfo("picker_created"));
  1872. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  1873. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  1874. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPickerButton, normal_style, "normal");
  1875. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPickerButton, background_icon, "bg");
  1876. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, ColorPickerButton, overbright_indicator, "overbright_indicator", "ColorPicker");
  1877. }
  1878. ColorPickerButton::ColorPickerButton(const String &p_text) :
  1879. Button(p_text) {
  1880. set_toggle_mode(true);
  1881. }
  1882. /////////////////
  1883. void ColorPresetButton::_notification(int p_what) {
  1884. switch (p_what) {
  1885. case NOTIFICATION_DRAW: {
  1886. const Rect2 r = Rect2(Point2(0, 0), get_size());
  1887. Ref<StyleBox> sb_raw = theme_cache.foreground_style->duplicate();
  1888. Ref<StyleBoxFlat> sb_flat = sb_raw;
  1889. Ref<StyleBoxTexture> sb_texture = sb_raw;
  1890. if (sb_flat.is_valid()) {
  1891. sb_flat->set_border_width(SIDE_BOTTOM, 2);
  1892. if (get_draw_mode() == DRAW_PRESSED || get_draw_mode() == DRAW_HOVER_PRESSED) {
  1893. sb_flat->set_border_color(Color(1, 1, 1, 1));
  1894. } else {
  1895. sb_flat->set_border_color(Color(0, 0, 0, 1));
  1896. }
  1897. if (preset_color.a < 1) {
  1898. // Draw a background pattern when the color is transparent.
  1899. sb_flat->set_bg_color(Color(1, 1, 1));
  1900. sb_flat->draw(get_canvas_item(), r);
  1901. Rect2 bg_texture_rect = r.grow_side(SIDE_LEFT, -sb_flat->get_margin(SIDE_LEFT));
  1902. bg_texture_rect = bg_texture_rect.grow_side(SIDE_RIGHT, -sb_flat->get_margin(SIDE_RIGHT));
  1903. bg_texture_rect = bg_texture_rect.grow_side(SIDE_TOP, -sb_flat->get_margin(SIDE_TOP));
  1904. bg_texture_rect = bg_texture_rect.grow_side(SIDE_BOTTOM, -sb_flat->get_margin(SIDE_BOTTOM));
  1905. draw_texture_rect(theme_cache.background_icon, bg_texture_rect, true);
  1906. sb_flat->set_bg_color(preset_color);
  1907. }
  1908. sb_flat->set_bg_color(preset_color);
  1909. sb_flat->draw(get_canvas_item(), r);
  1910. } else if (sb_texture.is_valid()) {
  1911. if (preset_color.a < 1) {
  1912. // Draw a background pattern when the color is transparent.
  1913. bool use_tile_texture = (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE) || (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE_FIT);
  1914. draw_texture_rect(theme_cache.background_icon, r, use_tile_texture);
  1915. }
  1916. sb_texture->set_modulate(preset_color);
  1917. sb_texture->draw(get_canvas_item(), r);
  1918. } else {
  1919. WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead.");
  1920. }
  1921. if (preset_color.r > 1 || preset_color.g > 1 || preset_color.b > 1) {
  1922. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  1923. draw_texture(theme_cache.overbright_indicator, Vector2(0, 0));
  1924. }
  1925. } break;
  1926. }
  1927. }
  1928. void ColorPresetButton::set_preset_color(const Color &p_color) {
  1929. preset_color = p_color;
  1930. }
  1931. Color ColorPresetButton::get_preset_color() const {
  1932. return preset_color;
  1933. }
  1934. void ColorPresetButton::_bind_methods() {
  1935. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, foreground_style, "preset_fg");
  1936. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ColorPresetButton, background_icon, "preset_bg");
  1937. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPresetButton, overbright_indicator);
  1938. }
  1939. ColorPresetButton::ColorPresetButton(Color p_color, int p_size) {
  1940. preset_color = p_color;
  1941. set_toggle_mode(true);
  1942. set_custom_minimum_size(Size2(p_size, p_size));
  1943. }
  1944. ColorPresetButton::~ColorPresetButton() {
  1945. }