theme_db.h 8.3 KB

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