theme_editor_plugin.cpp 154 KB

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