theme_db.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**************************************************************************/
  2. /* theme_db.cpp */
  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. #include "theme_db.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/resource_loader.h"
  33. #include "scene/resources/default_theme/default_theme.h"
  34. #include "scene/resources/font.h"
  35. #include "scene/resources/style_box.h"
  36. #include "scene/resources/texture.h"
  37. #include "scene/resources/theme.h"
  38. #include "servers/text_server.h"
  39. // Default engine theme creation and configuration.
  40. void ThemeDB::initialize_theme() {
  41. // Allow creating the default theme at a different scale to suit higher/lower base resolutions.
  42. float default_theme_scale = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/theme/default_theme_scale", PROPERTY_HINT_RANGE, "0.5,8,0.01", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), 1.0);
  43. String theme_path = GLOBAL_DEF_RST(PropertyInfo(Variant::STRING, "gui/theme/custom", PROPERTY_HINT_FILE, "*.tres,*.res,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), "");
  44. String font_path = GLOBAL_DEF_RST(PropertyInfo(Variant::STRING, "gui/theme/custom_font", PROPERTY_HINT_FILE, "*.tres,*.res,*.otf,*.ttf,*.woff,*.woff2,*.fnt,*.font,*.pfb,*.pfm", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), "");
  45. TextServer::FontAntialiasing font_antialiasing = (TextServer::FontAntialiasing)(int)GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), 1);
  46. TextServer::Hinting font_hinting = (TextServer::Hinting)(int)GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), TextServer::HINTING_LIGHT);
  47. TextServer::SubpixelPositioning font_subpixel_positioning = (TextServer::SubpixelPositioning)(int)GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), TextServer::SUBPIXEL_POSITIONING_AUTO);
  48. const bool font_msdf = GLOBAL_DEF_RST("gui/theme/default_font_multichannel_signed_distance_field", false);
  49. const bool font_generate_mipmaps = GLOBAL_DEF_RST("gui/theme/default_font_generate_mipmaps", false);
  50. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/lcd_subpixel_layout", PROPERTY_HINT_ENUM, "Disabled,Horizontal RGB,Horizontal BGR,Vertical RGB,Vertical BGR"), 1);
  51. ProjectSettings::get_singleton()->set_restart_if_changed("gui/theme/lcd_subpixel_layout", false);
  52. Ref<Font> font;
  53. if (!font_path.is_empty()) {
  54. font = ResourceLoader::load(font_path);
  55. if (font.is_valid()) {
  56. set_fallback_font(font);
  57. } else {
  58. ERR_PRINT("Error loading custom font '" + font_path + "'");
  59. }
  60. }
  61. // Always make the default theme to avoid invalid default font/icon/style in the given theme.
  62. if (RenderingServer::get_singleton()) {
  63. make_default_theme(default_theme_scale, font, font_subpixel_positioning, font_hinting, font_antialiasing, font_msdf, font_generate_mipmaps);
  64. }
  65. if (!theme_path.is_empty()) {
  66. Ref<Theme> theme = ResourceLoader::load(theme_path);
  67. if (theme.is_valid()) {
  68. set_project_theme(theme);
  69. } else {
  70. ERR_PRINT("Error loading custom theme '" + theme_path + "'");
  71. }
  72. }
  73. }
  74. void ThemeDB::initialize_theme_noproject() {
  75. if (RenderingServer::get_singleton()) {
  76. make_default_theme(1.0, Ref<Font>());
  77. }
  78. }
  79. // Universal fallback Theme resources.
  80. void ThemeDB::set_default_theme(const Ref<Theme> &p_default) {
  81. default_theme = p_default;
  82. }
  83. Ref<Theme> ThemeDB::get_default_theme() {
  84. return default_theme;
  85. }
  86. void ThemeDB::set_project_theme(const Ref<Theme> &p_project_default) {
  87. project_theme = p_project_default;
  88. }
  89. Ref<Theme> ThemeDB::get_project_theme() {
  90. return project_theme;
  91. }
  92. // Universal fallback values for theme item types.
  93. void ThemeDB::set_fallback_base_scale(float p_base_scale) {
  94. if (fallback_base_scale == p_base_scale) {
  95. return;
  96. }
  97. fallback_base_scale = p_base_scale;
  98. emit_signal(SNAME("fallback_changed"));
  99. }
  100. float ThemeDB::get_fallback_base_scale() {
  101. return fallback_base_scale;
  102. }
  103. void ThemeDB::set_fallback_font(const Ref<Font> &p_font) {
  104. if (fallback_font == p_font) {
  105. return;
  106. }
  107. fallback_font = p_font;
  108. emit_signal(SNAME("fallback_changed"));
  109. }
  110. Ref<Font> ThemeDB::get_fallback_font() {
  111. return fallback_font;
  112. }
  113. void ThemeDB::set_fallback_font_size(int p_font_size) {
  114. if (fallback_font_size == p_font_size) {
  115. return;
  116. }
  117. fallback_font_size = p_font_size;
  118. emit_signal(SNAME("fallback_changed"));
  119. }
  120. int ThemeDB::get_fallback_font_size() {
  121. return fallback_font_size;
  122. }
  123. void ThemeDB::set_fallback_icon(const Ref<Texture2D> &p_icon) {
  124. if (fallback_icon == p_icon) {
  125. return;
  126. }
  127. fallback_icon = p_icon;
  128. emit_signal(SNAME("fallback_changed"));
  129. }
  130. Ref<Texture2D> ThemeDB::get_fallback_icon() {
  131. return fallback_icon;
  132. }
  133. void ThemeDB::set_fallback_stylebox(const Ref<StyleBox> &p_stylebox) {
  134. if (fallback_stylebox == p_stylebox) {
  135. return;
  136. }
  137. fallback_stylebox = p_stylebox;
  138. emit_signal(SNAME("fallback_changed"));
  139. }
  140. Ref<StyleBox> ThemeDB::get_fallback_stylebox() {
  141. return fallback_stylebox;
  142. }
  143. // Object methods.
  144. void ThemeDB::_bind_methods() {
  145. ClassDB::bind_method(D_METHOD("get_default_theme"), &ThemeDB::get_default_theme);
  146. ClassDB::bind_method(D_METHOD("get_project_theme"), &ThemeDB::get_project_theme);
  147. ClassDB::bind_method(D_METHOD("set_fallback_base_scale", "base_scale"), &ThemeDB::set_fallback_base_scale);
  148. ClassDB::bind_method(D_METHOD("get_fallback_base_scale"), &ThemeDB::get_fallback_base_scale);
  149. ClassDB::bind_method(D_METHOD("set_fallback_font", "font"), &ThemeDB::set_fallback_font);
  150. ClassDB::bind_method(D_METHOD("get_fallback_font"), &ThemeDB::get_fallback_font);
  151. ClassDB::bind_method(D_METHOD("set_fallback_font_size", "font_size"), &ThemeDB::set_fallback_font_size);
  152. ClassDB::bind_method(D_METHOD("get_fallback_font_size"), &ThemeDB::get_fallback_font_size);
  153. ClassDB::bind_method(D_METHOD("set_fallback_icon", "icon"), &ThemeDB::set_fallback_icon);
  154. ClassDB::bind_method(D_METHOD("get_fallback_icon"), &ThemeDB::get_fallback_icon);
  155. ClassDB::bind_method(D_METHOD("set_fallback_stylebox", "stylebox"), &ThemeDB::set_fallback_stylebox);
  156. ClassDB::bind_method(D_METHOD("get_fallback_stylebox"), &ThemeDB::get_fallback_stylebox);
  157. ADD_GROUP("Fallback values", "fallback_");
  158. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fallback_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_fallback_base_scale", "get_fallback_base_scale");
  159. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_font", PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_NONE), "set_fallback_font", "get_fallback_font");
  160. ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"), "set_fallback_font_size", "get_fallback_font_size");
  161. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NONE), "set_fallback_icon", "get_fallback_icon");
  162. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_stylebox", PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_NONE), "set_fallback_stylebox", "get_fallback_stylebox");
  163. ADD_SIGNAL(MethodInfo("fallback_changed"));
  164. }
  165. // Memory management, reference, and initialization
  166. ThemeDB *ThemeDB::singleton = nullptr;
  167. ThemeDB *ThemeDB::get_singleton() {
  168. return singleton;
  169. }
  170. ThemeDB::ThemeDB() {
  171. singleton = this;
  172. // Universal default values, final fallback for every theme.
  173. fallback_base_scale = 1.0;
  174. fallback_font_size = 16;
  175. }
  176. ThemeDB::~ThemeDB() {
  177. default_theme.unref();
  178. project_theme.unref();
  179. fallback_font.unref();
  180. fallback_icon.unref();
  181. fallback_stylebox.unref();
  182. singleton = nullptr;
  183. }