1 | /**************************************************************************/ |
2 | /* theme_db.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_DB_H |
32 | #define THEME_DB_H |
33 | |
34 | #include "core/object/class_db.h" |
35 | #include "core/object/ref_counted.h" |
36 | |
37 | #include <functional> |
38 | |
39 | class Font; |
40 | class Node; |
41 | class StyleBox; |
42 | class Texture2D; |
43 | class Theme; |
44 | class ThemeContext; |
45 | |
46 | // Macros for binding theme items of this class. This information is used for the documentation, theme |
47 | // overrides, etc. This is also the basis for theme cache. |
48 | |
49 | #define BIND_THEME_ITEM(m_data_type, m_class, m_prop) \ |
50 | ThemeDB::get_singleton()->bind_class_item(get_class_static(), #m_prop, #m_prop, [](Node *p_instance) { \ |
51 | m_class *p_cast = Object::cast_to<m_class>(p_instance); \ |
52 | p_cast->theme_cache.m_prop = p_cast->get_theme_item(m_data_type, _scs_create(#m_prop)); \ |
53 | }) |
54 | |
55 | #define BIND_THEME_ITEM_CUSTOM(m_data_type, m_class, m_prop, m_item_name) \ |
56 | ThemeDB::get_singleton()->bind_class_item(get_class_static(), #m_prop, m_item_name, [](Node *p_instance) { \ |
57 | m_class *p_cast = Object::cast_to<m_class>(p_instance); \ |
58 | p_cast->theme_cache.m_prop = p_cast->get_theme_item(m_data_type, _scs_create(m_item_name)); \ |
59 | }) |
60 | |
61 | // Macro for binding theme items used by this class, but defined/binded by other classes. This is primarily used for |
62 | // the theme cache. Can also be used to list such items in documentation. |
63 | |
64 | #define BIND_THEME_ITEM_EXT(m_data_type, m_class, m_prop, m_item_name, m_type_name) \ |
65 | ThemeDB::get_singleton()->bind_class_external_item(get_class_static(), #m_prop, m_item_name, m_type_name, [](Node *p_instance) { \ |
66 | m_class *p_cast = Object::cast_to<m_class>(p_instance); \ |
67 | p_cast->theme_cache.m_prop = p_cast->get_theme_item(m_data_type, _scs_create(m_item_name), _scs_create(m_type_name)); \ |
68 | }) |
69 | |
70 | class ThemeDB : public Object { |
71 | GDCLASS(ThemeDB, Object); |
72 | |
73 | static ThemeDB *singleton; |
74 | |
75 | // Global Theme resources used by the default theme context. |
76 | |
77 | Ref<Theme> default_theme; |
78 | Ref<Theme> project_theme; |
79 | |
80 | // Universal default values, final fallback for every theme. |
81 | |
82 | float fallback_base_scale = 1.0; |
83 | Ref<Font> fallback_font; |
84 | int fallback_font_size = 16; |
85 | Ref<Texture2D> fallback_icon; |
86 | Ref<StyleBox> fallback_stylebox; |
87 | |
88 | // Global theme contexts used to scope global Theme resources. |
89 | |
90 | ThemeContext *default_theme_context = nullptr; |
91 | HashMap<Node *, ThemeContext *> theme_contexts; |
92 | |
93 | void _propagate_theme_context(Node *p_from_node, ThemeContext *p_context); |
94 | void _init_default_theme_context(); |
95 | void _finalize_theme_contexts(); |
96 | |
97 | // Binding of theme items to Node classes. |
98 | |
99 | typedef std::function<void(Node *)> ThemeItemSetter; |
100 | |
101 | struct ThemeItemBind { |
102 | StringName class_name; |
103 | StringName item_name; |
104 | StringName type_name; |
105 | bool external = false; |
106 | |
107 | ThemeItemSetter setter; |
108 | }; |
109 | |
110 | HashMap<StringName, HashMap<StringName, ThemeItemBind>> theme_item_binds; |
111 | |
112 | protected: |
113 | static void _bind_methods(); |
114 | |
115 | public: |
116 | void initialize_theme(); |
117 | void initialize_theme_noproject(); |
118 | void finalize_theme(); |
119 | |
120 | // Global Theme resources. |
121 | |
122 | void set_default_theme(const Ref<Theme> &p_default); |
123 | Ref<Theme> get_default_theme(); |
124 | |
125 | void set_project_theme(const Ref<Theme> &p_project_default); |
126 | Ref<Theme> get_project_theme(); |
127 | |
128 | // Universal fallback values. |
129 | |
130 | void set_fallback_base_scale(float p_base_scale); |
131 | float get_fallback_base_scale(); |
132 | |
133 | void set_fallback_font(const Ref<Font> &p_font); |
134 | Ref<Font> get_fallback_font(); |
135 | |
136 | void set_fallback_font_size(int p_font_size); |
137 | int get_fallback_font_size(); |
138 | |
139 | void set_fallback_icon(const Ref<Texture2D> &p_icon); |
140 | Ref<Texture2D> get_fallback_icon(); |
141 | |
142 | void set_fallback_stylebox(const Ref<StyleBox> &p_stylebox); |
143 | Ref<StyleBox> get_fallback_stylebox(); |
144 | |
145 | void get_native_type_dependencies(const StringName &p_base_type, List<StringName> *p_list); |
146 | |
147 | // Global theme contexts. |
148 | |
149 | ThemeContext *create_theme_context(Node *p_node, List<Ref<Theme>> &p_themes); |
150 | void destroy_theme_context(Node *p_node); |
151 | |
152 | ThemeContext *get_theme_context(Node *p_node) const; |
153 | ThemeContext *get_default_theme_context() const; |
154 | ThemeContext *get_nearest_theme_context(Node *p_for_node) const; |
155 | |
156 | // Theme item binding. |
157 | |
158 | void bind_class_item(const StringName &p_class_name, const StringName &p_prop_name, const StringName &p_item_name, ThemeItemSetter p_setter); |
159 | void bind_class_external_item(const StringName &p_class_name, const StringName &p_prop_name, const StringName &p_item_name, const StringName &p_type_name, ThemeItemSetter p_setter); |
160 | void update_class_instance_items(Node *p_instance); |
161 | |
162 | // Memory management, reference, and initialization. |
163 | |
164 | static ThemeDB *get_singleton(); |
165 | ThemeDB(); |
166 | ~ThemeDB(); |
167 | }; |
168 | |
169 | class ThemeContext : public Object { |
170 | GDCLASS(ThemeContext, Object); |
171 | |
172 | friend class ThemeDB; |
173 | |
174 | Node *node = nullptr; |
175 | ThemeContext *parent = nullptr; |
176 | |
177 | // Themes are stacked in the order of relevance, for easy iteration. |
178 | // This means that the first theme is the one you should check first, |
179 | // and the last theme is the fallback theme where every lookup ends. |
180 | List<Ref<Theme>> themes; |
181 | |
182 | void _emit_changed(); |
183 | |
184 | protected: |
185 | static void _bind_methods(); |
186 | |
187 | public: |
188 | void set_themes(List<Ref<Theme>> &p_themes); |
189 | List<Ref<Theme>> get_themes() const; |
190 | Ref<Theme> get_fallback_theme() const; |
191 | }; |
192 | |
193 | #endif // THEME_DB_H |
194 | |