| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 62 |
|
| 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 |
|
| 77 |
|
| 78 |
Ref<Theme> default_theme; |
| 79 |
Ref<Theme> project_theme; |
| 80 |
|
| 81 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 193 |
|
| 194 |
|
| 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 |
}; |