| 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 | |
| 31 | #ifndef THEME_H |
| 32 | #define THEME_H |
| 33 | |
| 34 | #include "core/io/resource.h" |
| 35 | #include "scene/resources/font.h" |
| 36 | #include "scene/resources/style_box.h" |
| 37 | #include "scene/resources/texture.h" |
| 38 | |
| 39 | class Theme : public Resource { |
| 40 | GDCLASS(Theme, Resource); |
| 41 | RES_BASE_EXTENSION("theme" ); |
| 42 | |
| 43 | #ifdef TOOLS_ENABLED |
| 44 | friend class ThemeItemImportTree; |
| 45 | friend class ThemeItemEditorDialog; |
| 46 | friend class ThemeTypeEditor; |
| 47 | #endif |
| 48 | |
| 49 | public: |
| 50 | using ThemeIconMap = HashMap<StringName, Ref<Texture2D>>; |
| 51 | using ThemeStyleMap = HashMap<StringName, Ref<StyleBox>>; |
| 52 | using ThemeFontMap = HashMap<StringName, Ref<Font>>; |
| 53 | using ThemeFontSizeMap = HashMap<StringName, int>; |
| 54 | using ThemeColorMap = HashMap<StringName, Color>; |
| 55 | using ThemeConstantMap = HashMap<StringName, int>; |
| 56 | |
| 57 | enum DataType { |
| 58 | DATA_TYPE_COLOR, |
| 59 | DATA_TYPE_CONSTANT, |
| 60 | DATA_TYPE_FONT, |
| 61 | DATA_TYPE_FONT_SIZE, |
| 62 | DATA_TYPE_ICON, |
| 63 | DATA_TYPE_STYLEBOX, |
| 64 | DATA_TYPE_MAX |
| 65 | }; |
| 66 | |
| 67 | private: |
| 68 | bool no_change_propagation = false; |
| 69 | |
| 70 | void _emit_theme_changed(bool p_notify_list_changed = false); |
| 71 | |
| 72 | Vector<String> _get_icon_list(const String &p_theme_type) const; |
| 73 | Vector<String> _get_icon_type_list() const; |
| 74 | Vector<String> _get_stylebox_list(const String &p_theme_type) const; |
| 75 | Vector<String> _get_stylebox_type_list() const; |
| 76 | Vector<String> _get_font_list(const String &p_theme_type) const; |
| 77 | Vector<String> _get_font_type_list() const; |
| 78 | Vector<String> _get_font_size_list(const String &p_theme_type) const; |
| 79 | Vector<String> _get_font_size_type_list() const; |
| 80 | Vector<String> _get_color_list(const String &p_theme_type) const; |
| 81 | Vector<String> _get_color_type_list() const; |
| 82 | Vector<String> _get_constant_list(const String &p_theme_type) const; |
| 83 | Vector<String> _get_constant_type_list() const; |
| 84 | |
| 85 | Vector<String> _get_theme_item_list(DataType p_data_type, const String &p_theme_type) const; |
| 86 | Vector<String> _get_theme_item_type_list(DataType p_data_type) const; |
| 87 | |
| 88 | Vector<String> _get_type_variation_list(const StringName &p_theme_type) const; |
| 89 | Vector<String> _get_type_list() const; |
| 90 | |
| 91 | protected: |
| 92 | bool _set(const StringName &p_name, const Variant &p_value); |
| 93 | bool _get(const StringName &p_name, Variant &r_ret) const; |
| 94 | void _get_property_list(List<PropertyInfo> *p_list) const; |
| 95 | |
| 96 | // Default values configurable for each individual theme. |
| 97 | float default_base_scale = 0.0; |
| 98 | Ref<Font> default_font; |
| 99 | int default_font_size = -1; |
| 100 | |
| 101 | HashMap<StringName, ThemeIconMap> icon_map; |
| 102 | HashMap<StringName, ThemeStyleMap> style_map; |
| 103 | HashMap<StringName, ThemeFontMap> font_map; |
| 104 | HashMap<StringName, ThemeFontSizeMap> font_size_map; |
| 105 | HashMap<StringName, ThemeColorMap> color_map; |
| 106 | HashMap<StringName, ThemeConstantMap> constant_map; |
| 107 | HashMap<StringName, StringName> variation_map; |
| 108 | HashMap<StringName, List<StringName>> variation_base_map; |
| 109 | |
| 110 | static void _bind_methods(); |
| 111 | |
| 112 | void _freeze_change_propagation(); |
| 113 | void _unfreeze_and_propagate_changes(); |
| 114 | |
| 115 | virtual void reset_state() override; |
| 116 | |
| 117 | public: |
| 118 | static bool is_valid_type_name(const String &p_name); |
| 119 | static bool is_valid_item_name(const String &p_name); |
| 120 | |
| 121 | void set_default_base_scale(float p_base_scale); |
| 122 | float get_default_base_scale() const; |
| 123 | bool has_default_base_scale() const; |
| 124 | |
| 125 | void set_default_font(const Ref<Font> &p_default_font); |
| 126 | Ref<Font> get_default_font() const; |
| 127 | bool has_default_font() const; |
| 128 | |
| 129 | void set_default_font_size(int p_font_size); |
| 130 | int get_default_font_size() const; |
| 131 | bool has_default_font_size() const; |
| 132 | |
| 133 | void set_icon(const StringName &p_name, const StringName &p_theme_type, const Ref<Texture2D> &p_icon); |
| 134 | virtual Ref<Texture2D> get_icon(const StringName &p_name, const StringName &p_theme_type) const; |
| 135 | bool has_icon(const StringName &p_name, const StringName &p_theme_type) const; |
| 136 | bool has_icon_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 137 | void rename_icon(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 138 | void clear_icon(const StringName &p_name, const StringName &p_theme_type); |
| 139 | void get_icon_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 140 | void add_icon_type(const StringName &p_theme_type); |
| 141 | void remove_icon_type(const StringName &p_theme_type); |
| 142 | void get_icon_type_list(List<StringName> *p_list) const; |
| 143 | |
| 144 | void set_stylebox(const StringName &p_name, const StringName &p_theme_type, const Ref<StyleBox> &p_style); |
| 145 | virtual Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_theme_type) const; |
| 146 | bool has_stylebox(const StringName &p_name, const StringName &p_theme_type) const; |
| 147 | bool has_stylebox_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 148 | void rename_stylebox(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 149 | void clear_stylebox(const StringName &p_name, const StringName &p_theme_type); |
| 150 | void get_stylebox_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 151 | void add_stylebox_type(const StringName &p_theme_type); |
| 152 | void remove_stylebox_type(const StringName &p_theme_type); |
| 153 | void get_stylebox_type_list(List<StringName> *p_list) const; |
| 154 | |
| 155 | void set_font(const StringName &p_name, const StringName &p_theme_type, const Ref<Font> &p_font); |
| 156 | virtual Ref<Font> get_font(const StringName &p_name, const StringName &p_theme_type) const; |
| 157 | bool has_font(const StringName &p_name, const StringName &p_theme_type) const; |
| 158 | bool has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 159 | void rename_font(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 160 | void clear_font(const StringName &p_name, const StringName &p_theme_type); |
| 161 | void get_font_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 162 | void add_font_type(const StringName &p_theme_type); |
| 163 | void remove_font_type(const StringName &p_theme_type); |
| 164 | void get_font_type_list(List<StringName> *p_list) const; |
| 165 | |
| 166 | void set_font_size(const StringName &p_name, const StringName &p_theme_type, int p_font_size); |
| 167 | virtual int get_font_size(const StringName &p_name, const StringName &p_theme_type) const; |
| 168 | bool has_font_size(const StringName &p_name, const StringName &p_theme_type) const; |
| 169 | bool has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 170 | void rename_font_size(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 171 | void clear_font_size(const StringName &p_name, const StringName &p_theme_type); |
| 172 | void get_font_size_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 173 | void add_font_size_type(const StringName &p_theme_type); |
| 174 | void remove_font_size_type(const StringName &p_theme_type); |
| 175 | void get_font_size_type_list(List<StringName> *p_list) const; |
| 176 | |
| 177 | void set_color(const StringName &p_name, const StringName &p_theme_type, const Color &p_color); |
| 178 | virtual Color get_color(const StringName &p_name, const StringName &p_theme_type) const; |
| 179 | bool has_color(const StringName &p_name, const StringName &p_theme_type) const; |
| 180 | bool has_color_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 181 | void rename_color(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 182 | void clear_color(const StringName &p_name, const StringName &p_theme_type); |
| 183 | void get_color_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 184 | void add_color_type(const StringName &p_theme_type); |
| 185 | void remove_color_type(const StringName &p_theme_type); |
| 186 | void get_color_type_list(List<StringName> *p_list) const; |
| 187 | |
| 188 | void set_constant(const StringName &p_name, const StringName &p_theme_type, int p_constant); |
| 189 | virtual int get_constant(const StringName &p_name, const StringName &p_theme_type) const; |
| 190 | bool has_constant(const StringName &p_name, const StringName &p_theme_type) const; |
| 191 | bool has_constant_nocheck(const StringName &p_name, const StringName &p_theme_type) const; |
| 192 | void rename_constant(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 193 | void clear_constant(const StringName &p_name, const StringName &p_theme_type); |
| 194 | void get_constant_list(StringName p_theme_type, List<StringName> *p_list) const; |
| 195 | void add_constant_type(const StringName &p_theme_type); |
| 196 | void remove_constant_type(const StringName &p_theme_type); |
| 197 | void get_constant_type_list(List<StringName> *p_list) const; |
| 198 | |
| 199 | void set_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type, const Variant &p_value); |
| 200 | Variant get_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const; |
| 201 | bool has_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const; |
| 202 | bool has_theme_item_nocheck(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const; |
| 203 | void rename_theme_item(DataType p_data_type, const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type); |
| 204 | void clear_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type); |
| 205 | void get_theme_item_list(DataType p_data_type, StringName p_theme_type, List<StringName> *p_list) const; |
| 206 | void add_theme_item_type(DataType p_data_type, const StringName &p_theme_type); |
| 207 | void remove_theme_item_type(DataType p_data_type, const StringName &p_theme_type); |
| 208 | void get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const; |
| 209 | |
| 210 | void set_type_variation(const StringName &p_theme_type, const StringName &p_base_type); |
| 211 | bool is_type_variation(const StringName &p_theme_type, const StringName &p_base_type) const; |
| 212 | void clear_type_variation(const StringName &p_theme_type); |
| 213 | StringName get_type_variation_base(const StringName &p_theme_type) const; |
| 214 | void get_type_variation_list(const StringName &p_base_type, List<StringName> *p_list) const; |
| 215 | |
| 216 | void add_type(const StringName &p_theme_type); |
| 217 | void remove_type(const StringName &p_theme_type); |
| 218 | void get_type_list(List<StringName> *p_list) const; |
| 219 | void get_type_dependencies(const StringName &p_base_type, const StringName &p_type_variant, List<StringName> *p_list); |
| 220 | |
| 221 | void merge_with(const Ref<Theme> &p_other); |
| 222 | void clear(); |
| 223 | |
| 224 | Theme(); |
| 225 | ~Theme(); |
| 226 | }; |
| 227 | |
| 228 | VARIANT_ENUM_CAST(Theme::DataType); |
| 229 | |
| 230 | #endif // THEME_H |
| 231 | |