theme_db.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, [](Node *p_instance) { \
  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, _scs_create(#m_prop)); \
  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, [](Node *p_instance) { \
  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, _scs_create(m_item_name)); \
  52. })
  53. // Macro for binding theme items used by this class, but defined/binded by other classes. This is primarily used for
  54. // the theme cache. Can also be used to list such items in documentation.
  55. #define BIND_THEME_ITEM_EXT(m_data_type, m_class, m_prop, m_item_name, m_type_name) \
  56. ThemeDB::get_singleton()->bind_class_external_item(m_data_type, get_class_static(), #m_prop, m_item_name, m_type_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), _scs_create(m_type_name)); \
  59. })
  60. class ThemeDB : public Object {
  61. GDCLASS(ThemeDB, Object);
  62. static ThemeDB *singleton;
  63. // Global Theme resources used by the default theme context.
  64. Ref<Theme> default_theme;
  65. Ref<Theme> project_theme;
  66. // Universal default values, final fallback for every theme.
  67. float fallback_base_scale = 1.0;
  68. Ref<Font> fallback_font;
  69. int fallback_font_size = 16;
  70. Ref<Texture2D> fallback_icon;
  71. Ref<StyleBox> fallback_stylebox;
  72. // Global theme contexts used to scope global Theme resources.
  73. ThemeContext *default_theme_context = nullptr;
  74. HashMap<Node *, ThemeContext *> theme_contexts;
  75. void _propagate_theme_context(Node *p_from_node, ThemeContext *p_context);
  76. void _init_default_theme_context();
  77. void _finalize_theme_contexts();
  78. // Binding of theme items to Node classes.
  79. public:
  80. typedef std::function<void(Node *)> ThemeItemSetter;
  81. struct ThemeItemBind {
  82. Theme::DataType data_type;
  83. StringName class_name;
  84. StringName item_name;
  85. StringName type_name;
  86. bool external = false;
  87. ThemeItemSetter setter;
  88. };
  89. private:
  90. HashMap<StringName, HashMap<StringName, ThemeItemBind>> theme_item_binds;
  91. protected:
  92. static void _bind_methods();
  93. public:
  94. void initialize_theme();
  95. void initialize_theme_noproject();
  96. void finalize_theme();
  97. // Global Theme resources.
  98. void set_default_theme(const Ref<Theme> &p_default);
  99. Ref<Theme> get_default_theme();
  100. void set_project_theme(const Ref<Theme> &p_project_default);
  101. Ref<Theme> get_project_theme();
  102. // Universal fallback values.
  103. void set_fallback_base_scale(float p_base_scale);
  104. float get_fallback_base_scale();
  105. void set_fallback_font(const Ref<Font> &p_font);
  106. Ref<Font> get_fallback_font();
  107. void set_fallback_font_size(int p_font_size);
  108. int get_fallback_font_size();
  109. void set_fallback_icon(const Ref<Texture2D> &p_icon);
  110. Ref<Texture2D> get_fallback_icon();
  111. void set_fallback_stylebox(const Ref<StyleBox> &p_stylebox);
  112. Ref<StyleBox> get_fallback_stylebox();
  113. void get_native_type_dependencies(const StringName &p_base_type, List<StringName> *p_list);
  114. // Global theme contexts.
  115. ThemeContext *create_theme_context(Node *p_node, List<Ref<Theme>> &p_themes);
  116. void destroy_theme_context(Node *p_node);
  117. ThemeContext *get_theme_context(Node *p_node) const;
  118. ThemeContext *get_default_theme_context() const;
  119. ThemeContext *get_nearest_theme_context(Node *p_for_node) const;
  120. // Theme item binding.
  121. 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);
  122. 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);
  123. void update_class_instance_items(Node *p_instance);
  124. void get_class_own_items(const StringName &p_class_name, List<ThemeItemBind> *r_list);
  125. // Memory management, reference, and initialization.
  126. static ThemeDB *get_singleton();
  127. ThemeDB();
  128. ~ThemeDB();
  129. };
  130. class ThemeContext : public Object {
  131. GDCLASS(ThemeContext, Object);
  132. friend class ThemeDB;
  133. Node *node = nullptr;
  134. ThemeContext *parent = nullptr;
  135. // Themes are stacked in the order of relevance, for easy iteration.
  136. // This means that the first theme is the one you should check first,
  137. // and the last theme is the fallback theme where every lookup ends.
  138. List<Ref<Theme>> themes;
  139. void _emit_changed();
  140. protected:
  141. static void _bind_methods();
  142. public:
  143. void set_themes(List<Ref<Theme>> &p_themes);
  144. List<Ref<Theme>> get_themes() const;
  145. Ref<Theme> get_fallback_theme() const;
  146. };
  147. #endif // THEME_DB_H