theme.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*************************************************************************/
  2. /* theme.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "theme.h"
  31. #include "core/os/file_access.h"
  32. #include "core/print_string.h"
  33. void Theme::_emit_theme_changed() {
  34. emit_changed();
  35. }
  36. PoolVector<String> Theme::_get_icon_list(const String &p_type) const {
  37. PoolVector<String> ilret;
  38. List<StringName> il;
  39. get_icon_list(p_type, &il);
  40. ilret.resize(il.size());
  41. int i = 0;
  42. PoolVector<String>::Write w = ilret.write();
  43. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  44. w[i] = E->get();
  45. }
  46. return ilret;
  47. }
  48. PoolVector<String> Theme::_get_stylebox_list(const String &p_type) const {
  49. PoolVector<String> ilret;
  50. List<StringName> il;
  51. get_stylebox_list(p_type, &il);
  52. ilret.resize(il.size());
  53. int i = 0;
  54. PoolVector<String>::Write w = ilret.write();
  55. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  56. w[i] = E->get();
  57. }
  58. return ilret;
  59. }
  60. PoolVector<String> Theme::_get_stylebox_types(void) const {
  61. PoolVector<String> ilret;
  62. List<StringName> il;
  63. get_stylebox_types(&il);
  64. ilret.resize(il.size());
  65. int i = 0;
  66. PoolVector<String>::Write w = ilret.write();
  67. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  68. w[i] = E->get();
  69. }
  70. return ilret;
  71. }
  72. PoolVector<String> Theme::_get_font_list(const String &p_type) const {
  73. PoolVector<String> ilret;
  74. List<StringName> il;
  75. get_font_list(p_type, &il);
  76. ilret.resize(il.size());
  77. int i = 0;
  78. PoolVector<String>::Write w = ilret.write();
  79. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  80. w[i] = E->get();
  81. }
  82. return ilret;
  83. }
  84. PoolVector<String> Theme::_get_color_list(const String &p_type) const {
  85. PoolVector<String> ilret;
  86. List<StringName> il;
  87. get_color_list(p_type, &il);
  88. ilret.resize(il.size());
  89. int i = 0;
  90. PoolVector<String>::Write w = ilret.write();
  91. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  92. w[i] = E->get();
  93. }
  94. return ilret;
  95. }
  96. PoolVector<String> Theme::_get_constant_list(const String &p_type) const {
  97. PoolVector<String> ilret;
  98. List<StringName> il;
  99. get_constant_list(p_type, &il);
  100. ilret.resize(il.size());
  101. int i = 0;
  102. PoolVector<String>::Write w = ilret.write();
  103. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  104. w[i] = E->get();
  105. }
  106. return ilret;
  107. }
  108. PoolVector<String> Theme::_get_type_list(const String &p_type) const {
  109. PoolVector<String> ilret;
  110. List<StringName> il;
  111. get_type_list(&il);
  112. ilret.resize(il.size());
  113. int i = 0;
  114. PoolVector<String>::Write w = ilret.write();
  115. for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
  116. w[i] = E->get();
  117. }
  118. return ilret;
  119. }
  120. bool Theme::_set(const StringName &p_name, const Variant &p_value) {
  121. String sname = p_name;
  122. if (sname.find("/") != -1) {
  123. String type = sname.get_slicec('/', 1);
  124. String node_type = sname.get_slicec('/', 0);
  125. String name = sname.get_slicec('/', 2);
  126. if (type == "icons") {
  127. set_icon(name, node_type, p_value);
  128. } else if (type == "styles") {
  129. set_stylebox(name, node_type, p_value);
  130. } else if (type == "fonts") {
  131. set_font(name, node_type, p_value);
  132. } else if (type == "colors") {
  133. set_color(name, node_type, p_value);
  134. } else if (type == "constants") {
  135. set_constant(name, node_type, p_value);
  136. } else
  137. return false;
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool Theme::_get(const StringName &p_name, Variant &r_ret) const {
  143. String sname = p_name;
  144. if (sname.find("/") != -1) {
  145. String type = sname.get_slicec('/', 1);
  146. String node_type = sname.get_slicec('/', 0);
  147. String name = sname.get_slicec('/', 2);
  148. if (type == "icons") {
  149. if (!has_icon(name, node_type))
  150. r_ret = Ref<Texture>();
  151. else
  152. r_ret = get_icon(name, node_type);
  153. } else if (type == "styles") {
  154. if (!has_stylebox(name, node_type))
  155. r_ret = Ref<StyleBox>();
  156. else
  157. r_ret = get_stylebox(name, node_type);
  158. } else if (type == "fonts") {
  159. if (!has_font(name, node_type))
  160. r_ret = Ref<Font>();
  161. else
  162. r_ret = get_font(name, node_type);
  163. } else if (type == "colors") {
  164. r_ret = get_color(name, node_type);
  165. } else if (type == "constants") {
  166. r_ret = get_constant(name, node_type);
  167. } else
  168. return false;
  169. return true;
  170. }
  171. return false;
  172. }
  173. void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
  174. List<PropertyInfo> list;
  175. const StringName *key = NULL;
  176. while ((key = icon_map.next(key))) {
  177. const StringName *key2 = NULL;
  178. while ((key2 = icon_map[*key].next(key2))) {
  179. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/icons/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  180. }
  181. }
  182. key = NULL;
  183. while ((key = style_map.next(key))) {
  184. const StringName *key2 = NULL;
  185. while ((key2 = style_map[*key].next(key2))) {
  186. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/styles/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  187. }
  188. }
  189. key = NULL;
  190. while ((key = font_map.next(key))) {
  191. const StringName *key2 = NULL;
  192. while ((key2 = font_map[*key].next(key2))) {
  193. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/fonts/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  194. }
  195. }
  196. key = NULL;
  197. while ((key = color_map.next(key))) {
  198. const StringName *key2 = NULL;
  199. while ((key2 = color_map[*key].next(key2))) {
  200. list.push_back(PropertyInfo(Variant::COLOR, String() + *key + "/colors/" + *key2));
  201. }
  202. }
  203. key = NULL;
  204. while ((key = constant_map.next(key))) {
  205. const StringName *key2 = NULL;
  206. while ((key2 = constant_map[*key].next(key2))) {
  207. list.push_back(PropertyInfo(Variant::INT, String() + *key + "/constants/" + *key2));
  208. }
  209. }
  210. list.sort();
  211. for (List<PropertyInfo>::Element *E = list.front(); E; E = E->next()) {
  212. p_list->push_back(E->get());
  213. }
  214. }
  215. void Theme::set_default_theme_font(const Ref<Font> &p_default_font) {
  216. if (default_theme_font == p_default_font)
  217. return;
  218. if (default_theme_font.is_valid()) {
  219. default_theme_font->disconnect("changed", this, "_emit_theme_changed");
  220. }
  221. default_theme_font = p_default_font;
  222. if (default_theme_font.is_valid()) {
  223. default_theme_font->connect("changed", this, "_emit_theme_changed", varray(), CONNECT_REFERENCE_COUNTED);
  224. }
  225. _change_notify();
  226. emit_changed();
  227. }
  228. Ref<Font> Theme::get_default_theme_font() const {
  229. return default_theme_font;
  230. }
  231. Ref<Theme> Theme::project_default_theme;
  232. Ref<Theme> Theme::default_theme;
  233. Ref<Texture> Theme::default_icon;
  234. Ref<StyleBox> Theme::default_style;
  235. Ref<Font> Theme::default_font;
  236. Ref<Theme> Theme::get_default() {
  237. return default_theme;
  238. }
  239. void Theme::set_default(const Ref<Theme> &p_default) {
  240. default_theme = p_default;
  241. }
  242. Ref<Theme> Theme::get_project_default() {
  243. return project_default_theme;
  244. }
  245. void Theme::set_project_default(const Ref<Theme> &p_project_default) {
  246. project_default_theme = p_project_default;
  247. }
  248. void Theme::set_default_icon(const Ref<Texture> &p_icon) {
  249. default_icon = p_icon;
  250. }
  251. void Theme::set_default_style(const Ref<StyleBox> &p_style) {
  252. default_style = p_style;
  253. }
  254. void Theme::set_default_font(const Ref<Font> &p_font) {
  255. default_font = p_font;
  256. }
  257. void Theme::set_icon(const StringName &p_name, const StringName &p_type, const Ref<Texture> &p_icon) {
  258. //ERR_FAIL_COND(p_icon.is_null());
  259. bool new_value = !icon_map.has(p_type) || !icon_map[p_type].has(p_name);
  260. if (icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid()) {
  261. icon_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  262. }
  263. icon_map[p_type][p_name] = p_icon;
  264. if (p_icon.is_valid()) {
  265. icon_map[p_type][p_name]->connect("changed", this, "_emit_theme_changed", varray(), CONNECT_REFERENCE_COUNTED);
  266. }
  267. if (new_value) {
  268. _change_notify();
  269. emit_changed();
  270. }
  271. }
  272. Ref<Texture> Theme::get_icon(const StringName &p_name, const StringName &p_type) const {
  273. if (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid()) {
  274. return icon_map[p_type][p_name];
  275. } else {
  276. return default_icon;
  277. }
  278. }
  279. bool Theme::has_icon(const StringName &p_name, const StringName &p_type) const {
  280. return (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid());
  281. }
  282. void Theme::clear_icon(const StringName &p_name, const StringName &p_type) {
  283. ERR_FAIL_COND(!icon_map.has(p_type));
  284. ERR_FAIL_COND(!icon_map[p_type].has(p_name));
  285. if (icon_map[p_type][p_name].is_valid()) {
  286. icon_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  287. }
  288. icon_map[p_type].erase(p_name);
  289. _change_notify();
  290. emit_changed();
  291. }
  292. void Theme::get_icon_list(StringName p_type, List<StringName> *p_list) const {
  293. ERR_FAIL_NULL(p_list);
  294. if (!icon_map.has(p_type))
  295. return;
  296. const StringName *key = NULL;
  297. while ((key = icon_map[p_type].next(key))) {
  298. p_list->push_back(*key);
  299. }
  300. }
  301. void Theme::set_shader(const StringName &p_name, const StringName &p_type, const Ref<Shader> &p_shader) {
  302. bool new_value = !shader_map.has(p_type) || !shader_map[p_type].has(p_name);
  303. shader_map[p_type][p_name] = p_shader;
  304. if (new_value) {
  305. _change_notify();
  306. emit_changed();
  307. }
  308. }
  309. Ref<Shader> Theme::get_shader(const StringName &p_name, const StringName &p_type) const {
  310. if (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid()) {
  311. return shader_map[p_type][p_name];
  312. } else {
  313. return NULL;
  314. }
  315. }
  316. bool Theme::has_shader(const StringName &p_name, const StringName &p_type) const {
  317. return (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid());
  318. }
  319. void Theme::clear_shader(const StringName &p_name, const StringName &p_type) {
  320. ERR_FAIL_COND(!shader_map.has(p_type));
  321. ERR_FAIL_COND(!shader_map[p_type].has(p_name));
  322. shader_map[p_type].erase(p_name);
  323. _change_notify();
  324. emit_changed();
  325. }
  326. void Theme::get_shader_list(const StringName &p_type, List<StringName> *p_list) const {
  327. ERR_FAIL_NULL(p_list);
  328. if (!shader_map.has(p_type))
  329. return;
  330. const StringName *key = NULL;
  331. while ((key = shader_map[p_type].next(key))) {
  332. p_list->push_back(*key);
  333. }
  334. }
  335. void Theme::set_stylebox(const StringName &p_name, const StringName &p_type, const Ref<StyleBox> &p_style) {
  336. //ERR_FAIL_COND(p_style.is_null());
  337. bool new_value = !style_map.has(p_type) || !style_map[p_type].has(p_name);
  338. if (style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid()) {
  339. style_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  340. }
  341. style_map[p_type][p_name] = p_style;
  342. if (p_style.is_valid()) {
  343. style_map[p_type][p_name]->connect("changed", this, "_emit_theme_changed", varray(), CONNECT_REFERENCE_COUNTED);
  344. }
  345. if (new_value)
  346. _change_notify();
  347. emit_changed();
  348. }
  349. Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_type) const {
  350. if (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid()) {
  351. return style_map[p_type][p_name];
  352. } else {
  353. return default_style;
  354. }
  355. }
  356. bool Theme::has_stylebox(const StringName &p_name, const StringName &p_type) const {
  357. return (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid());
  358. }
  359. void Theme::clear_stylebox(const StringName &p_name, const StringName &p_type) {
  360. ERR_FAIL_COND(!style_map.has(p_type));
  361. ERR_FAIL_COND(!style_map[p_type].has(p_name));
  362. if (style_map[p_type][p_name].is_valid()) {
  363. style_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  364. }
  365. style_map[p_type].erase(p_name);
  366. _change_notify();
  367. emit_changed();
  368. }
  369. void Theme::get_stylebox_list(StringName p_type, List<StringName> *p_list) const {
  370. ERR_FAIL_NULL(p_list);
  371. if (!style_map.has(p_type))
  372. return;
  373. const StringName *key = NULL;
  374. while ((key = style_map[p_type].next(key))) {
  375. p_list->push_back(*key);
  376. }
  377. }
  378. void Theme::get_stylebox_types(List<StringName> *p_list) const {
  379. ERR_FAIL_NULL(p_list);
  380. const StringName *key = NULL;
  381. while ((key = style_map.next(key))) {
  382. p_list->push_back(*key);
  383. }
  384. }
  385. void Theme::set_font(const StringName &p_name, const StringName &p_type, const Ref<Font> &p_font) {
  386. //ERR_FAIL_COND(p_font.is_null());
  387. bool new_value = !font_map.has(p_type) || !font_map[p_type].has(p_name);
  388. if (font_map[p_type][p_name].is_valid()) {
  389. font_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  390. }
  391. font_map[p_type][p_name] = p_font;
  392. if (p_font.is_valid()) {
  393. font_map[p_type][p_name]->connect("changed", this, "_emit_theme_changed", varray(), CONNECT_REFERENCE_COUNTED);
  394. }
  395. if (new_value) {
  396. _change_notify();
  397. emit_changed();
  398. }
  399. }
  400. Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_type) const {
  401. if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid())
  402. return font_map[p_type][p_name];
  403. else if (default_theme_font.is_valid())
  404. return default_theme_font;
  405. else
  406. return default_font;
  407. }
  408. bool Theme::has_font(const StringName &p_name, const StringName &p_type) const {
  409. return (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid());
  410. }
  411. void Theme::clear_font(const StringName &p_name, const StringName &p_type) {
  412. ERR_FAIL_COND(!font_map.has(p_type));
  413. ERR_FAIL_COND(!font_map[p_type].has(p_name));
  414. if (font_map[p_type][p_name].is_valid()) {
  415. font_map[p_type][p_name]->disconnect("changed", this, "_emit_theme_changed");
  416. }
  417. font_map[p_type].erase(p_name);
  418. _change_notify();
  419. emit_changed();
  420. }
  421. void Theme::get_font_list(StringName p_type, List<StringName> *p_list) const {
  422. ERR_FAIL_NULL(p_list);
  423. if (!font_map.has(p_type))
  424. return;
  425. const StringName *key = NULL;
  426. while ((key = font_map[p_type].next(key))) {
  427. p_list->push_back(*key);
  428. }
  429. }
  430. void Theme::set_color(const StringName &p_name, const StringName &p_type, const Color &p_color) {
  431. bool new_value = !color_map.has(p_type) || !color_map[p_type].has(p_name);
  432. color_map[p_type][p_name] = p_color;
  433. if (new_value) {
  434. _change_notify();
  435. emit_changed();
  436. }
  437. }
  438. Color Theme::get_color(const StringName &p_name, const StringName &p_type) const {
  439. if (color_map.has(p_type) && color_map[p_type].has(p_name))
  440. return color_map[p_type][p_name];
  441. else
  442. return Color();
  443. }
  444. bool Theme::has_color(const StringName &p_name, const StringName &p_type) const {
  445. return (color_map.has(p_type) && color_map[p_type].has(p_name));
  446. }
  447. void Theme::clear_color(const StringName &p_name, const StringName &p_type) {
  448. ERR_FAIL_COND(!color_map.has(p_type));
  449. ERR_FAIL_COND(!color_map[p_type].has(p_name));
  450. color_map[p_type].erase(p_name);
  451. _change_notify();
  452. emit_changed();
  453. }
  454. void Theme::get_color_list(StringName p_type, List<StringName> *p_list) const {
  455. ERR_FAIL_NULL(p_list);
  456. if (!color_map.has(p_type))
  457. return;
  458. const StringName *key = NULL;
  459. while ((key = color_map[p_type].next(key))) {
  460. p_list->push_back(*key);
  461. }
  462. }
  463. void Theme::set_constant(const StringName &p_name, const StringName &p_type, int p_constant) {
  464. bool new_value = !constant_map.has(p_type) || !constant_map[p_type].has(p_name);
  465. constant_map[p_type][p_name] = p_constant;
  466. if (new_value) {
  467. _change_notify();
  468. emit_changed();
  469. }
  470. }
  471. int Theme::get_constant(const StringName &p_name, const StringName &p_type) const {
  472. if (constant_map.has(p_type) && constant_map[p_type].has(p_name))
  473. return constant_map[p_type][p_name];
  474. else {
  475. return 0;
  476. }
  477. }
  478. bool Theme::has_constant(const StringName &p_name, const StringName &p_type) const {
  479. return (constant_map.has(p_type) && constant_map[p_type].has(p_name));
  480. }
  481. void Theme::clear_constant(const StringName &p_name, const StringName &p_type) {
  482. ERR_FAIL_COND(!constant_map.has(p_type));
  483. ERR_FAIL_COND(!constant_map[p_type].has(p_name));
  484. constant_map[p_type].erase(p_name);
  485. _change_notify();
  486. emit_changed();
  487. }
  488. void Theme::get_constant_list(StringName p_type, List<StringName> *p_list) const {
  489. ERR_FAIL_NULL(p_list);
  490. if (!constant_map.has(p_type))
  491. return;
  492. const StringName *key = NULL;
  493. while ((key = constant_map[p_type].next(key))) {
  494. p_list->push_back(*key);
  495. }
  496. }
  497. void Theme::clear() {
  498. //these need disconnecting
  499. {
  500. const StringName *K = NULL;
  501. while ((K = icon_map.next(K))) {
  502. const StringName *L = NULL;
  503. while ((L = icon_map[*K].next(L))) {
  504. Ref<Texture> icon = icon_map[*K][*L];
  505. if (icon.is_valid()) {
  506. icon->disconnect("changed", this, "_emit_theme_changed");
  507. }
  508. }
  509. }
  510. }
  511. {
  512. const StringName *K = NULL;
  513. while ((K = style_map.next(K))) {
  514. const StringName *L = NULL;
  515. while ((L = style_map[*K].next(L))) {
  516. Ref<StyleBox> style = style_map[*K][*L];
  517. if (style.is_valid()) {
  518. style->disconnect("changed", this, "_emit_theme_changed");
  519. }
  520. }
  521. }
  522. }
  523. {
  524. const StringName *K = NULL;
  525. while ((K = font_map.next(K))) {
  526. const StringName *L = NULL;
  527. while ((L = font_map[*K].next(L))) {
  528. Ref<Font> font = font_map[*K][*L];
  529. if (font.is_valid()) {
  530. font->disconnect("changed", this, "_emit_theme_changed");
  531. }
  532. }
  533. }
  534. }
  535. icon_map.clear();
  536. style_map.clear();
  537. font_map.clear();
  538. shader_map.clear();
  539. color_map.clear();
  540. constant_map.clear();
  541. _change_notify();
  542. emit_changed();
  543. }
  544. void Theme::copy_default_theme() {
  545. Ref<Theme> default_theme2 = get_default();
  546. copy_theme(default_theme2);
  547. }
  548. void Theme::copy_theme(const Ref<Theme> &p_other) {
  549. if (p_other.is_null()) {
  550. clear();
  551. return;
  552. }
  553. //these need reconnecting, so add normally
  554. {
  555. const StringName *K = NULL;
  556. while ((K = p_other->icon_map.next(K))) {
  557. const StringName *L = NULL;
  558. while ((L = p_other->icon_map[*K].next(L))) {
  559. set_icon(*L, *K, p_other->icon_map[*K][*L]);
  560. }
  561. }
  562. }
  563. {
  564. const StringName *K = NULL;
  565. while ((K = p_other->style_map.next(K))) {
  566. const StringName *L = NULL;
  567. while ((L = p_other->style_map[*K].next(L))) {
  568. set_stylebox(*L, *K, p_other->style_map[*K][*L]);
  569. }
  570. }
  571. }
  572. {
  573. const StringName *K = NULL;
  574. while ((K = p_other->font_map.next(K))) {
  575. const StringName *L = NULL;
  576. while ((L = p_other->font_map[*K].next(L))) {
  577. set_font(*L, *K, p_other->font_map[*K][*L]);
  578. }
  579. }
  580. }
  581. //these are ok to just copy
  582. color_map = p_other->color_map;
  583. constant_map = p_other->constant_map;
  584. shader_map = p_other->shader_map;
  585. _change_notify();
  586. emit_changed();
  587. }
  588. void Theme::get_type_list(List<StringName> *p_list) const {
  589. ERR_FAIL_NULL(p_list);
  590. Set<StringName> types;
  591. const StringName *key = NULL;
  592. while ((key = icon_map.next(key))) {
  593. types.insert(*key);
  594. }
  595. key = NULL;
  596. while ((key = style_map.next(key))) {
  597. types.insert(*key);
  598. }
  599. key = NULL;
  600. while ((key = font_map.next(key))) {
  601. types.insert(*key);
  602. }
  603. key = NULL;
  604. while ((key = color_map.next(key))) {
  605. types.insert(*key);
  606. }
  607. key = NULL;
  608. while ((key = constant_map.next(key))) {
  609. types.insert(*key);
  610. }
  611. for (Set<StringName>::Element *E = types.front(); E; E = E->next()) {
  612. p_list->push_back(E->get());
  613. }
  614. }
  615. void Theme::_bind_methods() {
  616. ClassDB::bind_method(D_METHOD("set_icon", "name", "type", "texture"), &Theme::set_icon);
  617. ClassDB::bind_method(D_METHOD("get_icon", "name", "type"), &Theme::get_icon);
  618. ClassDB::bind_method(D_METHOD("has_icon", "name", "type"), &Theme::has_icon);
  619. ClassDB::bind_method(D_METHOD("clear_icon", "name", "type"), &Theme::clear_icon);
  620. ClassDB::bind_method(D_METHOD("get_icon_list", "type"), &Theme::_get_icon_list);
  621. ClassDB::bind_method(D_METHOD("set_stylebox", "name", "type", "texture"), &Theme::set_stylebox);
  622. ClassDB::bind_method(D_METHOD("get_stylebox", "name", "type"), &Theme::get_stylebox);
  623. ClassDB::bind_method(D_METHOD("has_stylebox", "name", "type"), &Theme::has_stylebox);
  624. ClassDB::bind_method(D_METHOD("clear_stylebox", "name", "type"), &Theme::clear_stylebox);
  625. ClassDB::bind_method(D_METHOD("get_stylebox_list", "type"), &Theme::_get_stylebox_list);
  626. ClassDB::bind_method(D_METHOD("get_stylebox_types"), &Theme::_get_stylebox_types);
  627. ClassDB::bind_method(D_METHOD("set_font", "name", "type", "font"), &Theme::set_font);
  628. ClassDB::bind_method(D_METHOD("get_font", "name", "type"), &Theme::get_font);
  629. ClassDB::bind_method(D_METHOD("has_font", "name", "type"), &Theme::has_font);
  630. ClassDB::bind_method(D_METHOD("clear_font", "name", "type"), &Theme::clear_font);
  631. ClassDB::bind_method(D_METHOD("get_font_list", "type"), &Theme::_get_font_list);
  632. ClassDB::bind_method(D_METHOD("set_color", "name", "type", "color"), &Theme::set_color);
  633. ClassDB::bind_method(D_METHOD("get_color", "name", "type"), &Theme::get_color);
  634. ClassDB::bind_method(D_METHOD("has_color", "name", "type"), &Theme::has_color);
  635. ClassDB::bind_method(D_METHOD("clear_color", "name", "type"), &Theme::clear_color);
  636. ClassDB::bind_method(D_METHOD("get_color_list", "type"), &Theme::_get_color_list);
  637. ClassDB::bind_method(D_METHOD("set_constant", "name", "type", "constant"), &Theme::set_constant);
  638. ClassDB::bind_method(D_METHOD("get_constant", "name", "type"), &Theme::get_constant);
  639. ClassDB::bind_method(D_METHOD("has_constant", "name", "type"), &Theme::has_constant);
  640. ClassDB::bind_method(D_METHOD("clear_constant", "name", "type"), &Theme::clear_constant);
  641. ClassDB::bind_method(D_METHOD("get_constant_list", "type"), &Theme::_get_constant_list);
  642. ClassDB::bind_method(D_METHOD("clear"), &Theme::clear);
  643. ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_theme_font);
  644. ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_theme_font);
  645. ClassDB::bind_method(D_METHOD("get_type_list", "type"), &Theme::_get_type_list);
  646. ClassDB::bind_method(D_METHOD("_emit_theme_changed"), &Theme::_emit_theme_changed);
  647. ClassDB::bind_method("copy_default_theme", &Theme::copy_default_theme);
  648. ClassDB::bind_method(D_METHOD("copy_theme", "other"), &Theme::copy_theme);
  649. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");
  650. }
  651. Theme::Theme() {
  652. }
  653. Theme::~Theme() {
  654. }