theme.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*************************************************************************/
  2. /* theme.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/file_access.h"
  32. #include "print_string.h"
  33. Ref<Theme> Theme::default_theme;
  34. void Theme::_emit_theme_changed() {
  35. emit_changed();
  36. }
  37. void Theme::_ref_font(Ref<Font> p_sc) {
  38. if (!font_refcount.has(p_sc)) {
  39. font_refcount[p_sc] = 1;
  40. p_sc->connect("changed", this, "_emit_theme_changed");
  41. } else {
  42. font_refcount[p_sc] += 1;
  43. }
  44. }
  45. void Theme::_unref_font(Ref<Font> p_sc) {
  46. ERR_FAIL_COND(!font_refcount.has(p_sc));
  47. font_refcount[p_sc]--;
  48. if (font_refcount[p_sc] == 0) {
  49. p_sc->disconnect("changed", this, "_emit_theme_changed");
  50. font_refcount.erase(p_sc);
  51. }
  52. }
  53. bool Theme::_set(const StringName &p_name, const Variant &p_value) {
  54. String sname = p_name;
  55. if (sname.find("/") != -1) {
  56. String type = sname.get_slicec('/', 1);
  57. String node_type = sname.get_slicec('/', 0);
  58. String name = sname.get_slicec('/', 2);
  59. if (type == "icons") {
  60. set_icon(name, node_type, p_value);
  61. } else if (type == "styles") {
  62. set_stylebox(name, node_type, p_value);
  63. } else if (type == "fonts") {
  64. set_font(name, node_type, p_value);
  65. } else if (type == "colors") {
  66. set_color(name, node_type, p_value);
  67. } else if (type == "constants") {
  68. set_constant(name, node_type, p_value);
  69. } else
  70. return false;
  71. return true;
  72. }
  73. return false;
  74. }
  75. bool Theme::_get(const StringName &p_name, Variant &r_ret) const {
  76. String sname = p_name;
  77. if (sname.find("/") != -1) {
  78. String type = sname.get_slicec('/', 1);
  79. String node_type = sname.get_slicec('/', 0);
  80. String name = sname.get_slicec('/', 2);
  81. if (type == "icons") {
  82. if (!has_icon(name, node_type))
  83. r_ret = Ref<Texture>();
  84. else
  85. r_ret = get_icon(name, node_type);
  86. } else if (type == "styles") {
  87. if (!has_stylebox(name, node_type))
  88. r_ret = Ref<StyleBox>();
  89. else
  90. r_ret = get_stylebox(name, node_type);
  91. } else if (type == "fonts") {
  92. if (!has_font(name, node_type))
  93. r_ret = Ref<Font>();
  94. else
  95. r_ret = get_font(name, node_type);
  96. } else if (type == "colors") {
  97. r_ret = get_color(name, node_type);
  98. } else if (type == "constants") {
  99. r_ret = get_constant(name, node_type);
  100. } else
  101. return false;
  102. return true;
  103. }
  104. return false;
  105. }
  106. void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
  107. List<PropertyInfo> list;
  108. const StringName *key = NULL;
  109. while ((key = icon_map.next(key))) {
  110. const StringName *key2 = NULL;
  111. while ((key2 = icon_map[*key].next(key2))) {
  112. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/icons/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  113. }
  114. }
  115. key = NULL;
  116. while ((key = style_map.next(key))) {
  117. const StringName *key2 = NULL;
  118. while ((key2 = style_map[*key].next(key2))) {
  119. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/styles/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  120. }
  121. }
  122. key = NULL;
  123. while ((key = font_map.next(key))) {
  124. const StringName *key2 = NULL;
  125. while ((key2 = font_map[*key].next(key2))) {
  126. list.push_back(PropertyInfo(Variant::OBJECT, String() + *key + "/fonts/" + *key2, PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  127. }
  128. }
  129. key = NULL;
  130. while ((key = color_map.next(key))) {
  131. const StringName *key2 = NULL;
  132. while ((key2 = color_map[*key].next(key2))) {
  133. list.push_back(PropertyInfo(Variant::COLOR, String() + *key + "/colors/" + *key2));
  134. }
  135. }
  136. key = NULL;
  137. while ((key = constant_map.next(key))) {
  138. const StringName *key2 = NULL;
  139. while ((key2 = constant_map[*key].next(key2))) {
  140. list.push_back(PropertyInfo(Variant::INT, String() + *key + "/constants/" + *key2));
  141. }
  142. }
  143. list.sort();
  144. for (List<PropertyInfo>::Element *E = list.front(); E; E = E->next()) {
  145. p_list->push_back(E->get());
  146. }
  147. }
  148. Ref<Theme> Theme::get_default() {
  149. return default_theme;
  150. }
  151. void Theme::set_default_theme_font(const Ref<Font> &p_default_font) {
  152. if (default_theme_font == p_default_font)
  153. return;
  154. if (default_theme_font.is_valid()) {
  155. _unref_font(default_theme_font);
  156. }
  157. default_theme_font = p_default_font;
  158. if (default_theme_font.is_valid()) {
  159. _ref_font(default_theme_font);
  160. }
  161. _change_notify();
  162. emit_changed();
  163. }
  164. Ref<Font> Theme::get_default_theme_font() const {
  165. return default_theme_font;
  166. }
  167. void Theme::set_default(const Ref<Theme> &p_default) {
  168. default_theme = p_default;
  169. }
  170. Ref<Texture> Theme::default_icon;
  171. Ref<StyleBox> Theme::default_style;
  172. Ref<Font> Theme::default_font;
  173. void Theme::set_default_icon(const Ref<Texture> &p_icon) {
  174. default_icon = p_icon;
  175. }
  176. void Theme::set_default_style(const Ref<StyleBox> &p_style) {
  177. default_style = p_style;
  178. }
  179. void Theme::set_default_font(const Ref<Font> &p_font) {
  180. default_font = p_font;
  181. }
  182. void Theme::set_icon(const StringName &p_name, const StringName &p_type, const Ref<Texture> &p_icon) {
  183. //ERR_FAIL_COND(p_icon.is_null());
  184. bool new_value = !icon_map.has(p_type) || !icon_map[p_type].has(p_name);
  185. icon_map[p_type][p_name] = p_icon;
  186. if (new_value) {
  187. _change_notify();
  188. emit_changed();
  189. }
  190. }
  191. Ref<Texture> Theme::get_icon(const StringName &p_name, const StringName &p_type) const {
  192. if (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid()) {
  193. return icon_map[p_type][p_name];
  194. } else {
  195. return default_icon;
  196. }
  197. }
  198. bool Theme::has_icon(const StringName &p_name, const StringName &p_type) const {
  199. return (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid());
  200. }
  201. void Theme::clear_icon(const StringName &p_name, const StringName &p_type) {
  202. ERR_FAIL_COND(!icon_map.has(p_type));
  203. ERR_FAIL_COND(!icon_map[p_type].has(p_name));
  204. icon_map[p_type].erase(p_name);
  205. _change_notify();
  206. emit_changed();
  207. }
  208. void Theme::get_icon_list(StringName p_type, List<StringName> *p_list) const {
  209. if (!icon_map.has(p_type))
  210. return;
  211. const StringName *key = NULL;
  212. while ((key = icon_map[p_type].next(key))) {
  213. p_list->push_back(*key);
  214. }
  215. }
  216. void Theme::set_shader(const StringName &p_name, const StringName &p_type, const Ref<Shader> &p_shader) {
  217. bool new_value = !shader_map.has(p_type) || !shader_map[p_type].has(p_name);
  218. shader_map[p_type][p_name] = p_shader;
  219. if (new_value) {
  220. _change_notify();
  221. emit_changed();
  222. }
  223. }
  224. Ref<Shader> Theme::get_shader(const StringName &p_name, const StringName &p_type) const {
  225. if (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid()) {
  226. return shader_map[p_type][p_name];
  227. } else {
  228. return NULL;
  229. }
  230. }
  231. bool Theme::has_shader(const StringName &p_name, const StringName &p_type) const {
  232. return (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid());
  233. }
  234. void Theme::clear_shader(const StringName &p_name, const StringName &p_type) {
  235. ERR_FAIL_COND(!shader_map.has(p_type));
  236. ERR_FAIL_COND(!shader_map[p_type].has(p_name));
  237. shader_map[p_type].erase(p_name);
  238. _change_notify();
  239. emit_changed();
  240. }
  241. void Theme::get_shader_list(const StringName &p_type, List<StringName> *p_list) const {
  242. if (!shader_map.has(p_type))
  243. return;
  244. const StringName *key = NULL;
  245. while ((key = shader_map[p_type].next(key))) {
  246. p_list->push_back(*key);
  247. }
  248. }
  249. void Theme::set_stylebox(const StringName &p_name, const StringName &p_type, const Ref<StyleBox> &p_style) {
  250. //ERR_FAIL_COND(p_style.is_null());
  251. bool new_value = !style_map.has(p_type) || !style_map[p_type].has(p_name);
  252. style_map[p_type][p_name] = p_style;
  253. if (new_value)
  254. _change_notify();
  255. emit_changed();
  256. }
  257. Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_type) const {
  258. if (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid()) {
  259. return style_map[p_type][p_name];
  260. } else {
  261. return default_style;
  262. }
  263. }
  264. bool Theme::has_stylebox(const StringName &p_name, const StringName &p_type) const {
  265. return (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid());
  266. }
  267. void Theme::clear_stylebox(const StringName &p_name, const StringName &p_type) {
  268. ERR_FAIL_COND(!style_map.has(p_type));
  269. ERR_FAIL_COND(!style_map[p_type].has(p_name));
  270. style_map[p_type].erase(p_name);
  271. _change_notify();
  272. emit_changed();
  273. }
  274. void Theme::get_stylebox_list(StringName p_type, List<StringName> *p_list) const {
  275. if (!style_map.has(p_type))
  276. return;
  277. const StringName *key = NULL;
  278. while ((key = style_map[p_type].next(key))) {
  279. p_list->push_back(*key);
  280. }
  281. }
  282. void Theme::get_stylebox_types(List<StringName> *p_list) const {
  283. const StringName *key = NULL;
  284. while ((key = style_map.next(key))) {
  285. p_list->push_back(*key);
  286. }
  287. }
  288. void Theme::set_font(const StringName &p_name, const StringName &p_type, const Ref<Font> &p_font) {
  289. //ERR_FAIL_COND(p_font.is_null());
  290. bool new_value = !font_map.has(p_type) || !font_map[p_type].has(p_name);
  291. if (!new_value) {
  292. if (font_map[p_type][p_name].is_valid()) {
  293. _unref_font(font_map[p_type][p_name]);
  294. }
  295. }
  296. font_map[p_type][p_name] = p_font;
  297. if (p_font.is_valid()) {
  298. _ref_font(p_font);
  299. }
  300. if (new_value) {
  301. _change_notify();
  302. emit_changed();
  303. }
  304. }
  305. Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_type) const {
  306. if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid())
  307. return font_map[p_type][p_name];
  308. else if (default_theme_font.is_valid())
  309. return default_theme_font;
  310. else
  311. return default_font;
  312. }
  313. bool Theme::has_font(const StringName &p_name, const StringName &p_type) const {
  314. return (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid());
  315. }
  316. void Theme::clear_font(const StringName &p_name, const StringName &p_type) {
  317. ERR_FAIL_COND(!font_map.has(p_type));
  318. ERR_FAIL_COND(!font_map[p_type].has(p_name));
  319. if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid()) {
  320. _unref_font(font_map[p_type][p_name]);
  321. }
  322. font_map[p_type].erase(p_name);
  323. _change_notify();
  324. emit_changed();
  325. }
  326. void Theme::get_font_list(StringName p_type, List<StringName> *p_list) const {
  327. if (!font_map.has(p_type))
  328. return;
  329. const StringName *key = NULL;
  330. while ((key = font_map[p_type].next(key))) {
  331. p_list->push_back(*key);
  332. }
  333. }
  334. void Theme::set_color(const StringName &p_name, const StringName &p_type, const Color &p_color) {
  335. bool new_value = !color_map.has(p_type) || !color_map[p_type].has(p_name);
  336. color_map[p_type][p_name] = p_color;
  337. if (new_value) {
  338. _change_notify();
  339. emit_changed();
  340. }
  341. }
  342. Color Theme::get_color(const StringName &p_name, const StringName &p_type) const {
  343. if (color_map.has(p_type) && color_map[p_type].has(p_name))
  344. return color_map[p_type][p_name];
  345. else
  346. return Color();
  347. }
  348. bool Theme::has_color(const StringName &p_name, const StringName &p_type) const {
  349. return (color_map.has(p_type) && color_map[p_type].has(p_name));
  350. }
  351. void Theme::clear_color(const StringName &p_name, const StringName &p_type) {
  352. ERR_FAIL_COND(!color_map.has(p_type));
  353. ERR_FAIL_COND(!color_map[p_type].has(p_name));
  354. color_map[p_type].erase(p_name);
  355. _change_notify();
  356. emit_changed();
  357. }
  358. void Theme::get_color_list(StringName p_type, List<StringName> *p_list) const {
  359. if (!color_map.has(p_type))
  360. return;
  361. const StringName *key = NULL;
  362. while ((key = color_map[p_type].next(key))) {
  363. p_list->push_back(*key);
  364. }
  365. }
  366. void Theme::set_constant(const StringName &p_name, const StringName &p_type, int p_constant) {
  367. bool new_value = !constant_map.has(p_type) || !constant_map[p_type].has(p_name);
  368. constant_map[p_type][p_name] = p_constant;
  369. if (new_value) {
  370. _change_notify();
  371. emit_changed();
  372. }
  373. }
  374. int Theme::get_constant(const StringName &p_name, const StringName &p_type) const {
  375. if (constant_map.has(p_type) && constant_map[p_type].has(p_name))
  376. return constant_map[p_type][p_name];
  377. else {
  378. return 0;
  379. }
  380. }
  381. bool Theme::has_constant(const StringName &p_name, const StringName &p_type) const {
  382. return (constant_map.has(p_type) && constant_map[p_type].has(p_name));
  383. }
  384. void Theme::clear_constant(const StringName &p_name, const StringName &p_type) {
  385. ERR_FAIL_COND(!constant_map.has(p_type));
  386. ERR_FAIL_COND(!constant_map[p_type].has(p_name));
  387. constant_map[p_type].erase(p_name);
  388. _change_notify();
  389. emit_changed();
  390. }
  391. void Theme::get_constant_list(StringName p_type, List<StringName> *p_list) const {
  392. if (!constant_map.has(p_type))
  393. return;
  394. const StringName *key = NULL;
  395. while ((key = constant_map[p_type].next(key))) {
  396. p_list->push_back(*key);
  397. }
  398. }
  399. void Theme::copy_default_theme() {
  400. Ref<Theme> default_theme = get_default();
  401. icon_map = default_theme->icon_map;
  402. style_map = default_theme->style_map;
  403. font_map = default_theme->font_map;
  404. color_map = default_theme->color_map;
  405. constant_map = default_theme->constant_map;
  406. _change_notify();
  407. emit_changed();
  408. }
  409. void Theme::get_type_list(List<StringName> *p_list) const {
  410. Set<StringName> types;
  411. const StringName *key = NULL;
  412. while ((key = icon_map.next(key))) {
  413. types.insert(*key);
  414. }
  415. key = NULL;
  416. while ((key = style_map.next(key))) {
  417. types.insert(*key);
  418. }
  419. key = NULL;
  420. while ((key = font_map.next(key))) {
  421. types.insert(*key);
  422. }
  423. key = NULL;
  424. while ((key = color_map.next(key))) {
  425. types.insert(*key);
  426. }
  427. key = NULL;
  428. while ((key = constant_map.next(key))) {
  429. types.insert(*key);
  430. }
  431. for (Set<StringName>::Element *E = types.front(); E; E = E->next()) {
  432. p_list->push_back(E->get());
  433. }
  434. }
  435. void Theme::_bind_methods() {
  436. ClassDB::bind_method(D_METHOD("set_icon", "name", "type", "texture"), &Theme::set_icon);
  437. ClassDB::bind_method(D_METHOD("get_icon", "name", "type"), &Theme::get_icon);
  438. ClassDB::bind_method(D_METHOD("has_icon", "name", "type"), &Theme::has_icon);
  439. ClassDB::bind_method(D_METHOD("clear_icon", "name", "type"), &Theme::clear_icon);
  440. ClassDB::bind_method(D_METHOD("get_icon_list", "type"), &Theme::_get_icon_list);
  441. ClassDB::bind_method(D_METHOD("set_stylebox", "name", "type", "texture"), &Theme::set_stylebox);
  442. ClassDB::bind_method(D_METHOD("get_stylebox", "name", "type"), &Theme::get_stylebox);
  443. ClassDB::bind_method(D_METHOD("has_stylebox", "name", "type"), &Theme::has_stylebox);
  444. ClassDB::bind_method(D_METHOD("clear_stylebox", "name", "type"), &Theme::clear_stylebox);
  445. ClassDB::bind_method(D_METHOD("get_stylebox_list", "type"), &Theme::_get_stylebox_list);
  446. ClassDB::bind_method(D_METHOD("get_stylebox_types"), &Theme::_get_stylebox_types);
  447. ClassDB::bind_method(D_METHOD("set_font", "name", "type", "font"), &Theme::set_font);
  448. ClassDB::bind_method(D_METHOD("get_font", "name", "type"), &Theme::get_font);
  449. ClassDB::bind_method(D_METHOD("has_font", "name", "type"), &Theme::has_font);
  450. ClassDB::bind_method(D_METHOD("clear_font", "name", "type"), &Theme::clear_font);
  451. ClassDB::bind_method(D_METHOD("get_font_list", "type"), &Theme::_get_font_list);
  452. ClassDB::bind_method(D_METHOD("set_color", "name", "type", "color"), &Theme::set_color);
  453. ClassDB::bind_method(D_METHOD("get_color", "name", "type"), &Theme::get_color);
  454. ClassDB::bind_method(D_METHOD("has_color", "name", "type"), &Theme::has_color);
  455. ClassDB::bind_method(D_METHOD("clear_color", "name", "type"), &Theme::clear_color);
  456. ClassDB::bind_method(D_METHOD("get_color_list", "type"), &Theme::_get_color_list);
  457. ClassDB::bind_method(D_METHOD("set_constant", "name", "type", "constant"), &Theme::set_constant);
  458. ClassDB::bind_method(D_METHOD("get_constant", "name", "type"), &Theme::get_constant);
  459. ClassDB::bind_method(D_METHOD("has_constant", "name", "type"), &Theme::has_constant);
  460. ClassDB::bind_method(D_METHOD("clear_constant", "name", "type"), &Theme::clear_constant);
  461. ClassDB::bind_method(D_METHOD("get_constant_list", "type"), &Theme::_get_constant_list);
  462. ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_theme_font);
  463. ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_theme_font);
  464. ClassDB::bind_method(D_METHOD("get_type_list", "type"), &Theme::_get_type_list);
  465. ClassDB::bind_method(D_METHOD("_emit_theme_changed"), &Theme::_emit_theme_changed);
  466. ClassDB::bind_method("copy_default_theme", &Theme::copy_default_theme);
  467. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");
  468. }
  469. Theme::Theme() {
  470. }
  471. Theme::~Theme() {
  472. }
  473. RES ResourceFormatLoaderTheme::load(const String &p_path, const String &p_original_path, Error *r_error) {
  474. if (r_error)
  475. *r_error = ERR_CANT_OPEN;
  476. Error err;
  477. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  478. ERR_EXPLAIN("Unable to open theme file: " + p_path);
  479. ERR_FAIL_COND_V(err, RES());
  480. String base_path = p_path.get_base_dir();
  481. Ref<Theme> theme(memnew(Theme));
  482. Map<StringName, Variant> library;
  483. if (r_error)
  484. *r_error = ERR_FILE_CORRUPT;
  485. bool reading_library = false;
  486. int line = 0;
  487. while (!f->eof_reached()) {
  488. String l = f->get_line().strip_edges();
  489. line++;
  490. int comment = l.find(";");
  491. if (comment != -1)
  492. l = l.substr(0, comment);
  493. if (l == "")
  494. continue;
  495. if (l.begins_with("[")) {
  496. if (l == "[library]") {
  497. reading_library = true;
  498. } else if (l == "[theme]") {
  499. reading_library = false;
  500. } else {
  501. memdelete(f);
  502. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Unknown section type: '" + l + "'.");
  503. ERR_FAIL_V(RES());
  504. }
  505. continue;
  506. }
  507. int eqpos = l.find("=");
  508. if (eqpos == -1) {
  509. memdelete(f);
  510. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected '='.");
  511. ERR_FAIL_V(RES());
  512. }
  513. String right = l.substr(eqpos + 1, l.length()).strip_edges();
  514. if (right == "") {
  515. memdelete(f);
  516. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected value after '='.");
  517. ERR_FAIL_V(RES());
  518. }
  519. Variant value;
  520. if (right.is_valid_integer()) {
  521. //is number
  522. value = right.to_int();
  523. } else if (right.is_valid_html_color()) {
  524. //is html color
  525. value = Color::html(right);
  526. } else if (right.begins_with("@")) { //reference
  527. String reference = right.substr(1, right.length());
  528. if (!library.has(reference)) {
  529. memdelete(f);
  530. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid reference to '" + reference + "'.");
  531. ERR_FAIL_V(RES());
  532. }
  533. value = library[reference];
  534. } else if (right.begins_with("default")) { //use default
  535. //do none
  536. } else {
  537. //attempt to parse a constructor
  538. int popenpos = right.find("(");
  539. if (popenpos == -1) {
  540. memdelete(f);
  541. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor syntax: " + right);
  542. ERR_FAIL_V(RES());
  543. }
  544. int pclosepos = right.find_last(")");
  545. if (pclosepos == -1) {
  546. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor parameter syntax: " + right);
  547. ERR_FAIL_V(RES());
  548. }
  549. String type = right.substr(0, popenpos);
  550. String param = right.substr(popenpos + 1, pclosepos - popenpos - 1);
  551. if (type == "icon") {
  552. String path;
  553. if (param.is_abs_path())
  554. path = param;
  555. else
  556. path = base_path + "/" + param;
  557. Ref<Texture> texture = ResourceLoader::load(path);
  558. if (!texture.is_valid()) {
  559. memdelete(f);
  560. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Couldn't find icon at path: " + path);
  561. ERR_FAIL_V(RES());
  562. }
  563. value = texture;
  564. } else if (type == "sbox") {
  565. String path;
  566. if (param.is_abs_path())
  567. path = param;
  568. else
  569. path = base_path + "/" + param;
  570. Ref<StyleBox> stylebox = ResourceLoader::load(path);
  571. if (!stylebox.is_valid()) {
  572. memdelete(f);
  573. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Couldn't find stylebox at path: " + path);
  574. ERR_FAIL_V(RES());
  575. }
  576. value = stylebox;
  577. } else if (type == "sboxt") {
  578. Vector<String> params = param.split(",");
  579. if (params.size() != 5 && params.size() != 9) {
  580. memdelete(f);
  581. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid param count for sboxt(): '" + right + "'.");
  582. ERR_FAIL_V(RES());
  583. }
  584. String path = params[0];
  585. if (!param.is_abs_path())
  586. path = base_path + "/" + path;
  587. Ref<Texture> tex = ResourceLoader::load(path);
  588. if (tex.is_null()) {
  589. memdelete(f);
  590. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Could not open texture for sboxt at path: '" + params[0] + "'.");
  591. ERR_FAIL_V(RES());
  592. }
  593. Ref<StyleBoxTexture> sbtex(memnew(StyleBoxTexture));
  594. sbtex->set_texture(tex);
  595. for (int i = 0; i < 4; i++) {
  596. if (!params[i + 1].is_valid_integer()) {
  597. memdelete(f);
  598. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid expand margin parameter for sboxt #" + itos(i + 1) + ", expected integer constant, got: '" + params[i + 1] + "'.");
  599. ERR_FAIL_V(RES());
  600. }
  601. int margin = params[i + 1].to_int();
  602. sbtex->set_expand_margin_size(Margin(i), margin);
  603. }
  604. if (params.size() == 9) {
  605. for (int i = 0; i < 4; i++) {
  606. if (!params[i + 5].is_valid_integer()) {
  607. memdelete(f);
  608. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid expand margin parameter for sboxt #" + itos(i + 5) + ", expected integer constant, got: '" + params[i + 5] + "'.");
  609. ERR_FAIL_V(RES());
  610. }
  611. int margin = params[i + 5].to_int();
  612. sbtex->set_margin_size(Margin(i), margin);
  613. }
  614. }
  615. value = sbtex;
  616. } else if (type == "sboxf") {
  617. Vector<String> params = param.split(",");
  618. if (params.size() < 2) {
  619. memdelete(f);
  620. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid param count for sboxf(): '" + right + "'.");
  621. ERR_FAIL_V(RES());
  622. }
  623. Ref<StyleBoxFlat> sbflat(memnew(StyleBoxFlat));
  624. if (!params[0].is_valid_integer()) {
  625. memdelete(f);
  626. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected integer numeric constant for parameter 0 (border size).");
  627. ERR_FAIL_V(RES());
  628. }
  629. sbflat->set_border_width_all(params[0].to_int());
  630. if (!params[0].is_valid_integer()) {
  631. memdelete(f);
  632. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected integer numeric constant for parameter 0 (border size).");
  633. ERR_FAIL_V(RES());
  634. }
  635. int left = MIN(params.size() - 1, 3);
  636. int ccodes = 0;
  637. for (int i = 0; i < left; i++) {
  638. if (params[i + 1].is_valid_html_color())
  639. ccodes++;
  640. else
  641. break;
  642. }
  643. Color normal;
  644. Color bright;
  645. Color dark;
  646. if (ccodes < 1) {
  647. memdelete(f);
  648. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected at least 1, 2 or 3 html color codes.");
  649. ERR_FAIL_V(RES());
  650. } else if (ccodes == 1) {
  651. normal = Color::html(params[1]);
  652. bright = Color::html(params[1]);
  653. dark = Color::html(params[1]);
  654. } else if (ccodes == 2) {
  655. normal = Color::html(params[1]);
  656. bright = Color::html(params[2]);
  657. dark = Color::html(params[2]);
  658. } else {
  659. normal = Color::html(params[1]);
  660. bright = Color::html(params[2]);
  661. dark = Color::html(params[3]);
  662. }
  663. sbflat->set_border_color_all(bright);
  664. // sbflat->set_dark_color(dark);
  665. sbflat->set_bg_color(normal);
  666. if (params.size() == ccodes + 5) {
  667. //margins
  668. for (int i = 0; i < 4; i++) {
  669. if (!params[i + ccodes + 1].is_valid_integer()) {
  670. memdelete(f);
  671. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid expand margin parameter for sboxf #" + itos(i + ccodes + 1) + ", expected integer constant, got: '" + params[i + ccodes + 1] + "'.");
  672. ERR_FAIL_V(RES());
  673. }
  674. //int margin = params[i+ccodes+1].to_int();
  675. //sbflat->set_margin_size(Margin(i),margin);
  676. }
  677. } else if (params.size() != ccodes + 1) {
  678. memdelete(f);
  679. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid amount of margin parameters for sboxt.");
  680. ERR_FAIL_V(RES());
  681. }
  682. value = sbflat;
  683. } else {
  684. memdelete(f);
  685. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid constructor type: '" + type + "'.");
  686. ERR_FAIL_V(RES());
  687. }
  688. }
  689. //parse left and do something with it
  690. String left = l.substr(0, eqpos);
  691. if (reading_library) {
  692. left = left.strip_edges();
  693. if (!left.is_valid_identifier()) {
  694. memdelete(f);
  695. ERR_EXPLAIN(p_path + ":" + itos(line) + ": <LibraryItem> is not a valid identifier.");
  696. ERR_FAIL_V(RES());
  697. }
  698. if (library.has(left)) {
  699. memdelete(f);
  700. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Already in library: '" + left + "'.");
  701. ERR_FAIL_V(RES());
  702. }
  703. library[left] = value;
  704. } else {
  705. int pointpos = left.find(".");
  706. if (pointpos == -1) {
  707. memdelete(f);
  708. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Expected 'control.item=..' assign syntax.");
  709. ERR_FAIL_V(RES());
  710. }
  711. String control = left.substr(0, pointpos).strip_edges();
  712. if (!control.is_valid_identifier()) {
  713. memdelete(f);
  714. ERR_EXPLAIN(p_path + ":" + itos(line) + ": <Control> is not a valid identifier.");
  715. ERR_FAIL_V(RES());
  716. }
  717. String item = left.substr(pointpos + 1, left.size()).strip_edges();
  718. if (!item.is_valid_identifier()) {
  719. memdelete(f);
  720. ERR_EXPLAIN(p_path + ":" + itos(line) + ": <Item> is not a valid identifier.");
  721. ERR_FAIL_V(RES());
  722. }
  723. if (value.get_type() == Variant::NIL) {
  724. //try to use exiting
  725. if (Theme::get_default()->has_stylebox(item, control))
  726. value = Theme::get_default()->get_stylebox(item, control);
  727. else if (Theme::get_default()->has_font(item, control))
  728. value = Theme::get_default()->get_font(item, control);
  729. else if (Theme::get_default()->has_icon(item, control))
  730. value = Theme::get_default()->get_icon(item, control);
  731. else if (Theme::get_default()->has_color(item, control))
  732. value = Theme::get_default()->get_color(item, control);
  733. else if (Theme::get_default()->has_constant(item, control))
  734. value = Theme::get_default()->get_constant(item, control);
  735. else {
  736. memdelete(f);
  737. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Default not present for: '" + control + "." + item + "'.");
  738. ERR_FAIL_V(RES());
  739. }
  740. }
  741. if (value.get_type() == Variant::OBJECT) {
  742. Ref<Resource> res = value;
  743. if (!res.is_valid()) {
  744. memdelete(f);
  745. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid resource (NULL).");
  746. ERR_FAIL_V(RES());
  747. }
  748. if (Object::cast_to<StyleBox>(*res)) {
  749. theme->set_stylebox(item, control, res);
  750. } else if (Object::cast_to<Font>(*res)) {
  751. theme->set_font(item, control, res);
  752. } else if (Object::cast_to<Font>(*res)) {
  753. theme->set_font(item, control, res);
  754. } else if (Object::cast_to<Texture>(*res)) {
  755. theme->set_icon(item, control, res);
  756. } else {
  757. memdelete(f);
  758. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Invalid resource type.");
  759. ERR_FAIL_V(RES());
  760. }
  761. } else if (value.get_type() == Variant::COLOR) {
  762. theme->set_color(item, control, value);
  763. } else if (value.get_type() == Variant::INT) {
  764. theme->set_constant(item, control, value);
  765. } else {
  766. memdelete(f);
  767. ERR_EXPLAIN(p_path + ":" + itos(line) + ": Couldn't even determine what this setting is! what did you do!?");
  768. ERR_FAIL_V(RES());
  769. }
  770. }
  771. }
  772. f->close();
  773. memdelete(f);
  774. if (r_error)
  775. *r_error = OK;
  776. return theme;
  777. }
  778. void ResourceFormatLoaderTheme::get_recognized_extensions(List<String> *p_extensions) const {
  779. p_extensions->push_back("theme");
  780. }
  781. bool ResourceFormatLoaderTheme::handles_type(const String &p_type) const {
  782. return p_type == "Theme";
  783. }
  784. String ResourceFormatLoaderTheme::get_resource_type(const String &p_path) const {
  785. if (p_path.get_extension().to_lower() == "theme")
  786. return "Theme";
  787. return "";
  788. }