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