theme_editor_plugin.cpp 142 KB

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