theme.cpp 66 KB

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