editor_theme.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* editor_theme.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 "editor_theme.h"
  31. #include "editor/editor_string_names.h"
  32. #include "scene/theme/theme_db.h"
  33. Vector<StringName> EditorTheme::editor_theme_types;
  34. // TODO: Refactor these and corresponding Theme methods to use the bool get_xxx(r_value) pattern internally.
  35. // Keep in sync with Theme::get_color.
  36. Color EditorTheme::get_color(const StringName &p_name, const StringName &p_theme_type) const {
  37. if (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name)) {
  38. return color_map[p_theme_type][p_name];
  39. } else {
  40. if (editor_theme_types.has(p_theme_type)) {
  41. WARN_PRINT(vformat("Trying to access a non-existing editor theme color '%s' in '%s'.", p_name, p_theme_type));
  42. }
  43. return Color();
  44. }
  45. }
  46. // Keep in sync with Theme::get_constant.
  47. int EditorTheme::get_constant(const StringName &p_name, const StringName &p_theme_type) const {
  48. if (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name)) {
  49. return constant_map[p_theme_type][p_name];
  50. } else {
  51. if (editor_theme_types.has(p_theme_type)) {
  52. WARN_PRINT(vformat("Trying to access a non-existing editor theme constant '%s' in '%s'.", p_name, p_theme_type));
  53. }
  54. return 0;
  55. }
  56. }
  57. // Keep in sync with Theme::get_font.
  58. Ref<Font> EditorTheme::get_font(const StringName &p_name, const StringName &p_theme_type) const {
  59. if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) {
  60. return font_map[p_theme_type][p_name];
  61. } else if (has_default_font()) {
  62. if (editor_theme_types.has(p_theme_type)) {
  63. WARN_PRINT(vformat("Trying to access a non-existing editor theme font '%s' in '%s'.", p_name, p_theme_type));
  64. }
  65. return default_font;
  66. } else {
  67. if (editor_theme_types.has(p_theme_type)) {
  68. WARN_PRINT(vformat("Trying to access a non-existing editor theme font '%s' in '%s'.", p_name, p_theme_type));
  69. }
  70. return ThemeDB::get_singleton()->get_fallback_font();
  71. }
  72. }
  73. // Keep in sync with Theme::get_font_size.
  74. int EditorTheme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  75. if (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) {
  76. return font_size_map[p_theme_type][p_name];
  77. } else if (has_default_font_size()) {
  78. if (editor_theme_types.has(p_theme_type)) {
  79. WARN_PRINT(vformat("Trying to access a non-existing editor theme font size '%s' in '%s'.", p_name, p_theme_type));
  80. }
  81. return default_font_size;
  82. } else {
  83. if (editor_theme_types.has(p_theme_type)) {
  84. WARN_PRINT(vformat("Trying to access a non-existing editor theme font size '%s' in '%s'.", p_name, p_theme_type));
  85. }
  86. return ThemeDB::get_singleton()->get_fallback_font_size();
  87. }
  88. }
  89. // Keep in sync with Theme::get_icon.
  90. Ref<Texture2D> EditorTheme::get_icon(const StringName &p_name, const StringName &p_theme_type) const {
  91. if (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) {
  92. return icon_map[p_theme_type][p_name];
  93. } else {
  94. if (editor_theme_types.has(p_theme_type)) {
  95. WARN_PRINT(vformat("Trying to access a non-existing editor theme icon '%s' in '%s'.", p_name, p_theme_type));
  96. }
  97. return ThemeDB::get_singleton()->get_fallback_icon();
  98. }
  99. }
  100. // Keep in sync with Theme::get_stylebox.
  101. Ref<StyleBox> EditorTheme::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  102. if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
  103. return style_map[p_theme_type][p_name];
  104. } else {
  105. if (editor_theme_types.has(p_theme_type)) {
  106. WARN_PRINT(vformat("Trying to access a non-existing editor theme stylebox '%s' in '%s'.", p_name, p_theme_type));
  107. }
  108. return ThemeDB::get_singleton()->get_fallback_stylebox();
  109. }
  110. }
  111. void EditorTheme::initialize() {
  112. editor_theme_types.append(EditorStringName(Editor));
  113. editor_theme_types.append(EditorStringName(EditorFonts));
  114. editor_theme_types.append(EditorStringName(EditorIcons));
  115. editor_theme_types.append(EditorStringName(EditorStyles));
  116. }
  117. void EditorTheme::finalize() {
  118. editor_theme_types.clear();
  119. }