theme.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*************************************************************************/
  2. /* theme.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "theme.h"
  30. #include "print_string.h"
  31. #include "os/file_access.h"
  32. Ref<Theme> Theme::default_theme;
  33. void Theme::_emit_theme_changed() {
  34. emit_changed();
  35. }
  36. void Theme::_ref_font( Ref<Font> p_sc) {
  37. if (!font_refcount.has(p_sc)) {
  38. font_refcount[p_sc]=1;
  39. p_sc->connect("changed",this,"_emit_theme_changed");
  40. } else {
  41. font_refcount[p_sc]+=1;
  42. }
  43. }
  44. void Theme::_unref_font(Ref<Font> p_sc) {
  45. ERR_FAIL_COND(!font_refcount.has(p_sc));
  46. font_refcount[p_sc]--;
  47. if (font_refcount[p_sc]==0) {
  48. p_sc->disconnect("changed",this,"_emit_theme_changed");
  49. font_refcount.erase(p_sc);
  50. }
  51. }
  52. bool Theme::_set(const StringName& p_name, const Variant& p_value) {
  53. String sname=p_name;
  54. if (sname.find("/")!=-1) {
  55. String type=sname.get_slicec('/',1);
  56. String node_type=sname.get_slicec('/',0);
  57. String name=sname.get_slicec('/',2);
  58. if (type=="icons") {
  59. set_icon(name,node_type,p_value);
  60. } else if (type=="styles") {
  61. set_stylebox(name,node_type,p_value);
  62. } else if (type=="fonts") {
  63. set_font(name,node_type,p_value);
  64. } else if (type=="colors") {
  65. set_color(name,node_type,p_value);
  66. } else if (type=="constants") {
  67. set_constant(name,node_type,p_value);
  68. } else
  69. return false;
  70. return true;
  71. }
  72. return false;
  73. }
  74. bool Theme::_get(const StringName& p_name,Variant &r_ret) const {
  75. String sname=p_name;
  76. if (sname.find("/")!=-1) {
  77. String type=sname.get_slicec('/',1);
  78. String node_type=sname.get_slicec('/',0);
  79. String name=sname.get_slicec('/',2);
  80. if (type=="icons") {
  81. if (!has_icon(name,node_type))
  82. r_ret=Ref<Texture>();
  83. else
  84. r_ret= get_icon(name,node_type);
  85. } else if (type=="styles") {
  86. if (!has_stylebox(name,node_type))
  87. r_ret=Ref<StyleBox>();
  88. else
  89. r_ret= get_stylebox(name,node_type);
  90. } else if (type=="fonts") {
  91. if (!has_font(name,node_type))
  92. r_ret=Ref<Font>();
  93. else
  94. r_ret= get_font(name,node_type);
  95. } else if (type=="colors") {
  96. r_ret= get_color(name,node_type);
  97. } else if (type=="constants") {
  98. r_ret= get_constant(name,node_type);
  99. } else
  100. return false;
  101. return true;
  102. }
  103. return false;
  104. }
  105. void Theme::_get_property_list( List<PropertyInfo> *p_list) const {
  106. List<PropertyInfo> list;
  107. const StringName *key=NULL;
  108. while((key=icon_map.next(key))) {
  109. const StringName *key2=NULL;
  110. while((key2=icon_map[*key].next(key2))) {
  111. list.push_back( PropertyInfo( Variant::OBJECT, String()+*key+"/icons/"+*key2, PROPERTY_HINT_RESOURCE_TYPE, "Texture",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NULL ) );
  112. }
  113. }
  114. key=NULL;
  115. while((key=style_map.next(key))) {
  116. const StringName *key2=NULL;
  117. while((key2=style_map[*key].next(key2))) {
  118. list.push_back( PropertyInfo( Variant::OBJECT, String()+*key+"/styles/"+*key2, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NULL ) );
  119. }
  120. }
  121. key=NULL;
  122. while((key=font_map.next(key))) {
  123. const StringName *key2=NULL;
  124. while((key2=font_map[*key].next(key2))) {
  125. list.push_back( PropertyInfo( Variant::OBJECT, String()+*key+"/fonts/"+*key2, PROPERTY_HINT_RESOURCE_TYPE, "Font",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_STORE_IF_NULL ) );
  126. }
  127. }
  128. key=NULL;
  129. while((key=color_map.next(key))) {
  130. const StringName *key2=NULL;
  131. while((key2=color_map[*key].next(key2))) {
  132. list.push_back( PropertyInfo( Variant::COLOR, String()+*key+"/colors/"+*key2 ) );
  133. }
  134. }
  135. key=NULL;
  136. while((key=constant_map.next(key))) {
  137. const StringName *key2=NULL;
  138. while((key2=constant_map[*key].next(key2))) {
  139. list.push_back( PropertyInfo( Variant::INT, String()+*key+"/constants/"+*key2 ) );
  140. }
  141. }
  142. list.sort();
  143. for(List<PropertyInfo>::Element *E=list.front();E;E=E->next()) {
  144. p_list->push_back(E->get());
  145. }
  146. }
  147. Ref<Theme> Theme::get_default() {
  148. return default_theme;
  149. }
  150. void Theme::set_default_theme_font( const Ref<Font>& p_default_font ) {
  151. if (default_theme_font==p_default_font)
  152. return;
  153. if (default_theme_font.is_valid()) {
  154. _unref_font(default_theme_font);
  155. }
  156. default_theme_font=p_default_font;
  157. if (default_theme_font.is_valid()) {
  158. _ref_font(default_theme_font);
  159. }
  160. _change_notify();
  161. emit_changed();;
  162. }
  163. Ref<Font> Theme::get_default_theme_font() const {
  164. return default_theme_font;
  165. }
  166. void Theme::set_default(const Ref<Theme>& p_default) {
  167. default_theme=p_default;
  168. }
  169. Ref<Texture> Theme::default_icon;
  170. Ref<StyleBox> Theme::default_style;
  171. Ref<Font> Theme::default_font;
  172. void Theme::set_default_icon( const Ref<Texture>& p_icon ) {
  173. default_icon=p_icon;
  174. }
  175. void Theme::set_default_style( const Ref<StyleBox>& p_style) {
  176. default_style=p_style;
  177. }
  178. void Theme::set_default_font( const Ref<Font>& p_font ) {
  179. default_font=p_font;
  180. }
  181. void Theme::set_icon(const StringName& p_name,const StringName& p_type,const Ref<Texture>& p_icon) {
  182. // ERR_FAIL_COND(p_icon.is_null());
  183. bool new_value=!icon_map.has(p_type) || !icon_map[p_type].has(p_name);
  184. icon_map[p_type][p_name]=p_icon;
  185. if (new_value) {
  186. _change_notify();
  187. emit_changed();;
  188. }
  189. }
  190. Ref<Texture> Theme::get_icon(const StringName& p_name,const StringName& p_type) const {
  191. if (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid()) {
  192. return icon_map[p_type][p_name];
  193. } else {
  194. return default_icon;
  195. }
  196. }
  197. bool Theme::has_icon(const StringName& p_name,const StringName& p_type) const {
  198. return (icon_map.has(p_type) && icon_map[p_type].has(p_name) && icon_map[p_type][p_name].is_valid());
  199. }
  200. void Theme::clear_icon(const StringName& p_name,const StringName& p_type) {
  201. ERR_FAIL_COND(!icon_map.has(p_type));
  202. ERR_FAIL_COND(!icon_map[p_type].has(p_name));
  203. icon_map[p_type].erase(p_name);
  204. _change_notify();
  205. emit_changed();;
  206. }
  207. void Theme::get_icon_list(StringName p_type, List<StringName> *p_list) const {
  208. if (!icon_map.has(p_type))
  209. return;
  210. const StringName *key=NULL;
  211. while((key=icon_map[p_type].next(key))) {
  212. p_list->push_back(*key);
  213. }
  214. }
  215. void Theme::set_shader(const StringName &p_name,const StringName &p_type,const Ref<Shader>& p_shader) {
  216. bool new_value=!shader_map.has(p_type) || !shader_map[p_type].has(p_name);
  217. shader_map[p_type][p_name]=p_shader;
  218. if (new_value) {
  219. _change_notify();
  220. emit_changed();;
  221. }
  222. }
  223. Ref<Shader> Theme::get_shader(const StringName &p_name, const StringName &p_type) const {
  224. if (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid()) {
  225. return shader_map[p_type][p_name];
  226. } else {
  227. return NULL;
  228. }
  229. }
  230. bool Theme::has_shader(const StringName &p_name, const StringName &p_type) const {
  231. return (shader_map.has(p_type) && shader_map[p_type].has(p_name) && shader_map[p_type][p_name].is_valid());
  232. }
  233. void Theme::clear_shader(const StringName &p_name, const StringName &p_type) {
  234. ERR_FAIL_COND(!shader_map.has(p_type));
  235. ERR_FAIL_COND(!shader_map[p_type].has(p_name));
  236. shader_map[p_type].erase(p_name);
  237. _change_notify();
  238. emit_changed();;
  239. }
  240. void Theme::get_shader_list(const StringName &p_type, List<StringName> *p_list) const {
  241. if (!shader_map.has(p_type))
  242. return;
  243. const StringName *key=NULL;
  244. while((key=shader_map[p_type].next(key))) {
  245. p_list->push_back(*key);
  246. }
  247. }
  248. void Theme::set_stylebox(const StringName& p_name,const StringName& p_type,const Ref<StyleBox>& p_style) {
  249. // ERR_FAIL_COND(p_style.is_null());
  250. bool new_value=!style_map.has(p_type) || !style_map[p_type].has(p_name);
  251. style_map[p_type][p_name]=p_style;
  252. if (new_value)
  253. _change_notify();
  254. emit_changed();;
  255. }
  256. Ref<StyleBox> Theme::get_stylebox(const StringName& p_name,const StringName& p_type) const {
  257. if (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid()) {
  258. return style_map[p_type][p_name];
  259. } else {
  260. return default_style;
  261. }
  262. }
  263. bool Theme::has_stylebox(const StringName& p_name,const StringName& p_type) const {
  264. return (style_map.has(p_type) && style_map[p_type].has(p_name) && style_map[p_type][p_name].is_valid());
  265. }
  266. void Theme::clear_stylebox(const StringName& p_name,const StringName& p_type) {
  267. ERR_FAIL_COND(!style_map.has(p_type));
  268. ERR_FAIL_COND(!style_map[p_type].has(p_name));
  269. style_map[p_type].erase(p_name);
  270. _change_notify();
  271. emit_changed();;
  272. }
  273. void Theme::get_stylebox_list(StringName p_type, List<StringName> *p_list) const {
  274. if (!style_map.has(p_type))
  275. return;
  276. const StringName *key=NULL;
  277. while((key=style_map[p_type].next(key))) {
  278. p_list->push_back(*key);
  279. }
  280. }
  281. void Theme::get_stylebox_types(List<StringName> *p_list) const {
  282. const StringName *key=NULL;
  283. while((key=style_map.next(key))) {
  284. p_list->push_back(*key);
  285. }
  286. }
  287. void Theme::set_font(const StringName& p_name,const StringName& p_type,const Ref<Font>& p_font) {
  288. // ERR_FAIL_COND(p_font.is_null());
  289. bool new_value=!font_map.has(p_type) || !font_map[p_type].has(p_name);
  290. if (!new_value) {
  291. if (font_map[p_type][p_name].is_valid()) {
  292. _unref_font(font_map[p_type][p_name]);
  293. }
  294. }
  295. font_map[p_type][p_name]=p_font;
  296. if (p_font.is_valid()) {
  297. _ref_font(p_font);
  298. }
  299. if (new_value) {
  300. _change_notify();
  301. emit_changed();;
  302. }
  303. }
  304. Ref<Font> Theme::get_font(const StringName& p_name,const StringName& p_type) const {
  305. if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid())
  306. return font_map[p_type][p_name];
  307. else if (default_theme_font.is_valid())
  308. return default_theme_font;
  309. else
  310. return default_font;
  311. }
  312. bool Theme::has_font(const StringName& p_name,const StringName& p_type) const {
  313. return (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid());
  314. }
  315. void Theme::clear_font(const StringName& p_name,const StringName& p_type) {
  316. ERR_FAIL_COND(!font_map.has(p_type));
  317. ERR_FAIL_COND(!font_map[p_type].has(p_name));
  318. if (font_map.has(p_type) && font_map[p_type].has(p_name) && font_map[p_type][p_name].is_valid()) {
  319. _unref_font(font_map[p_type][p_name]);
  320. }
  321. font_map[p_type].erase(p_name);
  322. _change_notify();
  323. emit_changed();;
  324. }
  325. void Theme::get_font_list(StringName p_type, List<StringName> *p_list) const {
  326. if (!font_map.has(p_type))
  327. return;
  328. const StringName *key=NULL;
  329. while((key=font_map[p_type].next(key))) {
  330. p_list->push_back(*key);
  331. }
  332. }
  333. void Theme::set_color(const StringName& p_name,const StringName& p_type,const Color& p_color) {
  334. bool new_value=!color_map.has(p_type) || !color_map[p_type].has(p_name);
  335. color_map[p_type][p_name]=p_color;
  336. if (new_value) {
  337. _change_notify();
  338. emit_changed();;
  339. }
  340. }
  341. Color Theme::get_color(const StringName& p_name,const StringName& p_type) const {
  342. if (color_map.has(p_type) && color_map[p_type].has(p_name))
  343. return color_map[p_type][p_name];
  344. else
  345. return Color();
  346. }
  347. bool Theme::has_color(const StringName& p_name,const StringName& p_type) const {
  348. return (color_map.has(p_type) && color_map[p_type].has(p_name));
  349. }
  350. void Theme::clear_color(const StringName& p_name,const StringName& p_type) {
  351. ERR_FAIL_COND(!color_map.has(p_type));
  352. ERR_FAIL_COND(!color_map[p_type].has(p_name));
  353. color_map[p_type].erase(p_name);
  354. _change_notify();
  355. emit_changed();;
  356. }
  357. void Theme::get_color_list(StringName p_type, List<StringName> *p_list) const {
  358. if (!color_map.has(p_type))
  359. return;
  360. const StringName *key=NULL;
  361. while((key=color_map[p_type].next(key))) {
  362. p_list->push_back(*key);
  363. }
  364. }
  365. void Theme::set_constant(const StringName& p_name,const StringName& p_type,int p_constant) {
  366. bool new_value=!constant_map.has(p_type) || !constant_map[p_type].has(p_name);
  367. constant_map[p_type][p_name]=p_constant;
  368. if (new_value) {
  369. _change_notify();
  370. emit_changed();;
  371. }
  372. }
  373. int Theme::get_constant(const StringName& p_name,const StringName& p_type) const {
  374. if (constant_map.has(p_type) && constant_map[p_type].has(p_name))
  375. return constant_map[p_type][p_name];
  376. else {
  377. return 0;
  378. }
  379. }
  380. bool Theme::has_constant(const StringName& p_name,const StringName& p_type) const {
  381. return (constant_map.has(p_type) && constant_map[p_type].has(p_name));
  382. }
  383. void Theme::clear_constant(const StringName& p_name,const StringName& p_type) {
  384. ERR_FAIL_COND(!constant_map.has(p_type));
  385. ERR_FAIL_COND(!constant_map[p_type].has(p_name));
  386. constant_map[p_type].erase(p_name);
  387. _change_notify();
  388. emit_changed();;
  389. }
  390. void Theme::get_constant_list(StringName p_type, List<StringName> *p_list) const {
  391. if (!constant_map.has(p_type))
  392. return;
  393. const StringName *key=NULL;
  394. while((key=constant_map[p_type].next(key))) {
  395. p_list->push_back(*key);
  396. }
  397. }
  398. void Theme::copy_default_theme() {
  399. Ref<Theme> default_theme=get_default();
  400. icon_map=default_theme->icon_map;
  401. style_map=default_theme->style_map;
  402. font_map=default_theme->font_map;
  403. color_map=default_theme->color_map;
  404. constant_map=default_theme->constant_map;
  405. _change_notify();
  406. emit_changed();;
  407. }
  408. void Theme::get_type_list(List<StringName> *p_list) const {
  409. Set<StringName> types;
  410. const StringName *key=NULL;
  411. while((key=icon_map.next(key))) {
  412. types.insert(*key);
  413. }
  414. key=NULL;
  415. while((key=style_map.next(key))) {
  416. types.insert(*key);
  417. }
  418. key=NULL;
  419. while((key=font_map.next(key))) {
  420. types.insert(*key);
  421. }
  422. key=NULL;
  423. while((key=color_map.next(key))) {
  424. types.insert(*key);
  425. }
  426. key=NULL;
  427. while((key=constant_map.next(key))) {
  428. types.insert(*key);
  429. }
  430. for(Set<StringName>::Element *E=types.front();E;E=E->next()) {
  431. p_list->push_back(E->get());
  432. }
  433. }
  434. void Theme::_bind_methods() {
  435. ClassDB::bind_method(_MD("set_icon","name","type","texture:Texture"),&Theme::set_icon);
  436. ClassDB::bind_method(_MD("get_icon:Texture","name","type"),&Theme::get_icon);
  437. ClassDB::bind_method(_MD("has_icon","name","type"),&Theme::has_icon);
  438. ClassDB::bind_method(_MD("clear_icon","name","type"),&Theme::clear_icon);
  439. ClassDB::bind_method(_MD("get_icon_list","type"),&Theme::_get_icon_list);
  440. ClassDB::bind_method(_MD("set_stylebox","name","type","texture:StyleBox"),&Theme::set_stylebox);
  441. ClassDB::bind_method(_MD("get_stylebox:StyleBox","name","type"),&Theme::get_stylebox);
  442. ClassDB::bind_method(_MD("has_stylebox","name","type"),&Theme::has_stylebox);
  443. ClassDB::bind_method(_MD("clear_stylebox","name","type"),&Theme::clear_stylebox);
  444. ClassDB::bind_method(_MD("get_stylebox_list","type"),&Theme::_get_stylebox_list);
  445. ClassDB::bind_method(_MD("get_stylebox_types"),&Theme::_get_stylebox_types);
  446. ClassDB::bind_method(_MD("set_font","name","type","font:Font"),&Theme::set_font);
  447. ClassDB::bind_method(_MD("get_font:Font","name","type"),&Theme::get_font);
  448. ClassDB::bind_method(_MD("has_font","name","type"),&Theme::has_font);
  449. ClassDB::bind_method(_MD("clear_font","name","type"),&Theme::clear_font);
  450. ClassDB::bind_method(_MD("get_font_list","type"),&Theme::_get_font_list);
  451. ClassDB::bind_method(_MD("set_color","name","type","color"),&Theme::set_color);
  452. ClassDB::bind_method(_MD("get_color","name","type"),&Theme::get_color);
  453. ClassDB::bind_method(_MD("has_color","name","type"),&Theme::has_color);
  454. ClassDB::bind_method(_MD("clear_color","name","type"),&Theme::clear_color);
  455. ClassDB::bind_method(_MD("get_color_list","type"),&Theme::_get_color_list);
  456. ClassDB::bind_method(_MD("set_constant","name","type","constant"),&Theme::set_constant);
  457. ClassDB::bind_method(_MD("get_constant","name","type"),&Theme::get_constant);
  458. ClassDB::bind_method(_MD("has_constant","name","type"),&Theme::has_constant);
  459. ClassDB::bind_method(_MD("clear_constant","name","type"),&Theme::clear_constant);
  460. ClassDB::bind_method(_MD("get_constant_list","type"),&Theme::_get_constant_list);
  461. ClassDB::bind_method(_MD("set_default_font","font"),&Theme::set_default_theme_font);
  462. ClassDB::bind_method(_MD("get_default_font"),&Theme::get_default_theme_font);
  463. ClassDB::bind_method(_MD("get_type_list","type"),&Theme::_get_type_list);
  464. ClassDB::bind_method(_MD("_emit_theme_changed"),&Theme::_emit_theme_changed);
  465. ClassDB::bind_method("copy_default_theme",&Theme::copy_default_theme);
  466. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"default_font",PROPERTY_HINT_RESOURCE_TYPE,"Font"),_SCS("set_default_font"),_SCS("get_default_font"));
  467. }
  468. Theme::Theme() {
  469. }
  470. Theme::~Theme()
  471. {
  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_size(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_dark_color(dark);
  664. sbflat->set_light_color(bright);
  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 (res->cast_to<StyleBox>()) {
  749. theme->set_stylebox(item,control,res);
  750. } else if (res->cast_to<Font>()) {
  751. theme->set_font(item,control,res);
  752. } else if (res->cast_to<Font>()) {
  753. theme->set_font(item,control,res);
  754. } else if (res->cast_to<Texture>()) {
  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. }