theme.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* theme.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef THEME_H
  31. #define THEME_H
  32. #include "core/io/resource.h"
  33. #include "scene/resources/font.h"
  34. #include "scene/resources/style_box.h"
  35. #include "scene/resources/texture.h"
  36. class Theme : public Resource {
  37. GDCLASS(Theme, Resource);
  38. RES_BASE_EXTENSION("theme");
  39. #ifdef TOOLS_ENABLED
  40. friend class ThemeItemImportTree;
  41. friend class ThemeItemEditorDialog;
  42. friend class ThemeTypeEditor;
  43. #endif
  44. public:
  45. using ThemeIconMap = HashMap<StringName, Ref<Texture2D>>;
  46. using ThemeStyleMap = HashMap<StringName, Ref<StyleBox>>;
  47. using ThemeFontMap = HashMap<StringName, Ref<Font>>;
  48. using ThemeFontSizeMap = HashMap<StringName, int>;
  49. using ThemeColorMap = HashMap<StringName, Color>;
  50. using ThemeConstantMap = HashMap<StringName, int>;
  51. enum DataType {
  52. DATA_TYPE_COLOR,
  53. DATA_TYPE_CONSTANT,
  54. DATA_TYPE_FONT,
  55. DATA_TYPE_FONT_SIZE,
  56. DATA_TYPE_ICON,
  57. DATA_TYPE_STYLEBOX,
  58. DATA_TYPE_MAX
  59. };
  60. private:
  61. bool no_change_propagation = false;
  62. void _emit_theme_changed(bool p_notify_list_changed = false);
  63. Vector<String> _get_icon_list(const String &p_theme_type) const;
  64. Vector<String> _get_icon_type_list() const;
  65. Vector<String> _get_stylebox_list(const String &p_theme_type) const;
  66. Vector<String> _get_stylebox_type_list() const;
  67. Vector<String> _get_font_list(const String &p_theme_type) const;
  68. Vector<String> _get_font_type_list() const;
  69. Vector<String> _get_font_size_list(const String &p_theme_type) const;
  70. Vector<String> _get_font_size_type_list() const;
  71. Vector<String> _get_color_list(const String &p_theme_type) const;
  72. Vector<String> _get_color_type_list() const;
  73. Vector<String> _get_constant_list(const String &p_theme_type) const;
  74. Vector<String> _get_constant_type_list() const;
  75. Vector<String> _get_theme_item_list(DataType p_data_type, const String &p_theme_type) const;
  76. Vector<String> _get_theme_item_type_list(DataType p_data_type) const;
  77. Vector<String> _get_type_variation_list(const StringName &p_theme_type) const;
  78. Vector<String> _get_type_list() const;
  79. protected:
  80. bool _set(const StringName &p_name, const Variant &p_value);
  81. bool _get(const StringName &p_name, Variant &r_ret) const;
  82. void _get_property_list(List<PropertyInfo> *p_list) const;
  83. // Default values configurable for each individual theme.
  84. float default_base_scale = 0.0;
  85. Ref<Font> default_font;
  86. int default_font_size = -1;
  87. HashMap<StringName, ThemeIconMap> icon_map;
  88. HashMap<StringName, ThemeStyleMap> style_map;
  89. HashMap<StringName, ThemeFontMap> font_map;
  90. HashMap<StringName, ThemeFontSizeMap> font_size_map;
  91. HashMap<StringName, ThemeColorMap> color_map;
  92. HashMap<StringName, ThemeConstantMap> constant_map;
  93. HashMap<StringName, StringName> variation_map;
  94. HashMap<StringName, List<StringName>> variation_base_map;
  95. static void _bind_methods();
  96. void _freeze_change_propagation();
  97. void _unfreeze_and_propagate_changes();
  98. virtual void reset_state() override;
  99. public:
  100. static bool is_valid_type_name(const String &p_name);
  101. static bool is_valid_item_name(const String &p_name);
  102. void set_default_base_scale(float p_base_scale);
  103. float get_default_base_scale() const;
  104. bool has_default_base_scale() const;
  105. void set_default_font(const Ref<Font> &p_default_font);
  106. Ref<Font> get_default_font() const;
  107. bool has_default_font() const;
  108. void set_default_font_size(int p_font_size);
  109. int get_default_font_size() const;
  110. bool has_default_font_size() const;
  111. void set_icon(const StringName &p_name, const StringName &p_theme_type, const Ref<Texture2D> &p_icon);
  112. virtual Ref<Texture2D> get_icon(const StringName &p_name, const StringName &p_theme_type) const;
  113. bool has_icon(const StringName &p_name, const StringName &p_theme_type) const;
  114. bool has_icon_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  115. void rename_icon(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  116. void clear_icon(const StringName &p_name, const StringName &p_theme_type);
  117. void get_icon_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  118. void add_icon_type(const StringName &p_theme_type);
  119. void remove_icon_type(const StringName &p_theme_type);
  120. void get_icon_type_list(List<StringName> *p_list) const;
  121. void set_stylebox(const StringName &p_name, const StringName &p_theme_type, const Ref<StyleBox> &p_style);
  122. virtual Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_theme_type) const;
  123. bool has_stylebox(const StringName &p_name, const StringName &p_theme_type) const;
  124. bool has_stylebox_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  125. void rename_stylebox(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  126. void clear_stylebox(const StringName &p_name, const StringName &p_theme_type);
  127. void get_stylebox_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  128. void add_stylebox_type(const StringName &p_theme_type);
  129. void remove_stylebox_type(const StringName &p_theme_type);
  130. void get_stylebox_type_list(List<StringName> *p_list) const;
  131. void set_font(const StringName &p_name, const StringName &p_theme_type, const Ref<Font> &p_font);
  132. virtual Ref<Font> get_font(const StringName &p_name, const StringName &p_theme_type) const;
  133. bool has_font(const StringName &p_name, const StringName &p_theme_type) const;
  134. bool has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  135. void rename_font(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  136. void clear_font(const StringName &p_name, const StringName &p_theme_type);
  137. void get_font_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  138. void add_font_type(const StringName &p_theme_type);
  139. void remove_font_type(const StringName &p_theme_type);
  140. void get_font_type_list(List<StringName> *p_list) const;
  141. void set_font_size(const StringName &p_name, const StringName &p_theme_type, int p_font_size);
  142. virtual int get_font_size(const StringName &p_name, const StringName &p_theme_type) const;
  143. bool has_font_size(const StringName &p_name, const StringName &p_theme_type) const;
  144. bool has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  145. void rename_font_size(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  146. void clear_font_size(const StringName &p_name, const StringName &p_theme_type);
  147. void get_font_size_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  148. void add_font_size_type(const StringName &p_theme_type);
  149. void remove_font_size_type(const StringName &p_theme_type);
  150. void get_font_size_type_list(List<StringName> *p_list) const;
  151. void set_color(const StringName &p_name, const StringName &p_theme_type, const Color &p_color);
  152. virtual Color get_color(const StringName &p_name, const StringName &p_theme_type) const;
  153. bool has_color(const StringName &p_name, const StringName &p_theme_type) const;
  154. bool has_color_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  155. void rename_color(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  156. void clear_color(const StringName &p_name, const StringName &p_theme_type);
  157. void get_color_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  158. void add_color_type(const StringName &p_theme_type);
  159. void remove_color_type(const StringName &p_theme_type);
  160. void get_color_type_list(List<StringName> *p_list) const;
  161. void set_constant(const StringName &p_name, const StringName &p_theme_type, int p_constant);
  162. virtual int get_constant(const StringName &p_name, const StringName &p_theme_type) const;
  163. bool has_constant(const StringName &p_name, const StringName &p_theme_type) const;
  164. bool has_constant_nocheck(const StringName &p_name, const StringName &p_theme_type) const;
  165. void rename_constant(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  166. void clear_constant(const StringName &p_name, const StringName &p_theme_type);
  167. void get_constant_list(const StringName &p_theme_type, List<StringName> *p_list) const;
  168. void add_constant_type(const StringName &p_theme_type);
  169. void remove_constant_type(const StringName &p_theme_type);
  170. void get_constant_type_list(List<StringName> *p_list) const;
  171. void set_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type, const Variant &p_value);
  172. Variant get_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const;
  173. bool has_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const;
  174. bool has_theme_item_nocheck(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const;
  175. void rename_theme_item(DataType p_data_type, const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type);
  176. void clear_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type);
  177. void get_theme_item_list(DataType p_data_type, const StringName &p_theme_type, List<StringName> *p_list) const;
  178. void add_theme_item_type(DataType p_data_type, const StringName &p_theme_type);
  179. void remove_theme_item_type(DataType p_data_type, const StringName &p_theme_type);
  180. void get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const;
  181. void set_type_variation(const StringName &p_theme_type, const StringName &p_base_type);
  182. bool is_type_variation(const StringName &p_theme_type, const StringName &p_base_type) const;
  183. void clear_type_variation(const StringName &p_theme_type);
  184. StringName get_type_variation_base(const StringName &p_theme_type) const;
  185. void get_type_variation_list(const StringName &p_base_type, List<StringName> *p_list) const;
  186. void add_type(const StringName &p_theme_type);
  187. void remove_type(const StringName &p_theme_type);
  188. void get_type_list(List<StringName> *p_list) const;
  189. void get_type_dependencies(const StringName &p_base_type, const StringName &p_type_variant, Vector<StringName> &r_result);
  190. void merge_with(const Ref<Theme> &p_other);
  191. void clear();
  192. Theme();
  193. ~Theme();
  194. };
  195. VARIANT_ENUM_CAST(Theme::DataType);
  196. #endif // THEME_H