theme.cpp 61 KB

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