theme_editor_plugin.cpp 145 KB

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