theme.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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_slicec('/', 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::rename_icon_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  298. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  299. if (!icon_map.has(p_old_theme_type) || icon_map.has(p_theme_type)) {
  300. return;
  301. }
  302. icon_map[p_theme_type] = icon_map[p_old_theme_type];
  303. icon_map.erase(p_old_theme_type);
  304. }
  305. void Theme::get_icon_type_list(List<StringName> *p_list) const {
  306. ERR_FAIL_NULL(p_list);
  307. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  308. p_list->push_back(E.key);
  309. }
  310. }
  311. // Styleboxes.
  312. void Theme::set_stylebox(const StringName &p_name, const StringName &p_theme_type, const Ref<StyleBox> &p_style) {
  313. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  314. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  315. bool existing = false;
  316. if (style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
  317. existing = true;
  318. style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  319. }
  320. style_map[p_theme_type][p_name] = p_style;
  321. if (p_style.is_valid()) {
  322. style_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  323. }
  324. _emit_theme_changed(!existing);
  325. }
  326. Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  327. if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
  328. return style_map[p_theme_type][p_name];
  329. } else {
  330. return ThemeDB::get_singleton()->get_fallback_stylebox();
  331. }
  332. }
  333. bool Theme::has_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  334. return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid());
  335. }
  336. bool Theme::has_stylebox_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  337. return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name));
  338. }
  339. void Theme::rename_stylebox(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  340. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  341. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  342. 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.");
  343. 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.");
  344. 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.");
  345. style_map[p_theme_type][p_name] = style_map[p_theme_type][p_old_name];
  346. style_map[p_theme_type].erase(p_old_name);
  347. _emit_theme_changed(true);
  348. }
  349. void Theme::clear_stylebox(const StringName &p_name, const StringName &p_theme_type) {
  350. 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.");
  351. ERR_FAIL_COND_MSG(!style_map[p_theme_type].has(p_name), "Cannot clear the stylebox '" + String(p_name) + "' because it does not exist.");
  352. if (style_map[p_theme_type][p_name].is_valid()) {
  353. style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  354. }
  355. style_map[p_theme_type].erase(p_name);
  356. _emit_theme_changed(true);
  357. }
  358. void Theme::get_stylebox_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  359. ERR_FAIL_NULL(p_list);
  360. if (!style_map.has(p_theme_type)) {
  361. return;
  362. }
  363. for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
  364. p_list->push_back(E.key);
  365. }
  366. }
  367. void Theme::add_stylebox_type(const StringName &p_theme_type) {
  368. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  369. if (style_map.has(p_theme_type)) {
  370. return;
  371. }
  372. style_map[p_theme_type] = ThemeStyleMap();
  373. }
  374. void Theme::remove_stylebox_type(const StringName &p_theme_type) {
  375. if (!style_map.has(p_theme_type)) {
  376. return;
  377. }
  378. _freeze_change_propagation();
  379. for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
  380. Ref<StyleBox> style = E.value;
  381. if (style.is_valid()) {
  382. style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  383. }
  384. }
  385. style_map.erase(p_theme_type);
  386. _unfreeze_and_propagate_changes();
  387. }
  388. void Theme::rename_stylebox_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  389. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  390. if (!style_map.has(p_old_theme_type) || style_map.has(p_theme_type)) {
  391. return;
  392. }
  393. style_map[p_theme_type] = style_map[p_old_theme_type];
  394. style_map.erase(p_old_theme_type);
  395. }
  396. void Theme::get_stylebox_type_list(List<StringName> *p_list) const {
  397. ERR_FAIL_NULL(p_list);
  398. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  399. p_list->push_back(E.key);
  400. }
  401. }
  402. // Fonts.
  403. void Theme::set_font(const StringName &p_name, const StringName &p_theme_type, const Ref<Font> &p_font) {
  404. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  405. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  406. bool existing = false;
  407. if (font_map[p_theme_type][p_name].is_valid()) {
  408. existing = true;
  409. font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  410. }
  411. font_map[p_theme_type][p_name] = p_font;
  412. if (p_font.is_valid()) {
  413. font_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
  414. }
  415. _emit_theme_changed(!existing);
  416. }
  417. Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_theme_type) const {
  418. if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) {
  419. return font_map[p_theme_type][p_name];
  420. } else if (has_default_font()) {
  421. return default_font;
  422. } else {
  423. return ThemeDB::get_singleton()->get_fallback_font();
  424. }
  425. }
  426. bool Theme::has_font(const StringName &p_name, const StringName &p_theme_type) const {
  427. 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());
  428. }
  429. bool Theme::has_font_no_default(const StringName &p_name, const StringName &p_theme_type) const {
  430. return (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid());
  431. }
  432. bool Theme::has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  433. return (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name));
  434. }
  435. void Theme::rename_font(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  436. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  437. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  438. 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.");
  439. 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.");
  440. 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.");
  441. font_map[p_theme_type][p_name] = font_map[p_theme_type][p_old_name];
  442. font_map[p_theme_type].erase(p_old_name);
  443. _emit_theme_changed(true);
  444. }
  445. void Theme::clear_font(const StringName &p_name, const StringName &p_theme_type) {
  446. 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.");
  447. ERR_FAIL_COND_MSG(!font_map[p_theme_type].has(p_name), "Cannot clear the font '" + String(p_name) + "' because it does not exist.");
  448. if (font_map[p_theme_type][p_name].is_valid()) {
  449. font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  450. }
  451. font_map[p_theme_type].erase(p_name);
  452. _emit_theme_changed(true);
  453. }
  454. void Theme::get_font_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  455. ERR_FAIL_NULL(p_list);
  456. if (!font_map.has(p_theme_type)) {
  457. return;
  458. }
  459. for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
  460. p_list->push_back(E.key);
  461. }
  462. }
  463. void Theme::add_font_type(const StringName &p_theme_type) {
  464. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  465. if (font_map.has(p_theme_type)) {
  466. return;
  467. }
  468. font_map[p_theme_type] = ThemeFontMap();
  469. }
  470. void Theme::remove_font_type(const StringName &p_theme_type) {
  471. if (!font_map.has(p_theme_type)) {
  472. return;
  473. }
  474. _freeze_change_propagation();
  475. for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
  476. Ref<Font> font = E.value;
  477. if (font.is_valid()) {
  478. font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  479. }
  480. }
  481. font_map.erase(p_theme_type);
  482. _unfreeze_and_propagate_changes();
  483. }
  484. void Theme::rename_font_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  485. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  486. if (!font_map.has(p_old_theme_type) || font_map.has(p_theme_type)) {
  487. return;
  488. }
  489. font_map[p_theme_type] = font_map[p_old_theme_type];
  490. font_map.erase(p_old_theme_type);
  491. }
  492. void Theme::get_font_type_list(List<StringName> *p_list) const {
  493. ERR_FAIL_NULL(p_list);
  494. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  495. p_list->push_back(E.key);
  496. }
  497. }
  498. // Font sizes.
  499. void Theme::set_font_size(const StringName &p_name, const StringName &p_theme_type, int p_font_size) {
  500. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  501. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  502. bool existing = has_font_size_nocheck(p_name, p_theme_type);
  503. font_size_map[p_theme_type][p_name] = p_font_size;
  504. _emit_theme_changed(!existing);
  505. }
  506. int Theme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  507. 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)) {
  508. return font_size_map[p_theme_type][p_name];
  509. } else if (has_default_font_size()) {
  510. return default_font_size;
  511. } else {
  512. return ThemeDB::get_singleton()->get_fallback_font_size();
  513. }
  514. }
  515. bool Theme::has_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  516. 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());
  517. }
  518. bool Theme::has_font_size_no_default(const StringName &p_name, const StringName &p_theme_type) const {
  519. 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));
  520. }
  521. bool Theme::has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  522. return (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name));
  523. }
  524. void Theme::rename_font_size(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  525. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  526. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  527. 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.");
  528. 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.");
  529. 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.");
  530. font_size_map[p_theme_type][p_name] = font_size_map[p_theme_type][p_old_name];
  531. font_size_map[p_theme_type].erase(p_old_name);
  532. _emit_theme_changed(true);
  533. }
  534. void Theme::clear_font_size(const StringName &p_name, const StringName &p_theme_type) {
  535. 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.");
  536. 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.");
  537. font_size_map[p_theme_type].erase(p_name);
  538. _emit_theme_changed(true);
  539. }
  540. void Theme::get_font_size_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  541. ERR_FAIL_NULL(p_list);
  542. if (!font_size_map.has(p_theme_type)) {
  543. return;
  544. }
  545. for (const KeyValue<StringName, int> &E : font_size_map[p_theme_type]) {
  546. p_list->push_back(E.key);
  547. }
  548. }
  549. void Theme::add_font_size_type(const StringName &p_theme_type) {
  550. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  551. if (font_size_map.has(p_theme_type)) {
  552. return;
  553. }
  554. font_size_map[p_theme_type] = ThemeFontSizeMap();
  555. }
  556. void Theme::remove_font_size_type(const StringName &p_theme_type) {
  557. if (!font_size_map.has(p_theme_type)) {
  558. return;
  559. }
  560. font_size_map.erase(p_theme_type);
  561. }
  562. void Theme::rename_font_size_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  563. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  564. if (!font_size_map.has(p_old_theme_type) || font_size_map.has(p_theme_type)) {
  565. return;
  566. }
  567. font_size_map[p_theme_type] = font_size_map[p_old_theme_type];
  568. font_size_map.erase(p_old_theme_type);
  569. }
  570. void Theme::get_font_size_type_list(List<StringName> *p_list) const {
  571. ERR_FAIL_NULL(p_list);
  572. for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
  573. p_list->push_back(E.key);
  574. }
  575. }
  576. // Colors.
  577. void Theme::set_color(const StringName &p_name, const StringName &p_theme_type, const Color &p_color) {
  578. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  579. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  580. bool existing = has_color_nocheck(p_name, p_theme_type);
  581. color_map[p_theme_type][p_name] = p_color;
  582. _emit_theme_changed(!existing);
  583. }
  584. Color Theme::get_color(const StringName &p_name, const StringName &p_theme_type) const {
  585. if (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name)) {
  586. return color_map[p_theme_type][p_name];
  587. } else {
  588. return Color();
  589. }
  590. }
  591. bool Theme::has_color(const StringName &p_name, const StringName &p_theme_type) const {
  592. return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
  593. }
  594. bool Theme::has_color_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  595. return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
  596. }
  597. void Theme::rename_color(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  598. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  599. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  600. 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.");
  601. 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.");
  602. 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.");
  603. color_map[p_theme_type][p_name] = color_map[p_theme_type][p_old_name];
  604. color_map[p_theme_type].erase(p_old_name);
  605. _emit_theme_changed(true);
  606. }
  607. void Theme::clear_color(const StringName &p_name, const StringName &p_theme_type) {
  608. 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.");
  609. ERR_FAIL_COND_MSG(!color_map[p_theme_type].has(p_name), "Cannot clear the color '" + String(p_name) + "' because it does not exist.");
  610. color_map[p_theme_type].erase(p_name);
  611. _emit_theme_changed(true);
  612. }
  613. void Theme::get_color_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  614. ERR_FAIL_NULL(p_list);
  615. if (!color_map.has(p_theme_type)) {
  616. return;
  617. }
  618. for (const KeyValue<StringName, Color> &E : color_map[p_theme_type]) {
  619. p_list->push_back(E.key);
  620. }
  621. }
  622. void Theme::add_color_type(const StringName &p_theme_type) {
  623. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  624. if (color_map.has(p_theme_type)) {
  625. return;
  626. }
  627. color_map[p_theme_type] = ThemeColorMap();
  628. }
  629. void Theme::remove_color_type(const StringName &p_theme_type) {
  630. if (!color_map.has(p_theme_type)) {
  631. return;
  632. }
  633. color_map.erase(p_theme_type);
  634. }
  635. void Theme::rename_color_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  636. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  637. if (!color_map.has(p_old_theme_type) || color_map.has(p_theme_type)) {
  638. return;
  639. }
  640. color_map[p_theme_type] = color_map[p_old_theme_type];
  641. color_map.erase(p_old_theme_type);
  642. }
  643. void Theme::get_color_type_list(List<StringName> *p_list) const {
  644. ERR_FAIL_NULL(p_list);
  645. for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
  646. p_list->push_back(E.key);
  647. }
  648. }
  649. // Theme constants.
  650. void Theme::set_constant(const StringName &p_name, const StringName &p_theme_type, int p_constant) {
  651. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  652. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  653. bool existing = has_constant_nocheck(p_name, p_theme_type);
  654. constant_map[p_theme_type][p_name] = p_constant;
  655. _emit_theme_changed(!existing);
  656. }
  657. int Theme::get_constant(const StringName &p_name, const StringName &p_theme_type) const {
  658. if (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name)) {
  659. return constant_map[p_theme_type][p_name];
  660. } else {
  661. return 0;
  662. }
  663. }
  664. bool Theme::has_constant(const StringName &p_name, const StringName &p_theme_type) const {
  665. return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
  666. }
  667. bool Theme::has_constant_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
  668. return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
  669. }
  670. void Theme::rename_constant(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  671. ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
  672. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  673. 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.");
  674. 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.");
  675. 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.");
  676. constant_map[p_theme_type][p_name] = constant_map[p_theme_type][p_old_name];
  677. constant_map[p_theme_type].erase(p_old_name);
  678. _emit_theme_changed(true);
  679. }
  680. void Theme::clear_constant(const StringName &p_name, const StringName &p_theme_type) {
  681. 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.");
  682. ERR_FAIL_COND_MSG(!constant_map[p_theme_type].has(p_name), "Cannot clear the constant '" + String(p_name) + "' because it does not exist.");
  683. constant_map[p_theme_type].erase(p_name);
  684. _emit_theme_changed(true);
  685. }
  686. void Theme::get_constant_list(const StringName &p_theme_type, List<StringName> *p_list) const {
  687. ERR_FAIL_NULL(p_list);
  688. if (!constant_map.has(p_theme_type)) {
  689. return;
  690. }
  691. for (const KeyValue<StringName, int> &E : constant_map[p_theme_type]) {
  692. p_list->push_back(E.key);
  693. }
  694. }
  695. void Theme::add_constant_type(const StringName &p_theme_type) {
  696. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  697. if (constant_map.has(p_theme_type)) {
  698. return;
  699. }
  700. constant_map[p_theme_type] = ThemeConstantMap();
  701. }
  702. void Theme::remove_constant_type(const StringName &p_theme_type) {
  703. if (!constant_map.has(p_theme_type)) {
  704. return;
  705. }
  706. constant_map.erase(p_theme_type);
  707. }
  708. void Theme::rename_constant_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  709. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  710. if (!constant_map.has(p_old_theme_type) || constant_map.has(p_theme_type)) {
  711. return;
  712. }
  713. constant_map[p_theme_type] = constant_map[p_old_theme_type];
  714. constant_map.erase(p_old_theme_type);
  715. }
  716. void Theme::get_constant_type_list(List<StringName> *p_list) const {
  717. ERR_FAIL_NULL(p_list);
  718. for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
  719. p_list->push_back(E.key);
  720. }
  721. }
  722. // Generic methods for managing theme items.
  723. void Theme::set_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type, const Variant &p_value) {
  724. switch (p_data_type) {
  725. case DATA_TYPE_COLOR: {
  726. 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()) + ").");
  727. Color color_value = p_value;
  728. set_color(p_name, p_theme_type, color_value);
  729. } break;
  730. case DATA_TYPE_CONSTANT: {
  731. 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()) + ").");
  732. int constant_value = p_value;
  733. set_constant(p_name, p_theme_type, constant_value);
  734. } break;
  735. case DATA_TYPE_FONT: {
  736. 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()) + ").");
  737. Ref<Font> font_value = Object::cast_to<Font>(p_value.get_validated_object());
  738. set_font(p_name, p_theme_type, font_value);
  739. } break;
  740. case DATA_TYPE_FONT_SIZE: {
  741. 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()) + ").");
  742. int font_size_value = p_value;
  743. set_font_size(p_name, p_theme_type, font_size_value);
  744. } break;
  745. case DATA_TYPE_ICON: {
  746. 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()) + ").");
  747. Ref<Texture2D> icon_value = Object::cast_to<Texture2D>(p_value.get_validated_object());
  748. set_icon(p_name, p_theme_type, icon_value);
  749. } break;
  750. case DATA_TYPE_STYLEBOX: {
  751. 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()) + ").");
  752. Ref<StyleBox> stylebox_value = Object::cast_to<StyleBox>(p_value.get_validated_object());
  753. set_stylebox(p_name, p_theme_type, stylebox_value);
  754. } break;
  755. case DATA_TYPE_MAX:
  756. break; // Can't happen, but silences warning.
  757. }
  758. }
  759. Variant Theme::get_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  760. switch (p_data_type) {
  761. case DATA_TYPE_COLOR:
  762. return get_color(p_name, p_theme_type);
  763. case DATA_TYPE_CONSTANT:
  764. return get_constant(p_name, p_theme_type);
  765. case DATA_TYPE_FONT:
  766. return get_font(p_name, p_theme_type);
  767. case DATA_TYPE_FONT_SIZE:
  768. return get_font_size(p_name, p_theme_type);
  769. case DATA_TYPE_ICON:
  770. return get_icon(p_name, p_theme_type);
  771. case DATA_TYPE_STYLEBOX:
  772. return get_stylebox(p_name, p_theme_type);
  773. case DATA_TYPE_MAX:
  774. break; // Can't happen, but silences warning.
  775. }
  776. return Variant();
  777. }
  778. bool Theme::has_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  779. switch (p_data_type) {
  780. case DATA_TYPE_COLOR:
  781. return has_color(p_name, p_theme_type);
  782. case DATA_TYPE_CONSTANT:
  783. return has_constant(p_name, p_theme_type);
  784. case DATA_TYPE_FONT:
  785. if (!variation_map.has(p_theme_type)) {
  786. return has_font(p_name, p_theme_type);
  787. } else {
  788. return has_font_no_default(p_name, p_theme_type);
  789. }
  790. case DATA_TYPE_FONT_SIZE:
  791. if (!variation_map.has(p_theme_type)) {
  792. return has_font_size(p_name, p_theme_type);
  793. } else {
  794. return has_font_size_no_default(p_name, p_theme_type);
  795. }
  796. case DATA_TYPE_ICON:
  797. return has_icon(p_name, p_theme_type);
  798. case DATA_TYPE_STYLEBOX:
  799. return has_stylebox(p_name, p_theme_type);
  800. case DATA_TYPE_MAX:
  801. break; // Can't happen, but silences warning.
  802. }
  803. return false;
  804. }
  805. bool Theme::has_theme_item_nocheck(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  806. switch (p_data_type) {
  807. case DATA_TYPE_COLOR:
  808. return has_color_nocheck(p_name, p_theme_type);
  809. case DATA_TYPE_CONSTANT:
  810. return has_constant_nocheck(p_name, p_theme_type);
  811. case DATA_TYPE_FONT:
  812. return has_font_nocheck(p_name, p_theme_type);
  813. case DATA_TYPE_FONT_SIZE:
  814. return has_font_size_nocheck(p_name, p_theme_type);
  815. case DATA_TYPE_ICON:
  816. return has_icon_nocheck(p_name, p_theme_type);
  817. case DATA_TYPE_STYLEBOX:
  818. return has_stylebox_nocheck(p_name, p_theme_type);
  819. case DATA_TYPE_MAX:
  820. break; // Can't happen, but silences warning.
  821. }
  822. return false;
  823. }
  824. void Theme::rename_theme_item(DataType p_data_type, const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
  825. switch (p_data_type) {
  826. case DATA_TYPE_COLOR:
  827. rename_color(p_old_name, p_name, p_theme_type);
  828. break;
  829. case DATA_TYPE_CONSTANT:
  830. rename_constant(p_old_name, p_name, p_theme_type);
  831. break;
  832. case DATA_TYPE_FONT:
  833. rename_font(p_old_name, p_name, p_theme_type);
  834. break;
  835. case DATA_TYPE_FONT_SIZE:
  836. rename_font_size(p_old_name, p_name, p_theme_type);
  837. break;
  838. case DATA_TYPE_ICON:
  839. rename_icon(p_old_name, p_name, p_theme_type);
  840. break;
  841. case DATA_TYPE_STYLEBOX:
  842. rename_stylebox(p_old_name, p_name, p_theme_type);
  843. break;
  844. case DATA_TYPE_MAX:
  845. break; // Can't happen, but silences warning.
  846. }
  847. }
  848. void Theme::clear_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) {
  849. switch (p_data_type) {
  850. case DATA_TYPE_COLOR:
  851. clear_color(p_name, p_theme_type);
  852. break;
  853. case DATA_TYPE_CONSTANT:
  854. clear_constant(p_name, p_theme_type);
  855. break;
  856. case DATA_TYPE_FONT:
  857. clear_font(p_name, p_theme_type);
  858. break;
  859. case DATA_TYPE_FONT_SIZE:
  860. clear_font_size(p_name, p_theme_type);
  861. break;
  862. case DATA_TYPE_ICON:
  863. clear_icon(p_name, p_theme_type);
  864. break;
  865. case DATA_TYPE_STYLEBOX:
  866. clear_stylebox(p_name, p_theme_type);
  867. break;
  868. case DATA_TYPE_MAX:
  869. break; // Can't happen, but silences warning.
  870. }
  871. }
  872. void Theme::get_theme_item_list(DataType p_data_type, const StringName &p_theme_type, List<StringName> *p_list) const {
  873. switch (p_data_type) {
  874. case DATA_TYPE_COLOR:
  875. get_color_list(p_theme_type, p_list);
  876. break;
  877. case DATA_TYPE_CONSTANT:
  878. get_constant_list(p_theme_type, p_list);
  879. break;
  880. case DATA_TYPE_FONT:
  881. get_font_list(p_theme_type, p_list);
  882. break;
  883. case DATA_TYPE_FONT_SIZE:
  884. get_font_size_list(p_theme_type, p_list);
  885. break;
  886. case DATA_TYPE_ICON:
  887. get_icon_list(p_theme_type, p_list);
  888. break;
  889. case DATA_TYPE_STYLEBOX:
  890. get_stylebox_list(p_theme_type, p_list);
  891. break;
  892. case DATA_TYPE_MAX:
  893. break; // Can't happen, but silences warning.
  894. }
  895. }
  896. void Theme::add_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
  897. switch (p_data_type) {
  898. case DATA_TYPE_COLOR:
  899. add_color_type(p_theme_type);
  900. break;
  901. case DATA_TYPE_CONSTANT:
  902. add_constant_type(p_theme_type);
  903. break;
  904. case DATA_TYPE_FONT:
  905. add_font_type(p_theme_type);
  906. break;
  907. case DATA_TYPE_FONT_SIZE:
  908. add_font_size_type(p_theme_type);
  909. break;
  910. case DATA_TYPE_ICON:
  911. add_icon_type(p_theme_type);
  912. break;
  913. case DATA_TYPE_STYLEBOX:
  914. add_stylebox_type(p_theme_type);
  915. break;
  916. case DATA_TYPE_MAX:
  917. break; // Can't happen, but silences warning.
  918. }
  919. }
  920. void Theme::remove_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
  921. switch (p_data_type) {
  922. case DATA_TYPE_COLOR:
  923. remove_color_type(p_theme_type);
  924. break;
  925. case DATA_TYPE_CONSTANT:
  926. remove_constant_type(p_theme_type);
  927. break;
  928. case DATA_TYPE_FONT:
  929. remove_font_type(p_theme_type);
  930. break;
  931. case DATA_TYPE_FONT_SIZE:
  932. remove_font_size_type(p_theme_type);
  933. break;
  934. case DATA_TYPE_ICON:
  935. remove_icon_type(p_theme_type);
  936. break;
  937. case DATA_TYPE_STYLEBOX:
  938. remove_stylebox_type(p_theme_type);
  939. break;
  940. case DATA_TYPE_MAX:
  941. break; // Can't happen, but silences warning.
  942. }
  943. }
  944. void Theme::rename_theme_item_type(DataType p_data_type, const StringName &p_old_theme_type, const StringName &p_theme_type) {
  945. switch (p_data_type) {
  946. case DATA_TYPE_COLOR:
  947. rename_color_type(p_old_theme_type, p_theme_type);
  948. break;
  949. case DATA_TYPE_CONSTANT:
  950. rename_constant_type(p_old_theme_type, p_theme_type);
  951. break;
  952. case DATA_TYPE_FONT:
  953. rename_font_type(p_old_theme_type, p_theme_type);
  954. break;
  955. case DATA_TYPE_FONT_SIZE:
  956. rename_font_size_type(p_old_theme_type, p_theme_type);
  957. break;
  958. case DATA_TYPE_ICON:
  959. rename_icon_type(p_old_theme_type, p_theme_type);
  960. break;
  961. case DATA_TYPE_STYLEBOX:
  962. rename_stylebox_type(p_old_theme_type, p_theme_type);
  963. break;
  964. case DATA_TYPE_MAX:
  965. break; // Can't happen, but silences warning.
  966. }
  967. }
  968. void Theme::get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const {
  969. switch (p_data_type) {
  970. case DATA_TYPE_COLOR:
  971. get_color_type_list(p_list);
  972. break;
  973. case DATA_TYPE_CONSTANT:
  974. get_constant_type_list(p_list);
  975. break;
  976. case DATA_TYPE_FONT:
  977. get_font_type_list(p_list);
  978. break;
  979. case DATA_TYPE_FONT_SIZE:
  980. get_font_size_type_list(p_list);
  981. break;
  982. case DATA_TYPE_ICON:
  983. get_icon_type_list(p_list);
  984. break;
  985. case DATA_TYPE_STYLEBOX:
  986. get_stylebox_type_list(p_list);
  987. break;
  988. case DATA_TYPE_MAX:
  989. break; // Can't happen, but silences warning.
  990. }
  991. }
  992. // Theme type variations.
  993. void Theme::set_type_variation(const StringName &p_theme_type, const StringName &p_base_type) {
  994. ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
  995. ERR_FAIL_COND_MSG(!is_valid_type_name(p_base_type), vformat("Invalid type name: '%s'", p_base_type));
  996. 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));
  997. 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));
  998. 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.");
  999. if (variation_map.has(p_theme_type)) {
  1000. StringName old_base = variation_map[p_theme_type];
  1001. variation_base_map[old_base].erase(p_theme_type);
  1002. }
  1003. variation_map[p_theme_type] = p_base_type;
  1004. variation_base_map[p_base_type].push_back(p_theme_type);
  1005. _emit_theme_changed(true);
  1006. }
  1007. bool Theme::is_type_variation(const StringName &p_theme_type, const StringName &p_base_type) const {
  1008. return (variation_map.has(p_theme_type) && variation_map[p_theme_type] == p_base_type);
  1009. }
  1010. void Theme::clear_type_variation(const StringName &p_theme_type) {
  1011. ERR_FAIL_COND_MSG(!variation_map.has(p_theme_type), "Cannot clear the type variation '" + String(p_theme_type) + "' because it does not exist.");
  1012. StringName base_type = variation_map[p_theme_type];
  1013. variation_base_map[base_type].erase(p_theme_type);
  1014. variation_map.erase(p_theme_type);
  1015. _emit_theme_changed(true);
  1016. }
  1017. StringName Theme::get_type_variation_base(const StringName &p_theme_type) const {
  1018. if (!variation_map.has(p_theme_type)) {
  1019. return StringName();
  1020. }
  1021. return variation_map[p_theme_type];
  1022. }
  1023. void Theme::get_type_variation_list(const StringName &p_base_type, List<StringName> *p_list) const {
  1024. ERR_FAIL_NULL(p_list);
  1025. if (!variation_base_map.has(p_base_type)) {
  1026. return;
  1027. }
  1028. for (const StringName &E : variation_base_map[p_base_type]) {
  1029. // Prevent infinite loops if variants were set to be cross-dependent (that's still invalid usage, but handling for stability sake).
  1030. if (p_list->find(E)) {
  1031. continue;
  1032. }
  1033. p_list->push_back(E);
  1034. // Continue looking for sub-variations.
  1035. get_type_variation_list(E, p_list);
  1036. }
  1037. }
  1038. // Theme types.
  1039. void Theme::add_type(const StringName &p_theme_type) {
  1040. // Add a record to every data type map.
  1041. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  1042. Theme::DataType dt = (Theme::DataType)i;
  1043. add_theme_item_type(dt, p_theme_type);
  1044. }
  1045. _emit_theme_changed(true);
  1046. }
  1047. void Theme::remove_type(const StringName &p_theme_type) {
  1048. // Gracefully remove the record from every data type map.
  1049. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  1050. Theme::DataType dt = (Theme::DataType)i;
  1051. remove_theme_item_type(dt, p_theme_type);
  1052. }
  1053. // If type is a variation, remove that connection.
  1054. if (get_type_variation_base(p_theme_type) != StringName()) {
  1055. clear_type_variation(p_theme_type);
  1056. }
  1057. // If type is a variation base, remove all those connections.
  1058. List<StringName> names;
  1059. get_type_variation_list(p_theme_type, &names);
  1060. for (const StringName &E : names) {
  1061. clear_type_variation(E);
  1062. }
  1063. _emit_theme_changed(true);
  1064. }
  1065. void Theme::rename_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
  1066. // Gracefully rename the record in every data type map.
  1067. for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
  1068. Theme::DataType dt = (Theme::DataType)i;
  1069. rename_theme_item_type(dt, p_old_theme_type, p_theme_type);
  1070. }
  1071. // If type is a variation, replace that connection.
  1072. const StringName base_type = get_type_variation_base(p_old_theme_type);
  1073. if (base_type != StringName()) {
  1074. clear_type_variation(p_old_theme_type);
  1075. if (p_theme_type != StringName() && !ClassDB::class_exists(p_theme_type)) {
  1076. set_type_variation(p_theme_type, base_type);
  1077. }
  1078. }
  1079. // If type is a variation base, replace all those connections.
  1080. List<StringName> names;
  1081. get_type_variation_list(p_old_theme_type, &names);
  1082. for (const StringName &E : names) {
  1083. clear_type_variation(E);
  1084. if (p_theme_type != StringName()) {
  1085. set_type_variation(E, p_theme_type);
  1086. }
  1087. }
  1088. _emit_theme_changed(true);
  1089. }
  1090. void Theme::get_type_list(List<StringName> *p_list) const {
  1091. ERR_FAIL_NULL(p_list);
  1092. // This Set guarantees uniqueness.
  1093. // Because each map can have the same type defined, but for this method
  1094. // we only want one occurrence of each type.
  1095. HashSet<StringName> types;
  1096. // Icons.
  1097. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  1098. types.insert(E.key);
  1099. }
  1100. // Styles.
  1101. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  1102. types.insert(E.key);
  1103. }
  1104. // Fonts.
  1105. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  1106. types.insert(E.key);
  1107. }
  1108. // Font sizes.
  1109. for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
  1110. types.insert(E.key);
  1111. }
  1112. // Colors.
  1113. for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
  1114. types.insert(E.key);
  1115. }
  1116. // Constants.
  1117. for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
  1118. types.insert(E.key);
  1119. }
  1120. // Variations.
  1121. for (const KeyValue<StringName, StringName> &E : variation_map) {
  1122. types.insert(E.key);
  1123. }
  1124. for (const StringName &E : types) {
  1125. p_list->push_back(E);
  1126. }
  1127. }
  1128. void Theme::get_type_dependencies(const StringName &p_base_type, const StringName &p_type_variation, Vector<StringName> &r_result) {
  1129. // Build the dependency chain for type variations.
  1130. if (p_type_variation != StringName()) {
  1131. StringName variation_name = p_type_variation;
  1132. while (variation_name != StringName()) {
  1133. r_result.push_back(variation_name);
  1134. variation_name = get_type_variation_base(variation_name);
  1135. // If we have reached the base type dependency, it's safe to stop (assuming no funny business was done to the Theme).
  1136. if (variation_name == p_base_type) {
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. // Continue building the chain using native class hierarchy.
  1142. ThemeDB::get_singleton()->get_native_type_dependencies(p_base_type, r_result);
  1143. }
  1144. // Internal methods for getting lists as a Vector of String (compatible with public API).
  1145. Vector<String> Theme::_get_icon_list(const String &p_theme_type) const {
  1146. Vector<String> ilret;
  1147. List<StringName> il;
  1148. get_icon_list(p_theme_type, &il);
  1149. ilret.resize(il.size());
  1150. int i = 0;
  1151. String *w = ilret.ptrw();
  1152. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1153. w[i] = E->get();
  1154. }
  1155. return ilret;
  1156. }
  1157. Vector<String> Theme::_get_icon_type_list() const {
  1158. Vector<String> ilret;
  1159. List<StringName> il;
  1160. get_icon_type_list(&il);
  1161. ilret.resize(il.size());
  1162. int i = 0;
  1163. String *w = ilret.ptrw();
  1164. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1165. w[i] = E->get();
  1166. }
  1167. return ilret;
  1168. }
  1169. Vector<String> Theme::_get_stylebox_list(const String &p_theme_type) const {
  1170. Vector<String> ilret;
  1171. List<StringName> il;
  1172. get_stylebox_list(p_theme_type, &il);
  1173. ilret.resize(il.size());
  1174. int i = 0;
  1175. String *w = ilret.ptrw();
  1176. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1177. w[i] = E->get();
  1178. }
  1179. return ilret;
  1180. }
  1181. Vector<String> Theme::_get_stylebox_type_list() const {
  1182. Vector<String> ilret;
  1183. List<StringName> il;
  1184. get_stylebox_type_list(&il);
  1185. ilret.resize(il.size());
  1186. int i = 0;
  1187. String *w = ilret.ptrw();
  1188. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1189. w[i] = E->get();
  1190. }
  1191. return ilret;
  1192. }
  1193. Vector<String> Theme::_get_font_list(const String &p_theme_type) const {
  1194. Vector<String> ilret;
  1195. List<StringName> il;
  1196. get_font_list(p_theme_type, &il);
  1197. ilret.resize(il.size());
  1198. int i = 0;
  1199. String *w = ilret.ptrw();
  1200. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1201. w[i] = E->get();
  1202. }
  1203. return ilret;
  1204. }
  1205. Vector<String> Theme::_get_font_type_list() const {
  1206. Vector<String> ilret;
  1207. List<StringName> il;
  1208. get_font_type_list(&il);
  1209. ilret.resize(il.size());
  1210. int i = 0;
  1211. String *w = ilret.ptrw();
  1212. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1213. w[i] = E->get();
  1214. }
  1215. return ilret;
  1216. }
  1217. Vector<String> Theme::_get_font_size_list(const String &p_theme_type) const {
  1218. Vector<String> ilret;
  1219. List<StringName> il;
  1220. get_font_size_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_font_size_type_list() const {
  1230. Vector<String> ilret;
  1231. List<StringName> il;
  1232. get_font_size_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. Vector<String> Theme::_get_color_list(const String &p_theme_type) const {
  1242. Vector<String> ilret;
  1243. List<StringName> il;
  1244. get_color_list(p_theme_type, &il);
  1245. ilret.resize(il.size());
  1246. int i = 0;
  1247. String *w = ilret.ptrw();
  1248. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1249. w[i] = E->get();
  1250. }
  1251. return ilret;
  1252. }
  1253. Vector<String> Theme::_get_color_type_list() const {
  1254. Vector<String> ilret;
  1255. List<StringName> il;
  1256. get_color_type_list(&il);
  1257. ilret.resize(il.size());
  1258. int i = 0;
  1259. String *w = ilret.ptrw();
  1260. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1261. w[i] = E->get();
  1262. }
  1263. return ilret;
  1264. }
  1265. Vector<String> Theme::_get_constant_list(const String &p_theme_type) const {
  1266. Vector<String> ilret;
  1267. List<StringName> il;
  1268. get_constant_list(p_theme_type, &il);
  1269. ilret.resize(il.size());
  1270. int i = 0;
  1271. String *w = ilret.ptrw();
  1272. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1273. w[i] = E->get();
  1274. }
  1275. return ilret;
  1276. }
  1277. Vector<String> Theme::_get_constant_type_list() const {
  1278. Vector<String> ilret;
  1279. List<StringName> il;
  1280. get_constant_type_list(&il);
  1281. ilret.resize(il.size());
  1282. int i = 0;
  1283. String *w = ilret.ptrw();
  1284. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1285. w[i] = E->get();
  1286. }
  1287. return ilret;
  1288. }
  1289. Vector<String> Theme::_get_theme_item_list(DataType p_data_type, const String &p_theme_type) const {
  1290. switch (p_data_type) {
  1291. case DATA_TYPE_COLOR:
  1292. return _get_color_list(p_theme_type);
  1293. case DATA_TYPE_CONSTANT:
  1294. return _get_constant_list(p_theme_type);
  1295. case DATA_TYPE_FONT:
  1296. return _get_font_list(p_theme_type);
  1297. case DATA_TYPE_FONT_SIZE:
  1298. return _get_font_size_list(p_theme_type);
  1299. case DATA_TYPE_ICON:
  1300. return _get_icon_list(p_theme_type);
  1301. case DATA_TYPE_STYLEBOX:
  1302. return _get_stylebox_list(p_theme_type);
  1303. case DATA_TYPE_MAX:
  1304. break; // Can't happen, but silences warning.
  1305. }
  1306. return Vector<String>();
  1307. }
  1308. Vector<String> Theme::_get_theme_item_type_list(DataType p_data_type) const {
  1309. switch (p_data_type) {
  1310. case DATA_TYPE_COLOR:
  1311. return _get_color_type_list();
  1312. case DATA_TYPE_CONSTANT:
  1313. return _get_constant_type_list();
  1314. case DATA_TYPE_FONT:
  1315. return _get_font_type_list();
  1316. case DATA_TYPE_FONT_SIZE:
  1317. return _get_font_size_type_list();
  1318. case DATA_TYPE_ICON:
  1319. return _get_icon_type_list();
  1320. case DATA_TYPE_STYLEBOX:
  1321. return _get_stylebox_type_list();
  1322. case DATA_TYPE_MAX:
  1323. break; // Can't happen, but silences warning.
  1324. }
  1325. return Vector<String>();
  1326. }
  1327. Vector<String> Theme::_get_type_variation_list(const StringName &p_theme_type) const {
  1328. Vector<String> ilret;
  1329. List<StringName> il;
  1330. get_type_variation_list(p_theme_type, &il);
  1331. ilret.resize(il.size());
  1332. int i = 0;
  1333. String *w = ilret.ptrw();
  1334. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1335. w[i] = E->get();
  1336. }
  1337. return ilret;
  1338. }
  1339. Vector<String> Theme::_get_type_list() const {
  1340. Vector<String> ilret;
  1341. List<StringName> il;
  1342. get_type_list(&il);
  1343. ilret.resize(il.size());
  1344. int i = 0;
  1345. String *w = ilret.ptrw();
  1346. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  1347. w[i] = E->get();
  1348. }
  1349. return ilret;
  1350. }
  1351. // Theme bulk manipulations.
  1352. void Theme::_emit_theme_changed(bool p_notify_list_changed) {
  1353. if (no_change_propagation) {
  1354. return;
  1355. }
  1356. if (p_notify_list_changed) {
  1357. notify_property_list_changed();
  1358. }
  1359. emit_changed();
  1360. }
  1361. void Theme::_freeze_change_propagation() {
  1362. no_change_propagation = true;
  1363. }
  1364. void Theme::_unfreeze_and_propagate_changes() {
  1365. no_change_propagation = false;
  1366. _emit_theme_changed(true);
  1367. }
  1368. void Theme::merge_with(const Ref<Theme> &p_other) {
  1369. if (p_other.is_null()) {
  1370. return;
  1371. }
  1372. _freeze_change_propagation();
  1373. // Colors.
  1374. {
  1375. for (const KeyValue<StringName, ThemeColorMap> &E : p_other->color_map) {
  1376. for (const KeyValue<StringName, Color> &F : E.value) {
  1377. set_color(F.key, E.key, F.value);
  1378. }
  1379. }
  1380. }
  1381. // Constants.
  1382. {
  1383. for (const KeyValue<StringName, ThemeConstantMap> &E : p_other->constant_map) {
  1384. for (const KeyValue<StringName, int> &F : E.value) {
  1385. set_constant(F.key, E.key, F.value);
  1386. }
  1387. }
  1388. }
  1389. // Fonts.
  1390. {
  1391. for (const KeyValue<StringName, ThemeFontMap> &E : p_other->font_map) {
  1392. for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
  1393. set_font(F.key, E.key, F.value);
  1394. }
  1395. }
  1396. }
  1397. // Font sizes.
  1398. {
  1399. for (const KeyValue<StringName, ThemeFontSizeMap> &E : p_other->font_size_map) {
  1400. for (const KeyValue<StringName, int> &F : E.value) {
  1401. set_font_size(F.key, E.key, F.value);
  1402. }
  1403. }
  1404. }
  1405. // Icons.
  1406. {
  1407. for (const KeyValue<StringName, ThemeIconMap> &E : p_other->icon_map) {
  1408. for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
  1409. set_icon(F.key, E.key, F.value);
  1410. }
  1411. }
  1412. }
  1413. // Styleboxes.
  1414. {
  1415. for (const KeyValue<StringName, ThemeStyleMap> &E : p_other->style_map) {
  1416. for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
  1417. set_stylebox(F.key, E.key, F.value);
  1418. }
  1419. }
  1420. }
  1421. // Type variations.
  1422. {
  1423. for (const KeyValue<StringName, StringName> &E : p_other->variation_map) {
  1424. set_type_variation(E.key, E.value);
  1425. }
  1426. }
  1427. // Defaults.
  1428. if (p_other->has_default_font()) {
  1429. set_default_font(p_other->default_font);
  1430. }
  1431. if (p_other->has_default_font_size()) {
  1432. set_default_font_size(p_other->default_font_size);
  1433. }
  1434. if (p_other->has_default_base_scale()) {
  1435. set_default_base_scale(p_other->default_base_scale);
  1436. }
  1437. _unfreeze_and_propagate_changes();
  1438. }
  1439. void Theme::clear() {
  1440. // These items need disconnecting.
  1441. {
  1442. for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
  1443. for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
  1444. if (F.value.is_valid()) {
  1445. Ref<Texture2D> icon = F.value;
  1446. icon->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1447. }
  1448. }
  1449. }
  1450. }
  1451. {
  1452. for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
  1453. for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
  1454. if (F.value.is_valid()) {
  1455. Ref<StyleBox> style = F.value;
  1456. style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1457. }
  1458. }
  1459. }
  1460. }
  1461. {
  1462. for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
  1463. for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
  1464. if (F.value.is_valid()) {
  1465. Ref<Font> font = F.value;
  1466. font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
  1467. }
  1468. }
  1469. }
  1470. }
  1471. icon_map.clear();
  1472. style_map.clear();
  1473. font_map.clear();
  1474. font_size_map.clear();
  1475. color_map.clear();
  1476. constant_map.clear();
  1477. variation_map.clear();
  1478. variation_base_map.clear();
  1479. _emit_theme_changed(true);
  1480. }
  1481. void Theme::reset_state() {
  1482. clear();
  1483. }
  1484. void Theme::_bind_methods() {
  1485. ClassDB::bind_method(D_METHOD("set_icon", "name", "theme_type", "texture"), &Theme::set_icon);
  1486. ClassDB::bind_method(D_METHOD("get_icon", "name", "theme_type"), &Theme::get_icon);
  1487. ClassDB::bind_method(D_METHOD("has_icon", "name", "theme_type"), &Theme::has_icon);
  1488. ClassDB::bind_method(D_METHOD("rename_icon", "old_name", "name", "theme_type"), &Theme::rename_icon);
  1489. ClassDB::bind_method(D_METHOD("clear_icon", "name", "theme_type"), &Theme::clear_icon);
  1490. ClassDB::bind_method(D_METHOD("get_icon_list", "theme_type"), &Theme::_get_icon_list);
  1491. ClassDB::bind_method(D_METHOD("get_icon_type_list"), &Theme::_get_icon_type_list);
  1492. ClassDB::bind_method(D_METHOD("set_stylebox", "name", "theme_type", "texture"), &Theme::set_stylebox);
  1493. ClassDB::bind_method(D_METHOD("get_stylebox", "name", "theme_type"), &Theme::get_stylebox);
  1494. ClassDB::bind_method(D_METHOD("has_stylebox", "name", "theme_type"), &Theme::has_stylebox);
  1495. ClassDB::bind_method(D_METHOD("rename_stylebox", "old_name", "name", "theme_type"), &Theme::rename_stylebox);
  1496. ClassDB::bind_method(D_METHOD("clear_stylebox", "name", "theme_type"), &Theme::clear_stylebox);
  1497. ClassDB::bind_method(D_METHOD("get_stylebox_list", "theme_type"), &Theme::_get_stylebox_list);
  1498. ClassDB::bind_method(D_METHOD("get_stylebox_type_list"), &Theme::_get_stylebox_type_list);
  1499. ClassDB::bind_method(D_METHOD("set_font", "name", "theme_type", "font"), &Theme::set_font);
  1500. ClassDB::bind_method(D_METHOD("get_font", "name", "theme_type"), &Theme::get_font);
  1501. ClassDB::bind_method(D_METHOD("has_font", "name", "theme_type"), &Theme::has_font);
  1502. ClassDB::bind_method(D_METHOD("rename_font", "old_name", "name", "theme_type"), &Theme::rename_font);
  1503. ClassDB::bind_method(D_METHOD("clear_font", "name", "theme_type"), &Theme::clear_font);
  1504. ClassDB::bind_method(D_METHOD("get_font_list", "theme_type"), &Theme::_get_font_list);
  1505. ClassDB::bind_method(D_METHOD("get_font_type_list"), &Theme::_get_font_type_list);
  1506. ClassDB::bind_method(D_METHOD("set_font_size", "name", "theme_type", "font_size"), &Theme::set_font_size);
  1507. ClassDB::bind_method(D_METHOD("get_font_size", "name", "theme_type"), &Theme::get_font_size);
  1508. ClassDB::bind_method(D_METHOD("has_font_size", "name", "theme_type"), &Theme::has_font_size);
  1509. ClassDB::bind_method(D_METHOD("rename_font_size", "old_name", "name", "theme_type"), &Theme::rename_font_size);
  1510. ClassDB::bind_method(D_METHOD("clear_font_size", "name", "theme_type"), &Theme::clear_font_size);
  1511. ClassDB::bind_method(D_METHOD("get_font_size_list", "theme_type"), &Theme::_get_font_size_list);
  1512. ClassDB::bind_method(D_METHOD("get_font_size_type_list"), &Theme::_get_font_size_type_list);
  1513. ClassDB::bind_method(D_METHOD("set_color", "name", "theme_type", "color"), &Theme::set_color);
  1514. ClassDB::bind_method(D_METHOD("get_color", "name", "theme_type"), &Theme::get_color);
  1515. ClassDB::bind_method(D_METHOD("has_color", "name", "theme_type"), &Theme::has_color);
  1516. ClassDB::bind_method(D_METHOD("rename_color", "old_name", "name", "theme_type"), &Theme::rename_color);
  1517. ClassDB::bind_method(D_METHOD("clear_color", "name", "theme_type"), &Theme::clear_color);
  1518. ClassDB::bind_method(D_METHOD("get_color_list", "theme_type"), &Theme::_get_color_list);
  1519. ClassDB::bind_method(D_METHOD("get_color_type_list"), &Theme::_get_color_type_list);
  1520. ClassDB::bind_method(D_METHOD("set_constant", "name", "theme_type", "constant"), &Theme::set_constant);
  1521. ClassDB::bind_method(D_METHOD("get_constant", "name", "theme_type"), &Theme::get_constant);
  1522. ClassDB::bind_method(D_METHOD("has_constant", "name", "theme_type"), &Theme::has_constant);
  1523. ClassDB::bind_method(D_METHOD("rename_constant", "old_name", "name", "theme_type"), &Theme::rename_constant);
  1524. ClassDB::bind_method(D_METHOD("clear_constant", "name", "theme_type"), &Theme::clear_constant);
  1525. ClassDB::bind_method(D_METHOD("get_constant_list", "theme_type"), &Theme::_get_constant_list);
  1526. ClassDB::bind_method(D_METHOD("get_constant_type_list"), &Theme::_get_constant_type_list);
  1527. ClassDB::bind_method(D_METHOD("set_default_base_scale", "base_scale"), &Theme::set_default_base_scale);
  1528. ClassDB::bind_method(D_METHOD("get_default_base_scale"), &Theme::get_default_base_scale);
  1529. ClassDB::bind_method(D_METHOD("has_default_base_scale"), &Theme::has_default_base_scale);
  1530. ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_font);
  1531. ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_font);
  1532. ClassDB::bind_method(D_METHOD("has_default_font"), &Theme::has_default_font);
  1533. ClassDB::bind_method(D_METHOD("set_default_font_size", "font_size"), &Theme::set_default_font_size);
  1534. ClassDB::bind_method(D_METHOD("get_default_font_size"), &Theme::get_default_font_size);
  1535. ClassDB::bind_method(D_METHOD("has_default_font_size"), &Theme::has_default_font_size);
  1536. ClassDB::bind_method(D_METHOD("set_theme_item", "data_type", "name", "theme_type", "value"), &Theme::set_theme_item);
  1537. ClassDB::bind_method(D_METHOD("get_theme_item", "data_type", "name", "theme_type"), &Theme::get_theme_item);
  1538. ClassDB::bind_method(D_METHOD("has_theme_item", "data_type", "name", "theme_type"), &Theme::has_theme_item);
  1539. ClassDB::bind_method(D_METHOD("rename_theme_item", "data_type", "old_name", "name", "theme_type"), &Theme::rename_theme_item);
  1540. ClassDB::bind_method(D_METHOD("clear_theme_item", "data_type", "name", "theme_type"), &Theme::clear_theme_item);
  1541. ClassDB::bind_method(D_METHOD("get_theme_item_list", "data_type", "theme_type"), &Theme::_get_theme_item_list);
  1542. ClassDB::bind_method(D_METHOD("get_theme_item_type_list", "data_type"), &Theme::_get_theme_item_type_list);
  1543. ClassDB::bind_method(D_METHOD("set_type_variation", "theme_type", "base_type"), &Theme::set_type_variation);
  1544. ClassDB::bind_method(D_METHOD("is_type_variation", "theme_type", "base_type"), &Theme::is_type_variation);
  1545. ClassDB::bind_method(D_METHOD("clear_type_variation", "theme_type"), &Theme::clear_type_variation);
  1546. ClassDB::bind_method(D_METHOD("get_type_variation_base", "theme_type"), &Theme::get_type_variation_base);
  1547. ClassDB::bind_method(D_METHOD("get_type_variation_list", "base_type"), &Theme::_get_type_variation_list);
  1548. ClassDB::bind_method(D_METHOD("add_type", "theme_type"), &Theme::add_type);
  1549. ClassDB::bind_method(D_METHOD("remove_type", "theme_type"), &Theme::remove_type);
  1550. ClassDB::bind_method(D_METHOD("rename_type", "old_theme_type", "theme_type"), &Theme::rename_type);
  1551. ClassDB::bind_method(D_METHOD("get_type_list"), &Theme::_get_type_list);
  1552. ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with);
  1553. ClassDB::bind_method(D_METHOD("clear"), &Theme::clear);
  1554. 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");
  1555. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");
  1556. 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");
  1557. BIND_ENUM_CONSTANT(DATA_TYPE_COLOR);
  1558. BIND_ENUM_CONSTANT(DATA_TYPE_CONSTANT);
  1559. BIND_ENUM_CONSTANT(DATA_TYPE_FONT);
  1560. BIND_ENUM_CONSTANT(DATA_TYPE_FONT_SIZE);
  1561. BIND_ENUM_CONSTANT(DATA_TYPE_ICON);
  1562. BIND_ENUM_CONSTANT(DATA_TYPE_STYLEBOX);
  1563. BIND_ENUM_CONSTANT(DATA_TYPE_MAX);
  1564. }
  1565. Theme::Theme() {
  1566. }
  1567. Theme::~Theme() {
  1568. }