theme.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*************************************************************************/
  2. /* theme.h */
  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. #ifndef THEME_H
  31. #define THEME_H
  32. #include "io/resource_loader.h"
  33. #include "resource.h"
  34. #include "scene/resources/font.h"
  35. #include "scene/resources/shader.h"
  36. #include "scene/resources/style_box.h"
  37. #include "scene/resources/texture.h"
  38. /**
  39. @author Juan Linietsky <reduzio@gmail.com>
  40. */
  41. class Theme : public Resource {
  42. GDCLASS(Theme, Resource);
  43. RES_BASE_EXTENSION("theme");
  44. static Ref<Theme> default_theme;
  45. //keep a reference count to font, so each time the font changes, we emit theme changed too
  46. Map<Ref<Font>, int> font_refcount;
  47. void _ref_font(Ref<Font> p_sc);
  48. void _unref_font(Ref<Font> p_sc);
  49. void _emit_theme_changed();
  50. HashMap<StringName, HashMap<StringName, Ref<Texture>, StringNameHasher>, StringNameHasher> icon_map;
  51. HashMap<StringName, HashMap<StringName, Ref<StyleBox>, StringNameHasher>, StringNameHasher> style_map;
  52. HashMap<StringName, HashMap<StringName, Ref<Font>, StringNameHasher>, StringNameHasher> font_map;
  53. HashMap<StringName, HashMap<StringName, Ref<Shader>, StringNameHasher>, StringNameHasher> shader_map;
  54. HashMap<StringName, HashMap<StringName, Color, StringNameHasher>, StringNameHasher> color_map;
  55. HashMap<StringName, HashMap<StringName, int, StringNameHasher>, StringNameHasher> constant_map;
  56. protected:
  57. bool _set(const StringName &p_name, const Variant &p_value);
  58. bool _get(const StringName &p_name, Variant &r_ret) const;
  59. void _get_property_list(List<PropertyInfo> *p_list) const;
  60. static Ref<Texture> default_icon;
  61. static Ref<StyleBox> default_style;
  62. static Ref<Font> default_font;
  63. Ref<Font> default_theme_font;
  64. PoolVector<String> _get_icon_list(const String &p_type) const {
  65. PoolVector<String> ilret;
  66. List<StringName> il;
  67. get_icon_list(p_type, &il);
  68. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  69. ilret.push_back(E->get());
  70. }
  71. return ilret;
  72. }
  73. PoolVector<String> _get_stylebox_list(const String &p_type) const {
  74. PoolVector<String> ilret;
  75. List<StringName> il;
  76. get_stylebox_list(p_type, &il);
  77. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  78. ilret.push_back(E->get());
  79. }
  80. return ilret;
  81. }
  82. PoolVector<String> _get_stylebox_types(void) const {
  83. PoolVector<String> ilret;
  84. List<StringName> il;
  85. get_stylebox_types(&il);
  86. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  87. ilret.push_back(E->get());
  88. }
  89. return ilret;
  90. }
  91. PoolVector<String> _get_font_list(const String &p_type) const {
  92. PoolVector<String> ilret;
  93. List<StringName> il;
  94. get_font_list(p_type, &il);
  95. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  96. ilret.push_back(E->get());
  97. }
  98. return ilret;
  99. }
  100. PoolVector<String> _get_color_list(const String &p_type) const {
  101. PoolVector<String> ilret;
  102. List<StringName> il;
  103. get_color_list(p_type, &il);
  104. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  105. ilret.push_back(E->get());
  106. }
  107. return ilret;
  108. }
  109. PoolVector<String> _get_constant_list(const String &p_type) const {
  110. PoolVector<String> ilret;
  111. List<StringName> il;
  112. get_constant_list(p_type, &il);
  113. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  114. ilret.push_back(E->get());
  115. }
  116. return ilret;
  117. }
  118. PoolVector<String> _get_type_list(const String &p_type) const {
  119. PoolVector<String> ilret;
  120. List<StringName> il;
  121. get_type_list(&il);
  122. for (List<StringName>::Element *E = il.front(); E; E = E->next()) {
  123. ilret.push_back(E->get());
  124. }
  125. return ilret;
  126. }
  127. static void _bind_methods();
  128. public:
  129. static Ref<Theme> get_default();
  130. static void set_default(const Ref<Theme> &p_default);
  131. static void set_default_icon(const Ref<Texture> &p_icon);
  132. static void set_default_style(const Ref<StyleBox> &p_style);
  133. static void set_default_font(const Ref<Font> &p_font);
  134. void set_default_theme_font(const Ref<Font> &p_default_font);
  135. Ref<Font> get_default_theme_font() const;
  136. void set_icon(const StringName &p_name, const StringName &p_type, const Ref<Texture> &p_icon);
  137. Ref<Texture> get_icon(const StringName &p_name, const StringName &p_type) const;
  138. bool has_icon(const StringName &p_name, const StringName &p_type) const;
  139. void clear_icon(const StringName &p_name, const StringName &p_type);
  140. void get_icon_list(StringName p_type, List<StringName> *p_list) const;
  141. void set_shader(const StringName &p_name, const StringName &p_type, const Ref<Shader> &p_shader);
  142. Ref<Shader> get_shader(const StringName &p_name, const StringName &p_type) const;
  143. bool has_shader(const StringName &p_name, const StringName &p_type) const;
  144. void clear_shader(const StringName &p_name, const StringName &p_type);
  145. void get_shader_list(const StringName &p_type, List<StringName> *p_list) const;
  146. void set_stylebox(const StringName &p_name, const StringName &p_type, const Ref<StyleBox> &p_style);
  147. Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_type) const;
  148. bool has_stylebox(const StringName &p_name, const StringName &p_type) const;
  149. void clear_stylebox(const StringName &p_name, const StringName &p_type);
  150. void get_stylebox_list(StringName p_type, List<StringName> *p_list) const;
  151. void get_stylebox_types(List<StringName> *p_list) const;
  152. void set_font(const StringName &p_name, const StringName &p_type, const Ref<Font> &p_font);
  153. Ref<Font> get_font(const StringName &p_name, const StringName &p_type) const;
  154. bool has_font(const StringName &p_name, const StringName &p_type) const;
  155. void clear_font(const StringName &p_name, const StringName &p_type);
  156. void get_font_list(StringName p_type, List<StringName> *p_list) const;
  157. void set_color(const StringName &p_name, const StringName &p_type, const Color &p_color);
  158. Color get_color(const StringName &p_name, const StringName &p_type) const;
  159. bool has_color(const StringName &p_name, const StringName &p_type) const;
  160. void clear_color(const StringName &p_name, const StringName &p_type);
  161. void get_color_list(StringName p_type, List<StringName> *p_list) const;
  162. void set_constant(const StringName &p_name, const StringName &p_type, int p_constant);
  163. int get_constant(const StringName &p_name, const StringName &p_type) const;
  164. bool has_constant(const StringName &p_name, const StringName &p_type) const;
  165. void clear_constant(const StringName &p_name, const StringName &p_type);
  166. void get_constant_list(StringName p_type, List<StringName> *p_list) const;
  167. void get_type_list(List<StringName> *p_list) const;
  168. void copy_default_theme();
  169. Theme();
  170. ~Theme();
  171. };
  172. class ResourceFormatLoaderTheme : public ResourceFormatLoader {
  173. public:
  174. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  175. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  176. virtual bool handles_type(const String &p_type) const;
  177. virtual String get_resource_type(const String &p_path) const;
  178. };
  179. #endif