color_picker.cpp 92 KB

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