color_picker.cpp 87 KB

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