theme_editor_plugin.cpp 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132
  1. /**************************************************************************/
  2. /* theme_editor_plugin.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 "theme_editor_plugin.h"
  31. #include "editor/doc/editor_help.h"
  32. #include "editor/docks/editor_dock_manager.h"
  33. #include "editor/docks/filesystem_dock.h"
  34. #include "editor/docks/inspector_dock.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/file_system/editor_file_system.h"
  39. #include "editor/gui/editor_bottom_panel.h"
  40. #include "editor/gui/editor_file_dialog.h"
  41. #include "editor/gui/editor_spin_slider.h"
  42. #include "editor/gui/progress_dialog.h"
  43. #include "editor/inspector/editor_resource_picker.h"
  44. #include "editor/settings/editor_command_palette.h"
  45. #include "editor/settings/editor_settings.h"
  46. #include "editor/themes/editor_scale.h"
  47. #include "scene/gui/check_button.h"
  48. #include "scene/gui/color_picker.h"
  49. #include "scene/gui/item_list.h"
  50. #include "scene/gui/option_button.h"
  51. #include "scene/gui/panel_container.h"
  52. #include "scene/gui/scroll_container.h"
  53. #include "scene/gui/separator.h"
  54. #include "scene/gui/split_container.h"
  55. #include "scene/gui/tab_bar.h"
  56. #include "scene/gui/tab_container.h"
  57. #include "scene/gui/texture_rect.h"
  58. #include "scene/theme/theme_db.h"
  59. static void _rename_theme_type(EditorUndoRedoManager *p_ur, Theme *p_theme, const String &p_old_theme_type, const String &p_new_theme_type) {
  60. p_ur->add_do_method(p_theme, "rename_type", p_old_theme_type, p_new_theme_type);
  61. p_ur->add_undo_method(p_theme, "rename_type", p_new_theme_type, p_old_theme_type);
  62. // Renaming a theme type to an empty name or a variation to a type associated with a built-in class
  63. // removes type variation connections in a way that cannot be undone by reversing the rename alone.
  64. const StringName old_base_type = p_theme->get_type_variation_base(p_old_theme_type);
  65. if ((!p_old_theme_type.is_empty() && p_new_theme_type.is_empty()) || (old_base_type != StringName() && ClassDB::class_exists(p_new_theme_type))) {
  66. if (old_base_type != StringName()) {
  67. p_ur->add_undo_method(p_theme, "set_type_variation", p_old_theme_type, old_base_type);
  68. }
  69. List<StringName> names;
  70. p_theme->get_type_variation_list(p_old_theme_type, &names);
  71. for (const StringName &E : names) {
  72. p_ur->add_undo_method(p_theme, "set_type_variation", E, p_old_theme_type);
  73. }
  74. }
  75. }
  76. ///////////////////////
  77. void ThemeItemImportTree::_update_items_tree() {
  78. import_items_tree->clear();
  79. TreeItem *root = import_items_tree->create_item();
  80. if (base_theme.is_null()) {
  81. return;
  82. }
  83. String filter_text = import_items_filter->get_text();
  84. List<StringName> types;
  85. List<StringName> names;
  86. List<StringName> filtered_names;
  87. base_theme->get_type_list(&types);
  88. types.sort_custom<StringName::AlphCompare>();
  89. int color_amount = 0;
  90. int constant_amount = 0;
  91. int font_amount = 0;
  92. int font_size_amount = 0;
  93. int icon_amount = 0;
  94. int stylebox_amount = 0;
  95. tree_color_items.clear();
  96. tree_constant_items.clear();
  97. tree_font_items.clear();
  98. tree_font_size_items.clear();
  99. tree_icon_items.clear();
  100. tree_stylebox_items.clear();
  101. for (const StringName &E : types) {
  102. String type_name = (String)E;
  103. Ref<Texture2D> type_icon;
  104. if (E == "") {
  105. type_icon = get_editor_theme_icon(SNAME("NodeDisabled"));
  106. } else {
  107. type_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled");
  108. }
  109. TreeItem *type_node = import_items_tree->create_item(root);
  110. type_node->set_meta("_can_be_imported", false);
  111. type_node->set_collapsed(true);
  112. type_node->set_icon(0, type_icon);
  113. type_node->set_text(0, type_name);
  114. type_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK);
  115. type_node->set_checked(IMPORT_ITEM, false);
  116. type_node->set_editable(IMPORT_ITEM, true);
  117. type_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK);
  118. type_node->set_checked(IMPORT_ITEM_DATA, false);
  119. type_node->set_editable(IMPORT_ITEM_DATA, true);
  120. bool is_matching_filter = (filter_text.is_empty() || type_name.containsn(filter_text));
  121. bool has_filtered_items = false;
  122. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  123. Theme::DataType dt = (Theme::DataType)i;
  124. names.clear();
  125. filtered_names.clear();
  126. base_theme->get_theme_item_list(dt, E, &names);
  127. bool data_type_has_filtered_items = false;
  128. for (const StringName &F : names) {
  129. String item_name = (String)F;
  130. bool is_item_matching_filter = item_name.containsn(filter_text);
  131. if (!filter_text.is_empty() && !is_matching_filter && !is_item_matching_filter) {
  132. continue;
  133. }
  134. // Only mark this if actual items match the filter and not just the type group.
  135. if (!filter_text.is_empty() && is_item_matching_filter) {
  136. has_filtered_items = true;
  137. data_type_has_filtered_items = true;
  138. }
  139. filtered_names.push_back(F);
  140. }
  141. if (filtered_names.is_empty()) {
  142. continue;
  143. }
  144. TreeItem *data_type_node = import_items_tree->create_item(type_node);
  145. data_type_node->set_meta("_can_be_imported", false);
  146. data_type_node->set_metadata(0, i);
  147. data_type_node->set_collapsed(!data_type_has_filtered_items);
  148. data_type_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK);
  149. data_type_node->set_checked(IMPORT_ITEM, false);
  150. data_type_node->set_editable(IMPORT_ITEM, true);
  151. data_type_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK);
  152. data_type_node->set_checked(IMPORT_ITEM_DATA, false);
  153. data_type_node->set_editable(IMPORT_ITEM_DATA, true);
  154. List<TreeItem *> *item_list = nullptr;
  155. switch (dt) {
  156. case Theme::DATA_TYPE_COLOR:
  157. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("Color")));
  158. data_type_node->set_text(0, TTR("Colors"));
  159. item_list = &tree_color_items;
  160. color_amount += filtered_names.size();
  161. break;
  162. case Theme::DATA_TYPE_CONSTANT:
  163. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("MemberConstant")));
  164. data_type_node->set_text(0, TTR("Constants"));
  165. item_list = &tree_constant_items;
  166. constant_amount += filtered_names.size();
  167. break;
  168. case Theme::DATA_TYPE_FONT:
  169. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("FontItem")));
  170. data_type_node->set_text(0, TTR("Fonts"));
  171. item_list = &tree_font_items;
  172. font_amount += filtered_names.size();
  173. break;
  174. case Theme::DATA_TYPE_FONT_SIZE:
  175. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("FontSize")));
  176. data_type_node->set_text(0, TTR("Font Sizes"));
  177. item_list = &tree_font_size_items;
  178. font_size_amount += filtered_names.size();
  179. break;
  180. case Theme::DATA_TYPE_ICON:
  181. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("ImageTexture")));
  182. data_type_node->set_text(0, TTR("Icons"));
  183. item_list = &tree_icon_items;
  184. icon_amount += filtered_names.size();
  185. break;
  186. case Theme::DATA_TYPE_STYLEBOX:
  187. data_type_node->set_icon(0, get_editor_theme_icon(SNAME("StyleBoxFlat")));
  188. data_type_node->set_text(0, TTR("Styleboxes"));
  189. item_list = &tree_stylebox_items;
  190. stylebox_amount += filtered_names.size();
  191. break;
  192. case Theme::DATA_TYPE_MAX:
  193. break; // Can't happen, but silences warning.
  194. }
  195. filtered_names.sort_custom<StringName::AlphCompare>();
  196. for (const StringName &F : filtered_names) {
  197. TreeItem *item_node = import_items_tree->create_item(data_type_node);
  198. item_node->set_meta("_can_be_imported", true);
  199. item_node->set_text(0, F);
  200. item_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK);
  201. item_node->set_checked(IMPORT_ITEM, false);
  202. item_node->set_editable(IMPORT_ITEM, true);
  203. item_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK);
  204. item_node->set_checked(IMPORT_ITEM_DATA, false);
  205. item_node->set_editable(IMPORT_ITEM_DATA, true);
  206. _restore_selected_item(item_node);
  207. item_node->propagate_check(IMPORT_ITEM, false);
  208. item_node->propagate_check(IMPORT_ITEM_DATA, false);
  209. item_list->push_back(item_node);
  210. }
  211. }
  212. // Remove the item if it doesn't match the filter in any way.
  213. if (!is_matching_filter && !has_filtered_items) {
  214. root->remove_child(type_node);
  215. memdelete(type_node);
  216. continue;
  217. }
  218. // Show one level inside of a type group if there are matches in items.
  219. if (!filter_text.is_empty() && has_filtered_items) {
  220. type_node->set_collapsed(false);
  221. }
  222. }
  223. if (color_amount > 0) {
  224. Array arr = { color_amount };
  225. select_colors_label->set_text(TTRN("1 color", "{num} colors", color_amount).format(arr, "{num}"));
  226. select_all_colors_button->set_visible(true);
  227. select_full_colors_button->set_visible(true);
  228. deselect_all_colors_button->set_visible(true);
  229. } else {
  230. select_colors_label->set_text(TTR("No colors found."));
  231. select_all_colors_button->set_visible(false);
  232. select_full_colors_button->set_visible(false);
  233. deselect_all_colors_button->set_visible(false);
  234. }
  235. if (constant_amount > 0) {
  236. Array arr = { constant_amount };
  237. select_constants_label->set_text(TTRN("1 constant", "{num} constants", constant_amount).format(arr, "{num}"));
  238. select_all_constants_button->set_visible(true);
  239. select_full_constants_button->set_visible(true);
  240. deselect_all_constants_button->set_visible(true);
  241. } else {
  242. select_constants_label->set_text(TTR("No constants found."));
  243. select_all_constants_button->set_visible(false);
  244. select_full_constants_button->set_visible(false);
  245. deselect_all_constants_button->set_visible(false);
  246. }
  247. if (font_amount > 0) {
  248. Array arr = { font_amount };
  249. select_fonts_label->set_text(TTRN("1 font", "{num} fonts", font_amount).format(arr, "{num}"));
  250. select_all_fonts_button->set_visible(true);
  251. select_full_fonts_button->set_visible(true);
  252. deselect_all_fonts_button->set_visible(true);
  253. } else {
  254. select_fonts_label->set_text(TTR("No fonts found."));
  255. select_all_fonts_button->set_visible(false);
  256. select_full_fonts_button->set_visible(false);
  257. deselect_all_fonts_button->set_visible(false);
  258. }
  259. if (font_size_amount > 0) {
  260. Array arr = { font_size_amount };
  261. select_font_sizes_label->set_text(TTRN("1 font size", "{num} font sizes", font_size_amount).format(arr, "{num}"));
  262. select_all_font_sizes_button->set_visible(true);
  263. select_full_font_sizes_button->set_visible(true);
  264. deselect_all_font_sizes_button->set_visible(true);
  265. } else {
  266. select_font_sizes_label->set_text(TTR("No font sizes found."));
  267. select_all_font_sizes_button->set_visible(false);
  268. select_full_font_sizes_button->set_visible(false);
  269. deselect_all_font_sizes_button->set_visible(false);
  270. }
  271. if (icon_amount > 0) {
  272. Array arr = { icon_amount };
  273. select_icons_label->set_text(TTRN("1 icon", "{num} icons", icon_amount).format(arr, "{num}"));
  274. select_all_icons_button->set_visible(true);
  275. select_full_icons_button->set_visible(true);
  276. deselect_all_icons_button->set_visible(true);
  277. select_icons_warning_hb->set_visible(true);
  278. } else {
  279. select_icons_label->set_text(TTR("No icons found."));
  280. select_all_icons_button->set_visible(false);
  281. select_full_icons_button->set_visible(false);
  282. deselect_all_icons_button->set_visible(false);
  283. select_icons_warning_hb->set_visible(false);
  284. }
  285. if (stylebox_amount > 0) {
  286. Array arr = { stylebox_amount };
  287. select_styleboxes_label->set_text(TTRN("1 stylebox", "{num} styleboxes", stylebox_amount).format(arr, "{num}"));
  288. select_all_styleboxes_button->set_visible(true);
  289. select_full_styleboxes_button->set_visible(true);
  290. deselect_all_styleboxes_button->set_visible(true);
  291. } else {
  292. select_styleboxes_label->set_text(TTR("No styleboxes found."));
  293. select_all_styleboxes_button->set_visible(false);
  294. select_full_styleboxes_button->set_visible(false);
  295. deselect_all_styleboxes_button->set_visible(false);
  296. }
  297. }
  298. void ThemeItemImportTree::_toggle_type_items(bool p_collapse) {
  299. TreeItem *root = import_items_tree->get_root();
  300. if (!root) {
  301. return;
  302. }
  303. TreeItem *type_node = root->get_first_child();
  304. while (type_node) {
  305. type_node->set_collapsed(p_collapse);
  306. type_node = type_node->get_next();
  307. }
  308. }
  309. void ThemeItemImportTree::_filter_text_changed(const String &p_value) {
  310. _update_items_tree();
  311. }
  312. void ThemeItemImportTree::_store_selected_item(TreeItem *p_tree_item) {
  313. if (!p_tree_item->get_meta("_can_be_imported")) {
  314. return;
  315. }
  316. TreeItem *data_type_node = p_tree_item->get_parent();
  317. if (!data_type_node || data_type_node == import_items_tree->get_root()) {
  318. return;
  319. }
  320. TreeItem *type_node = data_type_node->get_parent();
  321. if (!type_node || type_node == import_items_tree->get_root()) {
  322. return;
  323. }
  324. ThemeItem ti;
  325. ti.item_name = p_tree_item->get_text(0);
  326. ti.data_type = (Theme::DataType)(int)data_type_node->get_metadata(0);
  327. ti.type_name = type_node->get_text(0);
  328. bool import = p_tree_item->is_checked(IMPORT_ITEM);
  329. bool with_data = p_tree_item->is_checked(IMPORT_ITEM_DATA);
  330. if (import && with_data) {
  331. selected_items[ti] = SELECT_IMPORT_FULL;
  332. } else if (import) {
  333. selected_items[ti] = SELECT_IMPORT_DEFINITION;
  334. } else {
  335. selected_items.erase(ti);
  336. }
  337. _update_total_selected(ti.data_type);
  338. }
  339. void ThemeItemImportTree::_restore_selected_item(TreeItem *p_tree_item) {
  340. if (!p_tree_item->get_meta("_can_be_imported")) {
  341. return;
  342. }
  343. TreeItem *data_type_node = p_tree_item->get_parent();
  344. if (!data_type_node || data_type_node == import_items_tree->get_root()) {
  345. return;
  346. }
  347. TreeItem *type_node = data_type_node->get_parent();
  348. if (!type_node || type_node == import_items_tree->get_root()) {
  349. return;
  350. }
  351. ThemeItem ti;
  352. ti.item_name = p_tree_item->get_text(0);
  353. ti.data_type = (Theme::DataType)(int)data_type_node->get_metadata(0);
  354. ti.type_name = type_node->get_text(0);
  355. if (!selected_items.has(ti)) {
  356. p_tree_item->set_checked(IMPORT_ITEM, false);
  357. p_tree_item->set_checked(IMPORT_ITEM_DATA, false);
  358. return;
  359. }
  360. if (selected_items[ti] == SELECT_IMPORT_FULL) {
  361. p_tree_item->set_checked(IMPORT_ITEM, true);
  362. p_tree_item->set_checked(IMPORT_ITEM_DATA, true);
  363. } else if (selected_items[ti] == SELECT_IMPORT_DEFINITION) {
  364. p_tree_item->set_checked(IMPORT_ITEM, true);
  365. p_tree_item->set_checked(IMPORT_ITEM_DATA, false);
  366. }
  367. }
  368. void ThemeItemImportTree::_update_total_selected(Theme::DataType p_data_type) {
  369. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  370. Label *total_selected_items_label = nullptr;
  371. switch (p_data_type) {
  372. case Theme::DATA_TYPE_COLOR:
  373. total_selected_items_label = total_selected_colors_label;
  374. break;
  375. case Theme::DATA_TYPE_CONSTANT:
  376. total_selected_items_label = total_selected_constants_label;
  377. break;
  378. case Theme::DATA_TYPE_FONT:
  379. total_selected_items_label = total_selected_fonts_label;
  380. break;
  381. case Theme::DATA_TYPE_FONT_SIZE:
  382. total_selected_items_label = total_selected_font_sizes_label;
  383. break;
  384. case Theme::DATA_TYPE_ICON:
  385. total_selected_items_label = total_selected_icons_label;
  386. break;
  387. case Theme::DATA_TYPE_STYLEBOX:
  388. total_selected_items_label = total_selected_styleboxes_label;
  389. break;
  390. case Theme::DATA_TYPE_MAX:
  391. return; // Can't happen, but silences warning.
  392. }
  393. if (!total_selected_items_label) {
  394. return;
  395. }
  396. int count = 0;
  397. for (const KeyValue<ThemeItem, ItemCheckedState> &E : selected_items) {
  398. ThemeItem ti = E.key;
  399. if (ti.data_type == p_data_type) {
  400. count++;
  401. }
  402. }
  403. if (count == 0) {
  404. total_selected_items_label->hide();
  405. } else {
  406. Array arr = { count };
  407. total_selected_items_label->set_text(TTRN("{num} currently selected", "{num} currently selected", count).format(arr, "{num}"));
  408. total_selected_items_label->show();
  409. }
  410. }
  411. void ThemeItemImportTree::_tree_item_edited() {
  412. if (updating_tree) {
  413. return;
  414. }
  415. TreeItem *edited_item = import_items_tree->get_edited();
  416. if (!edited_item) {
  417. return;
  418. }
  419. updating_tree = true;
  420. int edited_column = import_items_tree->get_edited_column();
  421. bool is_checked = edited_item->is_checked(edited_column);
  422. if (is_checked) {
  423. if (edited_column == IMPORT_ITEM_DATA) {
  424. edited_item->set_checked(IMPORT_ITEM, true);
  425. edited_item->propagate_check(IMPORT_ITEM);
  426. }
  427. } else {
  428. if (edited_column == IMPORT_ITEM) {
  429. edited_item->set_checked(IMPORT_ITEM_DATA, false);
  430. edited_item->propagate_check(IMPORT_ITEM_DATA);
  431. }
  432. }
  433. edited_item->propagate_check(edited_column);
  434. updating_tree = false;
  435. }
  436. void ThemeItemImportTree::_check_propagated_to_tree_item(Object *p_obj, int p_column) {
  437. TreeItem *item = Object::cast_to<TreeItem>(p_obj);
  438. // Skip "category" tree items by checking for children.
  439. if (item && !item->get_first_child()) {
  440. _store_selected_item(item);
  441. }
  442. }
  443. void ThemeItemImportTree::_select_all_subitems(TreeItem *p_root_item, bool p_select_with_data) {
  444. TreeItem *child_item = p_root_item->get_first_child();
  445. while (child_item) {
  446. child_item->set_checked(IMPORT_ITEM, true);
  447. if (p_select_with_data) {
  448. child_item->set_checked(IMPORT_ITEM_DATA, true);
  449. }
  450. _store_selected_item(child_item);
  451. _select_all_subitems(child_item, p_select_with_data);
  452. child_item = child_item->get_next();
  453. }
  454. }
  455. void ThemeItemImportTree::_deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely) {
  456. TreeItem *child_item = p_root_item->get_first_child();
  457. while (child_item) {
  458. child_item->set_checked(IMPORT_ITEM_DATA, false);
  459. if (p_deselect_completely) {
  460. child_item->set_checked(IMPORT_ITEM, false);
  461. }
  462. _store_selected_item(child_item);
  463. _deselect_all_subitems(child_item, p_deselect_completely);
  464. child_item = child_item->get_next();
  465. }
  466. }
  467. void ThemeItemImportTree::_select_all_items_pressed() {
  468. if (updating_tree) {
  469. return;
  470. }
  471. updating_tree = true;
  472. TreeItem *root = import_items_tree->get_root();
  473. _select_all_subitems(root, false);
  474. updating_tree = false;
  475. }
  476. void ThemeItemImportTree::_select_full_items_pressed() {
  477. if (updating_tree) {
  478. return;
  479. }
  480. updating_tree = true;
  481. TreeItem *root = import_items_tree->get_root();
  482. _select_all_subitems(root, true);
  483. updating_tree = false;
  484. }
  485. void ThemeItemImportTree::_deselect_all_items_pressed() {
  486. if (updating_tree) {
  487. return;
  488. }
  489. updating_tree = true;
  490. TreeItem *root = import_items_tree->get_root();
  491. _deselect_all_subitems(root, true);
  492. updating_tree = false;
  493. }
  494. void ThemeItemImportTree::_select_all_data_type_pressed(int p_data_type) {
  495. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  496. if (updating_tree) {
  497. return;
  498. }
  499. Theme::DataType data_type = (Theme::DataType)p_data_type;
  500. List<TreeItem *> *item_list = nullptr;
  501. switch (data_type) {
  502. case Theme::DATA_TYPE_COLOR:
  503. item_list = &tree_color_items;
  504. break;
  505. case Theme::DATA_TYPE_CONSTANT:
  506. item_list = &tree_constant_items;
  507. break;
  508. case Theme::DATA_TYPE_FONT:
  509. item_list = &tree_font_items;
  510. break;
  511. case Theme::DATA_TYPE_FONT_SIZE:
  512. item_list = &tree_font_size_items;
  513. break;
  514. case Theme::DATA_TYPE_ICON:
  515. item_list = &tree_icon_items;
  516. break;
  517. case Theme::DATA_TYPE_STYLEBOX:
  518. item_list = &tree_stylebox_items;
  519. break;
  520. case Theme::DATA_TYPE_MAX:
  521. return; // Can't happen, but silences warning.
  522. }
  523. updating_tree = true;
  524. for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) {
  525. TreeItem *child_item = E->get();
  526. if (!child_item) {
  527. continue;
  528. }
  529. child_item->set_checked(IMPORT_ITEM, true);
  530. child_item->propagate_check(IMPORT_ITEM, false);
  531. _store_selected_item(child_item);
  532. }
  533. updating_tree = false;
  534. }
  535. void ThemeItemImportTree::_select_full_data_type_pressed(int p_data_type) {
  536. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  537. if (updating_tree) {
  538. return;
  539. }
  540. Theme::DataType data_type = (Theme::DataType)p_data_type;
  541. List<TreeItem *> *item_list = nullptr;
  542. switch (data_type) {
  543. case Theme::DATA_TYPE_COLOR:
  544. item_list = &tree_color_items;
  545. break;
  546. case Theme::DATA_TYPE_CONSTANT:
  547. item_list = &tree_constant_items;
  548. break;
  549. case Theme::DATA_TYPE_FONT:
  550. item_list = &tree_font_items;
  551. break;
  552. case Theme::DATA_TYPE_FONT_SIZE:
  553. item_list = &tree_font_size_items;
  554. break;
  555. case Theme::DATA_TYPE_ICON:
  556. item_list = &tree_icon_items;
  557. break;
  558. case Theme::DATA_TYPE_STYLEBOX:
  559. item_list = &tree_stylebox_items;
  560. break;
  561. case Theme::DATA_TYPE_MAX:
  562. return; // Can't happen, but silences warning.
  563. }
  564. updating_tree = true;
  565. for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) {
  566. TreeItem *child_item = E->get();
  567. if (!child_item) {
  568. continue;
  569. }
  570. child_item->set_checked(IMPORT_ITEM, true);
  571. child_item->set_checked(IMPORT_ITEM_DATA, true);
  572. child_item->propagate_check(IMPORT_ITEM, false);
  573. child_item->propagate_check(IMPORT_ITEM_DATA, false);
  574. _store_selected_item(child_item);
  575. }
  576. updating_tree = false;
  577. }
  578. void ThemeItemImportTree::_deselect_all_data_type_pressed(int p_data_type) {
  579. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  580. if (updating_tree) {
  581. return;
  582. }
  583. Theme::DataType data_type = (Theme::DataType)p_data_type;
  584. List<TreeItem *> *item_list = nullptr;
  585. switch (data_type) {
  586. case Theme::DATA_TYPE_COLOR:
  587. item_list = &tree_color_items;
  588. break;
  589. case Theme::DATA_TYPE_CONSTANT:
  590. item_list = &tree_constant_items;
  591. break;
  592. case Theme::DATA_TYPE_FONT:
  593. item_list = &tree_font_items;
  594. break;
  595. case Theme::DATA_TYPE_FONT_SIZE:
  596. item_list = &tree_font_size_items;
  597. break;
  598. case Theme::DATA_TYPE_ICON:
  599. item_list = &tree_icon_items;
  600. break;
  601. case Theme::DATA_TYPE_STYLEBOX:
  602. item_list = &tree_stylebox_items;
  603. break;
  604. case Theme::DATA_TYPE_MAX:
  605. return; // Can't happen, but silences warning.
  606. }
  607. updating_tree = true;
  608. for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) {
  609. TreeItem *child_item = E->get();
  610. if (!child_item) {
  611. continue;
  612. }
  613. child_item->set_checked(IMPORT_ITEM, false);
  614. child_item->set_checked(IMPORT_ITEM_DATA, false);
  615. child_item->propagate_check(IMPORT_ITEM, false);
  616. child_item->propagate_check(IMPORT_ITEM_DATA, false);
  617. _store_selected_item(child_item);
  618. }
  619. updating_tree = false;
  620. }
  621. void ThemeItemImportTree::_import_selected() {
  622. if (selected_items.is_empty()) {
  623. EditorNode::get_singleton()->show_accept(TTR("Nothing was selected for the import."), TTR("OK"));
  624. return;
  625. }
  626. Ref<Theme> old_snapshot = edited_theme->duplicate();
  627. Ref<Theme> new_snapshot = edited_theme->duplicate();
  628. ProgressDialog::get_singleton()->add_task("import_theme_items", TTR("Importing Theme Items"), selected_items.size() + 2);
  629. int idx = 0;
  630. for (KeyValue<ThemeItem, ItemCheckedState> &E : selected_items) {
  631. // Arbitrary number of items to skip from reporting.
  632. // Reduces the number of UI updates that this causes when copying large themes.
  633. if (idx % 10 == 0) {
  634. Array arr = { idx + 1, selected_items.size() };
  635. ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Importing items {n}/{n}").format(arr, "{n}"), idx);
  636. }
  637. ItemCheckedState cs = E.value;
  638. ThemeItem ti = E.key;
  639. if (cs == SELECT_IMPORT_DEFINITION || cs == SELECT_IMPORT_FULL) {
  640. Variant item_value = Variant();
  641. if (cs == SELECT_IMPORT_FULL) {
  642. item_value = base_theme->get_theme_item(ti.data_type, ti.item_name, ti.type_name);
  643. } else {
  644. switch (ti.data_type) {
  645. case Theme::DATA_TYPE_COLOR:
  646. item_value = Color();
  647. break;
  648. case Theme::DATA_TYPE_CONSTANT:
  649. item_value = 0;
  650. break;
  651. case Theme::DATA_TYPE_FONT:
  652. item_value = Ref<Font>();
  653. break;
  654. case Theme::DATA_TYPE_FONT_SIZE:
  655. item_value = -1;
  656. break;
  657. case Theme::DATA_TYPE_ICON:
  658. item_value = Ref<Texture2D>();
  659. break;
  660. case Theme::DATA_TYPE_STYLEBOX:
  661. item_value = Ref<StyleBox>();
  662. break;
  663. case Theme::DATA_TYPE_MAX:
  664. break; // Can't happen, but silences warning.
  665. }
  666. }
  667. new_snapshot->set_theme_item(ti.data_type, ti.item_name, ti.type_name, item_value);
  668. }
  669. idx++;
  670. }
  671. // Allow changes to be reported now that the operation is finished.
  672. ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Updating the editor"), idx++);
  673. // Make sure the task is not ended before the editor freezes to update the Inspector.
  674. ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Finalizing"), idx++);
  675. ProgressDialog::get_singleton()->end_task("import_theme_items");
  676. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  677. ur->create_action(TTR("Import Theme Items"));
  678. ur->add_do_method(*edited_theme, "clear");
  679. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  680. ur->add_undo_method(*edited_theme, "clear");
  681. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  682. ur->add_do_method(this, "emit_signal", SNAME("items_imported"));
  683. ur->add_undo_method(this, "emit_signal", SNAME("items_imported"));
  684. ur->commit_action();
  685. }
  686. void ThemeItemImportTree::set_edited_theme(const Ref<Theme> &p_theme) {
  687. edited_theme = p_theme;
  688. }
  689. void ThemeItemImportTree::set_base_theme(const Ref<Theme> &p_theme) {
  690. base_theme = p_theme;
  691. }
  692. void ThemeItemImportTree::reset_item_tree() {
  693. import_items_filter->clear();
  694. selected_items.clear();
  695. total_selected_colors_label->hide();
  696. total_selected_constants_label->hide();
  697. total_selected_fonts_label->hide();
  698. total_selected_font_sizes_label->hide();
  699. total_selected_icons_label->hide();
  700. total_selected_styleboxes_label->hide();
  701. _update_items_tree();
  702. }
  703. bool ThemeItemImportTree::has_selected_items() const {
  704. return (selected_items.size() > 0);
  705. }
  706. void ThemeItemImportTree::_notification(int p_what) {
  707. switch (p_what) {
  708. case NOTIFICATION_THEME_CHANGED: {
  709. select_icons_warning_icon->set_texture(get_editor_theme_icon(SNAME("StatusWarning")));
  710. select_icons_warning->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  711. import_items_filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  712. // Bottom panel buttons.
  713. import_collapse_types_button->set_button_icon(get_editor_theme_icon(SNAME("CollapseTree")));
  714. import_expand_types_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandTree")));
  715. import_select_all_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  716. import_select_full_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  717. import_deselect_all_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  718. // Side panel buttons.
  719. select_colors_icon->set_texture(get_editor_theme_icon(SNAME("Color")));
  720. deselect_all_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  721. select_all_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  722. select_full_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  723. select_constants_icon->set_texture(get_editor_theme_icon(SNAME("MemberConstant")));
  724. deselect_all_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  725. select_all_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  726. select_full_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  727. select_fonts_icon->set_texture(get_editor_theme_icon(SNAME("FontItem")));
  728. deselect_all_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  729. select_all_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  730. select_full_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  731. select_font_sizes_icon->set_texture(get_editor_theme_icon(SNAME("FontSize")));
  732. deselect_all_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  733. select_all_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  734. select_full_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  735. select_icons_icon->set_texture(get_editor_theme_icon(SNAME("ImageTexture")));
  736. deselect_all_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  737. select_all_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  738. select_full_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  739. select_styleboxes_icon->set_texture(get_editor_theme_icon(SNAME("StyleBoxFlat")));
  740. deselect_all_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll")));
  741. select_all_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll")));
  742. select_full_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull")));
  743. } break;
  744. }
  745. }
  746. void ThemeItemImportTree::_bind_methods() {
  747. ADD_SIGNAL(MethodInfo("items_imported"));
  748. }
  749. ThemeItemImportTree::ThemeItemImportTree() {
  750. import_items_filter = memnew(LineEdit);
  751. import_items_filter->set_placeholder(TTR("Filter Items"));
  752. import_items_filter->set_clear_button_enabled(true);
  753. add_child(import_items_filter);
  754. import_items_filter->connect(SceneStringName(text_changed), callable_mp(this, &ThemeItemImportTree::_filter_text_changed));
  755. HBoxContainer *import_main_hb = memnew(HBoxContainer);
  756. import_main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  757. add_child(import_main_hb);
  758. import_items_tree = memnew(Tree);
  759. import_items_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  760. import_items_tree->set_hide_root(true);
  761. import_items_tree->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  762. import_main_hb->add_child(import_items_tree);
  763. import_items_tree->connect("item_edited", callable_mp(this, &ThemeItemImportTree::_tree_item_edited));
  764. import_items_tree->connect("check_propagated_to_item", callable_mp(this, &ThemeItemImportTree::_check_propagated_to_tree_item));
  765. import_items_tree->set_columns(3);
  766. import_items_tree->set_column_titles_visible(true);
  767. import_items_tree->set_column_title(IMPORT_ITEM, TTR("Import"));
  768. import_items_tree->set_column_title(IMPORT_ITEM_DATA, TTR("With Data"));
  769. import_items_tree->set_column_expand(0, true);
  770. import_items_tree->set_column_clip_content(0, true);
  771. import_items_tree->set_column_expand(IMPORT_ITEM, false);
  772. import_items_tree->set_column_expand(IMPORT_ITEM_DATA, false);
  773. import_items_tree->set_column_custom_minimum_width(0, 160 * EDSCALE);
  774. import_items_tree->set_column_custom_minimum_width(IMPORT_ITEM, 80 * EDSCALE);
  775. import_items_tree->set_column_custom_minimum_width(IMPORT_ITEM_DATA, 80 * EDSCALE);
  776. import_items_tree->set_column_clip_content(1, true);
  777. import_items_tree->set_column_clip_content(2, true);
  778. import_items_tree->set_theme_type_variation("TreeSecondary");
  779. ScrollContainer *import_bulk_sc = memnew(ScrollContainer);
  780. import_bulk_sc->set_custom_minimum_size(Size2(260.0, 0.0) * EDSCALE);
  781. import_bulk_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  782. import_bulk_sc->set_theme_type_variation("ScrollContainerSecondary");
  783. import_main_hb->add_child(import_bulk_sc);
  784. VBoxContainer *import_bulk_vb = memnew(VBoxContainer);
  785. import_bulk_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  786. import_bulk_sc->add_child(import_bulk_vb);
  787. Label *import_bulk_label = memnew(Label);
  788. import_bulk_label->set_text(TTR("Select by data type:"));
  789. import_bulk_vb->add_child(import_bulk_label);
  790. select_colors_icon = memnew(TextureRect);
  791. select_colors_label = memnew(Label);
  792. deselect_all_colors_button = memnew(Button);
  793. select_all_colors_button = memnew(Button);
  794. select_full_colors_button = memnew(Button);
  795. total_selected_colors_label = memnew(Label);
  796. select_constants_icon = memnew(TextureRect);
  797. select_constants_label = memnew(Label);
  798. deselect_all_constants_button = memnew(Button);
  799. select_all_constants_button = memnew(Button);
  800. select_full_constants_button = memnew(Button);
  801. total_selected_constants_label = memnew(Label);
  802. select_fonts_icon = memnew(TextureRect);
  803. select_fonts_label = memnew(Label);
  804. deselect_all_fonts_button = memnew(Button);
  805. select_all_fonts_button = memnew(Button);
  806. select_full_fonts_button = memnew(Button);
  807. total_selected_fonts_label = memnew(Label);
  808. select_font_sizes_icon = memnew(TextureRect);
  809. select_font_sizes_label = memnew(Label);
  810. deselect_all_font_sizes_button = memnew(Button);
  811. select_all_font_sizes_button = memnew(Button);
  812. select_full_font_sizes_button = memnew(Button);
  813. total_selected_font_sizes_label = memnew(Label);
  814. select_icons_icon = memnew(TextureRect);
  815. select_icons_label = memnew(Label);
  816. deselect_all_icons_button = memnew(Button);
  817. select_all_icons_button = memnew(Button);
  818. select_full_icons_button = memnew(Button);
  819. total_selected_icons_label = memnew(Label);
  820. select_styleboxes_icon = memnew(TextureRect);
  821. select_styleboxes_label = memnew(Label);
  822. deselect_all_styleboxes_button = memnew(Button);
  823. select_all_styleboxes_button = memnew(Button);
  824. select_full_styleboxes_button = memnew(Button);
  825. total_selected_styleboxes_label = memnew(Label);
  826. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  827. Theme::DataType dt = (Theme::DataType)i;
  828. TextureRect *select_items_icon = nullptr;
  829. Label *select_items_label = nullptr;
  830. Button *deselect_all_items_button = nullptr;
  831. Button *select_all_items_button = nullptr;
  832. Button *select_full_items_button = nullptr;
  833. Label *total_selected_items_label = nullptr;
  834. String items_title;
  835. String select_all_items_tooltip;
  836. String select_full_items_tooltip;
  837. String deselect_all_items_tooltip;
  838. switch (dt) {
  839. case Theme::DATA_TYPE_COLOR:
  840. select_items_icon = select_colors_icon;
  841. select_items_label = select_colors_label;
  842. deselect_all_items_button = deselect_all_colors_button;
  843. select_all_items_button = select_all_colors_button;
  844. select_full_items_button = select_full_colors_button;
  845. total_selected_items_label = total_selected_colors_label;
  846. items_title = TTR("Colors");
  847. select_all_items_tooltip = TTR("Select all visible color items.");
  848. select_full_items_tooltip = TTR("Select all visible color items and their data.");
  849. deselect_all_items_tooltip = TTR("Deselect all visible color items.");
  850. break;
  851. case Theme::DATA_TYPE_CONSTANT:
  852. select_items_icon = select_constants_icon;
  853. select_items_label = select_constants_label;
  854. deselect_all_items_button = deselect_all_constants_button;
  855. select_all_items_button = select_all_constants_button;
  856. select_full_items_button = select_full_constants_button;
  857. total_selected_items_label = total_selected_constants_label;
  858. items_title = TTR("Constants");
  859. select_all_items_tooltip = TTR("Select all visible constant items.");
  860. select_full_items_tooltip = TTR("Select all visible constant items and their data.");
  861. deselect_all_items_tooltip = TTR("Deselect all visible constant items.");
  862. break;
  863. case Theme::DATA_TYPE_FONT:
  864. select_items_icon = select_fonts_icon;
  865. select_items_label = select_fonts_label;
  866. deselect_all_items_button = deselect_all_fonts_button;
  867. select_all_items_button = select_all_fonts_button;
  868. select_full_items_button = select_full_fonts_button;
  869. total_selected_items_label = total_selected_fonts_label;
  870. items_title = TTR("Fonts");
  871. select_all_items_tooltip = TTR("Select all visible font items.");
  872. select_full_items_tooltip = TTR("Select all visible font items and their data.");
  873. deselect_all_items_tooltip = TTR("Deselect all visible font items.");
  874. break;
  875. case Theme::DATA_TYPE_FONT_SIZE:
  876. select_items_icon = select_font_sizes_icon;
  877. select_items_label = select_font_sizes_label;
  878. deselect_all_items_button = deselect_all_font_sizes_button;
  879. select_all_items_button = select_all_font_sizes_button;
  880. select_full_items_button = select_full_font_sizes_button;
  881. total_selected_items_label = total_selected_font_sizes_label;
  882. items_title = TTR("Font sizes");
  883. select_all_items_tooltip = TTR("Select all visible font size items.");
  884. select_full_items_tooltip = TTR("Select all visible font size items and their data.");
  885. deselect_all_items_tooltip = TTR("Deselect all visible font size items.");
  886. break;
  887. case Theme::DATA_TYPE_ICON:
  888. select_items_icon = select_icons_icon;
  889. select_items_label = select_icons_label;
  890. deselect_all_items_button = deselect_all_icons_button;
  891. select_all_items_button = select_all_icons_button;
  892. select_full_items_button = select_full_icons_button;
  893. total_selected_items_label = total_selected_icons_label;
  894. items_title = TTR("Icons");
  895. select_all_items_tooltip = TTR("Select all visible icon items.");
  896. select_full_items_tooltip = TTR("Select all visible icon items and their data.");
  897. deselect_all_items_tooltip = TTR("Deselect all visible icon items.");
  898. break;
  899. case Theme::DATA_TYPE_STYLEBOX:
  900. select_items_icon = select_styleboxes_icon;
  901. select_items_label = select_styleboxes_label;
  902. deselect_all_items_button = deselect_all_styleboxes_button;
  903. select_all_items_button = select_all_styleboxes_button;
  904. select_full_items_button = select_full_styleboxes_button;
  905. total_selected_items_label = total_selected_styleboxes_label;
  906. items_title = TTR("Styleboxes");
  907. select_all_items_tooltip = TTR("Select all visible stylebox items.");
  908. select_full_items_tooltip = TTR("Select all visible stylebox items and their data.");
  909. deselect_all_items_tooltip = TTR("Deselect all visible stylebox items.");
  910. break;
  911. case Theme::DATA_TYPE_MAX:
  912. continue; // Can't happen, but silences warning.
  913. }
  914. if (i > 0) {
  915. import_bulk_vb->add_child(memnew(HSeparator));
  916. }
  917. HBoxContainer *all_set = memnew(HBoxContainer);
  918. import_bulk_vb->add_child(all_set);
  919. HBoxContainer *label_set = memnew(HBoxContainer);
  920. label_set->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  921. all_set->add_child(label_set);
  922. select_items_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  923. label_set->add_child(select_items_icon);
  924. select_items_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  925. select_items_label->set_clip_text(true);
  926. select_items_label->set_text(items_title);
  927. label_set->add_child(select_items_label);
  928. HBoxContainer *button_set = memnew(HBoxContainer);
  929. button_set->set_alignment(BoxContainer::ALIGNMENT_END);
  930. all_set->add_child(button_set);
  931. select_all_items_button->set_flat(true);
  932. select_all_items_button->set_tooltip_text(select_all_items_tooltip);
  933. button_set->add_child(select_all_items_button);
  934. select_all_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_all_data_type_pressed).bind(i));
  935. select_full_items_button->set_flat(true);
  936. select_full_items_button->set_tooltip_text(select_full_items_tooltip);
  937. button_set->add_child(select_full_items_button);
  938. select_full_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_full_data_type_pressed).bind(i));
  939. deselect_all_items_button->set_flat(true);
  940. deselect_all_items_button->set_tooltip_text(deselect_all_items_tooltip);
  941. button_set->add_child(deselect_all_items_button);
  942. deselect_all_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_deselect_all_data_type_pressed).bind(i));
  943. total_selected_items_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  944. total_selected_items_label->hide();
  945. import_bulk_vb->add_child(total_selected_items_label);
  946. if (dt == Theme::DATA_TYPE_ICON) {
  947. select_icons_warning_hb = memnew(HBoxContainer);
  948. import_bulk_vb->add_child(select_icons_warning_hb);
  949. select_icons_warning_icon = memnew(TextureRect);
  950. select_icons_warning_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  951. select_icons_warning_hb->add_child(select_icons_warning_icon);
  952. select_icons_warning = memnew(Label);
  953. select_icons_warning->set_text(TTR("Caution: Adding icon data may considerably increase the size of your Theme resource."));
  954. select_icons_warning->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  955. select_icons_warning->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  956. select_icons_warning_hb->add_child(select_icons_warning);
  957. }
  958. }
  959. HBoxContainer *import_buttons = memnew(HBoxContainer);
  960. add_child(import_buttons);
  961. import_collapse_types_button = memnew(Button);
  962. import_collapse_types_button->set_flat(true);
  963. import_collapse_types_button->set_tooltip_text(TTR("Collapse types."));
  964. import_buttons->add_child(import_collapse_types_button);
  965. import_collapse_types_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_toggle_type_items).bind(true));
  966. import_expand_types_button = memnew(Button);
  967. import_expand_types_button->set_flat(true);
  968. import_expand_types_button->set_tooltip_text(TTR("Expand types."));
  969. import_buttons->add_child(import_expand_types_button);
  970. import_expand_types_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_toggle_type_items).bind(false));
  971. import_buttons->add_child(memnew(VSeparator));
  972. import_select_all_button = memnew(Button);
  973. import_select_all_button->set_flat(true);
  974. import_select_all_button->set_text(TTR("Select All"));
  975. import_select_all_button->set_tooltip_text(TTR("Select all Theme items."));
  976. import_buttons->add_child(import_select_all_button);
  977. import_select_all_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_all_items_pressed));
  978. import_select_full_button = memnew(Button);
  979. import_select_full_button->set_flat(true);
  980. import_select_full_button->set_text(TTR("Select With Data"));
  981. import_select_full_button->set_tooltip_text(TTR("Select all Theme items with item data."));
  982. import_buttons->add_child(import_select_full_button);
  983. import_select_full_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_full_items_pressed));
  984. import_deselect_all_button = memnew(Button);
  985. import_deselect_all_button->set_flat(true);
  986. import_deselect_all_button->set_text(TTR("Deselect All"));
  987. import_deselect_all_button->set_tooltip_text(TTR("Deselect all Theme items."));
  988. import_buttons->add_child(import_deselect_all_button);
  989. import_deselect_all_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_deselect_all_items_pressed));
  990. import_buttons->add_spacer();
  991. Button *import_add_selected_button = memnew(Button);
  992. import_add_selected_button->set_text(TTR("Import Selected"));
  993. import_buttons->add_child(import_add_selected_button);
  994. import_add_selected_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_import_selected));
  995. }
  996. ///////////////////////
  997. void ThemeItemEditorDialog::ok_pressed() {
  998. if (import_default_theme_items->has_selected_items() || import_editor_theme_items->has_selected_items() || import_other_theme_items->has_selected_items()) {
  999. confirm_closing_dialog->set_text(TTR("Import Items tab has some items selected. Selection will be lost upon closing this window.\nClose anyway?"));
  1000. confirm_closing_dialog->popup_centered(Size2(380, 120) * EDSCALE);
  1001. return;
  1002. }
  1003. hide();
  1004. }
  1005. void ThemeItemEditorDialog::_close_dialog() {
  1006. hide();
  1007. }
  1008. void ThemeItemEditorDialog::_dialog_about_to_show() {
  1009. ERR_FAIL_COND_MSG(edited_theme.is_null(), "Invalid state of the Theme Editor; the Theme resource is missing.");
  1010. _update_edit_types();
  1011. import_default_theme_items->set_edited_theme(edited_theme);
  1012. import_default_theme_items->set_base_theme(ThemeDB::get_singleton()->get_default_theme());
  1013. import_default_theme_items->reset_item_tree();
  1014. import_editor_theme_items->set_edited_theme(edited_theme);
  1015. import_editor_theme_items->set_base_theme(EditorNode::get_singleton()->get_editor_theme());
  1016. import_editor_theme_items->reset_item_tree();
  1017. import_other_theme_items->set_edited_theme(edited_theme);
  1018. import_other_theme_items->reset_item_tree();
  1019. }
  1020. void ThemeItemEditorDialog::_update_edit_types() {
  1021. Ref<Theme> base_theme = ThemeDB::get_singleton()->get_default_theme();
  1022. List<StringName> theme_types;
  1023. edited_theme->get_type_list(&theme_types);
  1024. theme_types.sort_custom<StringName::AlphCompare>();
  1025. bool item_reselected = false;
  1026. edit_type_list->clear();
  1027. TreeItem *list_root = edit_type_list->create_item();
  1028. for (const StringName &E : theme_types) {
  1029. Ref<Texture2D> item_icon;
  1030. if (E == "") {
  1031. item_icon = get_editor_theme_icon(SNAME("NodeDisabled"));
  1032. } else {
  1033. item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled");
  1034. }
  1035. TreeItem *list_item = edit_type_list->create_item(list_root);
  1036. list_item->set_text(0, E);
  1037. list_item->set_metadata(0, E);
  1038. list_item->set_editable(0, true);
  1039. list_item->set_icon(0, item_icon);
  1040. list_item->add_button(0, get_editor_theme_icon(SNAME("Remove")), TYPES_TREE_REMOVE_ITEM, false, TTRC("Remove Type"));
  1041. if (E == edited_item_type) {
  1042. list_item->select(0);
  1043. item_reselected = true;
  1044. }
  1045. }
  1046. if (!item_reselected) {
  1047. edited_item_type = "";
  1048. if (list_root->get_child_count() > 0) {
  1049. list_root->get_child(0)->select(0);
  1050. }
  1051. }
  1052. List<StringName> default_types;
  1053. base_theme->get_type_list(&default_types);
  1054. default_types.sort_custom<StringName::AlphCompare>();
  1055. String selected_type = "";
  1056. TreeItem *selected_item = edit_type_list->get_selected();
  1057. if (selected_item) {
  1058. selected_type = selected_item->get_text(0);
  1059. edit_items_add_color->set_disabled(false);
  1060. edit_items_add_constant->set_disabled(false);
  1061. edit_items_add_font->set_disabled(false);
  1062. edit_items_add_font_size->set_disabled(false);
  1063. edit_items_add_icon->set_disabled(false);
  1064. edit_items_add_stylebox->set_disabled(false);
  1065. edit_items_remove_class->set_disabled(false);
  1066. edit_items_remove_custom->set_disabled(false);
  1067. edit_items_remove_all->set_disabled(false);
  1068. edit_items_message->set_text("");
  1069. edit_items_message->hide();
  1070. } else {
  1071. edit_items_add_color->set_disabled(true);
  1072. edit_items_add_constant->set_disabled(true);
  1073. edit_items_add_font->set_disabled(true);
  1074. edit_items_add_font_size->set_disabled(true);
  1075. edit_items_add_icon->set_disabled(true);
  1076. edit_items_add_stylebox->set_disabled(true);
  1077. edit_items_remove_class->set_disabled(true);
  1078. edit_items_remove_custom->set_disabled(true);
  1079. edit_items_remove_all->set_disabled(true);
  1080. edit_items_message->set_text(TTR("Select a theme type from the list to edit its items.\nYou can add a custom type or import a type with its items from another theme."));
  1081. edit_items_message->show();
  1082. }
  1083. _update_edit_item_tree(selected_type);
  1084. }
  1085. void ThemeItemEditorDialog::_edited_type_selected() {
  1086. TreeItem *selected_item = edit_type_list->get_selected();
  1087. String selected_type = selected_item->get_text(0);
  1088. _update_edit_item_tree(selected_type);
  1089. }
  1090. void ThemeItemEditorDialog::_edited_type_edited() {
  1091. TreeItem *edited_item = edit_type_list->get_selected();
  1092. const String old_type_name = edited_item->get_metadata(0);
  1093. const String &new_type_name = Theme::validate_type_name(edited_item->get_text(0));
  1094. if (old_type_name == new_type_name) {
  1095. edited_item->set_text(0, old_type_name);
  1096. return;
  1097. }
  1098. List<StringName> theme_types;
  1099. edited_theme->get_type_list(&theme_types);
  1100. if (theme_types.find(new_type_name) != nullptr) {
  1101. edited_item->set_text(0, old_type_name);
  1102. return;
  1103. }
  1104. // The list will be recreated, but let's update the item just in case.
  1105. edited_item->set_metadata(0, new_type_name);
  1106. edited_item->set_text(0, new_type_name);
  1107. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1108. ur->create_action(TTR("Rename Theme Type"));
  1109. _rename_theme_type(ur, *edited_theme, old_type_name, new_type_name);
  1110. // Set `edited_item_type`.
  1111. ur->add_do_method(this, "_update_edit_item_tree", new_type_name);
  1112. ur->add_undo_method(this, "_update_edit_item_tree", old_type_name);
  1113. ur->add_do_method(this, "_update_edit_types");
  1114. ur->add_undo_method(this, "_update_edit_types");
  1115. ur->commit_action();
  1116. }
  1117. void ThemeItemEditorDialog::_edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  1118. if (p_button != MouseButton::LEFT) {
  1119. return;
  1120. }
  1121. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  1122. if (!item) {
  1123. return;
  1124. }
  1125. switch (p_id) {
  1126. case TYPES_TREE_REMOVE_ITEM: {
  1127. String type_name = item->get_text(0);
  1128. _remove_theme_type(type_name);
  1129. } break;
  1130. }
  1131. }
  1132. void ThemeItemEditorDialog::_update_edit_item_tree(String p_item_type) {
  1133. edited_item_type = p_item_type;
  1134. edit_items_tree->clear();
  1135. TreeItem *root = edit_items_tree->create_item();
  1136. List<StringName> names;
  1137. bool has_any_items = false;
  1138. { // Colors.
  1139. names.clear();
  1140. edited_theme->get_color_list(p_item_type, &names);
  1141. if (names.size() > 0) {
  1142. TreeItem *color_root = edit_items_tree->create_item(root);
  1143. color_root->set_metadata(0, Theme::DATA_TYPE_COLOR);
  1144. color_root->set_icon(0, get_editor_theme_icon(SNAME("Color")));
  1145. color_root->set_text(0, TTR("Colors"));
  1146. color_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Color Items"));
  1147. names.sort_custom<StringName::AlphCompare>();
  1148. for (const StringName &E : names) {
  1149. TreeItem *item = edit_items_tree->create_item(color_root);
  1150. item->set_text(0, E);
  1151. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1152. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1153. }
  1154. has_any_items = true;
  1155. }
  1156. }
  1157. { // Constants.
  1158. names.clear();
  1159. edited_theme->get_constant_list(p_item_type, &names);
  1160. if (names.size() > 0) {
  1161. TreeItem *constant_root = edit_items_tree->create_item(root);
  1162. constant_root->set_metadata(0, Theme::DATA_TYPE_CONSTANT);
  1163. constant_root->set_icon(0, get_editor_theme_icon(SNAME("MemberConstant")));
  1164. constant_root->set_text(0, TTR("Constants"));
  1165. constant_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Constant Items"));
  1166. names.sort_custom<StringName::AlphCompare>();
  1167. for (const StringName &E : names) {
  1168. TreeItem *item = edit_items_tree->create_item(constant_root);
  1169. item->set_text(0, E);
  1170. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1171. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1172. }
  1173. has_any_items = true;
  1174. }
  1175. }
  1176. { // Fonts.
  1177. names.clear();
  1178. edited_theme->get_font_list(p_item_type, &names);
  1179. if (names.size() > 0) {
  1180. TreeItem *font_root = edit_items_tree->create_item(root);
  1181. font_root->set_metadata(0, Theme::DATA_TYPE_FONT);
  1182. font_root->set_icon(0, get_editor_theme_icon(SNAME("FontItem")));
  1183. font_root->set_text(0, TTR("Fonts"));
  1184. font_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Font Items"));
  1185. names.sort_custom<StringName::AlphCompare>();
  1186. for (const StringName &E : names) {
  1187. TreeItem *item = edit_items_tree->create_item(font_root);
  1188. item->set_text(0, E);
  1189. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1190. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1191. }
  1192. has_any_items = true;
  1193. }
  1194. }
  1195. { // Font sizes.
  1196. names.clear();
  1197. edited_theme->get_font_size_list(p_item_type, &names);
  1198. if (names.size() > 0) {
  1199. TreeItem *font_size_root = edit_items_tree->create_item(root);
  1200. font_size_root->set_metadata(0, Theme::DATA_TYPE_FONT_SIZE);
  1201. font_size_root->set_icon(0, get_editor_theme_icon(SNAME("FontSize")));
  1202. font_size_root->set_text(0, TTR("Font Sizes"));
  1203. font_size_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Font Size Items"));
  1204. names.sort_custom<StringName::AlphCompare>();
  1205. for (const StringName &E : names) {
  1206. TreeItem *item = edit_items_tree->create_item(font_size_root);
  1207. item->set_text(0, E);
  1208. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1209. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1210. }
  1211. has_any_items = true;
  1212. }
  1213. }
  1214. { // Icons.
  1215. names.clear();
  1216. edited_theme->get_icon_list(p_item_type, &names);
  1217. if (names.size() > 0) {
  1218. TreeItem *icon_root = edit_items_tree->create_item(root);
  1219. icon_root->set_metadata(0, Theme::DATA_TYPE_ICON);
  1220. icon_root->set_icon(0, get_editor_theme_icon(SNAME("ImageTexture")));
  1221. icon_root->set_text(0, TTR("Icons"));
  1222. icon_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Icon Items"));
  1223. names.sort_custom<StringName::AlphCompare>();
  1224. for (const StringName &E : names) {
  1225. TreeItem *item = edit_items_tree->create_item(icon_root);
  1226. item->set_text(0, E);
  1227. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1228. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1229. }
  1230. has_any_items = true;
  1231. }
  1232. }
  1233. { // Styleboxes.
  1234. names.clear();
  1235. edited_theme->get_stylebox_list(p_item_type, &names);
  1236. if (names.size() > 0) {
  1237. TreeItem *stylebox_root = edit_items_tree->create_item(root);
  1238. stylebox_root->set_metadata(0, Theme::DATA_TYPE_STYLEBOX);
  1239. stylebox_root->set_icon(0, get_editor_theme_icon(SNAME("StyleBoxFlat")));
  1240. stylebox_root->set_text(0, TTR("Styleboxes"));
  1241. stylebox_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All StyleBox Items"));
  1242. names.sort_custom<StringName::AlphCompare>();
  1243. for (const StringName &E : names) {
  1244. TreeItem *item = edit_items_tree->create_item(stylebox_root);
  1245. item->set_text(0, E);
  1246. item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item"));
  1247. item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item"));
  1248. }
  1249. has_any_items = true;
  1250. }
  1251. }
  1252. // If some type is selected, but it doesn't seem to have any items, show a guiding message.
  1253. TreeItem *selected_item = edit_type_list->get_selected();
  1254. if (selected_item) {
  1255. if (!has_any_items) {
  1256. edit_items_message->set_text(TTR("This theme type is empty.\nAdd more items to it manually or by importing from another theme."));
  1257. edit_items_message->show();
  1258. } else {
  1259. edit_items_message->set_text("");
  1260. edit_items_message->hide();
  1261. }
  1262. }
  1263. }
  1264. void ThemeItemEditorDialog::_item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  1265. if (p_button != MouseButton::LEFT) {
  1266. return;
  1267. }
  1268. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  1269. if (!item) {
  1270. return;
  1271. }
  1272. switch (p_id) {
  1273. case ITEMS_TREE_RENAME_ITEM: {
  1274. String item_name = item->get_text(0);
  1275. int data_type = item->get_parent()->get_metadata(0);
  1276. _open_rename_theme_item_dialog((Theme::DataType)data_type, item_name);
  1277. _update_edit_item_tree(edited_item_type);
  1278. } break;
  1279. case ITEMS_TREE_REMOVE_ITEM: {
  1280. String item_name = item->get_text(0);
  1281. int data_type = item->get_parent()->get_metadata(0);
  1282. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1283. ur->create_action(TTR("Remove Theme Item"));
  1284. ur->add_do_method(*edited_theme, "clear_theme_item", (Theme::DataType)data_type, item_name, edited_item_type);
  1285. ur->add_undo_method(*edited_theme, "set_theme_item", (Theme::DataType)data_type, item_name, edited_item_type, edited_theme->get_theme_item((Theme::DataType)data_type, item_name, edited_item_type));
  1286. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1287. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1288. ur->commit_action();
  1289. } break;
  1290. case ITEMS_TREE_REMOVE_DATA_TYPE: {
  1291. int data_type = item->get_metadata(0);
  1292. _remove_data_type_items((Theme::DataType)data_type, edited_item_type);
  1293. } break;
  1294. }
  1295. }
  1296. void ThemeItemEditorDialog::_add_theme_type() {
  1297. const String &new_type_name = Theme::validate_type_name(edit_add_type_value->get_text());
  1298. edit_add_type_value->clear();
  1299. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1300. ur->create_action(TTR("Add Theme Type"));
  1301. ur->add_do_method(*edited_theme, "add_type", new_type_name);
  1302. ur->add_undo_method(*edited_theme, "remove_type", new_type_name);
  1303. ur->add_do_method(this, "_update_edit_types");
  1304. ur->add_undo_method(this, "_update_edit_types");
  1305. ur->commit_action();
  1306. }
  1307. void ThemeItemEditorDialog::_add_theme_item(Theme::DataType p_data_type, const String &p_item_name, const String &p_item_type) {
  1308. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1309. ur->create_action(TTR("Create Theme Item"));
  1310. switch (p_data_type) {
  1311. case Theme::DATA_TYPE_ICON:
  1312. ur->add_do_method(*edited_theme, "set_icon", p_item_name, p_item_type, Ref<Texture2D>());
  1313. ur->add_undo_method(*edited_theme, "clear_icon", p_item_name, p_item_type);
  1314. break;
  1315. case Theme::DATA_TYPE_STYLEBOX:
  1316. ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, p_item_type, Ref<StyleBox>());
  1317. ur->add_undo_method(*edited_theme, "clear_stylebox", p_item_name, p_item_type);
  1318. if (theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(p_item_name, p_item_type))) {
  1319. ur->add_undo_method(theme_type_editor, "_unpin_leading_stylebox");
  1320. }
  1321. break;
  1322. case Theme::DATA_TYPE_FONT:
  1323. ur->add_do_method(*edited_theme, "set_font", p_item_name, p_item_type, Ref<Font>());
  1324. ur->add_undo_method(*edited_theme, "clear_font", p_item_name, p_item_type);
  1325. break;
  1326. case Theme::DATA_TYPE_FONT_SIZE:
  1327. ur->add_do_method(*edited_theme, "set_font_size", p_item_name, p_item_type, -1);
  1328. ur->add_undo_method(*edited_theme, "clear_font_size", p_item_name, p_item_type);
  1329. break;
  1330. case Theme::DATA_TYPE_COLOR:
  1331. ur->add_do_method(*edited_theme, "set_color", p_item_name, p_item_type, Color());
  1332. ur->add_undo_method(*edited_theme, "clear_color", p_item_name, p_item_type);
  1333. break;
  1334. case Theme::DATA_TYPE_CONSTANT:
  1335. ur->add_do_method(*edited_theme, "set_constant", p_item_name, p_item_type, 0);
  1336. ur->add_undo_method(*edited_theme, "clear_constant", p_item_name, p_item_type);
  1337. break;
  1338. case Theme::DATA_TYPE_MAX:
  1339. break; // Can't happen, but silences warning.
  1340. }
  1341. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1342. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1343. ur->commit_action();
  1344. }
  1345. void ThemeItemEditorDialog::_remove_theme_type(const String &p_theme_type) {
  1346. Ref<Theme> old_snapshot = edited_theme->duplicate();
  1347. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1348. ur->create_action(TTR("Remove Theme Type"));
  1349. ur->add_do_method(*edited_theme, "remove_type", p_theme_type);
  1350. // If the type was empty, it cannot be restored with merge, but thankfully we can fake it.
  1351. ur->add_undo_method(*edited_theme, "add_type", p_theme_type);
  1352. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  1353. ur->add_do_method(this, "_update_edit_types");
  1354. ur->add_undo_method(this, "_update_edit_types");
  1355. ur->commit_action();
  1356. }
  1357. void ThemeItemEditorDialog::_remove_data_type_items(Theme::DataType p_data_type, String p_item_type) {
  1358. List<StringName> names;
  1359. Ref<Theme> old_snapshot = edited_theme->duplicate();
  1360. Ref<Theme> new_snapshot = edited_theme->duplicate();
  1361. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1362. ur->create_action(TTR("Remove Data Type Items From Theme"));
  1363. new_snapshot->get_theme_item_list(p_data_type, p_item_type, &names);
  1364. for (const StringName &E : names) {
  1365. new_snapshot->clear_theme_item(p_data_type, E, edited_item_type);
  1366. if (p_data_type == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, p_item_type))) {
  1367. ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox");
  1368. ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, p_item_type));
  1369. }
  1370. }
  1371. ur->add_do_method(*edited_theme, "clear");
  1372. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  1373. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  1374. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1375. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1376. ur->commit_action();
  1377. }
  1378. void ThemeItemEditorDialog::_remove_class_items() {
  1379. List<StringName> names;
  1380. Ref<Theme> old_snapshot = edited_theme->duplicate();
  1381. Ref<Theme> new_snapshot = edited_theme->duplicate();
  1382. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1383. ur->create_action(TTR("Remove Class Items From Theme"));
  1384. for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) {
  1385. Theme::DataType data_type = (Theme::DataType)dt;
  1386. names.clear();
  1387. ThemeDB::get_singleton()->get_default_theme()->get_theme_item_list(data_type, edited_item_type, &names);
  1388. for (const StringName &E : names) {
  1389. if (new_snapshot->has_theme_item_nocheck(data_type, E, edited_item_type)) {
  1390. new_snapshot->clear_theme_item(data_type, E, edited_item_type);
  1391. if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) {
  1392. ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox");
  1393. ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type));
  1394. }
  1395. }
  1396. }
  1397. }
  1398. ur->add_do_method(*edited_theme, "clear");
  1399. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  1400. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  1401. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1402. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1403. ur->commit_action();
  1404. }
  1405. void ThemeItemEditorDialog::_remove_custom_items() {
  1406. List<StringName> names;
  1407. Ref<Theme> old_snapshot = edited_theme->duplicate();
  1408. Ref<Theme> new_snapshot = edited_theme->duplicate();
  1409. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1410. ur->create_action(TTR("Remove Custom Items From Theme"));
  1411. for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) {
  1412. Theme::DataType data_type = (Theme::DataType)dt;
  1413. names.clear();
  1414. new_snapshot->get_theme_item_list(data_type, edited_item_type, &names);
  1415. for (const StringName &E : names) {
  1416. if (!ThemeDB::get_singleton()->get_default_theme()->has_theme_item_nocheck(data_type, E, edited_item_type)) {
  1417. new_snapshot->clear_theme_item(data_type, E, edited_item_type);
  1418. if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) {
  1419. ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox");
  1420. ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type));
  1421. }
  1422. }
  1423. }
  1424. }
  1425. ur->add_do_method(*edited_theme, "clear");
  1426. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  1427. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  1428. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1429. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1430. ur->commit_action();
  1431. }
  1432. void ThemeItemEditorDialog::_remove_all_items() {
  1433. List<StringName> names;
  1434. Ref<Theme> old_snapshot = edited_theme->duplicate();
  1435. Ref<Theme> new_snapshot = edited_theme->duplicate();
  1436. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1437. ur->create_action(TTR("Remove All Items From Theme"));
  1438. for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) {
  1439. Theme::DataType data_type = (Theme::DataType)dt;
  1440. names.clear();
  1441. new_snapshot->get_theme_item_list(data_type, edited_item_type, &names);
  1442. for (const StringName &E : names) {
  1443. new_snapshot->clear_theme_item(data_type, E, edited_item_type);
  1444. if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) {
  1445. ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox");
  1446. ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type));
  1447. }
  1448. }
  1449. }
  1450. ur->add_do_method(*edited_theme, "clear");
  1451. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  1452. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  1453. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1454. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1455. ur->commit_action();
  1456. }
  1457. void ThemeItemEditorDialog::_open_add_theme_item_dialog(int p_data_type) {
  1458. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  1459. item_popup_mode = CREATE_THEME_ITEM;
  1460. edit_item_data_type = (Theme::DataType)p_data_type;
  1461. switch (edit_item_data_type) {
  1462. case Theme::DATA_TYPE_COLOR:
  1463. edit_theme_item_dialog->set_title(TTR("Add Color Item"));
  1464. break;
  1465. case Theme::DATA_TYPE_CONSTANT:
  1466. edit_theme_item_dialog->set_title(TTR("Add Constant Item"));
  1467. break;
  1468. case Theme::DATA_TYPE_FONT:
  1469. edit_theme_item_dialog->set_title(TTR("Add Font Item"));
  1470. break;
  1471. case Theme::DATA_TYPE_FONT_SIZE:
  1472. edit_theme_item_dialog->set_title(TTR("Add Font Size Item"));
  1473. break;
  1474. case Theme::DATA_TYPE_ICON:
  1475. edit_theme_item_dialog->set_title(TTR("Add Icon Item"));
  1476. break;
  1477. case Theme::DATA_TYPE_STYLEBOX:
  1478. edit_theme_item_dialog->set_title(TTR("Add Stylebox Item"));
  1479. break;
  1480. case Theme::DATA_TYPE_MAX:
  1481. break; // Can't happen, but silences warning.
  1482. }
  1483. edit_theme_item_old_vb->hide();
  1484. theme_item_name->clear();
  1485. edit_theme_item_dialog->popup_centered(Size2(380, 110) * EDSCALE);
  1486. theme_item_name->grab_focus();
  1487. }
  1488. void ThemeItemEditorDialog::_open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name) {
  1489. ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds.");
  1490. item_popup_mode = RENAME_THEME_ITEM;
  1491. edit_item_data_type = p_data_type;
  1492. edit_item_old_name = p_item_name;
  1493. switch (edit_item_data_type) {
  1494. case Theme::DATA_TYPE_COLOR:
  1495. edit_theme_item_dialog->set_title(TTR("Rename Color Item"));
  1496. break;
  1497. case Theme::DATA_TYPE_CONSTANT:
  1498. edit_theme_item_dialog->set_title(TTR("Rename Constant Item"));
  1499. break;
  1500. case Theme::DATA_TYPE_FONT:
  1501. edit_theme_item_dialog->set_title(TTR("Rename Font Item"));
  1502. break;
  1503. case Theme::DATA_TYPE_FONT_SIZE:
  1504. edit_theme_item_dialog->set_title(TTR("Rename Font Size Item"));
  1505. break;
  1506. case Theme::DATA_TYPE_ICON:
  1507. edit_theme_item_dialog->set_title(TTR("Rename Icon Item"));
  1508. break;
  1509. case Theme::DATA_TYPE_STYLEBOX:
  1510. edit_theme_item_dialog->set_title(TTR("Rename Stylebox Item"));
  1511. break;
  1512. case Theme::DATA_TYPE_MAX:
  1513. break; // Can't happen, but silences warning.
  1514. }
  1515. edit_theme_item_old_vb->show();
  1516. theme_item_old_name->set_text(p_item_name);
  1517. theme_item_name->set_text(p_item_name);
  1518. edit_theme_item_dialog->popup_centered(Size2(380, 140) * EDSCALE);
  1519. theme_item_name->grab_focus();
  1520. }
  1521. void ThemeItemEditorDialog::_confirm_edit_theme_item() {
  1522. const String new_item_name = theme_item_name->get_text().strip_edges().validate_ascii_identifier();
  1523. if (item_popup_mode == CREATE_THEME_ITEM) {
  1524. _add_theme_item(edit_item_data_type, new_item_name, edited_item_type);
  1525. } else if (item_popup_mode == RENAME_THEME_ITEM) {
  1526. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1527. ur->create_action(TTR("Rename Theme Item"));
  1528. ur->add_do_method(*edited_theme, "rename_theme_item", edit_item_data_type, edit_item_old_name, new_item_name, edited_item_type);
  1529. ur->add_undo_method(*edited_theme, "rename_theme_item", edit_item_data_type, new_item_name, edit_item_old_name, edited_item_type);
  1530. ur->add_do_method(this, "_update_edit_item_tree", edited_item_type);
  1531. ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type);
  1532. ur->commit_action();
  1533. }
  1534. item_popup_mode = ITEM_POPUP_MODE_MAX;
  1535. edit_item_data_type = Theme::DATA_TYPE_MAX;
  1536. edit_item_old_name = "";
  1537. }
  1538. void ThemeItemEditorDialog::_edit_theme_item_gui_input(const Ref<InputEvent> &p_event) {
  1539. Ref<InputEventKey> k = p_event;
  1540. if (k.is_valid()) {
  1541. if (!k->is_pressed()) {
  1542. return;
  1543. }
  1544. if (k->is_action_pressed(SNAME("ui_text_submit"), false, true)) {
  1545. _confirm_edit_theme_item();
  1546. edit_theme_item_dialog->hide();
  1547. edit_theme_item_dialog->set_input_as_handled();
  1548. } else if (k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  1549. edit_theme_item_dialog->hide();
  1550. edit_theme_item_dialog->set_input_as_handled();
  1551. }
  1552. }
  1553. }
  1554. void ThemeItemEditorDialog::_open_select_another_theme() {
  1555. import_another_theme_dialog->popup_file_dialog();
  1556. }
  1557. void ThemeItemEditorDialog::_select_another_theme_cbk(const String &p_path) {
  1558. Ref<Theme> loaded_theme = ResourceLoader::load(p_path);
  1559. if (loaded_theme.is_null()) {
  1560. EditorNode::get_singleton()->show_warning(TTR("Invalid file, not a Theme resource."));
  1561. return;
  1562. }
  1563. if (loaded_theme == edited_theme) {
  1564. EditorNode::get_singleton()->show_warning(TTR("Invalid file, same as the edited Theme resource."));
  1565. return;
  1566. }
  1567. import_another_theme_value->set_text(p_path);
  1568. import_other_theme_items->set_base_theme(loaded_theme);
  1569. import_other_theme_items->reset_item_tree();
  1570. }
  1571. void ThemeItemEditorDialog::_notification(int p_what) {
  1572. switch (p_what) {
  1573. case NOTIFICATION_POSTINITIALIZE: {
  1574. connect("about_to_popup", callable_mp(this, &ThemeItemEditorDialog::_dialog_about_to_show));
  1575. } break;
  1576. case NOTIFICATION_THEME_CHANGED: {
  1577. edit_items_add_color->set_button_icon(get_editor_theme_icon(SNAME("Color")));
  1578. edit_items_add_constant->set_button_icon(get_editor_theme_icon(SNAME("MemberConstant")));
  1579. edit_items_add_font->set_button_icon(get_editor_theme_icon(SNAME("FontItem")));
  1580. edit_items_add_font_size->set_button_icon(get_editor_theme_icon(SNAME("FontSize")));
  1581. edit_items_add_icon->set_button_icon(get_editor_theme_icon(SNAME("ImageTexture")));
  1582. edit_items_add_stylebox->set_button_icon(get_editor_theme_icon(SNAME("StyleBoxFlat")));
  1583. edit_items_remove_class->set_button_icon(get_editor_theme_icon(SNAME("Control")));
  1584. edit_items_remove_custom->set_button_icon(get_editor_theme_icon(SNAME("ThemeRemoveCustomItems")));
  1585. edit_items_remove_all->set_button_icon(get_editor_theme_icon(SNAME("ThemeRemoveAllItems")));
  1586. edit_add_type_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1587. import_another_theme_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
  1588. } break;
  1589. }
  1590. }
  1591. void ThemeItemEditorDialog::_bind_methods() {
  1592. ClassDB::bind_method(D_METHOD("_update_edit_types"), &ThemeItemEditorDialog::_update_edit_types);
  1593. ClassDB::bind_method(D_METHOD("_update_edit_item_tree"), &ThemeItemEditorDialog::_update_edit_item_tree);
  1594. }
  1595. void ThemeItemEditorDialog::set_edited_theme(const Ref<Theme> &p_theme) {
  1596. edited_theme = p_theme;
  1597. }
  1598. ThemeItemEditorDialog::ThemeItemEditorDialog(ThemeTypeEditor *p_theme_type_editor) {
  1599. set_title(TTR("Manage Theme Items"));
  1600. set_ok_button_text(TTR("Close"));
  1601. set_hide_on_ok(false); // Closing may require a confirmation in some cases.
  1602. theme_type_editor = p_theme_type_editor;
  1603. tc = memnew(TabContainer);
  1604. add_child(tc);
  1605. tc->set_theme_type_variation("TabContainerOdd");
  1606. // Edit Items tab.
  1607. HSplitContainer *edit_dialog_hs = memnew(HSplitContainer);
  1608. tc->add_child(edit_dialog_hs);
  1609. tc->set_tab_title(0, TTR("Edit Items"));
  1610. VBoxContainer *edit_dialog_side_vb = memnew(VBoxContainer);
  1611. edit_dialog_side_vb->set_custom_minimum_size(Size2(200.0, 0.0) * EDSCALE);
  1612. edit_dialog_hs->add_child(edit_dialog_side_vb);
  1613. Label *edit_type_label = memnew(Label);
  1614. edit_type_label->set_text(TTR("Types:"));
  1615. edit_dialog_side_vb->add_child(edit_type_label);
  1616. edit_type_list = memnew(Tree);
  1617. edit_type_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1618. edit_type_list->set_hide_root(true);
  1619. edit_type_list->set_hide_folding(true);
  1620. edit_type_list->set_columns(1);
  1621. edit_type_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1622. edit_dialog_side_vb->add_child(edit_type_list);
  1623. edit_type_list->connect(SceneStringName(item_selected), callable_mp(this, &ThemeItemEditorDialog::_edited_type_selected));
  1624. edit_type_list->connect("item_edited", callable_mp(this, &ThemeItemEditorDialog::_edited_type_edited));
  1625. edit_type_list->connect("button_clicked", callable_mp(this, &ThemeItemEditorDialog::_edited_type_button_pressed));
  1626. edit_type_list->set_theme_type_variation("TreeSecondary");
  1627. Label *edit_add_type_label = memnew(Label);
  1628. edit_add_type_label->set_text(TTR("Add Type:"));
  1629. edit_dialog_side_vb->add_child(edit_add_type_label);
  1630. HBoxContainer *edit_add_type_hb = memnew(HBoxContainer);
  1631. edit_dialog_side_vb->add_child(edit_add_type_hb);
  1632. edit_add_type_value = memnew(LineEdit);
  1633. edit_add_type_value->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1634. edit_add_type_value->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeItemEditorDialog::_add_theme_type).unbind(1));
  1635. edit_add_type_hb->add_child(edit_add_type_value);
  1636. edit_add_type_button = memnew(Button);
  1637. edit_add_type_hb->add_child(edit_add_type_button);
  1638. edit_add_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_add_theme_type));
  1639. VBoxContainer *edit_items_vb = memnew(VBoxContainer);
  1640. edit_items_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1641. edit_dialog_hs->add_child(edit_items_vb);
  1642. HBoxContainer *edit_items_toolbar = memnew(HBoxContainer);
  1643. edit_items_vb->add_child(edit_items_toolbar);
  1644. Label *edit_items_toolbar_add_label = memnew(Label);
  1645. edit_items_toolbar_add_label->set_text(TTR("Add Item:"));
  1646. edit_items_toolbar->add_child(edit_items_toolbar_add_label);
  1647. edit_items_add_color = memnew(Button);
  1648. edit_items_add_color->set_tooltip_text(TTR("Add Color Item"));
  1649. edit_items_add_color->set_flat(true);
  1650. edit_items_add_color->set_disabled(true);
  1651. edit_items_toolbar->add_child(edit_items_add_color);
  1652. edit_items_add_color->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_COLOR));
  1653. edit_items_add_constant = memnew(Button);
  1654. edit_items_add_constant->set_tooltip_text(TTR("Add Constant Item"));
  1655. edit_items_add_constant->set_flat(true);
  1656. edit_items_add_constant->set_disabled(true);
  1657. edit_items_toolbar->add_child(edit_items_add_constant);
  1658. edit_items_add_constant->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_CONSTANT));
  1659. edit_items_add_font = memnew(Button);
  1660. edit_items_add_font->set_tooltip_text(TTR("Add Font Item"));
  1661. edit_items_add_font->set_flat(true);
  1662. edit_items_add_font->set_disabled(true);
  1663. edit_items_toolbar->add_child(edit_items_add_font);
  1664. edit_items_add_font->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_FONT));
  1665. edit_items_add_font_size = memnew(Button);
  1666. edit_items_add_font_size->set_tooltip_text(TTR("Add Font Size Item"));
  1667. edit_items_add_font_size->set_flat(true);
  1668. edit_items_add_font_size->set_disabled(true);
  1669. edit_items_toolbar->add_child(edit_items_add_font_size);
  1670. edit_items_add_font_size->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_FONT_SIZE));
  1671. edit_items_add_icon = memnew(Button);
  1672. edit_items_add_icon->set_tooltip_text(TTR("Add Icon Item"));
  1673. edit_items_add_icon->set_flat(true);
  1674. edit_items_add_icon->set_disabled(true);
  1675. edit_items_toolbar->add_child(edit_items_add_icon);
  1676. edit_items_add_icon->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_ICON));
  1677. edit_items_add_stylebox = memnew(Button);
  1678. edit_items_add_stylebox->set_tooltip_text(TTR("Add StyleBox Item"));
  1679. edit_items_add_stylebox->set_flat(true);
  1680. edit_items_add_stylebox->set_disabled(true);
  1681. edit_items_toolbar->add_child(edit_items_add_stylebox);
  1682. edit_items_add_stylebox->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_STYLEBOX));
  1683. edit_items_toolbar->add_child(memnew(VSeparator));
  1684. Label *edit_items_toolbar_remove_label = memnew(Label);
  1685. edit_items_toolbar_remove_label->set_text(TTR("Remove Items:"));
  1686. edit_items_toolbar->add_child(edit_items_toolbar_remove_label);
  1687. edit_items_remove_class = memnew(Button);
  1688. edit_items_remove_class->set_tooltip_text(TTR("Remove Class Items"));
  1689. edit_items_remove_class->set_flat(true);
  1690. edit_items_remove_class->set_disabled(true);
  1691. edit_items_toolbar->add_child(edit_items_remove_class);
  1692. edit_items_remove_class->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_class_items));
  1693. edit_items_remove_custom = memnew(Button);
  1694. edit_items_remove_custom->set_tooltip_text(TTR("Remove Custom Items"));
  1695. edit_items_remove_custom->set_flat(true);
  1696. edit_items_remove_custom->set_disabled(true);
  1697. edit_items_toolbar->add_child(edit_items_remove_custom);
  1698. edit_items_remove_custom->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_custom_items));
  1699. edit_items_remove_all = memnew(Button);
  1700. edit_items_remove_all->set_tooltip_text(TTR("Remove All Items"));
  1701. edit_items_remove_all->set_flat(true);
  1702. edit_items_remove_all->set_disabled(true);
  1703. edit_items_toolbar->add_child(edit_items_remove_all);
  1704. edit_items_remove_all->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_all_items));
  1705. edit_items_tree = memnew(Tree);
  1706. edit_items_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1707. edit_items_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1708. edit_items_tree->set_hide_root(true);
  1709. edit_items_tree->set_columns(1);
  1710. edit_items_tree->set_theme_type_variation("TreeSecondary");
  1711. edit_items_vb->add_child(edit_items_tree);
  1712. edit_items_tree->connect("button_clicked", callable_mp(this, &ThemeItemEditorDialog::_item_tree_button_pressed));
  1713. edit_items_message = memnew(Label);
  1714. edit_items_message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1715. edit_items_message->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1716. edit_items_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1717. edit_items_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1718. edit_items_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  1719. edit_items_tree->add_child(edit_items_message);
  1720. edit_theme_item_dialog = memnew(ConfirmationDialog);
  1721. edit_theme_item_dialog->set_title(TTR("Add Theme Item"));
  1722. add_child(edit_theme_item_dialog);
  1723. VBoxContainer *edit_theme_item_vb = memnew(VBoxContainer);
  1724. edit_theme_item_dialog->add_child(edit_theme_item_vb);
  1725. edit_theme_item_old_vb = memnew(VBoxContainer);
  1726. edit_theme_item_vb->add_child(edit_theme_item_old_vb);
  1727. Label *edit_theme_item_old = memnew(Label);
  1728. edit_theme_item_old->set_text(TTR("Old Name:"));
  1729. edit_theme_item_old_vb->add_child(edit_theme_item_old);
  1730. theme_item_old_name = memnew(Label);
  1731. edit_theme_item_old_vb->add_child(theme_item_old_name);
  1732. Label *edit_theme_item_label = memnew(Label);
  1733. edit_theme_item_label->set_text(TTR("Name:"));
  1734. edit_theme_item_vb->add_child(edit_theme_item_label);
  1735. theme_item_name = memnew(LineEdit);
  1736. edit_theme_item_vb->add_child(theme_item_name);
  1737. theme_item_name->connect(SceneStringName(gui_input), callable_mp(this, &ThemeItemEditorDialog::_edit_theme_item_gui_input));
  1738. edit_theme_item_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeItemEditorDialog::_confirm_edit_theme_item));
  1739. // Import Items tab.
  1740. TabContainer *import_tc = memnew(TabContainer);
  1741. import_tc->set_theme_type_variation("TabContainerInner");
  1742. import_tc->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
  1743. tc->add_child(import_tc);
  1744. tc->set_tab_title(1, TTR("Import Items"));
  1745. import_default_theme_items = memnew(ThemeItemImportTree);
  1746. import_tc->add_child(import_default_theme_items);
  1747. import_tc->set_tab_title(0, TTR("Default Theme"));
  1748. import_default_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types));
  1749. import_editor_theme_items = memnew(ThemeItemImportTree);
  1750. import_tc->add_child(import_editor_theme_items);
  1751. import_tc->set_tab_title(1, TTR("Editor Theme"));
  1752. import_editor_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types));
  1753. VBoxContainer *import_another_theme_vb = memnew(VBoxContainer);
  1754. HBoxContainer *import_another_file_hb = memnew(HBoxContainer);
  1755. import_another_theme_vb->add_child(import_another_file_hb);
  1756. import_another_theme_value = memnew(LineEdit);
  1757. import_another_theme_value->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1758. import_another_theme_value->set_editable(false);
  1759. import_another_file_hb->add_child(import_another_theme_value);
  1760. import_another_theme_button = memnew(Button);
  1761. import_another_file_hb->add_child(import_another_theme_button);
  1762. import_another_theme_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_select_another_theme));
  1763. import_another_theme_dialog = memnew(EditorFileDialog);
  1764. import_another_theme_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1765. import_another_theme_dialog->set_title(TTR("Select Another Theme Resource:"));
  1766. List<String> ext;
  1767. ResourceLoader::get_recognized_extensions_for_type("Theme", &ext);
  1768. for (const String &E : ext) {
  1769. import_another_theme_dialog->add_filter("*." + E, TTR("Theme Resource"));
  1770. }
  1771. import_another_file_hb->add_child(import_another_theme_dialog);
  1772. import_another_theme_dialog->connect("file_selected", callable_mp(this, &ThemeItemEditorDialog::_select_another_theme_cbk));
  1773. import_other_theme_items = memnew(ThemeItemImportTree);
  1774. import_other_theme_items->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1775. import_another_theme_vb->add_child(import_other_theme_items);
  1776. import_tc->add_child(import_another_theme_vb);
  1777. import_tc->set_tab_title(2, TTR("Another Theme"));
  1778. import_other_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types));
  1779. confirm_closing_dialog = memnew(ConfirmationDialog);
  1780. confirm_closing_dialog->set_autowrap(true);
  1781. add_child(confirm_closing_dialog);
  1782. confirm_closing_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeItemEditorDialog::_close_dialog));
  1783. }
  1784. ///////////////////////
  1785. void ThemeTypeDialog::_dialog_about_to_show() {
  1786. add_type_filter->set_text("");
  1787. add_type_filter->grab_focus();
  1788. _update_add_type_options();
  1789. }
  1790. void ThemeTypeDialog::ok_pressed() {
  1791. _add_type_selected(add_type_filter->get_text().strip_edges());
  1792. }
  1793. void ThemeTypeDialog::_update_add_type_options(const String &p_filter) {
  1794. add_type_options->clear();
  1795. List<StringName> names;
  1796. ThemeDB::get_singleton()->get_default_theme()->get_type_list(&names);
  1797. if (include_own_types) {
  1798. edited_theme->get_type_list(&names);
  1799. }
  1800. names.sort_custom<StringName::AlphCompare>();
  1801. Vector<StringName> unique_names;
  1802. for (const StringName &E : names) {
  1803. // Filter out undesired values.
  1804. if (!p_filter.is_subsequence_ofn(String(E))) {
  1805. continue;
  1806. }
  1807. // Skip duplicate values.
  1808. if (unique_names.has(E)) {
  1809. continue;
  1810. }
  1811. unique_names.append(E);
  1812. Ref<Texture2D> item_icon;
  1813. if (E == "") {
  1814. item_icon = get_editor_theme_icon(SNAME("NodeDisabled"));
  1815. } else {
  1816. item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled");
  1817. }
  1818. add_type_options->add_item(E, item_icon);
  1819. }
  1820. }
  1821. void ThemeTypeDialog::_add_type_filter_cbk(const String &p_value) {
  1822. _update_add_type_options(p_value);
  1823. }
  1824. void ThemeTypeDialog::_type_filter_input(const Ref<InputEvent> &p_event) {
  1825. // Redirect navigational key events to the item list.
  1826. Ref<InputEventKey> key = p_event;
  1827. if (key.is_valid()) {
  1828. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  1829. add_type_options->gui_input(key);
  1830. add_type_filter->accept_event();
  1831. }
  1832. }
  1833. }
  1834. void ThemeTypeDialog::_add_type_options_cbk(int p_index) {
  1835. add_type_filter->set_text(add_type_options->get_item_text(p_index));
  1836. add_type_filter->set_caret_column(add_type_filter->get_text().length());
  1837. }
  1838. void ThemeTypeDialog::_add_type_dialog_entered(const String &p_value) {
  1839. _add_type_selected(Theme::validate_type_name(p_value));
  1840. }
  1841. void ThemeTypeDialog::_add_type_dialog_activated(int p_index) {
  1842. _add_type_selected(add_type_options->get_item_text(p_index));
  1843. }
  1844. void ThemeTypeDialog::_add_type_selected(const String &p_type_name) {
  1845. pre_submitted_value = p_type_name;
  1846. if (p_type_name.is_empty()) {
  1847. add_type_confirmation->popup_centered();
  1848. return;
  1849. }
  1850. _add_type_confirmed();
  1851. }
  1852. void ThemeTypeDialog::_add_type_confirmed() {
  1853. emit_signal(SNAME("type_selected"), pre_submitted_value);
  1854. hide();
  1855. }
  1856. void ThemeTypeDialog::_notification(int p_what) {
  1857. switch (p_what) {
  1858. case NOTIFICATION_POSTINITIALIZE: {
  1859. connect("about_to_popup", callable_mp(this, &ThemeTypeDialog::_dialog_about_to_show));
  1860. } break;
  1861. case NOTIFICATION_THEME_CHANGED: {
  1862. _update_add_type_options();
  1863. } break;
  1864. case NOTIFICATION_VISIBILITY_CHANGED: {
  1865. if (is_visible()) {
  1866. add_type_filter->grab_focus();
  1867. }
  1868. } break;
  1869. }
  1870. }
  1871. void ThemeTypeDialog::_bind_methods() {
  1872. ADD_SIGNAL(MethodInfo("type_selected", PropertyInfo(Variant::STRING, "type_name")));
  1873. }
  1874. void ThemeTypeDialog::set_edited_theme(const Ref<Theme> &p_theme) {
  1875. edited_theme = p_theme;
  1876. }
  1877. void ThemeTypeDialog::set_include_own_types(bool p_enable) {
  1878. include_own_types = p_enable;
  1879. }
  1880. ThemeTypeDialog::ThemeTypeDialog() {
  1881. set_hide_on_ok(false);
  1882. VBoxContainer *add_type_vb = memnew(VBoxContainer);
  1883. add_child(add_type_vb);
  1884. Label *add_type_filter_label = memnew(Label);
  1885. add_type_filter_label->set_text(TTR("Filter the list of types or create a new custom type:"));
  1886. add_type_vb->add_child(add_type_filter_label);
  1887. add_type_filter = memnew(LineEdit);
  1888. add_type_vb->add_child(add_type_filter);
  1889. add_type_filter->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeDialog::_add_type_filter_cbk));
  1890. add_type_filter->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeDialog::_add_type_dialog_entered));
  1891. add_type_filter->connect(SceneStringName(gui_input), callable_mp(this, &ThemeTypeDialog::_type_filter_input));
  1892. Label *add_type_options_label = memnew(Label);
  1893. add_type_options_label->set_text(TTR("Available Node-based types:"));
  1894. add_type_vb->add_child(add_type_options_label);
  1895. MarginContainer *mc = memnew(MarginContainer);
  1896. mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1897. mc->set_theme_type_variation("NoBorderHorizontalWindow");
  1898. add_type_vb->add_child(mc);
  1899. add_type_options = memnew(ItemList);
  1900. add_type_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1901. add_type_options->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH);
  1902. mc->add_child(add_type_options);
  1903. add_type_options->connect(SceneStringName(item_selected), callable_mp(this, &ThemeTypeDialog::_add_type_options_cbk));
  1904. add_type_options->connect("item_activated", callable_mp(this, &ThemeTypeDialog::_add_type_dialog_activated));
  1905. add_type_confirmation = memnew(ConfirmationDialog);
  1906. add_type_confirmation->set_title(TTR("Type name is empty!"));
  1907. add_type_confirmation->set_text(TTR("Are you sure you want to create an empty type?"));
  1908. add_type_confirmation->connect(SceneStringName(confirmed), callable_mp(this, &ThemeTypeDialog::_add_type_confirmed));
  1909. add_child(add_type_confirmation);
  1910. }
  1911. ///////////////////////
  1912. Control *ThemeItemLabel::make_custom_tooltip(const String &p_text) const {
  1913. return EditorHelpBitTooltip::make_tooltip(const_cast<ThemeItemLabel *>(this), p_text);
  1914. }
  1915. VBoxContainer *ThemeTypeEditor::_create_item_list(Theme::DataType p_data_type) {
  1916. VBoxContainer *items_tab = memnew(VBoxContainer);
  1917. items_tab->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  1918. data_type_tabs->add_child(items_tab);
  1919. data_type_tabs->set_tab_title(data_type_tabs->get_tab_count() - 1, "");
  1920. ScrollContainer *items_sc = memnew(ScrollContainer);
  1921. items_sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1922. items_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  1923. items_sc->set_theme_type_variation("ScrollContainerSecondary");
  1924. items_tab->add_child(items_sc);
  1925. VBoxContainer *items_list = memnew(VBoxContainer);
  1926. items_list->set_h_size_flags(SIZE_EXPAND_FILL);
  1927. items_sc->add_child(items_list);
  1928. HBoxContainer *item_add_hb = memnew(HBoxContainer);
  1929. items_tab->add_child(item_add_hb);
  1930. LineEdit *item_add_edit = memnew(LineEdit);
  1931. item_add_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1932. item_add_hb->add_child(item_add_edit);
  1933. item_add_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeEditor::_item_add_lineedit_cbk).bind(p_data_type, item_add_edit));
  1934. Button *item_add_button = memnew(Button);
  1935. item_add_button->set_text(TTR("Add"));
  1936. item_add_button->set_disabled(true);
  1937. item_add_hb->add_child(item_add_button);
  1938. item_add_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_add_cbk).bind(p_data_type, item_add_edit));
  1939. item_add_edit->set_meta("button", item_add_button);
  1940. item_add_edit->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeEditor::_update_add_button).bind(item_add_edit));
  1941. return items_list;
  1942. }
  1943. void ThemeTypeEditor::_update_type_list() {
  1944. ERR_FAIL_COND(edited_theme.is_null());
  1945. if (updating) {
  1946. return;
  1947. }
  1948. updating = true;
  1949. Control *focused = get_viewport()->gui_get_focus_owner();
  1950. if (focused) {
  1951. if (focusables.has(focused)) {
  1952. // If focus is currently on one of the internal property editors, don't update.
  1953. updating = false;
  1954. return;
  1955. }
  1956. Node *focus_parent = focused->get_parent();
  1957. while (focus_parent) {
  1958. Control *c = Object::cast_to<Control>(focus_parent);
  1959. if (c && focusables.has(c)) {
  1960. // If focus is currently on one of the internal property editors, don't update.
  1961. updating = false;
  1962. return;
  1963. }
  1964. focus_parent = focus_parent->get_parent();
  1965. }
  1966. }
  1967. List<StringName> theme_types;
  1968. edited_theme->get_type_list(&theme_types);
  1969. theme_types.sort_custom<StringName::AlphCompare>();
  1970. theme_type_list->clear();
  1971. if (theme_types.is_empty()) {
  1972. theme_type_list->set_disabled(true);
  1973. theme_type_list->add_item(TTRC("None"));
  1974. theme_type_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
  1975. edited_type = "";
  1976. _update_type_items();
  1977. } else {
  1978. theme_type_list->set_disabled(false);
  1979. bool item_reselected = false;
  1980. int e_idx = 0;
  1981. for (const StringName &E : theme_types) {
  1982. Ref<Texture2D> item_icon;
  1983. if (E == "") {
  1984. item_icon = get_editor_theme_icon(SNAME("NodeDisabled"));
  1985. } else {
  1986. item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled");
  1987. }
  1988. theme_type_list->add_icon_item(item_icon, E);
  1989. if (E == edited_type) {
  1990. theme_type_list->select(e_idx);
  1991. item_reselected = true;
  1992. }
  1993. e_idx++;
  1994. }
  1995. if (item_reselected) {
  1996. _update_type_items();
  1997. } else {
  1998. theme_type_list->select(0);
  1999. _list_type_selected(0);
  2000. }
  2001. }
  2002. rename_type_button->set_disabled(theme_types.is_empty());
  2003. remove_type_button->set_disabled(theme_types.is_empty());
  2004. updating = false;
  2005. }
  2006. void ThemeTypeEditor::_update_type_list_debounced() {
  2007. update_debounce_timer->start();
  2008. }
  2009. HashMap<StringName, bool> ThemeTypeEditor::_get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default) {
  2010. HashMap<StringName, bool> items;
  2011. List<StringName> names;
  2012. if (p_include_default) {
  2013. names.clear();
  2014. String default_type;
  2015. {
  2016. const StringName variation_base = edited_theme->get_type_variation_base(p_type_name);
  2017. if (variation_base != StringName()) {
  2018. default_type = variation_base;
  2019. }
  2020. }
  2021. if (default_type.is_empty()) {
  2022. // If variation base was not found in the edited theme, look in the default theme.
  2023. const StringName variation_base = ThemeDB::get_singleton()->get_default_theme()->get_type_variation_base(p_type_name);
  2024. if (variation_base != StringName()) {
  2025. default_type = variation_base;
  2026. }
  2027. }
  2028. if (default_type.is_empty()) {
  2029. default_type = p_type_name;
  2030. }
  2031. List<ThemeDB::ThemeItemBind> theme_binds;
  2032. ThemeDB::get_singleton()->get_class_items(default_type, &theme_binds, true, p_type);
  2033. for (const ThemeDB::ThemeItemBind &E : theme_binds) {
  2034. names.push_back(E.item_name);
  2035. }
  2036. names.sort_custom<StringName::AlphCompare>();
  2037. for (const StringName &E : names) {
  2038. items[E] = false;
  2039. }
  2040. }
  2041. {
  2042. names.clear();
  2043. edited_theme->get_theme_item_list(p_type, p_type_name, &names);
  2044. names.sort_custom<StringName::AlphCompare>();
  2045. for (const StringName &E : names) {
  2046. items[E] = true;
  2047. }
  2048. }
  2049. List<StringName> keys;
  2050. for (const KeyValue<StringName, bool> &E : items) {
  2051. keys.push_back(E.key);
  2052. }
  2053. keys.sort_custom<StringName::AlphCompare>();
  2054. HashMap<StringName, bool> ordered_items;
  2055. for (const StringName &E : keys) {
  2056. ordered_items[E] = items[E];
  2057. }
  2058. return ordered_items;
  2059. }
  2060. HBoxContainer *ThemeTypeEditor::_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable) {
  2061. HBoxContainer *item_control = memnew(HBoxContainer);
  2062. HBoxContainer *item_name_container = memnew(HBoxContainer);
  2063. item_name_container->set_h_size_flags(SIZE_EXPAND_FILL);
  2064. item_name_container->set_stretch_ratio(2.0);
  2065. item_control->add_child(item_name_container);
  2066. Label *item_name = memnew(ThemeItemLabel);
  2067. item_name->set_h_size_flags(SIZE_EXPAND_FILL);
  2068. item_name->set_clip_text(true);
  2069. item_name->set_text(p_item_name);
  2070. // `|` separators used in `EditorHelpBit`.
  2071. item_name->set_tooltip_text("theme_item|" + edited_type + "|" + p_item_name);
  2072. item_name->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  2073. item_name_container->add_child(item_name);
  2074. if (p_editable) {
  2075. LineEdit *item_name_edit = memnew(LineEdit);
  2076. item_name_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  2077. item_name_edit->set_text(p_item_name);
  2078. item_name_container->add_child(item_name_edit);
  2079. item_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeEditor::_item_rename_entered).bind(p_data_type, p_item_name, item_name_container));
  2080. item_name_edit->hide();
  2081. Button *item_rename_button = memnew(Button);
  2082. item_rename_button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  2083. item_rename_button->set_tooltip_text(TTR("Rename Item"));
  2084. item_rename_button->set_flat(true);
  2085. item_name_container->add_child(item_rename_button);
  2086. item_rename_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_cbk).bind(p_data_type, p_item_name, item_name_container));
  2087. Button *item_remove_button = memnew(Button);
  2088. item_remove_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2089. item_remove_button->set_tooltip_text(TTR("Remove Item"));
  2090. item_remove_button->set_flat(true);
  2091. item_name_container->add_child(item_remove_button);
  2092. item_remove_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_remove_cbk).bind(p_data_type, p_item_name));
  2093. Button *item_rename_confirm_button = memnew(Button);
  2094. item_rename_confirm_button->set_button_icon(get_editor_theme_icon(SNAME("ImportCheck")));
  2095. item_rename_confirm_button->set_tooltip_text(TTR("Confirm Item Rename"));
  2096. item_rename_confirm_button->set_flat(true);
  2097. item_name_container->add_child(item_rename_confirm_button);
  2098. item_rename_confirm_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_confirmed).bind(p_data_type, p_item_name, item_name_container));
  2099. item_rename_confirm_button->hide();
  2100. Button *item_rename_cancel_button = memnew(Button);
  2101. item_rename_cancel_button->set_button_icon(get_editor_theme_icon(SNAME("ImportFail")));
  2102. item_rename_cancel_button->set_tooltip_text(TTR("Cancel Item Rename"));
  2103. item_rename_cancel_button->set_flat(true);
  2104. item_name_container->add_child(item_rename_cancel_button);
  2105. item_rename_cancel_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_canceled).bind(p_data_type, p_item_name, item_name_container));
  2106. item_rename_cancel_button->hide();
  2107. } else {
  2108. item_name->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  2109. Button *item_override_button = memnew(Button);
  2110. item_override_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  2111. item_override_button->set_tooltip_text(TTR("Override Item"));
  2112. item_override_button->set_flat(true);
  2113. item_name_container->add_child(item_override_button);
  2114. item_override_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_override_cbk).bind(p_data_type, p_item_name));
  2115. }
  2116. return item_control;
  2117. }
  2118. void ThemeTypeEditor::_add_focusable(Control *p_control) {
  2119. focusables.append(p_control);
  2120. }
  2121. void ThemeTypeEditor::_update_type_items() {
  2122. bool show_default = show_default_items_button->is_pressed();
  2123. focusables.clear();
  2124. // Colors.
  2125. {
  2126. for (int i = color_items_list->get_child_count() - 1; i >= 0; i--) {
  2127. Node *node = color_items_list->get_child(i);
  2128. node->queue_free();
  2129. color_items_list->remove_child(node);
  2130. }
  2131. HashMap<StringName, bool> color_items = _get_type_items(edited_type, Theme::DATA_TYPE_COLOR, show_default);
  2132. for (const KeyValue<StringName, bool> &E : color_items) {
  2133. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_COLOR, E.key, E.value);
  2134. ColorPickerButton *item_editor = memnew(ColorPickerButton);
  2135. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2136. item_control->add_child(item_editor);
  2137. if (E.value) {
  2138. item_editor->set_pick_color(edited_theme->get_color(E.key, edited_type));
  2139. item_editor->connect("color_changed", callable_mp(this, &ThemeTypeEditor::_color_item_changed).bind(E.key));
  2140. item_editor->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(item_editor->get_picker()));
  2141. } else {
  2142. item_editor->set_pick_color(ThemeDB::get_singleton()->get_default_theme()->get_color(E.key, edited_type));
  2143. item_editor->set_disabled(true);
  2144. }
  2145. _add_focusable(item_editor);
  2146. color_items_list->add_child(item_control);
  2147. }
  2148. }
  2149. // Constants.
  2150. {
  2151. for (int i = constant_items_list->get_child_count() - 1; i >= 0; i--) {
  2152. Node *node = constant_items_list->get_child(i);
  2153. node->queue_free();
  2154. constant_items_list->remove_child(node);
  2155. }
  2156. HashMap<StringName, bool> constant_items = _get_type_items(edited_type, Theme::DATA_TYPE_CONSTANT, show_default);
  2157. for (const KeyValue<StringName, bool> &E : constant_items) {
  2158. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_CONSTANT, E.key, E.value);
  2159. EditorSpinSlider *item_editor = memnew(EditorSpinSlider);
  2160. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2161. item_editor->set_min(-100000);
  2162. item_editor->set_max(100000);
  2163. item_editor->set_editing_integer(true);
  2164. item_editor->set_step(1);
  2165. item_editor->set_allow_lesser(true);
  2166. item_editor->set_allow_greater(true);
  2167. item_control->add_child(item_editor);
  2168. if (E.value) {
  2169. item_editor->set_value(edited_theme->get_constant(E.key, edited_type));
  2170. item_editor->connect(SceneStringName(value_changed), callable_mp(this, &ThemeTypeEditor::_constant_item_changed).bind(E.key));
  2171. } else {
  2172. item_editor->set_value(ThemeDB::get_singleton()->get_default_theme()->get_constant(E.key, edited_type));
  2173. item_editor->set_read_only(true);
  2174. }
  2175. _add_focusable(item_editor);
  2176. constant_items_list->add_child(item_control);
  2177. }
  2178. }
  2179. // Fonts.
  2180. {
  2181. for (int i = font_items_list->get_child_count() - 1; i >= 0; i--) {
  2182. Node *node = font_items_list->get_child(i);
  2183. node->queue_free();
  2184. font_items_list->remove_child(node);
  2185. }
  2186. HashMap<StringName, bool> font_items = _get_type_items(edited_type, Theme::DATA_TYPE_FONT, show_default);
  2187. for (const KeyValue<StringName, bool> &E : font_items) {
  2188. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_FONT, E.key, E.value);
  2189. EditorResourcePicker *item_editor = memnew(EditorResourcePicker);
  2190. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2191. item_editor->set_base_type("Font");
  2192. item_editor->set_resource_owner(*edited_theme);
  2193. item_control->add_child(item_editor);
  2194. if (E.value) {
  2195. if (edited_theme->has_font(E.key, edited_type)) {
  2196. item_editor->set_edited_resource(edited_theme->get_font(E.key, edited_type));
  2197. } else {
  2198. item_editor->set_edited_resource(Ref<Resource>());
  2199. }
  2200. item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item));
  2201. item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_font_item_changed).bind(E.key));
  2202. } else {
  2203. if (ThemeDB::get_singleton()->get_default_theme()->has_font(E.key, edited_type)) {
  2204. item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_font(E.key, edited_type));
  2205. } else {
  2206. item_editor->set_edited_resource(Ref<Resource>());
  2207. }
  2208. item_editor->set_editable(false);
  2209. }
  2210. _add_focusable(item_editor);
  2211. font_items_list->add_child(item_control);
  2212. }
  2213. }
  2214. // Fonts sizes.
  2215. {
  2216. for (int i = font_size_items_list->get_child_count() - 1; i >= 0; i--) {
  2217. Node *node = font_size_items_list->get_child(i);
  2218. node->queue_free();
  2219. font_size_items_list->remove_child(node);
  2220. }
  2221. HashMap<StringName, bool> font_size_items = _get_type_items(edited_type, Theme::DATA_TYPE_FONT_SIZE, show_default);
  2222. for (const KeyValue<StringName, bool> &E : font_size_items) {
  2223. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_FONT_SIZE, E.key, E.value);
  2224. EditorSpinSlider *item_editor = memnew(EditorSpinSlider);
  2225. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2226. item_editor->set_min(-100000);
  2227. item_editor->set_max(100000);
  2228. item_editor->set_editing_integer(true);
  2229. item_editor->set_step(1);
  2230. item_editor->set_allow_lesser(true);
  2231. item_editor->set_allow_greater(true);
  2232. item_control->add_child(item_editor);
  2233. if (E.value) {
  2234. item_editor->set_value(edited_theme->get_font_size(E.key, edited_type));
  2235. item_editor->connect(SceneStringName(value_changed), callable_mp(this, &ThemeTypeEditor::_font_size_item_changed).bind(E.key));
  2236. } else {
  2237. item_editor->set_value(ThemeDB::get_singleton()->get_default_theme()->get_font_size(E.key, edited_type));
  2238. item_editor->set_read_only(true);
  2239. }
  2240. _add_focusable(item_editor);
  2241. font_size_items_list->add_child(item_control);
  2242. }
  2243. }
  2244. // Icons.
  2245. {
  2246. for (int i = icon_items_list->get_child_count() - 1; i >= 0; i--) {
  2247. Node *node = icon_items_list->get_child(i);
  2248. node->queue_free();
  2249. icon_items_list->remove_child(node);
  2250. }
  2251. HashMap<StringName, bool> icon_items = _get_type_items(edited_type, Theme::DATA_TYPE_ICON, show_default);
  2252. for (const KeyValue<StringName, bool> &E : icon_items) {
  2253. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_ICON, E.key, E.value);
  2254. EditorResourcePicker *item_editor = memnew(EditorResourcePicker);
  2255. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2256. item_editor->set_base_type("Texture2D");
  2257. item_editor->set_resource_owner(*edited_theme);
  2258. item_control->add_child(item_editor);
  2259. if (E.value) {
  2260. if (edited_theme->has_icon(E.key, edited_type)) {
  2261. item_editor->set_edited_resource(edited_theme->get_icon(E.key, edited_type));
  2262. } else {
  2263. item_editor->set_edited_resource(Ref<Resource>());
  2264. }
  2265. item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item));
  2266. item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_icon_item_changed).bind(E.key));
  2267. } else {
  2268. if (ThemeDB::get_singleton()->get_default_theme()->has_icon(E.key, edited_type)) {
  2269. item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_icon(E.key, edited_type));
  2270. } else {
  2271. item_editor->set_edited_resource(Ref<Resource>());
  2272. }
  2273. item_editor->set_editable(false);
  2274. }
  2275. _add_focusable(item_editor);
  2276. icon_items_list->add_child(item_control);
  2277. }
  2278. }
  2279. // Styleboxes.
  2280. {
  2281. for (int i = stylebox_items_list->get_child_count() - 1; i >= 0; i--) {
  2282. Node *node = stylebox_items_list->get_child(i);
  2283. node->queue_free();
  2284. stylebox_items_list->remove_child(node);
  2285. }
  2286. if (leading_stylebox.pinned) {
  2287. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_STYLEBOX, leading_stylebox.item_name, true);
  2288. EditorResourcePicker *item_editor = memnew(EditorResourcePicker);
  2289. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2290. item_editor->set_stretch_ratio(1.5);
  2291. item_editor->set_base_type("StyleBox");
  2292. item_editor->set_resource_owner(*edited_theme);
  2293. Button *pin_leader_button = memnew(Button);
  2294. pin_leader_button->set_flat(true);
  2295. pin_leader_button->set_toggle_mode(true);
  2296. pin_leader_button->set_pressed(true);
  2297. pin_leader_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
  2298. pin_leader_button->set_tooltip_text(TTR("Unpin this StyleBox as a main style."));
  2299. item_control->add_child(pin_leader_button);
  2300. pin_leader_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_on_unpin_leader_button_pressed));
  2301. item_control->add_child(item_editor);
  2302. if (edited_theme->has_stylebox(leading_stylebox.item_name, edited_type)) {
  2303. item_editor->set_edited_resource(leading_stylebox.stylebox);
  2304. } else {
  2305. item_editor->set_edited_resource(Ref<Resource>());
  2306. }
  2307. item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item));
  2308. item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_stylebox_item_changed).bind(leading_stylebox.item_name));
  2309. stylebox_items_list->add_child(item_control);
  2310. stylebox_items_list->add_child(memnew(HSeparator));
  2311. }
  2312. HashMap<StringName, bool> stylebox_items = _get_type_items(edited_type, Theme::DATA_TYPE_STYLEBOX, show_default);
  2313. for (const KeyValue<StringName, bool> &E : stylebox_items) {
  2314. if (leading_stylebox.pinned && leading_stylebox.item_name == E.key) {
  2315. continue;
  2316. }
  2317. HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_STYLEBOX, E.key, E.value);
  2318. EditorResourcePicker *item_editor = memnew(EditorResourcePicker);
  2319. item_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  2320. item_editor->set_stretch_ratio(1.5);
  2321. item_editor->set_base_type("StyleBox");
  2322. item_editor->set_resource_owner(*edited_theme);
  2323. if (E.value) {
  2324. if (edited_theme->has_stylebox(E.key, edited_type)) {
  2325. item_editor->set_edited_resource(edited_theme->get_stylebox(E.key, edited_type));
  2326. } else {
  2327. item_editor->set_edited_resource(Ref<Resource>());
  2328. }
  2329. item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item));
  2330. item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_stylebox_item_changed).bind(E.key));
  2331. Button *pin_leader_button = memnew(Button);
  2332. pin_leader_button->set_flat(true);
  2333. pin_leader_button->set_toggle_mode(true);
  2334. pin_leader_button->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
  2335. pin_leader_button->set_tooltip_text(TTR("Pin this StyleBox as a main style. Editing its properties will update the same properties in all other StyleBoxes of this type."));
  2336. pin_leader_button->set_accessibility_name(TTRC("Pin this StyleBox as a main style."));
  2337. item_control->add_child(pin_leader_button);
  2338. pin_leader_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_on_pin_leader_button_pressed).bind(item_editor, E.key));
  2339. } else {
  2340. if (ThemeDB::get_singleton()->get_default_theme()->has_stylebox(E.key, edited_type)) {
  2341. item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_stylebox(E.key, edited_type));
  2342. } else {
  2343. item_editor->set_edited_resource(Ref<Resource>());
  2344. }
  2345. item_editor->set_editable(false);
  2346. }
  2347. item_control->add_child(item_editor);
  2348. _add_focusable(item_editor);
  2349. stylebox_items_list->add_child(item_control);
  2350. }
  2351. }
  2352. // Various type settings.
  2353. if (edited_type.is_empty() || ClassDB::class_exists(edited_type)) {
  2354. type_variation_edit->set_editable(false);
  2355. type_variation_edit->set_text("");
  2356. type_variation_button->hide();
  2357. type_variation_locked->set_visible(!edited_type.is_empty());
  2358. } else {
  2359. type_variation_edit->set_editable(true);
  2360. type_variation_edit->set_text(edited_theme->get_type_variation_base(edited_type));
  2361. _add_focusable(type_variation_edit);
  2362. type_variation_button->show();
  2363. type_variation_locked->hide();
  2364. }
  2365. }
  2366. void ThemeTypeEditor::_list_type_selected(int p_index) {
  2367. edited_type = theme_type_list->get_item_text(p_index);
  2368. _update_type_items();
  2369. }
  2370. void ThemeTypeEditor::_add_type_button_cbk() {
  2371. add_type_mode = ADD_THEME_TYPE;
  2372. add_type_dialog->set_title(TTR("Add Item Type"));
  2373. add_type_dialog->set_ok_button_text(TTR("Add Type"));
  2374. add_type_dialog->set_include_own_types(false);
  2375. add_type_dialog->popup_centered(Size2(560, 420) * EDSCALE);
  2376. }
  2377. void ThemeTypeEditor::_rename_type_button_cbk() {
  2378. theme_type_rename_line_edit->set_text(edited_type);
  2379. theme_type_rename_dialog->reset_size();
  2380. theme_type_rename_dialog->popup_centered();
  2381. theme_type_rename_line_edit->grab_focus();
  2382. }
  2383. void ThemeTypeEditor::_theme_type_rename_dialog_confirmed() {
  2384. const String &new_type_name = Theme::validate_type_name(theme_type_rename_line_edit->get_text());
  2385. if (edited_type == new_type_name) {
  2386. return;
  2387. }
  2388. List<StringName> theme_types;
  2389. edited_theme->get_type_list(&theme_types);
  2390. if (theme_types.find(new_type_name) != nullptr) {
  2391. return;
  2392. }
  2393. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2394. ur->create_action(TTR("Rename Theme Type"));
  2395. _rename_theme_type(ur, *edited_theme, edited_type, new_type_name);
  2396. ur->add_do_method(this, "select_type", new_type_name);
  2397. ur->add_undo_method(this, "select_type", edited_type);
  2398. ur->commit_action();
  2399. }
  2400. void ThemeTypeEditor::_remove_type_button_cbk() {
  2401. Ref<Theme> old_snapshot = edited_theme->duplicate();
  2402. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2403. ur->create_action(TTR("Remove Theme Type"));
  2404. ur->add_do_method(*edited_theme, "remove_type", edited_type);
  2405. // If the type was empty, it cannot be restored with merge, but thankfully we can fake it.
  2406. ur->add_undo_method(*edited_theme, "add_type", edited_type);
  2407. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  2408. ur->commit_action();
  2409. }
  2410. void ThemeTypeEditor::_add_default_type_items() {
  2411. List<StringName> names;
  2412. String default_type = edited_type;
  2413. if (edited_theme->get_type_variation_base(edited_type) != StringName()) {
  2414. default_type = edited_theme->get_type_variation_base(edited_type);
  2415. }
  2416. Ref<Theme> old_snapshot = edited_theme->duplicate();
  2417. Ref<Theme> new_snapshot = edited_theme->duplicate();
  2418. updating = true;
  2419. {
  2420. names.clear();
  2421. ThemeDB::get_singleton()->get_default_theme()->get_icon_list(default_type, &names);
  2422. for (const StringName &E : names) {
  2423. if (!new_snapshot->has_icon(E, edited_type)) {
  2424. new_snapshot->set_icon(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_icon(E, edited_type));
  2425. }
  2426. }
  2427. }
  2428. {
  2429. names.clear();
  2430. ThemeDB::get_singleton()->get_default_theme()->get_stylebox_list(default_type, &names);
  2431. for (const StringName &E : names) {
  2432. if (!new_snapshot->has_stylebox(E, edited_type)) {
  2433. new_snapshot->set_stylebox(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_stylebox(E, edited_type));
  2434. }
  2435. }
  2436. }
  2437. {
  2438. names.clear();
  2439. ThemeDB::get_singleton()->get_default_theme()->get_font_list(default_type, &names);
  2440. for (const StringName &E : names) {
  2441. if (!new_snapshot->has_font(E, edited_type)) {
  2442. new_snapshot->set_font(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font(E, edited_type));
  2443. }
  2444. }
  2445. }
  2446. {
  2447. names.clear();
  2448. ThemeDB::get_singleton()->get_default_theme()->get_font_size_list(default_type, &names);
  2449. for (const StringName &E : names) {
  2450. if (!new_snapshot->has_font_size(E, edited_type)) {
  2451. new_snapshot->set_font_size(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font_size(E, edited_type));
  2452. }
  2453. }
  2454. }
  2455. {
  2456. names.clear();
  2457. ThemeDB::get_singleton()->get_default_theme()->get_color_list(default_type, &names);
  2458. for (const StringName &E : names) {
  2459. if (!new_snapshot->has_color(E, edited_type)) {
  2460. new_snapshot->set_color(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_color(E, edited_type));
  2461. }
  2462. }
  2463. }
  2464. {
  2465. names.clear();
  2466. ThemeDB::get_singleton()->get_default_theme()->get_constant_list(default_type, &names);
  2467. for (const StringName &E : names) {
  2468. if (!new_snapshot->has_constant(E, edited_type)) {
  2469. new_snapshot->set_constant(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_constant(E, edited_type));
  2470. }
  2471. }
  2472. }
  2473. updating = false;
  2474. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2475. ur->create_action(TTR("Override All Default Theme Items"));
  2476. ur->add_do_method(*edited_theme, "merge_with", new_snapshot);
  2477. ur->add_undo_method(*edited_theme, "clear");
  2478. ur->add_undo_method(*edited_theme, "merge_with", old_snapshot);
  2479. ur->add_do_method(this, "_update_type_items");
  2480. ur->add_undo_method(this, "_update_type_items");
  2481. ur->commit_action();
  2482. }
  2483. void ThemeTypeEditor::_update_add_button(const String &p_text, LineEdit *p_for_edit) {
  2484. Button *button = Object::cast_to<Button>(p_for_edit->get_meta("button"));
  2485. button->set_disabled(p_text.strip_edges().is_empty());
  2486. }
  2487. void ThemeTypeEditor::_item_add_cbk(int p_data_type, Control *p_control) {
  2488. LineEdit *le = Object::cast_to<LineEdit>(p_control);
  2489. if (le->get_text().strip_edges().is_empty()) {
  2490. return;
  2491. }
  2492. const String item_name = le->get_text().strip_edges().validate_ascii_identifier();
  2493. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2494. ur->create_action(TTR("Add Theme Item"));
  2495. switch (p_data_type) {
  2496. case Theme::DATA_TYPE_COLOR: {
  2497. ur->add_do_method(*edited_theme, "set_color", item_name, edited_type, Color());
  2498. ur->add_undo_method(*edited_theme, "clear_color", item_name, edited_type);
  2499. } break;
  2500. case Theme::DATA_TYPE_CONSTANT: {
  2501. ur->add_do_method(*edited_theme, "set_constant", item_name, edited_type, 0);
  2502. ur->add_undo_method(*edited_theme, "clear_constant", item_name, edited_type);
  2503. } break;
  2504. case Theme::DATA_TYPE_FONT: {
  2505. ur->add_do_method(*edited_theme, "set_font", item_name, edited_type, Ref<Font>());
  2506. ur->add_undo_method(*edited_theme, "clear_font", item_name, edited_type);
  2507. } break;
  2508. case Theme::DATA_TYPE_FONT_SIZE: {
  2509. ur->add_do_method(*edited_theme, "set_font_size", item_name, edited_type, -1);
  2510. ur->add_undo_method(*edited_theme, "clear_font_size", item_name, edited_type);
  2511. } break;
  2512. case Theme::DATA_TYPE_ICON: {
  2513. ur->add_do_method(*edited_theme, "set_icon", item_name, edited_type, Ref<Texture2D>());
  2514. ur->add_undo_method(*edited_theme, "clear_icon", item_name, edited_type);
  2515. } break;
  2516. case Theme::DATA_TYPE_STYLEBOX: {
  2517. Ref<StyleBox> sb;
  2518. ur->add_do_method(*edited_theme, "set_stylebox", item_name, edited_type, sb);
  2519. ur->add_undo_method(*edited_theme, "clear_stylebox", item_name, edited_type);
  2520. if (is_stylebox_pinned(sb)) {
  2521. ur->add_undo_method(this, "_unpin_leading_stylebox");
  2522. }
  2523. } break;
  2524. }
  2525. ur->commit_action();
  2526. le->set_text("");
  2527. _update_add_button("", le);
  2528. }
  2529. void ThemeTypeEditor::_item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control) {
  2530. _item_add_cbk(p_data_type, p_control);
  2531. }
  2532. void ThemeTypeEditor::_item_override_cbk(int p_data_type, String p_item_name) {
  2533. // Avoid errors if the action is triggered multiple times.
  2534. if (edited_theme->has_theme_item_nocheck((Theme::DataType)p_data_type, p_item_name, edited_type)) {
  2535. return;
  2536. }
  2537. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2538. ur->create_action(TTR("Override Theme Item"));
  2539. switch (p_data_type) {
  2540. case Theme::DATA_TYPE_COLOR: {
  2541. ur->add_do_method(*edited_theme, "set_color", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_color(p_item_name, edited_type));
  2542. ur->add_undo_method(*edited_theme, "clear_color", p_item_name, edited_type);
  2543. } break;
  2544. case Theme::DATA_TYPE_CONSTANT: {
  2545. ur->add_do_method(*edited_theme, "set_constant", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_constant(p_item_name, edited_type));
  2546. ur->add_undo_method(*edited_theme, "clear_constant", p_item_name, edited_type);
  2547. } break;
  2548. case Theme::DATA_TYPE_FONT: {
  2549. ur->add_do_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>());
  2550. ur->add_undo_method(*edited_theme, "clear_font", p_item_name, edited_type);
  2551. } break;
  2552. case Theme::DATA_TYPE_FONT_SIZE: {
  2553. ur->add_do_method(*edited_theme, "set_font_size", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font_size(p_item_name, edited_type));
  2554. ur->add_undo_method(*edited_theme, "clear_font_size", p_item_name, edited_type);
  2555. } break;
  2556. case Theme::DATA_TYPE_ICON: {
  2557. ur->add_do_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>());
  2558. ur->add_undo_method(*edited_theme, "clear_icon", p_item_name, edited_type);
  2559. } break;
  2560. case Theme::DATA_TYPE_STYLEBOX: {
  2561. Ref<StyleBox> sb;
  2562. ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, edited_type, sb);
  2563. ur->add_undo_method(*edited_theme, "clear_stylebox", p_item_name, edited_type);
  2564. if (is_stylebox_pinned(sb)) {
  2565. ur->add_undo_method(this, "_unpin_leading_stylebox");
  2566. }
  2567. } break;
  2568. }
  2569. ur->commit_action();
  2570. }
  2571. void ThemeTypeEditor::_item_remove_cbk(int p_data_type, String p_item_name) {
  2572. // Avoid errors if the action is triggered multiple times.
  2573. if (!edited_theme->has_theme_item_nocheck((Theme::DataType)p_data_type, p_item_name, edited_type)) {
  2574. return;
  2575. }
  2576. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2577. ur->create_action(TTR("Remove Theme Item"));
  2578. switch (p_data_type) {
  2579. case Theme::DATA_TYPE_COLOR: {
  2580. ur->add_do_method(*edited_theme, "clear_color", p_item_name, edited_type);
  2581. ur->add_undo_method(*edited_theme, "set_color", p_item_name, edited_type, edited_theme->get_color(p_item_name, edited_type));
  2582. } break;
  2583. case Theme::DATA_TYPE_CONSTANT: {
  2584. ur->add_do_method(*edited_theme, "clear_constant", p_item_name, edited_type);
  2585. ur->add_undo_method(*edited_theme, "set_constant", p_item_name, edited_type, edited_theme->get_constant(p_item_name, edited_type));
  2586. } break;
  2587. case Theme::DATA_TYPE_FONT: {
  2588. ur->add_do_method(*edited_theme, "clear_font", p_item_name, edited_type);
  2589. if (edited_theme->has_font(p_item_name, edited_type)) {
  2590. ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, edited_theme->get_font(p_item_name, edited_type));
  2591. } else {
  2592. ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>());
  2593. }
  2594. } break;
  2595. case Theme::DATA_TYPE_FONT_SIZE: {
  2596. ur->add_do_method(*edited_theme, "clear_font_size", p_item_name, edited_type);
  2597. ur->add_undo_method(*edited_theme, "set_font_size", p_item_name, edited_type, edited_theme->get_font_size(p_item_name, edited_type));
  2598. } break;
  2599. case Theme::DATA_TYPE_ICON: {
  2600. ur->add_do_method(*edited_theme, "clear_icon", p_item_name, edited_type);
  2601. if (edited_theme->has_icon(p_item_name, edited_type)) {
  2602. ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, edited_theme->get_icon(p_item_name, edited_type));
  2603. } else {
  2604. ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>());
  2605. }
  2606. } break;
  2607. case Theme::DATA_TYPE_STYLEBOX: {
  2608. Ref<StyleBox> sb = edited_theme->get_stylebox(p_item_name, edited_type);
  2609. ur->add_do_method(*edited_theme, "clear_stylebox", p_item_name, edited_type);
  2610. if (edited_theme->has_stylebox(p_item_name, edited_type)) {
  2611. ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, sb);
  2612. } else {
  2613. ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, Ref<StyleBox>());
  2614. }
  2615. if (is_stylebox_pinned(sb)) {
  2616. ur->add_do_method(this, "_unpin_leading_stylebox");
  2617. ur->add_undo_method(this, "_pin_leading_stylebox", p_item_name, sb);
  2618. }
  2619. } break;
  2620. }
  2621. ur->commit_action();
  2622. }
  2623. void ThemeTypeEditor::_item_rename_cbk(int p_data_type, String p_item_name, Control *p_control) {
  2624. // Label
  2625. Object::cast_to<Label>(p_control->get_child(0))->hide();
  2626. // Label buttons
  2627. Object::cast_to<Button>(p_control->get_child(2))->hide();
  2628. Object::cast_to<Button>(p_control->get_child(3))->hide();
  2629. // LineEdit
  2630. Object::cast_to<LineEdit>(p_control->get_child(1))->set_text(p_item_name);
  2631. Object::cast_to<LineEdit>(p_control->get_child(1))->show();
  2632. // LineEdit buttons
  2633. Object::cast_to<Button>(p_control->get_child(4))->show();
  2634. Object::cast_to<Button>(p_control->get_child(5))->show();
  2635. }
  2636. void ThemeTypeEditor::_item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control) {
  2637. LineEdit *le = Object::cast_to<LineEdit>(p_control->get_child(1));
  2638. if (le->get_text().strip_edges().is_empty()) {
  2639. return;
  2640. }
  2641. const String new_name = le->get_text().strip_edges().validate_ascii_identifier();
  2642. if (new_name == p_item_name) {
  2643. _item_rename_canceled(p_data_type, p_item_name, p_control);
  2644. return;
  2645. }
  2646. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2647. ur->create_action(TTR("Rename Theme Item"));
  2648. switch (p_data_type) {
  2649. case Theme::DATA_TYPE_COLOR: {
  2650. ur->add_do_method(*edited_theme, "rename_color", p_item_name, new_name, edited_type);
  2651. ur->add_undo_method(*edited_theme, "rename_color", new_name, p_item_name, edited_type);
  2652. } break;
  2653. case Theme::DATA_TYPE_CONSTANT: {
  2654. ur->add_do_method(*edited_theme, "rename_constant", p_item_name, new_name, edited_type);
  2655. ur->add_undo_method(*edited_theme, "rename_constant", new_name, p_item_name, edited_type);
  2656. } break;
  2657. case Theme::DATA_TYPE_FONT: {
  2658. ur->add_do_method(*edited_theme, "rename_font", p_item_name, new_name, edited_type);
  2659. ur->add_undo_method(*edited_theme, "rename_font", new_name, p_item_name, edited_type);
  2660. } break;
  2661. case Theme::DATA_TYPE_FONT_SIZE: {
  2662. ur->add_do_method(*edited_theme, "rename_font_size", p_item_name, new_name, edited_type);
  2663. ur->add_undo_method(*edited_theme, "rename_font_size", new_name, p_item_name, edited_type);
  2664. } break;
  2665. case Theme::DATA_TYPE_ICON: {
  2666. ur->add_do_method(*edited_theme, "rename_icon", p_item_name, new_name, edited_type);
  2667. ur->add_undo_method(*edited_theme, "rename_icon", new_name, p_item_name, edited_type);
  2668. } break;
  2669. case Theme::DATA_TYPE_STYLEBOX: {
  2670. ur->add_do_method(*edited_theme, "rename_stylebox", p_item_name, new_name, edited_type);
  2671. ur->add_undo_method(*edited_theme, "rename_stylebox", new_name, p_item_name, edited_type);
  2672. if (leading_stylebox.pinned && leading_stylebox.item_name == p_item_name) {
  2673. leading_stylebox.item_name = new_name;
  2674. }
  2675. } break;
  2676. }
  2677. ur->commit_action();
  2678. }
  2679. void ThemeTypeEditor::_item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control) {
  2680. _item_rename_confirmed(p_data_type, p_item_name, p_control);
  2681. }
  2682. void ThemeTypeEditor::_item_rename_canceled(int p_data_type, String p_item_name, Control *p_control) {
  2683. // LineEdit
  2684. Object::cast_to<LineEdit>(p_control->get_child(1))->hide();
  2685. // LineEdit buttons
  2686. Object::cast_to<Button>(p_control->get_child(4))->hide();
  2687. Object::cast_to<Button>(p_control->get_child(5))->hide();
  2688. // Label
  2689. Object::cast_to<Label>(p_control->get_child(0))->show();
  2690. // Label buttons
  2691. Object::cast_to<Button>(p_control->get_child(2))->show();
  2692. Object::cast_to<Button>(p_control->get_child(3))->show();
  2693. }
  2694. void ThemeTypeEditor::_color_item_changed(Color p_value, String p_item_name) {
  2695. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2696. ur->create_action(TTR("Set Color Item in Theme"), UndoRedo::MERGE_ENDS);
  2697. ur->add_do_method(*edited_theme, "set_color", p_item_name, edited_type, p_value);
  2698. ur->add_undo_method(*edited_theme, "set_color", p_item_name, edited_type, edited_theme->get_color(p_item_name, edited_type));
  2699. ur->commit_action();
  2700. }
  2701. void ThemeTypeEditor::_constant_item_changed(float p_value, String p_item_name) {
  2702. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2703. ur->create_action(TTR("Set Constant Item in Theme"));
  2704. ur->add_do_method(*edited_theme, "set_constant", p_item_name, edited_type, p_value);
  2705. ur->add_undo_method(*edited_theme, "set_constant", p_item_name, edited_type, edited_theme->get_constant(p_item_name, edited_type));
  2706. ur->commit_action();
  2707. }
  2708. void ThemeTypeEditor::_font_size_item_changed(float p_value, String p_item_name) {
  2709. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2710. ur->create_action(TTR("Set Font Size Item in Theme"));
  2711. ur->add_do_method(*edited_theme, "set_font_size", p_item_name, edited_type, p_value);
  2712. ur->add_undo_method(*edited_theme, "set_font_size", p_item_name, edited_type, edited_theme->get_font_size(p_item_name, edited_type));
  2713. ur->commit_action();
  2714. }
  2715. void ThemeTypeEditor::_edit_resource_item(Ref<Resource> p_resource, bool p_edit) {
  2716. EditorNode::get_singleton()->edit_resource(p_resource);
  2717. }
  2718. void ThemeTypeEditor::_font_item_changed(Ref<Font> p_value, String p_item_name) {
  2719. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2720. ur->create_action(TTR("Set Font Item in Theme"));
  2721. ur->add_do_method(*edited_theme, "set_font", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<Font>());
  2722. if (edited_theme->has_font(p_item_name, edited_type)) {
  2723. ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, edited_theme->get_font(p_item_name, edited_type));
  2724. } else {
  2725. ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>());
  2726. }
  2727. ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items");
  2728. ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items");
  2729. ur->commit_action();
  2730. }
  2731. void ThemeTypeEditor::_icon_item_changed(Ref<Texture2D> p_value, String p_item_name) {
  2732. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2733. ur->create_action(TTR("Set Icon Item in Theme"));
  2734. ur->add_do_method(*edited_theme, "set_icon", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<Texture2D>());
  2735. if (edited_theme->has_icon(p_item_name, edited_type)) {
  2736. ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, edited_theme->get_icon(p_item_name, edited_type));
  2737. } else {
  2738. ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>());
  2739. }
  2740. ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items");
  2741. ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items");
  2742. ur->commit_action();
  2743. }
  2744. void ThemeTypeEditor::_stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name) {
  2745. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2746. ur->create_action(TTR("Set Stylebox Item in Theme"));
  2747. ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<StyleBox>());
  2748. if (edited_theme->has_stylebox(p_item_name, edited_type)) {
  2749. ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, edited_theme->get_stylebox(p_item_name, edited_type));
  2750. } else {
  2751. ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, Ref<StyleBox>());
  2752. }
  2753. ur->add_do_method(this, "_change_pinned_stylebox");
  2754. ur->add_undo_method(this, "_change_pinned_stylebox");
  2755. ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items");
  2756. ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items");
  2757. ur->commit_action();
  2758. }
  2759. void ThemeTypeEditor::_change_pinned_stylebox() {
  2760. if (leading_stylebox.pinned) {
  2761. if (leading_stylebox.stylebox.is_valid()) {
  2762. leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2763. }
  2764. Ref<StyleBox> new_stylebox = edited_theme->get_stylebox(leading_stylebox.item_name, edited_type);
  2765. leading_stylebox.stylebox = new_stylebox;
  2766. leading_stylebox.ref_stylebox = (new_stylebox.is_valid() ? new_stylebox->duplicate() : Ref<Resource>());
  2767. if (leading_stylebox.stylebox.is_valid()) {
  2768. new_stylebox->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2769. }
  2770. } else if (leading_stylebox.stylebox.is_valid()) {
  2771. leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2772. }
  2773. }
  2774. void ThemeTypeEditor::_on_pin_leader_button_pressed(Control *p_editor, String p_item_name) {
  2775. Ref<StyleBox> stylebox;
  2776. if (Object::cast_to<EditorResourcePicker>(p_editor)) {
  2777. stylebox = Object::cast_to<EditorResourcePicker>(p_editor)->get_edited_resource();
  2778. }
  2779. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2780. ur->create_action(TTR("Pin Stylebox"));
  2781. ur->add_do_method(this, "_pin_leading_stylebox", p_item_name, stylebox);
  2782. if (leading_stylebox.pinned) {
  2783. ur->add_undo_method(this, "_pin_leading_stylebox", leading_stylebox.item_name, leading_stylebox.stylebox);
  2784. } else {
  2785. ur->add_undo_method(this, "_unpin_leading_stylebox");
  2786. }
  2787. ur->commit_action();
  2788. }
  2789. void ThemeTypeEditor::_pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox) {
  2790. if (leading_stylebox.stylebox.is_valid()) {
  2791. leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2792. }
  2793. LeadingStylebox leader;
  2794. leader.pinned = true;
  2795. leader.item_name = p_item_name;
  2796. leader.stylebox = p_stylebox;
  2797. leader.ref_stylebox = (p_stylebox.is_valid() ? p_stylebox->duplicate() : Ref<Resource>());
  2798. leading_stylebox = leader;
  2799. if (p_stylebox.is_valid()) {
  2800. p_stylebox->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2801. }
  2802. _update_type_items();
  2803. }
  2804. void ThemeTypeEditor::_on_unpin_leader_button_pressed() {
  2805. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2806. ur->create_action(TTR("Unpin Stylebox"));
  2807. ur->add_do_method(this, "_unpin_leading_stylebox");
  2808. ur->add_undo_method(this, "_pin_leading_stylebox", leading_stylebox.item_name, leading_stylebox.stylebox);
  2809. ur->commit_action();
  2810. }
  2811. void ThemeTypeEditor::_unpin_leading_stylebox() {
  2812. if (leading_stylebox.stylebox.is_valid()) {
  2813. leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading));
  2814. }
  2815. LeadingStylebox leader;
  2816. leader.pinned = false;
  2817. leading_stylebox = leader;
  2818. _update_type_items();
  2819. }
  2820. void ThemeTypeEditor::_update_stylebox_from_leading() {
  2821. if (!leading_stylebox.pinned || leading_stylebox.stylebox.is_null()) {
  2822. return;
  2823. }
  2824. ERR_FAIL_COND_MSG(edited_theme.is_null(), "Leading stylebox does not have an edited theme to update");
  2825. // Prevent changes from immediately being reported while the operation is still ongoing.
  2826. edited_theme->_freeze_change_propagation();
  2827. List<StringName> names;
  2828. edited_theme->get_stylebox_list(edited_type, &names);
  2829. List<Ref<StyleBox>> styleboxes;
  2830. for (const StringName &E : names) {
  2831. Ref<StyleBox> sb = edited_theme->get_stylebox(E, edited_type);
  2832. // Avoid itself, stylebox can be shared between items.
  2833. if (sb == leading_stylebox.stylebox) {
  2834. continue;
  2835. }
  2836. if (sb->get_class() == leading_stylebox.stylebox->get_class()) {
  2837. styleboxes.push_back(sb);
  2838. }
  2839. }
  2840. List<PropertyInfo> props;
  2841. leading_stylebox.stylebox->get_property_list(&props);
  2842. for (const PropertyInfo &E : props) {
  2843. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2844. continue;
  2845. }
  2846. Variant value = leading_stylebox.stylebox->get(E.name);
  2847. Variant ref_value = leading_stylebox.ref_stylebox->get(E.name);
  2848. if (value == ref_value) {
  2849. continue;
  2850. }
  2851. for (const Ref<StyleBox> &F : styleboxes) {
  2852. Ref<StyleBox> sb = F;
  2853. sb->set(E.name, value);
  2854. }
  2855. }
  2856. leading_stylebox.ref_stylebox = leading_stylebox.stylebox->duplicate();
  2857. // Allow changes to be reported now that the operation is finished.
  2858. edited_theme->_unfreeze_and_propagate_changes();
  2859. }
  2860. void ThemeTypeEditor::_type_variation_changed(const String p_value) {
  2861. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  2862. ur->create_action(TTR("Set Theme Type Variation"));
  2863. if (p_value.is_empty()) {
  2864. ur->add_do_method(*edited_theme, "clear_type_variation", edited_type);
  2865. } else {
  2866. ur->add_do_method(*edited_theme, "set_type_variation", edited_type, StringName(p_value));
  2867. }
  2868. if (edited_theme->get_type_variation_base(edited_type) == "") {
  2869. ur->add_undo_method(*edited_theme, "clear_type_variation", edited_type);
  2870. } else {
  2871. ur->add_undo_method(*edited_theme, "set_type_variation", edited_type, edited_theme->get_type_variation_base(edited_type));
  2872. }
  2873. ur->commit_action();
  2874. }
  2875. void ThemeTypeEditor::_add_type_variation_cbk() {
  2876. add_type_mode = ADD_VARIATION_BASE;
  2877. add_type_dialog->set_title(TTR("Set Variation Base Type"));
  2878. add_type_dialog->set_ok_button_text(TTR("Set Base Type"));
  2879. add_type_dialog->set_include_own_types(true);
  2880. add_type_dialog->popup_centered(Size2(560, 420) * EDSCALE);
  2881. }
  2882. void ThemeTypeEditor::_add_type_dialog_selected(const String p_type_name) {
  2883. if (add_type_mode == ADD_THEME_TYPE) {
  2884. select_type(p_type_name);
  2885. } else if (add_type_mode == ADD_VARIATION_BASE) {
  2886. _type_variation_changed(p_type_name);
  2887. }
  2888. }
  2889. void ThemeTypeEditor::_notification(int p_what) {
  2890. switch (p_what) {
  2891. case NOTIFICATION_THEME_CHANGED: {
  2892. add_type_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  2893. rename_type_button->set_button_icon(get_editor_theme_icon(SNAME("Rename")));
  2894. remove_type_button->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2895. const String theme_style = EDITOR_GET("interface/theme/style");
  2896. data_type_tabs->set_theme_type_variation(theme_style == "Classic" ? "TabContainerOdd" : "TabContainerInner");
  2897. data_type_tabs->set_tab_icon(0, get_editor_theme_icon(SNAME("Color")));
  2898. data_type_tabs->set_tab_icon(1, get_editor_theme_icon(SNAME("MemberConstant")));
  2899. data_type_tabs->set_tab_icon(2, get_editor_theme_icon(SNAME("FontItem")));
  2900. data_type_tabs->set_tab_icon(3, get_editor_theme_icon(SNAME("FontSize")));
  2901. data_type_tabs->set_tab_icon(4, get_editor_theme_icon(SNAME("ImageTexture")));
  2902. data_type_tabs->set_tab_icon(5, get_editor_theme_icon(SNAME("StyleBoxFlat")));
  2903. data_type_tabs->set_tab_icon(6, get_editor_theme_icon(SNAME("Tools")));
  2904. type_variation_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  2905. } break;
  2906. }
  2907. }
  2908. void ThemeTypeEditor::_bind_methods() {
  2909. ClassDB::bind_method(D_METHOD("_update_type_items"), &ThemeTypeEditor::_update_type_items);
  2910. ClassDB::bind_method(D_METHOD("_pin_leading_stylebox"), &ThemeTypeEditor::_pin_leading_stylebox);
  2911. ClassDB::bind_method(D_METHOD("_unpin_leading_stylebox"), &ThemeTypeEditor::_unpin_leading_stylebox);
  2912. ClassDB::bind_method(D_METHOD("_change_pinned_stylebox"), &ThemeTypeEditor::_change_pinned_stylebox);
  2913. ClassDB::bind_method(D_METHOD("select_type", "type_name"), &ThemeTypeEditor::select_type);
  2914. }
  2915. void ThemeTypeEditor::set_edited_theme(const Ref<Theme> &p_theme) {
  2916. if (edited_theme.is_valid()) {
  2917. edited_theme->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_type_list_debounced));
  2918. }
  2919. edited_theme = p_theme;
  2920. if (edited_theme.is_valid()) {
  2921. edited_theme->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_type_list_debounced));
  2922. _update_type_list();
  2923. }
  2924. add_type_dialog->set_edited_theme(edited_theme);
  2925. }
  2926. void ThemeTypeEditor::select_type(String p_type_name) {
  2927. edited_type = p_type_name;
  2928. bool type_exists = false;
  2929. for (int i = 0; i < theme_type_list->get_item_count(); i++) {
  2930. String type_name = theme_type_list->get_item_text(i);
  2931. if (type_name == edited_type) {
  2932. theme_type_list->select(i);
  2933. type_exists = true;
  2934. break;
  2935. }
  2936. }
  2937. if (type_exists) {
  2938. _update_type_items();
  2939. } else {
  2940. edited_theme->add_icon_type(edited_type);
  2941. edited_theme->add_stylebox_type(edited_type);
  2942. edited_theme->add_font_type(edited_type);
  2943. edited_theme->add_font_size_type(edited_type);
  2944. edited_theme->add_color_type(edited_type);
  2945. edited_theme->add_constant_type(edited_type);
  2946. _update_type_list();
  2947. }
  2948. }
  2949. bool ThemeTypeEditor::is_stylebox_pinned(Ref<StyleBox> p_stylebox) {
  2950. return leading_stylebox.pinned && leading_stylebox.stylebox == p_stylebox;
  2951. }
  2952. ThemeTypeEditor::ThemeTypeEditor() {
  2953. VBoxContainer *main_vb = memnew(VBoxContainer);
  2954. add_child(main_vb);
  2955. HBoxContainer *type_list_hb = memnew(HBoxContainer);
  2956. main_vb->add_child(type_list_hb);
  2957. Label *type_list_label = memnew(Label);
  2958. type_list_label->set_text(TTR("Type:"));
  2959. type_list_hb->add_child(type_list_label);
  2960. theme_type_list = memnew(OptionButton);
  2961. theme_type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  2962. theme_type_list->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  2963. theme_type_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  2964. theme_type_list->set_accessibility_name(TTRC("Type"));
  2965. type_list_hb->add_child(theme_type_list);
  2966. theme_type_list->connect(SceneStringName(item_selected), callable_mp(this, &ThemeTypeEditor::_list_type_selected));
  2967. add_type_button = memnew(Button);
  2968. add_type_button->set_tooltip_text(TTR("Add a type from a list of available types or create a new one."));
  2969. type_list_hb->add_child(add_type_button);
  2970. add_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_type_button_cbk));
  2971. rename_type_button = memnew(Button);
  2972. rename_type_button->set_disabled(true);
  2973. rename_type_button->set_tooltip_text(TTRC("Rename current type."));
  2974. type_list_hb->add_child(rename_type_button);
  2975. rename_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_rename_type_button_cbk));
  2976. theme_type_rename_dialog = memnew(ConfirmationDialog);
  2977. theme_type_rename_dialog->set_title(TTRC("Rename Theme Type"));
  2978. theme_type_rename_dialog->set_min_size(Size2(256, 64) * EDSCALE);
  2979. add_child(theme_type_rename_dialog);
  2980. theme_type_rename_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeTypeEditor::_theme_type_rename_dialog_confirmed));
  2981. theme_type_rename_line_edit = memnew(LineEdit);
  2982. theme_type_rename_line_edit->set_select_all_on_focus(true);
  2983. theme_type_rename_dialog->add_child(theme_type_rename_line_edit);
  2984. theme_type_rename_dialog->register_text_enter(theme_type_rename_line_edit);
  2985. remove_type_button = memnew(Button);
  2986. remove_type_button->set_disabled(true);
  2987. remove_type_button->set_tooltip_text(TTRC("Remove current type."));
  2988. type_list_hb->add_child(remove_type_button);
  2989. remove_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_remove_type_button_cbk));
  2990. HBoxContainer *type_controls = memnew(HBoxContainer);
  2991. main_vb->add_child(type_controls);
  2992. show_default_items_button = memnew(CheckButton);
  2993. show_default_items_button->set_h_size_flags(SIZE_EXPAND_FILL);
  2994. show_default_items_button->set_text(TTR("Show Default"));
  2995. show_default_items_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  2996. show_default_items_button->set_tooltip_text(TTR("Show default type items alongside items that have been overridden."));
  2997. show_default_items_button->set_pressed(true);
  2998. type_controls->add_child(show_default_items_button);
  2999. show_default_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_update_type_items));
  3000. Button *add_default_items_button = memnew(Button);
  3001. add_default_items_button->set_h_size_flags(SIZE_EXPAND_FILL);
  3002. add_default_items_button->set_text(TTR("Override All"));
  3003. add_default_items_button->set_tooltip_text(TTR("Override all default type items."));
  3004. type_controls->add_child(add_default_items_button);
  3005. add_default_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_default_type_items));
  3006. data_type_tabs = memnew(TabContainer);
  3007. data_type_tabs->set_tab_alignment(TabBar::ALIGNMENT_CENTER);
  3008. main_vb->add_child(data_type_tabs);
  3009. data_type_tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  3010. data_type_tabs->set_use_hidden_tabs_for_min_size(true);
  3011. color_items_list = _create_item_list(Theme::DATA_TYPE_COLOR);
  3012. constant_items_list = _create_item_list(Theme::DATA_TYPE_CONSTANT);
  3013. font_items_list = _create_item_list(Theme::DATA_TYPE_FONT);
  3014. font_size_items_list = _create_item_list(Theme::DATA_TYPE_FONT_SIZE);
  3015. icon_items_list = _create_item_list(Theme::DATA_TYPE_ICON);
  3016. stylebox_items_list = _create_item_list(Theme::DATA_TYPE_STYLEBOX);
  3017. VBoxContainer *type_settings_tab = memnew(VBoxContainer);
  3018. type_settings_tab->set_custom_minimum_size(Size2(0, 160) * EDSCALE);
  3019. data_type_tabs->add_child(type_settings_tab);
  3020. data_type_tabs->set_tab_title(data_type_tabs->get_tab_count() - 1, "");
  3021. ScrollContainer *type_settings_sc = memnew(ScrollContainer);
  3022. type_settings_sc->set_v_size_flags(SIZE_EXPAND_FILL);
  3023. type_settings_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
  3024. type_settings_tab->add_child(type_settings_sc);
  3025. VBoxContainer *type_settings_list = memnew(VBoxContainer);
  3026. type_settings_list->set_h_size_flags(SIZE_EXPAND_FILL);
  3027. type_settings_sc->add_child(type_settings_list);
  3028. VBoxContainer *type_variation_vb = memnew(VBoxContainer);
  3029. type_settings_list->add_child(type_variation_vb);
  3030. HBoxContainer *type_variation_hb = memnew(HBoxContainer);
  3031. type_variation_vb->add_child(type_variation_hb);
  3032. Label *type_variation_label = memnew(Label);
  3033. type_variation_hb->add_child(type_variation_label);
  3034. type_variation_label->set_text(TTR("Base Type"));
  3035. type_variation_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3036. type_variation_edit = memnew(LineEdit);
  3037. type_variation_hb->add_child(type_variation_edit);
  3038. type_variation_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3039. type_variation_edit->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeEditor::_type_variation_changed));
  3040. type_variation_edit->connect(SceneStringName(focus_exited), callable_mp(this, &ThemeTypeEditor::_update_type_items));
  3041. type_variation_edit->set_accessibility_name(TTRC("Base Type"));
  3042. type_variation_button = memnew(Button);
  3043. type_variation_hb->add_child(type_variation_button);
  3044. type_variation_button->set_tooltip_text(TTR("Select the variation base type from a list of available types."));
  3045. type_variation_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_type_variation_cbk));
  3046. type_variation_locked = memnew(Label);
  3047. type_variation_vb->add_child(type_variation_locked);
  3048. type_variation_locked->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3049. type_variation_locked->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  3050. type_variation_locked->set_text(TTR("A type associated with a built-in class cannot be marked as a variation of another type."));
  3051. type_variation_locked->hide();
  3052. add_type_dialog = memnew(ThemeTypeDialog);
  3053. add_child(add_type_dialog);
  3054. add_type_dialog->connect("type_selected", callable_mp(this, &ThemeTypeEditor::_add_type_dialog_selected));
  3055. update_debounce_timer = memnew(Timer);
  3056. update_debounce_timer->set_one_shot(true);
  3057. update_debounce_timer->set_wait_time(0.5);
  3058. update_debounce_timer->connect("timeout", callable_mp(this, &ThemeTypeEditor::_update_type_list));
  3059. add_child(update_debounce_timer);
  3060. }
  3061. ///////////////////////
  3062. void ThemeEditor::edit(const Ref<Theme> &p_theme) {
  3063. if (theme == p_theme) {
  3064. return;
  3065. }
  3066. theme = p_theme;
  3067. theme_type_editor->set_edited_theme(p_theme);
  3068. theme_edit_dialog->set_edited_theme(p_theme);
  3069. for (int i = 0; i < preview_tabs_content->get_child_count(); i++) {
  3070. ThemeEditorPreview *preview_tab = Object::cast_to<ThemeEditorPreview>(preview_tabs_content->get_child(i));
  3071. if (!preview_tab) {
  3072. continue;
  3073. }
  3074. preview_tab->set_preview_theme(p_theme);
  3075. }
  3076. if (theme.is_valid()) {
  3077. _update_theme_name(theme->get_path().get_file());
  3078. }
  3079. }
  3080. Ref<Theme> ThemeEditor::get_edited_theme() {
  3081. return theme;
  3082. }
  3083. void ThemeEditor::_theme_save_button_cbk(bool p_save_as) {
  3084. ERR_FAIL_COND_MSG(theme.is_null(), "Invalid state of the Theme Editor; the Theme resource is missing.");
  3085. if (p_save_as) {
  3086. EditorNode::get_singleton()->save_resource_as(theme);
  3087. } else {
  3088. EditorNode::get_singleton()->save_resource(theme);
  3089. }
  3090. }
  3091. void ThemeEditor::_theme_edit_button_cbk() {
  3092. theme_edit_dialog->popup_centered_clamped(Size2(850, 700) * EDSCALE, 0.8);
  3093. }
  3094. void ThemeEditor::_theme_close_button_cbk() {
  3095. close();
  3096. _dock_closed_cbk();
  3097. }
  3098. void ThemeEditor::_dock_closed_cbk() {
  3099. if (theme.is_valid() && InspectorDock::get_inspector_singleton()->get_edited_object() == theme.ptr()) {
  3100. EditorNode::get_singleton()->push_item(nullptr);
  3101. }
  3102. theme = Ref<Theme>();
  3103. }
  3104. void ThemeEditor::_scene_closed(const String &p_path) {
  3105. if (theme.is_valid() && theme->is_built_in() && theme->get_path().get_slice("::", 0) == p_path) {
  3106. theme = Ref<Theme>();
  3107. EditorNode::get_singleton()->hide_unused_editors(plugin);
  3108. }
  3109. }
  3110. void ThemeEditor::_resource_saved(const Ref<Resource> &p_resource) {
  3111. if (theme.is_valid() && theme == p_resource) {
  3112. _update_theme_name(theme->get_path().get_file());
  3113. }
  3114. }
  3115. void ThemeEditor::_files_moved(const String &p_old_path, const String &p_new_path) {
  3116. // Theme's path may not have been updated to new path yet - need to check both old and new.
  3117. if (theme.is_valid() && (theme->get_path() == p_old_path || theme->get_path() == p_new_path)) {
  3118. _update_theme_name(p_new_path.get_file());
  3119. }
  3120. }
  3121. void ThemeEditor::_update_theme_name(const String &p_name) {
  3122. theme_name->set_text(p_name);
  3123. theme_name->set_tooltip_text(p_name);
  3124. int label_min_width = theme_name->get_minimum_size().x + theme_name->get_character_bounds(0).size.x;
  3125. theme_name->set_custom_minimum_size(Size2(label_min_width, 0));
  3126. }
  3127. void ThemeEditor::_add_preview_button_cbk() {
  3128. preview_scene_dialog->popup_file_dialog();
  3129. }
  3130. void ThemeEditor::_preview_scene_dialog_cbk(const String &p_path) {
  3131. SceneThemeEditorPreview *preview_tab = memnew(SceneThemeEditorPreview);
  3132. if (!preview_tab->set_preview_scene(p_path)) {
  3133. memdelete(preview_tab);
  3134. return;
  3135. }
  3136. _add_preview_tab(preview_tab, p_path.get_file(), get_editor_theme_icon(SNAME("PackedScene")));
  3137. preview_tab->connect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid).bind(preview_tab));
  3138. preview_tab->connect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab).bind(preview_tab));
  3139. }
  3140. void ThemeEditor::_add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon) {
  3141. p_preview_tab->set_preview_theme(theme);
  3142. preview_tabs->add_tab(p_preview_name, p_icon);
  3143. preview_tabs_content->add_child(p_preview_tab);
  3144. preview_tabs->set_tab_button_icon(preview_tabs->get_tab_count() - 1, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("close"), SNAME("TabBar")));
  3145. p_preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked));
  3146. preview_tabs->set_current_tab(preview_tabs->get_tab_count() - 1);
  3147. _preview_tabs_resized();
  3148. }
  3149. void ThemeEditor::_change_preview_tab(int p_tab) {
  3150. ERR_FAIL_INDEX_MSG(p_tab, preview_tabs_content->get_child_count(), "Attempting to open a preview tab that doesn't exist.");
  3151. for (int i = 0; i < preview_tabs_content->get_child_count(); i++) {
  3152. Control *c = Object::cast_to<Control>(preview_tabs_content->get_child(i));
  3153. if (!c) {
  3154. continue;
  3155. }
  3156. c->set_visible(i == p_tab);
  3157. }
  3158. }
  3159. void ThemeEditor::_remove_preview_tab(int p_tab) {
  3160. ERR_FAIL_INDEX_MSG(p_tab, preview_tabs_content->get_child_count(), "Attempting to remove a preview tab that doesn't exist.");
  3161. ThemeEditorPreview *preview_tab = Object::cast_to<ThemeEditorPreview>(preview_tabs_content->get_child(p_tab));
  3162. ERR_FAIL_COND_MSG(Object::cast_to<DefaultThemeEditorPreview>(preview_tab), "Attemptying to remove the default preview tab.");
  3163. if (preview_tab) {
  3164. preview_tab->disconnect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked));
  3165. if (preview_tab->is_connected("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid))) {
  3166. preview_tab->disconnect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid));
  3167. }
  3168. if (preview_tab->is_connected("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab))) {
  3169. preview_tab->disconnect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab));
  3170. }
  3171. preview_tabs_content->remove_child(preview_tab);
  3172. preview_tab->queue_free();
  3173. preview_tabs->remove_tab(p_tab);
  3174. _change_preview_tab(preview_tabs->get_current_tab());
  3175. _preview_tabs_resized();
  3176. }
  3177. }
  3178. void ThemeEditor::_remove_preview_tab_invalid(Node *p_tab_control) {
  3179. int tab_index = p_tab_control->get_index();
  3180. _remove_preview_tab(tab_index);
  3181. }
  3182. void ThemeEditor::_update_preview_tab(Node *p_tab_control) {
  3183. if (!Object::cast_to<SceneThemeEditorPreview>(p_tab_control)) {
  3184. return;
  3185. }
  3186. int tab_index = p_tab_control->get_index();
  3187. SceneThemeEditorPreview *scene_preview = Object::cast_to<SceneThemeEditorPreview>(p_tab_control);
  3188. preview_tabs->set_tab_title(tab_index, scene_preview->get_preview_scene_path().get_file());
  3189. _preview_tabs_resized();
  3190. }
  3191. void ThemeEditor::_preview_control_picked(String p_class_name) {
  3192. theme_type_editor->select_type(p_class_name);
  3193. }
  3194. bool ThemeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  3195. const Dictionary d = p_data;
  3196. if (!d.has("type")) {
  3197. return false;
  3198. }
  3199. if (String(d["type"]) == "files") {
  3200. const Vector<String> files = d["files"];
  3201. if (files.size() != 1) {
  3202. return false;
  3203. }
  3204. const String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  3205. return ftype == "PackedScene";
  3206. }
  3207. return false;
  3208. }
  3209. void ThemeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  3210. Dictionary d = p_data;
  3211. Vector<String> files = d["files"];
  3212. const String &path = files[0];
  3213. SceneThemeEditorPreview *preview_tab = memnew(SceneThemeEditorPreview);
  3214. if (!preview_tab->set_preview_scene(path)) {
  3215. memdelete(preview_tab);
  3216. return;
  3217. }
  3218. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("PackedScene"));
  3219. preview_tab->set_preview_theme(theme);
  3220. preview_tabs->add_tab(path.get_file(), icon);
  3221. preview_tabs_content->add_child(preview_tab);
  3222. preview_tabs->set_tab_button_icon(preview_tabs->get_tab_count() - 1, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("close"), SNAME("TabBar")));
  3223. preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked));
  3224. preview_tabs->set_current_tab(preview_tabs->get_tab_count() - 1);
  3225. preview_tab->connect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid).bind(preview_tab));
  3226. preview_tab->connect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab).bind(preview_tab));
  3227. }
  3228. void ThemeEditor::_preview_tabs_resized() {
  3229. const Size2 add_button_size = Size2(add_preview_button->get_size().x, preview_tabs->get_size().y);
  3230. if (preview_tabs->get_offset_buttons_visible()) {
  3231. // Move the add button to a fixed position.
  3232. if (add_preview_button->get_parent() == preview_tabs) {
  3233. add_preview_button->reparent(add_preview_button_ph);
  3234. add_preview_button->set_rect(Rect2(Point2(), add_button_size));
  3235. }
  3236. } else {
  3237. // Move the add button to be after the last tab.
  3238. if (add_preview_button->get_parent() == add_preview_button_ph) {
  3239. add_preview_button->reparent(preview_tabs);
  3240. }
  3241. Rect2 last_tab = preview_tabs->get_tab_rect(preview_tabs->get_tab_count() - 1);
  3242. int hsep = preview_tabs->get_theme_constant(SNAME("h_separation"));
  3243. if (preview_tabs->is_layout_rtl()) {
  3244. add_preview_button->set_rect(Rect2(Point2(last_tab.position.x - add_button_size.x - hsep, last_tab.position.y), add_button_size));
  3245. } else {
  3246. add_preview_button->set_rect(Rect2(Point2(last_tab.position.x + last_tab.size.width + hsep, last_tab.position.y), add_button_size));
  3247. }
  3248. }
  3249. }
  3250. void ThemeEditor::_notification(int p_what) {
  3251. switch (p_what) {
  3252. case NOTIFICATION_READY: {
  3253. connect("closed", callable_mp(this, &ThemeEditor::_dock_closed_cbk));
  3254. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ThemeEditor::_scene_closed));
  3255. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ThemeEditor::_resource_saved));
  3256. FileSystemDock::get_singleton()->connect("files_moved", callable_mp(this, &ThemeEditor::_files_moved));
  3257. } break;
  3258. case NOTIFICATION_THEME_CHANGED: {
  3259. theme_edit_button->set_button_icon(get_editor_theme_icon(SNAME("Tools")));
  3260. theme_close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  3261. if (EDITOR_GET("interface/theme/style") == "Classic") {
  3262. preview_tabs->add_theme_style_override("tab_selected", get_theme_stylebox(SNAME("ThemeEditorPreviewFG"), EditorStringName(EditorStyles)));
  3263. preview_tabs->add_theme_style_override("tab_unselected", get_theme_stylebox(SNAME("ThemeEditorPreviewBG"), EditorStringName(EditorStyles)));
  3264. preview_tabs_content->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainerOdd")));
  3265. }
  3266. add_preview_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  3267. add_preview_button_ph->set_custom_minimum_size(add_preview_button->get_minimum_size());
  3268. int label_min_width = theme_name->get_minimum_size().x + theme_name->get_character_bounds(0).size.x;
  3269. theme_name->set_custom_minimum_size(Size2(label_min_width, 0));
  3270. } break;
  3271. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  3272. case NOTIFICATION_TRANSLATION_CHANGED: {
  3273. _preview_tabs_resized();
  3274. } break;
  3275. }
  3276. }
  3277. ThemeEditor::ThemeEditor() {
  3278. set_name(TTRC("Theme"));
  3279. set_icon_name("ThemeDock");
  3280. set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_theme_bottom_panel", TTRC("Toggle Theme Dock")));
  3281. set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
  3282. set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
  3283. set_global(false);
  3284. set_transient(true);
  3285. set_closable(true);
  3286. set_custom_minimum_size(Size2(0, 200 * EDSCALE));
  3287. VBoxContainer *content_vb = memnew(VBoxContainer);
  3288. add_child(content_vb);
  3289. HBoxContainer *top_menu = memnew(HBoxContainer);
  3290. content_vb->add_child(top_menu);
  3291. Label *theme_label = memnew(Label);
  3292. theme_label->set_text(TTRC("Theme:"));
  3293. top_menu->add_child(theme_label);
  3294. theme_name = memnew(Label);
  3295. theme_name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  3296. theme_name->set_theme_type_variation("HeaderSmall");
  3297. theme_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  3298. theme_name->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  3299. theme_name->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  3300. top_menu->add_child(theme_name);
  3301. theme_edit_button = memnew(Button);
  3302. theme_edit_button->set_text(TTRC("Manage Items..."));
  3303. theme_edit_button->set_tooltip_text(TTRC("Add, remove, organize, and import Theme items."));
  3304. theme_edit_button->set_flat(true);
  3305. theme_edit_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_edit_button_cbk));
  3306. top_menu->add_child(theme_edit_button);
  3307. top_menu->add_child(memnew(VSeparator));
  3308. Button *theme_save_button = memnew(Button);
  3309. theme_save_button->set_text(TTR("Save"));
  3310. theme_save_button->set_flat(true);
  3311. theme_save_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_save_button_cbk).bind(false));
  3312. top_menu->add_child(theme_save_button);
  3313. Button *theme_save_as_button = memnew(Button);
  3314. theme_save_as_button->set_text(TTR("Save As..."));
  3315. theme_save_as_button->set_flat(true);
  3316. theme_save_as_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_save_button_cbk).bind(true));
  3317. top_menu->add_child(theme_save_as_button);
  3318. top_menu->add_child(memnew(VSeparator));
  3319. theme_close_button = memnew(Button);
  3320. theme_close_button->set_tooltip_text(TTRC("Close"));
  3321. theme_close_button->set_flat(true);
  3322. theme_close_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_close_button_cbk));
  3323. top_menu->add_child(theme_close_button);
  3324. theme_type_editor = memnew(ThemeTypeEditor);
  3325. theme_edit_dialog = memnew(ThemeItemEditorDialog(theme_type_editor));
  3326. theme_edit_dialog->hide();
  3327. top_menu->add_child(theme_edit_dialog);
  3328. HSplitContainer *main_hs = memnew(HSplitContainer);
  3329. main_hs->set_v_size_flags(SIZE_EXPAND_FILL);
  3330. content_vb->add_child(main_hs);
  3331. main_hs->set_split_offset(520 * EDSCALE);
  3332. VBoxContainer *preview_tabs_vb = memnew(VBoxContainer);
  3333. preview_tabs_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  3334. preview_tabs_vb->add_theme_constant_override("separation", 2 * EDSCALE);
  3335. main_hs->add_child(preview_tabs_vb);
  3336. PanelContainer *tabs_panel = memnew(PanelContainer);
  3337. tabs_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  3338. tabs_panel->set_theme_type_variation("PanelContainerTabbarInner");
  3339. preview_tabs_vb->add_child(tabs_panel);
  3340. HBoxContainer *preview_tabbar_hb = memnew(HBoxContainer);
  3341. tabs_panel->add_child(preview_tabbar_hb);
  3342. preview_tabs_content = memnew(PanelContainer);
  3343. preview_tabs_content->set_v_size_flags(SIZE_EXPAND_FILL);
  3344. preview_tabs_content->set_draw_behind_parent(true);
  3345. preview_tabs_vb->add_child(preview_tabs_content);
  3346. preview_tabs = memnew(TabBar);
  3347. preview_tabs->set_h_size_flags(SIZE_EXPAND_FILL);
  3348. preview_tabs->set_theme_type_variation("TabContainerInner");
  3349. preview_tabbar_hb->add_child(preview_tabs);
  3350. preview_tabs->connect("tab_changed", callable_mp(this, &ThemeEditor::_change_preview_tab));
  3351. preview_tabs->connect("tab_button_pressed", callable_mp(this, &ThemeEditor::_remove_preview_tab));
  3352. preview_tabs->connect(SceneStringName(resized), callable_mp(this, &ThemeEditor::_preview_tabs_resized), CONNECT_DEFERRED);
  3353. add_preview_button = memnew(Button);
  3354. add_preview_button->set_tooltip_text(TTRC("Add Preview"));
  3355. add_preview_button->set_flat(true);
  3356. preview_tabs->add_child(add_preview_button);
  3357. add_preview_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_add_preview_button_cbk));
  3358. add_preview_button_ph = memnew(Control);
  3359. add_preview_button_ph->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  3360. add_preview_button_ph->set_custom_minimum_size(add_preview_button->get_minimum_size());
  3361. preview_tabbar_hb->add_child(add_preview_button_ph);
  3362. DefaultThemeEditorPreview *default_preview_tab = memnew(DefaultThemeEditorPreview);
  3363. preview_tabs_content->add_child(default_preview_tab);
  3364. default_preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked));
  3365. preview_tabs->add_tab(TTR("Default Preview"));
  3366. preview_scene_dialog = memnew(EditorFileDialog);
  3367. preview_scene_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  3368. preview_scene_dialog->set_title(TTR("Select UI Scene:"));
  3369. List<String> ext;
  3370. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &ext);
  3371. for (const String &E : ext) {
  3372. preview_scene_dialog->add_filter("*." + E, TTR("Scene"));
  3373. }
  3374. main_hs->add_child(preview_scene_dialog);
  3375. preview_scene_dialog->connect("file_selected", callable_mp(this, &ThemeEditor::_preview_scene_dialog_cbk));
  3376. main_hs->add_child(theme_type_editor);
  3377. theme_type_editor->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
  3378. SET_DRAG_FORWARDING_CD(top_menu, ThemeEditor);
  3379. SET_DRAG_FORWARDING_CD(preview_tabs, ThemeEditor);
  3380. }
  3381. ///////////////////////
  3382. void ThemeEditorPlugin::edit(Object *p_object) {
  3383. theme_editor->edit(Ref<Theme>(p_object));
  3384. }
  3385. bool ThemeEditorPlugin::handles(Object *p_object) const {
  3386. return Object::cast_to<Theme>(p_object) != nullptr;
  3387. }
  3388. void ThemeEditorPlugin::make_visible(bool p_visible) {
  3389. if (p_visible) {
  3390. theme_editor->make_visible();
  3391. } else {
  3392. theme_editor->close();
  3393. }
  3394. }
  3395. bool ThemeEditorPlugin::can_auto_hide() const {
  3396. return theme_editor->theme.is_null();
  3397. }
  3398. ThemeEditorPlugin::ThemeEditorPlugin() {
  3399. theme_editor = memnew(ThemeEditor);
  3400. theme_editor->plugin = this;
  3401. EditorDockManager::get_singleton()->add_dock(theme_editor);
  3402. theme_editor->close();
  3403. }