theme_editor_plugin.cpp 145 KB

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