theme.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. /**************************************************************************/
  2. /* theme.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.h"
  31. #include "core/string/print_string.h"
  32. #include "scene/theme/theme_db.h"
  33. // Dynamic properties.
  34. bool Theme::_set(const StringName &p_name, const Variant &p_value) {
  35. String sname = p_name;
  36. if (sname.contains("/")) {
  37. String type = sname.get_slicec('/', 1);
  38. String theme_type = sname.get_slicec('/', 0);
  39. String prop_name = sname.get_slicec('/', 2);
  40. if (type == "icons") {
  41. set_icon(prop_name, theme_type, p_value);
  42. } else if (type == "styles") {
  43. set_stylebox(prop_name, theme_type, p_value);
  44. } else if (type == "fonts") {
  45. set_font(prop_name, theme_type, p_value);
  46. } else if (type == "font_sizes") {
  47. set_font_size(prop_name, theme_type, p_value);
  48. } else if (type == "colors") {
  49. set_color(prop_name, theme_type, p_value);
  50. } else if (type == "constants") {
  51. set_constant(prop_name, theme_type, p_value);
  52. } else if (type == "base_type") {
  53. set_type_variation(theme_type, p_value);
  54. } else {
  55. return false;
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool Theme::_get(const StringName &p_name, Variant &r_ret) const {
  62. String sname = p_name;
  63. if (sname.contains("/")) {
  64. String type = sname.get_slicec('/', 1);
  65. String theme_type = sname.get_slicec('/', 0);
  66. String prop_name = sname.get_slicec('/', 2);
  67. if (type == "icons") {
  68. if (!has_icon(prop_name, theme_type)) {
  69. r_ret = Ref<Texture2D>();
  70. } else {
  71. r_ret = get_icon(prop_name, theme_type);
  72. }
  73. } else if (type == "styles") {
  74. if (!has_stylebox(prop_name, theme_type)) {
  75. r_ret = Ref<StyleBox>();
  76. } else {
  77. r_ret = get_stylebox(prop_name, theme_type);
  78. }
  79. } else if (type == "fonts") {
  80. if (!has_font(prop_name, theme_type)) {
  81. r_ret = Ref<Font>();
  82. } else {
  83. r_ret = get_font(prop_name, theme_type);
  84. }
  85. } else if (type == "font_sizes") {
  86. r_ret = get_font_size(prop_name, theme_type);
  87. } else if (type == "colors") {
  88. r_ret = get_color(prop_name, theme_type);
  89. } else if (type == "constants") {
  90. r_ret = get_constant(prop_name, theme_type);
  91. } else if (type == "base_type") {
  92. r_ret = get_type_variation_base(theme_type);
  93. } else {
  94. return false;
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
  101. List<PropertyInfo> list;
  102. // Type variations.
  103. for (const KeyValue<StringName, StringName> &E : variation_map) {
  104. list.push_back(PropertyInfo(Variant::STRING_NAME, String() + E.key + "/base_type"));
  105. }
  106. // Icons.
  107. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  108. for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
  109. list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/icons/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  110. }
  111. }
  112. // Styles.
  113. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  114. for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
  115. list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/styles/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  116. }
  117. }
  118. // Fonts.
  119. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  120. for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
  121. list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/fonts/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  122. }
  123. }
  124. // Font sizes.
  125. for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
  126. for (const KeyValue<StringName, int> &F : E.value) {
  127. list.push_back(PropertyInfo(Variant::INT, String() + E.key + "/font_sizes/" + F.key, PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"));
  128. }
  129. }
  130. // Colors.
  131. for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
  132. for (const KeyValue<StringName, Color> &F : E.value) {
  133. list.push_back(PropertyInfo(Variant::COLOR, String() + E.key + "/colors/" + F.key));
  134. }
  135. }
  136. // Constants.
  137. for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
  138. for (const KeyValue<StringName, int> &F : E.value) {
  139. list.push_back(PropertyInfo(Variant::INT, String() + E.key + "/constants/" + F.key));
  140. }
  141. }
  142. // Sort and store properties.
  143. list.sort();
  144. String prev_type;
  145. for (const PropertyInfo &E : list) {
  146. // Add groups for types so that their names are left unchanged in the inspector.
  147. String current_type = E.name.get_slice("/", 0);
  148. if (prev_type != current_type) {
  149. p_list->push_back(PropertyInfo(Variant::NIL, current_type, PROPERTY_HINT_NONE, current_type + "/", PROPERTY_USAGE_GROUP));
  150. prev_type = current_type;
  151. }
  152. p_list->push_back(E);
  153. }
  154. }
  155. // Static helpers.
  156. bool Theme::is_valid_type_name(const String &p_name) {
  157. for (int i = 0; i < p_name.length(); i++) {
  158. if (!is_ascii_identifier_char(p_name[i])) {
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. bool Theme::is_valid_item_name(const String &p_name) {
  165. if (p_name.is_empty()) {
  166. return false;
  167. }
  168. for (int i = 0; i < p_name.length(); i++) {
  169. if (!is_ascii_identifier_char(p_name[i])) {
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. // Fallback values for theme item types, configurable per theme.
  176. void Theme::set_default_base_scale(float p_base_scale) {
  177. if (default_base_scale == p_base_scale) {
  178. return;
  179. }
  180. default_base_scale = p_base_scale;
  181. _emit_theme_changed();
  182. }
  183. float Theme::get_default_base_scale() const {
  184. return default_base_scale;
  185. }
  186. bool Theme::has_default_base_scale() const {
  187. return default_base_scale > 0.0;
  188. }
  189. void Theme::set_default_font(const Ref<Font> &p_default_font) {
  190. if (default_font == p_default_font) {
  191. return;
  192. }
  193. if (default_font.is_valid()) {
  194. default_font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  195. }
  196. default_font = p_default_font;
  197. if (default_font.is_valid()) {
  198. default_font->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  199. }
  200. _emit_theme_changed();
  201. }
  202. Ref<Font> Theme::get_default_font() const {
  203. return default_font;
  204. }
  205. bool Theme::has_default_font() const {
  206. return default_font.is_valid();
  207. }
  208. void Theme::set_default_font_size(int p_font_size) {
  209. if (default_font_size == p_font_size) {
  210. return;
  211. }
  212. default_font_size = p_font_size;
  213. _emit_theme_changed();
  214. }
  215. int Theme::get_default_font_size() const {
  216. return default_font_size;
  217. }
  218. bool Theme::has_default_font_size() const {
  219. return default_font_size > 0;
  220. }
  221. // Icons.
  222. void Theme::set_icon(const StringName &p_name, const StringName &p_theme_type, const Ref<Texture2D> &p_icon) {
  223. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  224. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  225. bool existing = false;
  226. if (icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) {
  227. existing = true;
  228. icon_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  229. }
  230. icon_map[p_theme_type][p_name] = p_icon;
  231. if (p_icon.is_valid()) {
  232. icon_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  233. }
  234. _emit_theme_changed(!existing);
  235. }
  236. Ref<Texture2D> Theme::get_icon(const StringName &p_name, const StringName &p_theme_type) const {
  237. if (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) {
  238. return icon_map[p_theme_type][p_name];
  239. } else {
  240. return ThemeDB::get_singleton()->get_fallback_icon();
  241. }
  242. }
  243. bool Theme::has_icon(const StringName &p_name, const StringName &p_theme_type) const {
  244. return (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid());
  245. }
  246. bool Theme::has_icon_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  247. return (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name));
  248. }
  249. void Theme::rename_icon(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  250. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  251. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  252. ERR_FAIL_COND_MSG(!icon_map.has(p_theme_type), "Cannot rename the icon '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  253. ERR_FAIL_COND_MSG(icon_map[p_theme_type].has(p_name), "Cannot rename the icon '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  254. ERR_FAIL_COND_MSG(!icon_map[p_theme_type].has(p_old_name), "Cannot rename the icon '" + String(p_old_name) + "' because it does not exist.");
  255. icon_map[p_theme_type][p_name] = icon_map[p_theme_type][p_old_name];
  256. icon_map[p_theme_type].erase(p_old_name);
  257. _emit_theme_changed(true);
  258. }
  259. void Theme::clear_icon(const StringName &p_name, const StringName &p_theme_type) {
  260. ERR_FAIL_COND_MSG(!icon_map.has(p_theme_type), "Cannot clear the icon '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  261. ERR_FAIL_COND_MSG(!icon_map[p_theme_type].has(p_name), "Cannot clear the icon '" + String(p_name) + "' because it does not exist.");
  262. if (icon_map[p_theme_type][p_name].is_valid()) {
  263. icon_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  264. }
  265. icon_map[p_theme_type].erase(p_name);
  266. _emit_theme_changed(true);
  267. }
  268. void Theme::get_icon_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  269. ERR_FAIL_NULL(p_list);
  270. if (!icon_map.has(p_theme_type)) {
  271. return;
  272. }
  273. for (const KeyValue<StringName, Ref<Texture2D>> &E : icon_map[p_theme_type]) {
  274. p_list->push_back(E.key);
  275. }
  276. }
  277. void Theme::add_icon_type(const StringName &p_theme_type) {
  278. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  279. if (icon_map.has(p_theme_type)) {
  280. return;
  281. }
  282. icon_map[p_theme_type] = ThemeIconMap();
  283. }
  284. void Theme::remove_icon_type(const StringName &p_theme_type) {
  285. if (!icon_map.has(p_theme_type)) {
  286. return;
  287. }
  288. _freeze_change_propagation();
  289. for (const KeyValue<StringName, Ref<Texture2D>> &E : icon_map[p_theme_type]) {
  290. Ref<Texture2D> icon = E.value;
  291. if (icon.is_valid()) {
  292. icon->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  293. }
  294. }
  295. icon_map.erase(p_theme_type);
  296. _unfreeze_and_propagate_changes();
  297. }
  298. void Theme::get_icon_type_list(List<StringName> *p_list) const {
  299. ERR_FAIL_NULL(p_list);
  300. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  301. p_list->push_back(E.key);
  302. }
  303. }
  304. // Styleboxes.
  305. void Theme::set_stylebox(const StringName &p_name, const StringName &p_theme_type, const Ref<StyleBox> &p_style) {
  306. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  307. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  308. bool existing = false;
  309. if (style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
  310. existing = true;
  311. style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  312. }
  313. style_map[p_theme_type][p_name] = p_style;
  314. if (p_style.is_valid()) {
  315. style_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  316. }
  317. _emit_theme_changed(!existing);
  318. }
  319. Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  320. if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
  321. return style_map[p_theme_type][p_name];
  322. } else {
  323. return ThemeDB::get_singleton()->get_fallback_stylebox();
  324. }
  325. }
  326. bool Theme::has_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  327. return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid());
  328. }
  329. bool Theme::has_stylebox_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  330. return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name));
  331. }
  332. void Theme::rename_stylebox(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  333. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  334. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  335. ERR_FAIL_COND_MSG(!style_map.has(p_theme_type), "Cannot rename the stylebox '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  336. ERR_FAIL_COND_MSG(style_map[p_theme_type].has(p_name), "Cannot rename the stylebox '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  337. ERR_FAIL_COND_MSG(!style_map[p_theme_type].has(p_old_name), "Cannot rename the stylebox '" + String(p_old_name) + "' because it does not exist.");
  338. style_map[p_theme_type][p_name] = style_map[p_theme_type][p_old_name];
  339. style_map[p_theme_type].erase(p_old_name);
  340. _emit_theme_changed(true);
  341. }
  342. void Theme::clear_stylebox(const StringName &p_name, const StringName &p_theme_type) {
  343. ERR_FAIL_COND_MSG(!style_map.has(p_theme_type), "Cannot clear the stylebox '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  344. ERR_FAIL_COND_MSG(!style_map[p_theme_type].has(p_name), "Cannot clear the stylebox '" + String(p_name) + "' because it does not exist.");
  345. if (style_map[p_theme_type][p_name].is_valid()) {
  346. style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  347. }
  348. style_map[p_theme_type].erase(p_name);
  349. _emit_theme_changed(true);
  350. }
  351. void Theme::get_stylebox_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  352. ERR_FAIL_NULL(p_list);
  353. if (!style_map.has(p_theme_type)) {
  354. return;
  355. }
  356. for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
  357. p_list->push_back(E.key);
  358. }
  359. }
  360. void Theme::add_stylebox_type(const StringName &p_theme_type) {
  361. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  362. if (style_map.has(p_theme_type)) {
  363. return;
  364. }
  365. style_map[p_theme_type] = ThemeStyleMap();
  366. }
  367. void Theme::remove_stylebox_type(const StringName &p_theme_type) {
  368. if (!style_map.has(p_theme_type)) {
  369. return;
  370. }
  371. _freeze_change_propagation();
  372. for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
  373. Ref<StyleBox> style = E.value;
  374. if (style.is_valid()) {
  375. style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  376. }
  377. }
  378. style_map.erase(p_theme_type);
  379. _unfreeze_and_propagate_changes();
  380. }
  381. void Theme::get_stylebox_type_list(List<StringName> *p_list) const {
  382. ERR_FAIL_NULL(p_list);
  383. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  384. p_list->push_back(E.key);
  385. }
  386. }
  387. // Fonts.
  388. void Theme::set_font(const StringName &p_name, const StringName &p_theme_type, const Ref<Font> &p_font) {
  389. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  390. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  391. bool existing = false;
  392. if (font_map[p_theme_type][p_name].is_valid()) {
  393. existing = true;
  394. font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  395. }
  396. font_map[p_theme_type][p_name] = p_font;
  397. if (p_font.is_valid()) {
  398. font_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  399. }
  400. _emit_theme_changed(!existing);
  401. }
  402. Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_theme_type) const {
  403. if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) {
  404. return font_map[p_theme_type][p_name];
  405. } else if (has_default_font()) {
  406. return default_font;
  407. } else {
  408. return ThemeDB::get_singleton()->get_fallback_font();
  409. }
  410. }
  411. bool Theme::has_font(const StringName &p_name, const StringName &p_theme_type) const {
  412. return ((font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) || has_default_font());
  413. }
  414. bool Theme::has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  415. return (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name));
  416. }
  417. void Theme::rename_font(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  418. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  419. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  420. ERR_FAIL_COND_MSG(!font_map.has(p_theme_type), "Cannot rename the font '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  421. ERR_FAIL_COND_MSG(font_map[p_theme_type].has(p_name), "Cannot rename the font '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  422. ERR_FAIL_COND_MSG(!font_map[p_theme_type].has(p_old_name), "Cannot rename the font '" + String(p_old_name) + "' because it does not exist.");
  423. font_map[p_theme_type][p_name] = font_map[p_theme_type][p_old_name];
  424. font_map[p_theme_type].erase(p_old_name);
  425. _emit_theme_changed(true);
  426. }
  427. void Theme::clear_font(const StringName &p_name, const StringName &p_theme_type) {
  428. ERR_FAIL_COND_MSG(!font_map.has(p_theme_type), "Cannot clear the font '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  429. ERR_FAIL_COND_MSG(!font_map[p_theme_type].has(p_name), "Cannot clear the font '" + String(p_name) + "' because it does not exist.");
  430. if (font_map[p_theme_type][p_name].is_valid()) {
  431. font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  432. }
  433. font_map[p_theme_type].erase(p_name);
  434. _emit_theme_changed(true);
  435. }
  436. void Theme::get_font_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  437. ERR_FAIL_NULL(p_list);
  438. if (!font_map.has(p_theme_type)) {
  439. return;
  440. }
  441. for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
  442. p_list->push_back(E.key);
  443. }
  444. }
  445. void Theme::add_font_type(const StringName &p_theme_type) {
  446. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  447. if (font_map.has(p_theme_type)) {
  448. return;
  449. }
  450. font_map[p_theme_type] = ThemeFontMap();
  451. }
  452. void Theme::remove_font_type(const StringName &p_theme_type) {
  453. if (!font_map.has(p_theme_type)) {
  454. return;
  455. }
  456. _freeze_change_propagation();
  457. for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
  458. Ref<Font> font = E.value;
  459. if (font.is_valid()) {
  460. font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  461. }
  462. }
  463. font_map.erase(p_theme_type);
  464. _unfreeze_and_propagate_changes();
  465. }
  466. void Theme::get_font_type_list(List<StringName> *p_list) const {
  467. ERR_FAIL_NULL(p_list);
  468. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  469. p_list->push_back(E.key);
  470. }
  471. }
  472. // Font sizes.
  473. void Theme::set_font_size(const StringName &p_name, const StringName &p_theme_type, int p_font_size) {
  474. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  475. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  476. bool existing = has_font_size_nocheck(p_name, p_theme_type);
  477. font_size_map[p_theme_type][p_name] = p_font_size;
  478. _emit_theme_changed(!existing);
  479. }
  480. int Theme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  481. if (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) {
  482. return font_size_map[p_theme_type][p_name];
  483. } else if (has_default_font_size()) {
  484. return default_font_size;
  485. } else {
  486. return ThemeDB::get_singleton()->get_fallback_font_size();
  487. }
  488. }
  489. bool Theme::has_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  490. return ((font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) || has_default_font_size());
  491. }
  492. bool Theme::has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  493. return (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name));
  494. }
  495. void Theme::rename_font_size(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  496. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  497. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  498. ERR_FAIL_COND_MSG(!font_size_map.has(p_theme_type), "Cannot rename the font size '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  499. ERR_FAIL_COND_MSG(font_size_map[p_theme_type].has(p_name), "Cannot rename the font size '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  500. ERR_FAIL_COND_MSG(!font_size_map[p_theme_type].has(p_old_name), "Cannot rename the font size '" + String(p_old_name) + "' because it does not exist.");
  501. font_size_map[p_theme_type][p_name] = font_size_map[p_theme_type][p_old_name];
  502. font_size_map[p_theme_type].erase(p_old_name);
  503. _emit_theme_changed(true);
  504. }
  505. void Theme::clear_font_size(const StringName &p_name, const StringName &p_theme_type) {
  506. ERR_FAIL_COND_MSG(!font_size_map.has(p_theme_type), "Cannot clear the font size '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  507. ERR_FAIL_COND_MSG(!font_size_map[p_theme_type].has(p_name), "Cannot clear the font size '" + String(p_name) + "' because it does not exist.");
  508. font_size_map[p_theme_type].erase(p_name);
  509. _emit_theme_changed(true);
  510. }
  511. void Theme::get_font_size_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  512. ERR_FAIL_NULL(p_list);
  513. if (!font_size_map.has(p_theme_type)) {
  514. return;
  515. }
  516. for (const KeyValue<StringName, int> &E : font_size_map[p_theme_type]) {
  517. p_list->push_back(E.key);
  518. }
  519. }
  520. void Theme::add_font_size_type(const StringName &p_theme_type) {
  521. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  522. if (font_size_map.has(p_theme_type)) {
  523. return;
  524. }
  525. font_size_map[p_theme_type] = ThemeFontSizeMap();
  526. }
  527. void Theme::remove_font_size_type(const StringName &p_theme_type) {
  528. if (!font_size_map.has(p_theme_type)) {
  529. return;
  530. }
  531. font_size_map.erase(p_theme_type);
  532. }
  533. void Theme::get_font_size_type_list(List<StringName> *p_list) const {
  534. ERR_FAIL_NULL(p_list);
  535. for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
  536. p_list->push_back(E.key);
  537. }
  538. }
  539. // Colors.
  540. void Theme::set_color(const StringName &p_name, const StringName &p_theme_type, const Color &p_color) {
  541. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  542. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  543. bool existing = has_color_nocheck(p_name, p_theme_type);
  544. color_map[p_theme_type][p_name] = p_color;
  545. _emit_theme_changed(!existing);
  546. }
  547. Color Theme::get_color(const StringName &p_name, const StringName &p_theme_type) const {
  548. if (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name)) {
  549. return color_map[p_theme_type][p_name];
  550. } else {
  551. return Color();
  552. }
  553. }
  554. bool Theme::has_color(const StringName &p_name, const StringName &p_theme_type) const {
  555. return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
  556. }
  557. bool Theme::has_color_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  558. return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
  559. }
  560. void Theme::rename_color(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  561. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  562. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  563. ERR_FAIL_COND_MSG(!color_map.has(p_theme_type), "Cannot rename the color '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  564. ERR_FAIL_COND_MSG(color_map[p_theme_type].has(p_name), "Cannot rename the color '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  565. ERR_FAIL_COND_MSG(!color_map[p_theme_type].has(p_old_name), "Cannot rename the color '" + String(p_old_name) + "' because it does not exist.");
  566. color_map[p_theme_type][p_name] = color_map[p_theme_type][p_old_name];
  567. color_map[p_theme_type].erase(p_old_name);
  568. _emit_theme_changed(true);
  569. }
  570. void Theme::clear_color(const StringName &p_name, const StringName &p_theme_type) {
  571. ERR_FAIL_COND_MSG(!color_map.has(p_theme_type), "Cannot clear the color '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  572. ERR_FAIL_COND_MSG(!color_map[p_theme_type].has(p_name), "Cannot clear the color '" + String(p_name) + "' because it does not exist.");
  573. color_map[p_theme_type].erase(p_name);
  574. _emit_theme_changed(true);
  575. }
  576. void Theme::get_color_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  577. ERR_FAIL_NULL(p_list);
  578. if (!color_map.has(p_theme_type)) {
  579. return;
  580. }
  581. for (const KeyValue<StringName, Color> &E : color_map[p_theme_type]) {
  582. p_list->push_back(E.key);
  583. }
  584. }
  585. void Theme::add_color_type(const StringName &p_theme_type) {
  586. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  587. if (color_map.has(p_theme_type)) {
  588. return;
  589. }
  590. color_map[p_theme_type] = ThemeColorMap();
  591. }
  592. void Theme::remove_color_type(const StringName &p_theme_type) {
  593. if (!color_map.has(p_theme_type)) {
  594. return;
  595. }
  596. color_map.erase(p_theme_type);
  597. }
  598. void Theme::get_color_type_list(List<StringName> *p_list) const {
  599. ERR_FAIL_NULL(p_list);
  600. for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
  601. p_list->push_back(E.key);
  602. }
  603. }
  604. // Theme constants.
  605. void Theme::set_constant(const StringName &p_name, const StringName &p_theme_type, int p_constant) {
  606. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  607. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  608. bool existing = has_constant_nocheck(p_name, p_theme_type);
  609. constant_map[p_theme_type][p_name] = p_constant;
  610. _emit_theme_changed(!existing);
  611. }
  612. int Theme::get_constant(const StringName &p_name, const StringName &p_theme_type) const {
  613. if (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name)) {
  614. return constant_map[p_theme_type][p_name];
  615. } else {
  616. return 0;
  617. }
  618. }
  619. bool Theme::has_constant(const StringName &p_name, const StringName &p_theme_type) const {
  620. return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
  621. }
  622. bool Theme::has_constant_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  623. return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
  624. }
  625. void Theme::rename_constant(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  626. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  627. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  628. ERR_FAIL_COND_MSG(!constant_map.has(p_theme_type), "Cannot rename the constant '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  629. ERR_FAIL_COND_MSG(constant_map[p_theme_type].has(p_name), "Cannot rename the constant '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
  630. ERR_FAIL_COND_MSG(!constant_map[p_theme_type].has(p_old_name), "Cannot rename the constant '" + String(p_old_name) + "' because it does not exist.");
  631. constant_map[p_theme_type][p_name] = constant_map[p_theme_type][p_old_name];
  632. constant_map[p_theme_type].erase(p_old_name);
  633. _emit_theme_changed(true);
  634. }
  635. void Theme::clear_constant(const StringName &p_name, const StringName &p_theme_type) {
  636. ERR_FAIL_COND_MSG(!constant_map.has(p_theme_type), "Cannot clear the constant '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
  637. ERR_FAIL_COND_MSG(!constant_map[p_theme_type].has(p_name), "Cannot clear the constant '" + String(p_name) + "' because it does not exist.");
  638. constant_map[p_theme_type].erase(p_name);
  639. _emit_theme_changed(true);
  640. }
  641. void Theme::get_constant_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  642. ERR_FAIL_NULL(p_list);
  643. if (!constant_map.has(p_theme_type)) {
  644. return;
  645. }
  646. for (const KeyValue<StringName, int> &E : constant_map[p_theme_type]) {
  647. p_list->push_back(E.key);
  648. }
  649. }
  650. void Theme::add_constant_type(const StringName &p_theme_type) {
  651. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  652. if (constant_map.has(p_theme_type)) {
  653. return;
  654. }
  655. constant_map[p_theme_type] = ThemeConstantMap();
  656. }
  657. void Theme::remove_constant_type(const StringName &p_theme_type) {
  658. if (!constant_map.has(p_theme_type)) {
  659. return;
  660. }
  661. constant_map.erase(p_theme_type);
  662. }
  663. void Theme::get_constant_type_list(List<StringName> *p_list) const {
  664. ERR_FAIL_NULL(p_list);
  665. for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
  666. p_list->push_back(E.key);
  667. }
  668. }
  669. // Generic methods for managing theme items.
  670. void Theme::set_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type, const Variant &p_value) {
  671. switch (p_data_type) {
  672. case DATA_TYPE_COLOR: {
  673. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::COLOR, "Theme item's data type (Color) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  674. Color color_value = p_value;
  675. set_color(p_name, p_theme_type, color_value);
  676. } break;
  677. case DATA_TYPE_CONSTANT: {
  678. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::INT, "Theme item's data type (int) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  679. int constant_value = p_value;
  680. set_constant(p_name, p_theme_type, constant_value);
  681. } break;
  682. case DATA_TYPE_FONT: {
  683. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  684. Ref<Font> font_value = Object::cast_to<Font>(p_value.get_validated_object());
  685. set_font(p_name, p_theme_type, font_value);
  686. } break;
  687. case DATA_TYPE_FONT_SIZE: {
  688. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::INT, "Theme item's data type (int) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  689. int font_size_value = p_value;
  690. set_font_size(p_name, p_theme_type, font_size_value);
  691. } break;
  692. case DATA_TYPE_ICON: {
  693. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  694. Ref<Texture2D> icon_value = Object::cast_to<Texture2D>(p_value.get_validated_object());
  695. set_icon(p_name, p_theme_type, icon_value);
  696. } break;
  697. case DATA_TYPE_STYLEBOX: {
  698. ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
  699. Ref<StyleBox> stylebox_value = Object::cast_to<StyleBox>(p_value.get_validated_object());
  700. set_stylebox(p_name, p_theme_type, stylebox_value);
  701. } break;
  702. case DATA_TYPE_MAX:
  703. break; // Can't happen, but silences warning.
  704. }
  705. }
  706. Variant Theme::get_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  707. switch (p_data_type) {
  708. case DATA_TYPE_COLOR:
  709. return get_color(p_name, p_theme_type);
  710. case DATA_TYPE_CONSTANT:
  711. return get_constant(p_name, p_theme_type);
  712. case DATA_TYPE_FONT:
  713. return get_font(p_name, p_theme_type);
  714. case DATA_TYPE_FONT_SIZE:
  715. return get_font_size(p_name, p_theme_type);
  716. case DATA_TYPE_ICON:
  717. return get_icon(p_name, p_theme_type);
  718. case DATA_TYPE_STYLEBOX:
  719. return get_stylebox(p_name, p_theme_type);
  720. case DATA_TYPE_MAX:
  721. break; // Can't happen, but silences warning.
  722. }
  723. return Variant();
  724. }
  725. bool Theme::has_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  726. switch (p_data_type) {
  727. case DATA_TYPE_COLOR:
  728. return has_color(p_name, p_theme_type);
  729. case DATA_TYPE_CONSTANT:
  730. return has_constant(p_name, p_theme_type);
  731. case DATA_TYPE_FONT:
  732. return has_font(p_name, p_theme_type);
  733. case DATA_TYPE_FONT_SIZE:
  734. return has_font_size(p_name, p_theme_type);
  735. case DATA_TYPE_ICON:
  736. return has_icon(p_name, p_theme_type);
  737. case DATA_TYPE_STYLEBOX:
  738. return has_stylebox(p_name, p_theme_type);
  739. case DATA_TYPE_MAX:
  740. break; // Can't happen, but silences warning.
  741. }
  742. return false;
  743. }
  744. bool Theme::has_theme_item_nocheck(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  745. switch (p_data_type) {
  746. case DATA_TYPE_COLOR:
  747. return has_color_nocheck(p_name, p_theme_type);
  748. case DATA_TYPE_CONSTANT:
  749. return has_constant_nocheck(p_name, p_theme_type);
  750. case DATA_TYPE_FONT:
  751. return has_font_nocheck(p_name, p_theme_type);
  752. case DATA_TYPE_FONT_SIZE:
  753. return has_font_size_nocheck(p_name, p_theme_type);
  754. case DATA_TYPE_ICON:
  755. return has_icon_nocheck(p_name, p_theme_type);
  756. case DATA_TYPE_STYLEBOX:
  757. return has_stylebox_nocheck(p_name, p_theme_type);
  758. case DATA_TYPE_MAX:
  759. break; // Can't happen, but silences warning.
  760. }
  761. return false;
  762. }
  763. void Theme::rename_theme_item(DataType p_data_type, const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  764. switch (p_data_type) {
  765. case DATA_TYPE_COLOR:
  766. rename_color(p_old_name, p_name, p_theme_type);
  767. break;
  768. case DATA_TYPE_CONSTANT:
  769. rename_constant(p_old_name, p_name, p_theme_type);
  770. break;
  771. case DATA_TYPE_FONT:
  772. rename_font(p_old_name, p_name, p_theme_type);
  773. break;
  774. case DATA_TYPE_FONT_SIZE:
  775. rename_font_size(p_old_name, p_name, p_theme_type);
  776. break;
  777. case DATA_TYPE_ICON:
  778. rename_icon(p_old_name, p_name, p_theme_type);
  779. break;
  780. case DATA_TYPE_STYLEBOX:
  781. rename_stylebox(p_old_name, p_name, p_theme_type);
  782. break;
  783. case DATA_TYPE_MAX:
  784. break; // Can't happen, but silences warning.
  785. }
  786. }
  787. void Theme::clear_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) {
  788. switch (p_data_type) {
  789. case DATA_TYPE_COLOR:
  790. clear_color(p_name, p_theme_type);
  791. break;
  792. case DATA_TYPE_CONSTANT:
  793. clear_constant(p_name, p_theme_type);
  794. break;
  795. case DATA_TYPE_FONT:
  796. clear_font(p_name, p_theme_type);
  797. break;
  798. case DATA_TYPE_FONT_SIZE:
  799. clear_font_size(p_name, p_theme_type);
  800. break;
  801. case DATA_TYPE_ICON:
  802. clear_icon(p_name, p_theme_type);
  803. break;
  804. case DATA_TYPE_STYLEBOX:
  805. clear_stylebox(p_name, p_theme_type);
  806. break;
  807. case DATA_TYPE_MAX:
  808. break; // Can't happen, but silences warning.
  809. }
  810. }
  811. void Theme::get_theme_item_list(DataType p_data_type, const StringName &p_theme_type, List<StringName> *p_list) const {
  812. switch (p_data_type) {
  813. case DATA_TYPE_COLOR:
  814. get_color_list(p_theme_type, p_list);
  815. break;
  816. case DATA_TYPE_CONSTANT:
  817. get_constant_list(p_theme_type, p_list);
  818. break;
  819. case DATA_TYPE_FONT:
  820. get_font_list(p_theme_type, p_list);
  821. break;
  822. case DATA_TYPE_FONT_SIZE:
  823. get_font_size_list(p_theme_type, p_list);
  824. break;
  825. case DATA_TYPE_ICON:
  826. get_icon_list(p_theme_type, p_list);
  827. break;
  828. case DATA_TYPE_STYLEBOX:
  829. get_stylebox_list(p_theme_type, p_list);
  830. break;
  831. case DATA_TYPE_MAX:
  832. break; // Can't happen, but silences warning.
  833. }
  834. }
  835. void Theme::add_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
  836. switch (p_data_type) {
  837. case DATA_TYPE_COLOR:
  838. add_color_type(p_theme_type);
  839. break;
  840. case DATA_TYPE_CONSTANT:
  841. add_constant_type(p_theme_type);
  842. break;
  843. case DATA_TYPE_FONT:
  844. add_font_type(p_theme_type);
  845. break;
  846. case DATA_TYPE_FONT_SIZE:
  847. add_font_size_type(p_theme_type);
  848. break;
  849. case DATA_TYPE_ICON:
  850. add_icon_type(p_theme_type);
  851. break;
  852. case DATA_TYPE_STYLEBOX:
  853. add_stylebox_type(p_theme_type);
  854. break;
  855. case DATA_TYPE_MAX:
  856. break; // Can't happen, but silences warning.
  857. }
  858. }
  859. void Theme::remove_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
  860. switch (p_data_type) {
  861. case DATA_TYPE_COLOR:
  862. remove_color_type(p_theme_type);
  863. break;
  864. case DATA_TYPE_CONSTANT:
  865. remove_constant_type(p_theme_type);
  866. break;
  867. case DATA_TYPE_FONT:
  868. remove_font_type(p_theme_type);
  869. break;
  870. case DATA_TYPE_FONT_SIZE:
  871. remove_font_size_type(p_theme_type);
  872. break;
  873. case DATA_TYPE_ICON:
  874. remove_icon_type(p_theme_type);
  875. break;
  876. case DATA_TYPE_STYLEBOX:
  877. remove_stylebox_type(p_theme_type);
  878. break;
  879. case DATA_TYPE_MAX:
  880. break; // Can't happen, but silences warning.
  881. }
  882. }
  883. void Theme::get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const {
  884. switch (p_data_type) {
  885. case DATA_TYPE_COLOR:
  886. get_color_type_list(p_list);
  887. break;
  888. case DATA_TYPE_CONSTANT:
  889. get_constant_type_list(p_list);
  890. break;
  891. case DATA_TYPE_FONT:
  892. get_font_type_list(p_list);
  893. break;
  894. case DATA_TYPE_FONT_SIZE:
  895. get_font_size_type_list(p_list);
  896. break;
  897. case DATA_TYPE_ICON:
  898. get_icon_type_list(p_list);
  899. break;
  900. case DATA_TYPE_STYLEBOX:
  901. get_stylebox_type_list(p_list);
  902. break;
  903. case DATA_TYPE_MAX:
  904. break; // Can't happen, but silences warning.
  905. }
  906. }
  907. // Theme type variations.
  908. void Theme::set_type_variation(const StringName &p_theme_type, const StringName &p_base_type) {
  909. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  910. ERR_FAIL_COND_MSG(!is_valid_type_name(p_base_type), vformat("Invalid type name: '%s'", p_base_type));
  911. ERR_FAIL_COND_MSG(p_theme_type == StringName(), "An empty theme type cannot be marked as a variation of another type.");
  912. ERR_FAIL_COND_MSG(ClassDB::class_exists(p_theme_type), "A type associated with a built-in class cannot be marked as a variation of another type.");
  913. ERR_FAIL_COND_MSG(p_base_type == StringName(), "An empty theme type cannot be the base type of a variation. Use clear_type_variation() instead if you want to unmark '" + String(p_theme_type) + "' as a variation.");
  914. if (variation_map.has(p_theme_type)) {
  915. StringName old_base = variation_map[p_theme_type];
  916. variation_base_map[old_base].erase(p_theme_type);
  917. }
  918. variation_map[p_theme_type] = p_base_type;
  919. variation_base_map[p_base_type].push_back(p_theme_type);
  920. _emit_theme_changed(true);
  921. }
  922. bool Theme::is_type_variation(const StringName &p_theme_type, const StringName &p_base_type) const {
  923. return (variation_map.has(p_theme_type) && variation_map[p_theme_type] == p_base_type);
  924. }
  925. void Theme::clear_type_variation(const StringName &p_theme_type) {
  926. ERR_FAIL_COND_MSG(!variation_map.has(p_theme_type), "Cannot clear the type variation '" + String(p_theme_type) + "' because it does not exist.");
  927. StringName base_type = variation_map[p_theme_type];
  928. variation_base_map[base_type].erase(p_theme_type);
  929. variation_map.erase(p_theme_type);
  930. _emit_theme_changed(true);
  931. }
  932. StringName Theme::get_type_variation_base(const StringName &p_theme_type) const {
  933. if (!variation_map.has(p_theme_type)) {
  934. return StringName();
  935. }
  936. return variation_map[p_theme_type];
  937. }
  938. void Theme::get_type_variation_list(const StringName &p_base_type, List<StringName> *p_list) const {
  939. ERR_FAIL_NULL(p_list);
  940. if (!variation_base_map.has(p_base_type)) {
  941. return;
  942. }
  943. for (const StringName &E : variation_base_map[p_base_type]) {
  944. // Prevent infinite loops if variants were set to be cross-dependent (that's still invalid usage, but handling for stability sake).
  945. if (p_list->find(E)) {
  946. continue;
  947. }
  948. p_list->push_back(E);
  949. // Continue looking for sub-variations.
  950. get_type_variation_list(E, p_list);
  951. }
  952. }
  953. // Theme types.
  954. void Theme::add_type(const StringName &p_theme_type) {
  955. // Add a record to every data type map.
  956. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  957. Theme::DataType dt = (Theme::DataType)i;
  958. add_theme_item_type(dt, p_theme_type);
  959. }
  960. _emit_theme_changed(true);
  961. }
  962. void Theme::remove_type(const StringName &p_theme_type) {
  963. // Gracefully remove the record from every data type map.
  964. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  965. Theme::DataType dt = (Theme::DataType)i;
  966. remove_theme_item_type(dt, p_theme_type);
  967. }
  968. // If type is a variation, remove that connection.
  969. if (get_type_variation_base(p_theme_type) != StringName()) {
  970. clear_type_variation(p_theme_type);
  971. }
  972. // If type is a variation base, remove all those connections.
  973. List<StringName> names;
  974. get_type_variation_list(p_theme_type, &names);
  975. for (const StringName &E : names) {
  976. clear_type_variation(E);
  977. }
  978. _emit_theme_changed(true);
  979. }
  980. void Theme::get_type_list(List<StringName> *p_list) const {
  981. ERR_FAIL_NULL(p_list);
  982. // This Set guarantees uniqueness.
  983. // Because each map can have the same type defined, but for this method
  984. // we only want one occurrence of each type.
  985. HashSet<StringName> types;
  986. // Icons.
  987. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  988. types.insert(E.key);
  989. }
  990. // Styles.
  991. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  992. types.insert(E.key);
  993. }
  994. // Fonts.
  995. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  996. types.insert(E.key);
  997. }
  998. // Font sizes.
  999. for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
  1000. types.insert(E.key);
  1001. }
  1002. // Colors.
  1003. for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
  1004. types.insert(E.key);
  1005. }
  1006. // Constants.
  1007. for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
  1008. types.insert(E.key);
  1009. }
  1010. // Variations.
  1011. for (const KeyValue<StringName, StringName> &E : variation_map) {
  1012. types.insert(E.key);
  1013. }
  1014. for (const StringName &E : types) {
  1015. p_list->push_back(E);
  1016. }
  1017. }
  1018. void Theme::get_type_dependencies(const StringName &p_base_type, const StringName &p_type_variation, Vector<StringName> &r_result) {
  1019. // Build the dependency chain for type variations.
  1020. if (p_type_variation != StringName()) {
  1021. StringName variation_name = p_type_variation;
  1022. while (variation_name != StringName()) {
  1023. r_result.push_back(variation_name);
  1024. variation_name = get_type_variation_base(variation_name);
  1025. // If we have reached the base type dependency, it's safe to stop (assuming no funny business was done to the Theme).
  1026. if (variation_name == p_base_type) {
  1027. break;
  1028. }
  1029. }
  1030. }
  1031. // Continue building the chain using native class hierarchy.
  1032. ThemeDB::get_singleton()->get_native_type_dependencies(p_base_type, r_result);
  1033. }
  1034. // Internal methods for getting lists as a Vector of String (compatible with public API).
  1035. Vector<String> Theme::_get_icon_list(const String &p_theme_type) const {
  1036. Vector<String> ilret;
  1037. List<StringName> il;
  1038. get_icon_list(p_theme_type, &il);
  1039. ilret.resize(il.size());
  1040. int i = 0;
  1041. String *w = ilret.ptrw();
  1042. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1043. w[i] = E->get();
  1044. }
  1045. return ilret;
  1046. }
  1047. Vector<String> Theme::_get_icon_type_list() const {
  1048. Vector<String> ilret;
  1049. List<StringName> il;
  1050. get_icon_type_list(&il);
  1051. ilret.resize(il.size());
  1052. int i = 0;
  1053. String *w = ilret.ptrw();
  1054. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1055. w[i] = E->get();
  1056. }
  1057. return ilret;
  1058. }
  1059. Vector<String> Theme::_get_stylebox_list(const String &p_theme_type) const {
  1060. Vector<String> ilret;
  1061. List<StringName> il;
  1062. get_stylebox_list(p_theme_type, &il);
  1063. ilret.resize(il.size());
  1064. int i = 0;
  1065. String *w = ilret.ptrw();
  1066. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1067. w[i] = E->get();
  1068. }
  1069. return ilret;
  1070. }
  1071. Vector<String> Theme::_get_stylebox_type_list() const {
  1072. Vector<String> ilret;
  1073. List<StringName> il;
  1074. get_stylebox_type_list(&il);
  1075. ilret.resize(il.size());
  1076. int i = 0;
  1077. String *w = ilret.ptrw();
  1078. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1079. w[i] = E->get();
  1080. }
  1081. return ilret;
  1082. }
  1083. Vector<String> Theme::_get_font_list(const String &p_theme_type) const {
  1084. Vector<String> ilret;
  1085. List<StringName> il;
  1086. get_font_list(p_theme_type, &il);
  1087. ilret.resize(il.size());
  1088. int i = 0;
  1089. String *w = ilret.ptrw();
  1090. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1091. w[i] = E->get();
  1092. }
  1093. return ilret;
  1094. }
  1095. Vector<String> Theme::_get_font_type_list() const {
  1096. Vector<String> ilret;
  1097. List<StringName> il;
  1098. get_font_type_list(&il);
  1099. ilret.resize(il.size());
  1100. int i = 0;
  1101. String *w = ilret.ptrw();
  1102. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1103. w[i] = E->get();
  1104. }
  1105. return ilret;
  1106. }
  1107. Vector<String> Theme::_get_font_size_list(const String &p_theme_type) const {
  1108. Vector<String> ilret;
  1109. List<StringName> il;
  1110. get_font_size_list(p_theme_type, &il);
  1111. ilret.resize(il.size());
  1112. int i = 0;
  1113. String *w = ilret.ptrw();
  1114. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1115. w[i] = E->get();
  1116. }
  1117. return ilret;
  1118. }
  1119. Vector<String> Theme::_get_font_size_type_list() const {
  1120. Vector<String> ilret;
  1121. List<StringName> il;
  1122. get_font_size_type_list(&il);
  1123. ilret.resize(il.size());
  1124. int i = 0;
  1125. String *w = ilret.ptrw();
  1126. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1127. w[i] = E->get();
  1128. }
  1129. return ilret;
  1130. }
  1131. Vector<String> Theme::_get_color_list(const String &p_theme_type) const {
  1132. Vector<String> ilret;
  1133. List<StringName> il;
  1134. get_color_list(p_theme_type, &il);
  1135. ilret.resize(il.size());
  1136. int i = 0;
  1137. String *w = ilret.ptrw();
  1138. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1139. w[i] = E->get();
  1140. }
  1141. return ilret;
  1142. }
  1143. Vector<String> Theme::_get_color_type_list() const {
  1144. Vector<String> ilret;
  1145. List<StringName> il;
  1146. get_color_type_list(&il);
  1147. ilret.resize(il.size());
  1148. int i = 0;
  1149. String *w = ilret.ptrw();
  1150. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1151. w[i] = E->get();
  1152. }
  1153. return ilret;
  1154. }
  1155. Vector<String> Theme::_get_constant_list(const String &p_theme_type) const {
  1156. Vector<String> ilret;
  1157. List<StringName> il;
  1158. get_constant_list(p_theme_type, &il);
  1159. ilret.resize(il.size());
  1160. int i = 0;
  1161. String *w = ilret.ptrw();
  1162. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1163. w[i] = E->get();
  1164. }
  1165. return ilret;
  1166. }
  1167. Vector<String> Theme::_get_constant_type_list() const {
  1168. Vector<String> ilret;
  1169. List<StringName> il;
  1170. get_constant_type_list(&il);
  1171. ilret.resize(il.size());
  1172. int i = 0;
  1173. String *w = ilret.ptrw();
  1174. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1175. w[i] = E->get();
  1176. }
  1177. return ilret;
  1178. }
  1179. Vector<String> Theme::_get_theme_item_list(DataType p_data_type, const String &p_theme_type) const {
  1180. switch (p_data_type) {
  1181. case DATA_TYPE_COLOR:
  1182. return _get_color_list(p_theme_type);
  1183. case DATA_TYPE_CONSTANT:
  1184. return _get_constant_list(p_theme_type);
  1185. case DATA_TYPE_FONT:
  1186. return _get_font_list(p_theme_type);
  1187. case DATA_TYPE_FONT_SIZE:
  1188. return _get_font_size_list(p_theme_type);
  1189. case DATA_TYPE_ICON:
  1190. return _get_icon_list(p_theme_type);
  1191. case DATA_TYPE_STYLEBOX:
  1192. return _get_stylebox_list(p_theme_type);
  1193. case DATA_TYPE_MAX:
  1194. break; // Can't happen, but silences warning.
  1195. }
  1196. return Vector<String>();
  1197. }
  1198. Vector<String> Theme::_get_theme_item_type_list(DataType p_data_type) const {
  1199. switch (p_data_type) {
  1200. case DATA_TYPE_COLOR:
  1201. return _get_color_type_list();
  1202. case DATA_TYPE_CONSTANT:
  1203. return _get_constant_type_list();
  1204. case DATA_TYPE_FONT:
  1205. return _get_font_type_list();
  1206. case DATA_TYPE_FONT_SIZE:
  1207. return _get_font_size_type_list();
  1208. case DATA_TYPE_ICON:
  1209. return _get_icon_type_list();
  1210. case DATA_TYPE_STYLEBOX:
  1211. return _get_stylebox_type_list();
  1212. case DATA_TYPE_MAX:
  1213. break; // Can't happen, but silences warning.
  1214. }
  1215. return Vector<String>();
  1216. }
  1217. Vector<String> Theme::_get_type_variation_list(const StringName &p_theme_type) const {
  1218. Vector<String> ilret;
  1219. List<StringName> il;
  1220. get_type_variation_list(p_theme_type, &il);
  1221. ilret.resize(il.size());
  1222. int i = 0;
  1223. String *w = ilret.ptrw();
  1224. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1225. w[i] = E->get();
  1226. }
  1227. return ilret;
  1228. }
  1229. Vector<String> Theme::_get_type_list() const {
  1230. Vector<String> ilret;
  1231. List<StringName> il;
  1232. get_type_list(&il);
  1233. ilret.resize(il.size());
  1234. int i = 0;
  1235. String *w = ilret.ptrw();
  1236. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1237. w[i] = E->get();
  1238. }
  1239. return ilret;
  1240. }
  1241. // Theme bulk manipulations.
  1242. void Theme::_emit_theme_changed(bool p_notify_list_changed) {
  1243. if (no_change_propagation) {
  1244. return;
  1245. }
  1246. if (p_notify_list_changed) {
  1247. notify_property_list_changed();
  1248. }
  1249. emit_changed();
  1250. }
  1251. void Theme::_freeze_change_propagation() {
  1252. no_change_propagation = true;
  1253. }
  1254. void Theme::_unfreeze_and_propagate_changes() {
  1255. no_change_propagation = false;
  1256. _emit_theme_changed(true);
  1257. }
  1258. void Theme::merge_with(const Ref<Theme> &p_other) {
  1259. if (p_other.is_null()) {
  1260. return;
  1261. }
  1262. _freeze_change_propagation();
  1263. // Colors.
  1264. {
  1265. for (const KeyValue<StringName, ThemeColorMap> &E : p_other->color_map) {
  1266. for (const KeyValue<StringName, Color> &F : E.value) {
  1267. set_color(F.key, E.key, F.value);
  1268. }
  1269. }
  1270. }
  1271. // Constants.
  1272. {
  1273. for (const KeyValue<StringName, ThemeConstantMap> &E : p_other->constant_map) {
  1274. for (const KeyValue<StringName, int> &F : E.value) {
  1275. set_constant(F.key, E.key, F.value);
  1276. }
  1277. }
  1278. }
  1279. // Fonts.
  1280. {
  1281. for (const KeyValue<StringName, ThemeFontMap> &E : p_other->font_map) {
  1282. for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
  1283. set_font(F.key, E.key, F.value);
  1284. }
  1285. }
  1286. }
  1287. // Font sizes.
  1288. {
  1289. for (const KeyValue<StringName, ThemeFontSizeMap> &E : p_other->font_size_map) {
  1290. for (const KeyValue<StringName, int> &F : E.value) {
  1291. set_font_size(F.key, E.key, F.value);
  1292. }
  1293. }
  1294. }
  1295. // Icons.
  1296. {
  1297. for (const KeyValue<StringName, ThemeIconMap> &E : p_other->icon_map) {
  1298. for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
  1299. set_icon(F.key, E.key, F.value);
  1300. }
  1301. }
  1302. }
  1303. // Styleboxes.
  1304. {
  1305. for (const KeyValue<StringName, ThemeStyleMap> &E : p_other->style_map) {
  1306. for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
  1307. set_stylebox(F.key, E.key, F.value);
  1308. }
  1309. }
  1310. }
  1311. // Type variations.
  1312. {
  1313. for (const KeyValue<StringName, StringName> &E : p_other->variation_map) {
  1314. set_type_variation(E.key, E.value);
  1315. }
  1316. }
  1317. // Defaults.
  1318. if (p_other->has_default_font()) {
  1319. set_default_font(p_other->default_font);
  1320. }
  1321. if (p_other->has_default_font_size()) {
  1322. set_default_font_size(p_other->default_font_size);
  1323. }
  1324. if (p_other->has_default_base_scale()) {
  1325. set_default_base_scale(p_other->default_base_scale);
  1326. }
  1327. _unfreeze_and_propagate_changes();
  1328. }
  1329. void Theme::clear() {
  1330. // These items need disconnecting.
  1331. {
  1332. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  1333. for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
  1334. if (F.value.is_valid()) {
  1335. Ref<Texture2D> icon = F.value;
  1336. icon->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1337. }
  1338. }
  1339. }
  1340. }
  1341. {
  1342. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  1343. for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
  1344. if (F.value.is_valid()) {
  1345. Ref<StyleBox> style = F.value;
  1346. style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1347. }
  1348. }
  1349. }
  1350. }
  1351. {
  1352. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  1353. for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
  1354. if (F.value.is_valid()) {
  1355. Ref<Font> font = F.value;
  1356. font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1357. }
  1358. }
  1359. }
  1360. }
  1361. icon_map.clear();
  1362. style_map.clear();
  1363. font_map.clear();
  1364. font_size_map.clear();
  1365. color_map.clear();
  1366. constant_map.clear();
  1367. variation_map.clear();
  1368. variation_base_map.clear();
  1369. _emit_theme_changed(true);
  1370. }
  1371. void Theme::reset_state() {
  1372. clear();
  1373. }
  1374. void Theme::_bind_methods() {
  1375. ClassDB::bind_method(D_METHOD("set_icon", "name", "theme_type", "texture"), &Theme::set_icon);
  1376. ClassDB::bind_method(D_METHOD("get_icon", "name", "theme_type"), &Theme::get_icon);
  1377. ClassDB::bind_method(D_METHOD("has_icon", "name", "theme_type"), &Theme::has_icon);
  1378. ClassDB::bind_method(D_METHOD("rename_icon", "old_name", "name", "theme_type"), &Theme::rename_icon);
  1379. ClassDB::bind_method(D_METHOD("clear_icon", "name", "theme_type"), &Theme::clear_icon);
  1380. ClassDB::bind_method(D_METHOD("get_icon_list", "theme_type"), &Theme::_get_icon_list);
  1381. ClassDB::bind_method(D_METHOD("get_icon_type_list"), &Theme::_get_icon_type_list);
  1382. ClassDB::bind_method(D_METHOD("set_stylebox", "name", "theme_type", "texture"), &Theme::set_stylebox);
  1383. ClassDB::bind_method(D_METHOD("get_stylebox", "name", "theme_type"), &Theme::get_stylebox);
  1384. ClassDB::bind_method(D_METHOD("has_stylebox", "name", "theme_type"), &Theme::has_stylebox);
  1385. ClassDB::bind_method(D_METHOD("rename_stylebox", "old_name", "name", "theme_type"), &Theme::rename_stylebox);
  1386. ClassDB::bind_method(D_METHOD("clear_stylebox", "name", "theme_type"), &Theme::clear_stylebox);
  1387. ClassDB::bind_method(D_METHOD("get_stylebox_list", "theme_type"), &Theme::_get_stylebox_list);
  1388. ClassDB::bind_method(D_METHOD("get_stylebox_type_list"), &Theme::_get_stylebox_type_list);
  1389. ClassDB::bind_method(D_METHOD("set_font", "name", "theme_type", "font"), &Theme::set_font);
  1390. ClassDB::bind_method(D_METHOD("get_font", "name", "theme_type"), &Theme::get_font);
  1391. ClassDB::bind_method(D_METHOD("has_font", "name", "theme_type"), &Theme::has_font);
  1392. ClassDB::bind_method(D_METHOD("rename_font", "old_name", "name", "theme_type"), &Theme::rename_font);
  1393. ClassDB::bind_method(D_METHOD("clear_font", "name", "theme_type"), &Theme::clear_font);
  1394. ClassDB::bind_method(D_METHOD("get_font_list", "theme_type"), &Theme::_get_font_list);
  1395. ClassDB::bind_method(D_METHOD("get_font_type_list"), &Theme::_get_font_type_list);
  1396. ClassDB::bind_method(D_METHOD("set_font_size", "name", "theme_type", "font_size"), &Theme::set_font_size);
  1397. ClassDB::bind_method(D_METHOD("get_font_size", "name", "theme_type"), &Theme::get_font_size);
  1398. ClassDB::bind_method(D_METHOD("has_font_size", "name", "theme_type"), &Theme::has_font_size);
  1399. ClassDB::bind_method(D_METHOD("rename_font_size", "old_name", "name", "theme_type"), &Theme::rename_font_size);
  1400. ClassDB::bind_method(D_METHOD("clear_font_size", "name", "theme_type"), &Theme::clear_font_size);
  1401. ClassDB::bind_method(D_METHOD("get_font_size_list", "theme_type"), &Theme::_get_font_size_list);
  1402. ClassDB::bind_method(D_METHOD("get_font_size_type_list"), &Theme::_get_font_size_type_list);
  1403. ClassDB::bind_method(D_METHOD("set_color", "name", "theme_type", "color"), &Theme::set_color);
  1404. ClassDB::bind_method(D_METHOD("get_color", "name", "theme_type"), &Theme::get_color);
  1405. ClassDB::bind_method(D_METHOD("has_color", "name", "theme_type"), &Theme::has_color);
  1406. ClassDB::bind_method(D_METHOD("rename_color", "old_name", "name", "theme_type"), &Theme::rename_color);
  1407. ClassDB::bind_method(D_METHOD("clear_color", "name", "theme_type"), &Theme::clear_color);
  1408. ClassDB::bind_method(D_METHOD("get_color_list", "theme_type"), &Theme::_get_color_list);
  1409. ClassDB::bind_method(D_METHOD("get_color_type_list"), &Theme::_get_color_type_list);
  1410. ClassDB::bind_method(D_METHOD("set_constant", "name", "theme_type", "constant"), &Theme::set_constant);
  1411. ClassDB::bind_method(D_METHOD("get_constant", "name", "theme_type"), &Theme::get_constant);
  1412. ClassDB::bind_method(D_METHOD("has_constant", "name", "theme_type"), &Theme::has_constant);
  1413. ClassDB::bind_method(D_METHOD("rename_constant", "old_name", "name", "theme_type"), &Theme::rename_constant);
  1414. ClassDB::bind_method(D_METHOD("clear_constant", "name", "theme_type"), &Theme::clear_constant);
  1415. ClassDB::bind_method(D_METHOD("get_constant_list", "theme_type"), &Theme::_get_constant_list);
  1416. ClassDB::bind_method(D_METHOD("get_constant_type_list"), &Theme::_get_constant_type_list);
  1417. ClassDB::bind_method(D_METHOD("set_default_base_scale", "base_scale"), &Theme::set_default_base_scale);
  1418. ClassDB::bind_method(D_METHOD("get_default_base_scale"), &Theme::get_default_base_scale);
  1419. ClassDB::bind_method(D_METHOD("has_default_base_scale"), &Theme::has_default_base_scale);
  1420. ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_font);
  1421. ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_font);
  1422. ClassDB::bind_method(D_METHOD("has_default_font"), &Theme::has_default_font);
  1423. ClassDB::bind_method(D_METHOD("set_default_font_size", "font_size"), &Theme::set_default_font_size);
  1424. ClassDB::bind_method(D_METHOD("get_default_font_size"), &Theme::get_default_font_size);
  1425. ClassDB::bind_method(D_METHOD("has_default_font_size"), &Theme::has_default_font_size);
  1426. ClassDB::bind_method(D_METHOD("set_theme_item", "data_type", "name", "theme_type", "value"), &Theme::set_theme_item);
  1427. ClassDB::bind_method(D_METHOD("get_theme_item", "data_type", "name", "theme_type"), &Theme::get_theme_item);
  1428. ClassDB::bind_method(D_METHOD("has_theme_item", "data_type", "name", "theme_type"), &Theme::has_theme_item);
  1429. ClassDB::bind_method(D_METHOD("rename_theme_item", "data_type", "old_name", "name", "theme_type"), &Theme::rename_theme_item);
  1430. ClassDB::bind_method(D_METHOD("clear_theme_item", "data_type", "name", "theme_type"), &Theme::clear_theme_item);
  1431. ClassDB::bind_method(D_METHOD("get_theme_item_list", "data_type", "theme_type"), &Theme::_get_theme_item_list);
  1432. ClassDB::bind_method(D_METHOD("get_theme_item_type_list", "data_type"), &Theme::_get_theme_item_type_list);
  1433. ClassDB::bind_method(D_METHOD("set_type_variation", "theme_type", "base_type"), &Theme::set_type_variation);
  1434. ClassDB::bind_method(D_METHOD("is_type_variation", "theme_type", "base_type"), &Theme::is_type_variation);
  1435. ClassDB::bind_method(D_METHOD("clear_type_variation", "theme_type"), &Theme::clear_type_variation);
  1436. ClassDB::bind_method(D_METHOD("get_type_variation_base", "theme_type"), &Theme::get_type_variation_base);
  1437. ClassDB::bind_method(D_METHOD("get_type_variation_list", "base_type"), &Theme::_get_type_variation_list);
  1438. ClassDB::bind_method(D_METHOD("add_type", "theme_type"), &Theme::add_type);
  1439. ClassDB::bind_method(D_METHOD("remove_type", "theme_type"), &Theme::remove_type);
  1440. ClassDB::bind_method(D_METHOD("get_type_list"), &Theme::_get_type_list);
  1441. ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with);
  1442. ClassDB::bind_method(D_METHOD("clear"), &Theme::clear);
  1443. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_default_base_scale", "get_default_base_scale");
  1444. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");
  1445. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"), "set_default_font_size", "get_default_font_size");
  1446. BIND_ENUM_CONSTANT(DATA_TYPE_COLOR);
  1447. BIND_ENUM_CONSTANT(DATA_TYPE_CONSTANT);
  1448. BIND_ENUM_CONSTANT(DATA_TYPE_FONT);
  1449. BIND_ENUM_CONSTANT(DATA_TYPE_FONT_SIZE);
  1450. BIND_ENUM_CONSTANT(DATA_TYPE_ICON);
  1451. BIND_ENUM_CONSTANT(DATA_TYPE_STYLEBOX);
  1452. BIND_ENUM_CONSTANT(DATA_TYPE_MAX);
  1453. }
  1454. Theme::Theme() {
  1455. }
  1456. Theme::~Theme() {
  1457. }