color_picker.cpp 93 KB

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