theme_editor_plugin.cpp 147 KB

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