theme_db.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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/gui/control.h"
  34. #include "scene/main/node.h"
  35. #include "scene/main/window.h"
  36. #include "scene/resources/font.h"
  37. #include "scene/resources/style_box.h"
  38. #include "scene/resources/texture.h"
  39. #include "scene/theme/default_theme.h"
  40. #include "servers/text_server.h"
  41. // Default engine theme creation and configuration.
  42. void ThemeDB::initialize_theme() {
  43. // Default theme-related project settings.
  44. // Allow creating the default theme at a different scale to suit higher/lower base resolutions.
  45. 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);
  46. String project_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), "");
  47. String project_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), "");
  48. 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);
  49. 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);
  50. 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);
  51. const bool font_msdf = GLOBAL_DEF_RST("gui/theme/default_font_multichannel_signed_distance_field", false);
  52. const bool font_generate_mipmaps = GLOBAL_DEF_RST("gui/theme/default_font_generate_mipmaps", false);
  53. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/lcd_subpixel_layout", PROPERTY_HINT_ENUM, "Disabled,Horizontal RGB,Horizontal BGR,Vertical RGB,Vertical BGR"), 1);
  54. ProjectSettings::get_singleton()->set_restart_if_changed("gui/theme/lcd_subpixel_layout", false);
  55. // Attempt to load custom project theme and font.
  56. if (!project_theme_path.is_empty()) {
  57. Ref<Theme> theme = ResourceLoader::load(project_theme_path);
  58. if (theme.is_valid()) {
  59. set_project_theme(theme);
  60. } else {
  61. ERR_PRINT("Error loading custom project theme '" + project_theme_path + "'");
  62. }
  63. }
  64. Ref<Font> project_font;
  65. if (!project_font_path.is_empty()) {
  66. project_font = ResourceLoader::load(project_font_path);
  67. if (project_font.is_valid()) {
  68. set_fallback_font(project_font);
  69. } else {
  70. ERR_PRINT("Error loading custom project font '" + project_font_path + "'");
  71. }
  72. }
  73. // Always generate the default theme to serve as a fallback for all required theme definitions.
  74. if (RenderingServer::get_singleton()) {
  75. make_default_theme(default_theme_scale, project_font, font_subpixel_positioning, font_hinting, font_antialiasing, font_msdf, font_generate_mipmaps);
  76. }
  77. _init_default_theme_context();
  78. }
  79. void ThemeDB::initialize_theme_noproject() {
  80. if (RenderingServer::get_singleton()) {
  81. make_default_theme(1.0, Ref<Font>());
  82. }
  83. _init_default_theme_context();
  84. }
  85. void ThemeDB::finalize_theme() {
  86. if (!RenderingServer::get_singleton()) {
  87. WARN_PRINT("Finalizing theme when there is no RenderingServer is an error; check the order of operations.");
  88. }
  89. _finalize_theme_contexts();
  90. default_theme.unref();
  91. fallback_font.unref();
  92. fallback_icon.unref();
  93. fallback_stylebox.unref();
  94. }
  95. // Global Theme resources.
  96. void ThemeDB::set_default_theme(const Ref<Theme> &p_default) {
  97. default_theme = p_default;
  98. }
  99. Ref<Theme> ThemeDB::get_default_theme() {
  100. return default_theme;
  101. }
  102. void ThemeDB::set_project_theme(const Ref<Theme> &p_project_default) {
  103. project_theme = p_project_default;
  104. }
  105. Ref<Theme> ThemeDB::get_project_theme() {
  106. return project_theme;
  107. }
  108. // Universal fallback values for theme item types.
  109. void ThemeDB::set_fallback_base_scale(float p_base_scale) {
  110. if (fallback_base_scale == p_base_scale) {
  111. return;
  112. }
  113. fallback_base_scale = p_base_scale;
  114. emit_signal(SNAME("fallback_changed"));
  115. }
  116. float ThemeDB::get_fallback_base_scale() {
  117. return fallback_base_scale;
  118. }
  119. void ThemeDB::set_fallback_font(const Ref<Font> &p_font) {
  120. if (fallback_font == p_font) {
  121. return;
  122. }
  123. fallback_font = p_font;
  124. emit_signal(SNAME("fallback_changed"));
  125. }
  126. Ref<Font> ThemeDB::get_fallback_font() {
  127. return fallback_font;
  128. }
  129. void ThemeDB::set_fallback_font_size(int p_font_size) {
  130. if (fallback_font_size == p_font_size) {
  131. return;
  132. }
  133. fallback_font_size = p_font_size;
  134. emit_signal(SNAME("fallback_changed"));
  135. }
  136. int ThemeDB::get_fallback_font_size() {
  137. return fallback_font_size;
  138. }
  139. void ThemeDB::set_fallback_icon(const Ref<Texture2D> &p_icon) {
  140. if (fallback_icon == p_icon) {
  141. return;
  142. }
  143. fallback_icon = p_icon;
  144. emit_signal(SNAME("fallback_changed"));
  145. }
  146. Ref<Texture2D> ThemeDB::get_fallback_icon() {
  147. return fallback_icon;
  148. }
  149. void ThemeDB::set_fallback_stylebox(const Ref<StyleBox> &p_stylebox) {
  150. if (fallback_stylebox == p_stylebox) {
  151. return;
  152. }
  153. fallback_stylebox = p_stylebox;
  154. emit_signal(SNAME("fallback_changed"));
  155. }
  156. Ref<StyleBox> ThemeDB::get_fallback_stylebox() {
  157. return fallback_stylebox;
  158. }
  159. void ThemeDB::get_native_type_dependencies(const StringName &p_base_type, List<StringName> *p_list) {
  160. ERR_FAIL_NULL(p_list);
  161. // TODO: It may make sense to stop at Control/Window, because their parent classes cannot be used in
  162. // a meaningful way.
  163. StringName class_name = p_base_type;
  164. while (class_name != StringName()) {
  165. p_list->push_back(class_name);
  166. class_name = ClassDB::get_parent_class_nocheck(class_name);
  167. }
  168. }
  169. // Global theme contexts.
  170. ThemeContext *ThemeDB::create_theme_context(Node *p_node, List<Ref<Theme>> &p_themes) {
  171. ERR_FAIL_COND_V(!p_node->is_inside_tree(), nullptr);
  172. ERR_FAIL_COND_V(theme_contexts.has(p_node), nullptr);
  173. ERR_FAIL_COND_V(p_themes.is_empty(), nullptr);
  174. ThemeContext *context = memnew(ThemeContext);
  175. context->node = p_node;
  176. context->parent = get_nearest_theme_context(p_node);
  177. context->set_themes(p_themes);
  178. theme_contexts[p_node] = context;
  179. _propagate_theme_context(p_node, context);
  180. p_node->connect("tree_exited", callable_mp(this, &ThemeDB::destroy_theme_context).bind(p_node));
  181. return context;
  182. }
  183. void ThemeDB::destroy_theme_context(Node *p_node) {
  184. ERR_FAIL_COND(!theme_contexts.has(p_node));
  185. p_node->disconnect("tree_exited", callable_mp(this, &ThemeDB::destroy_theme_context));
  186. ThemeContext *context = theme_contexts[p_node];
  187. theme_contexts.erase(p_node);
  188. _propagate_theme_context(p_node, context->parent);
  189. memdelete(context);
  190. }
  191. void ThemeDB::_propagate_theme_context(Node *p_from_node, ThemeContext *p_context) {
  192. Control *from_control = Object::cast_to<Control>(p_from_node);
  193. Window *from_window = from_control ? nullptr : Object::cast_to<Window>(p_from_node);
  194. if (from_control) {
  195. from_control->set_theme_context(p_context);
  196. } else if (from_window) {
  197. from_window->set_theme_context(p_context);
  198. }
  199. for (int i = 0; i < p_from_node->get_child_count(); i++) {
  200. Node *child_node = p_from_node->get_child(i);
  201. // If the child is the root of another global context, stop the propagation
  202. // in this branch.
  203. if (theme_contexts.has(child_node)) {
  204. theme_contexts[child_node]->parent = p_context;
  205. continue;
  206. }
  207. _propagate_theme_context(child_node, p_context);
  208. }
  209. }
  210. void ThemeDB::_init_default_theme_context() {
  211. default_theme_context = memnew(ThemeContext);
  212. List<Ref<Theme>> themes;
  213. // Only add the project theme to the default context when running projects.
  214. #ifdef TOOLS_ENABLED
  215. if (!Engine::get_singleton()->is_editor_hint()) {
  216. themes.push_back(project_theme);
  217. }
  218. #else
  219. themes.push_back(project_theme);
  220. #endif
  221. themes.push_back(default_theme);
  222. default_theme_context->set_themes(themes);
  223. }
  224. void ThemeDB::_finalize_theme_contexts() {
  225. if (default_theme_context) {
  226. memdelete(default_theme_context);
  227. default_theme_context = nullptr;
  228. }
  229. while (theme_contexts.size()) {
  230. HashMap<Node *, ThemeContext *>::Iterator E = theme_contexts.begin();
  231. memdelete(E->value);
  232. theme_contexts.remove(E);
  233. }
  234. }
  235. ThemeContext *ThemeDB::get_theme_context(Node *p_node) const {
  236. if (!theme_contexts.has(p_node)) {
  237. return nullptr;
  238. }
  239. return theme_contexts[p_node];
  240. }
  241. ThemeContext *ThemeDB::get_default_theme_context() const {
  242. return default_theme_context;
  243. }
  244. ThemeContext *ThemeDB::get_nearest_theme_context(Node *p_for_node) const {
  245. ERR_FAIL_COND_V(!p_for_node->is_inside_tree(), nullptr);
  246. Node *parent_node = p_for_node->get_parent();
  247. while (parent_node) {
  248. if (theme_contexts.has(parent_node)) {
  249. return theme_contexts[parent_node];
  250. }
  251. parent_node = parent_node->get_parent();
  252. }
  253. return nullptr;
  254. }
  255. // Theme item binding.
  256. void ThemeDB::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) {
  257. ERR_FAIL_COND_MSG(theme_item_binds[p_class_name].has(p_prop_name), vformat("Failed to bind theme item '%s' in class '%s': already bound", p_prop_name, p_class_name));
  258. ThemeItemBind bind;
  259. bind.data_type = p_data_type;
  260. bind.class_name = p_class_name;
  261. bind.item_name = p_item_name;
  262. bind.setter = p_setter;
  263. theme_item_binds[p_class_name][p_prop_name] = bind;
  264. }
  265. void ThemeDB::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) {
  266. ERR_FAIL_COND_MSG(theme_item_binds[p_class_name].has(p_prop_name), vformat("Failed to bind theme item '%s' in class '%s': already bound", p_prop_name, p_class_name));
  267. ThemeItemBind bind;
  268. bind.data_type = p_data_type;
  269. bind.class_name = p_class_name;
  270. bind.item_name = p_item_name;
  271. bind.type_name = p_type_name;
  272. bind.external = true;
  273. bind.setter = p_setter;
  274. theme_item_binds[p_class_name][p_prop_name] = bind;
  275. }
  276. void ThemeDB::update_class_instance_items(Node *p_instance) {
  277. ERR_FAIL_NULL(p_instance);
  278. // Use the hierarchy to initialize all inherited theme caches. Setters carry the necessary
  279. // context and will set the values appropriately.
  280. StringName class_name = p_instance->get_class();
  281. while (class_name != StringName()) {
  282. HashMap<StringName, HashMap<StringName, ThemeItemBind>>::Iterator E = theme_item_binds.find(class_name);
  283. if (E) {
  284. for (const KeyValue<StringName, ThemeItemBind> &F : E->value) {
  285. F.value.setter(p_instance);
  286. }
  287. }
  288. class_name = ClassDB::get_parent_class_nocheck(class_name);
  289. }
  290. }
  291. void ThemeDB::get_class_own_items(const StringName &p_class_name, List<ThemeItemBind> *r_list) {
  292. List<StringName> class_hierarchy;
  293. StringName class_name = p_class_name;
  294. while (class_name != StringName()) {
  295. class_hierarchy.push_front(class_name); // Put parent classes in front.
  296. class_name = ClassDB::get_parent_class_nocheck(class_name);
  297. }
  298. HashSet<StringName> inherited_props;
  299. for (const StringName &theme_type : class_hierarchy) {
  300. HashMap<StringName, HashMap<StringName, ThemeItemBind>>::Iterator E = theme_item_binds.find(theme_type);
  301. if (E) {
  302. for (const KeyValue<StringName, ThemeItemBind> &F : E->value) {
  303. if (inherited_props.has(F.value.item_name)) {
  304. continue; // Skip inherited properties.
  305. }
  306. if (F.value.external || F.value.class_name != p_class_name) {
  307. inherited_props.insert(F.value.item_name);
  308. continue; // Track properties defined in parent classes, and skip them.
  309. }
  310. r_list->push_back(F.value);
  311. }
  312. }
  313. }
  314. }
  315. // Object methods.
  316. void ThemeDB::_bind_methods() {
  317. ClassDB::bind_method(D_METHOD("get_default_theme"), &ThemeDB::get_default_theme);
  318. ClassDB::bind_method(D_METHOD("get_project_theme"), &ThemeDB::get_project_theme);
  319. ClassDB::bind_method(D_METHOD("set_fallback_base_scale", "base_scale"), &ThemeDB::set_fallback_base_scale);
  320. ClassDB::bind_method(D_METHOD("get_fallback_base_scale"), &ThemeDB::get_fallback_base_scale);
  321. ClassDB::bind_method(D_METHOD("set_fallback_font", "font"), &ThemeDB::set_fallback_font);
  322. ClassDB::bind_method(D_METHOD("get_fallback_font"), &ThemeDB::get_fallback_font);
  323. ClassDB::bind_method(D_METHOD("set_fallback_font_size", "font_size"), &ThemeDB::set_fallback_font_size);
  324. ClassDB::bind_method(D_METHOD("get_fallback_font_size"), &ThemeDB::get_fallback_font_size);
  325. ClassDB::bind_method(D_METHOD("set_fallback_icon", "icon"), &ThemeDB::set_fallback_icon);
  326. ClassDB::bind_method(D_METHOD("get_fallback_icon"), &ThemeDB::get_fallback_icon);
  327. ClassDB::bind_method(D_METHOD("set_fallback_stylebox", "stylebox"), &ThemeDB::set_fallback_stylebox);
  328. ClassDB::bind_method(D_METHOD("get_fallback_stylebox"), &ThemeDB::get_fallback_stylebox);
  329. ADD_GROUP("Fallback values", "fallback_");
  330. 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");
  331. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_font", PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_NONE), "set_fallback_font", "get_fallback_font");
  332. 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");
  333. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NONE), "set_fallback_icon", "get_fallback_icon");
  334. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_stylebox", PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_NONE), "set_fallback_stylebox", "get_fallback_stylebox");
  335. ADD_SIGNAL(MethodInfo("fallback_changed"));
  336. }
  337. // Memory management, reference, and initialization.
  338. ThemeDB *ThemeDB::singleton = nullptr;
  339. ThemeDB *ThemeDB::get_singleton() {
  340. return singleton;
  341. }
  342. ThemeDB::ThemeDB() {
  343. singleton = this;
  344. }
  345. ThemeDB::~ThemeDB() {
  346. // For technical reasons unit tests recreate and destroy the default
  347. // theme over and over again. Make sure that finalize_theme() also
  348. // frees any objects that can be recreated by initialize_theme*().
  349. _finalize_theme_contexts();
  350. default_theme.unref();
  351. project_theme.unref();
  352. fallback_font.unref();
  353. fallback_icon.unref();
  354. fallback_stylebox.unref();
  355. singleton = nullptr;
  356. }
  357. void ThemeContext::_emit_changed() {
  358. emit_signal(SNAME("changed"));
  359. }
  360. void ThemeContext::set_themes(List<Ref<Theme>> &p_themes) {
  361. for (const Ref<Theme> &theme : themes) {
  362. theme->disconnect_changed(callable_mp(this, &ThemeContext::_emit_changed));
  363. }
  364. themes.clear();
  365. for (const Ref<Theme> &theme : p_themes) {
  366. if (theme.is_null()) {
  367. continue;
  368. }
  369. themes.push_back(theme);
  370. theme->connect_changed(callable_mp(this, &ThemeContext::_emit_changed));
  371. }
  372. _emit_changed();
  373. }
  374. List<Ref<Theme>> ThemeContext::get_themes() const {
  375. return themes;
  376. }
  377. Ref<Theme> ThemeContext::get_fallback_theme() const {
  378. // We expect all contexts to be valid and non-empty, but just in case...
  379. if (themes.size() == 0) {
  380. return ThemeDB::get_singleton()->get_default_theme();
  381. }
  382. return themes.back()->get();
  383. }
  384. void ThemeContext::_bind_methods() {
  385. ADD_SIGNAL(MethodInfo("changed"));
  386. }