theme_editor_plugin.cpp 145 KB

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