theme_editor_plugin.cpp 148 KB

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