theme.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. }