theme_db.h 8.3 KB

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