editor_icons.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**************************************************************************/
  2. /* editor_icons.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_icons.h"
  31. #include "editor/editor_settings.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/themes/editor_color_map.h"
  34. #include "editor/themes/editor_icons.gen.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/resources/image_texture.h"
  37. #include "scene/resources/texture.h"
  38. #include "modules/modules_enabled.gen.h" // For svg.
  39. #ifdef MODULE_SVG_ENABLED
  40. #include "modules/svg/image_loader_svg.h"
  41. #endif
  42. void editor_configure_icons(bool p_dark_theme) {
  43. #ifdef MODULE_SVG_ENABLED
  44. if (p_dark_theme) {
  45. ImageLoaderSVG::set_forced_color_map(HashMap<Color, Color>());
  46. } else {
  47. ImageLoaderSVG::set_forced_color_map(EditorColorMap::get_color_conversion_map());
  48. }
  49. #else
  50. WARN_PRINT("SVG support disabled, editor icons won't be rendered.");
  51. #endif
  52. }
  53. // See also `generate_icon()` in `scene/theme/default_theme.cpp`.
  54. Ref<ImageTexture> editor_generate_icon(int p_index, float p_scale, float p_saturation, const HashMap<Color, Color> &p_convert_colors = HashMap<Color, Color>()) {
  55. Ref<Image> img = memnew(Image);
  56. #ifdef MODULE_SVG_ENABLED
  57. // Upsample icon generation only if the editor scale isn't an integer multiplier.
  58. // Generating upsampled icons is slower, and the benefit is hardly visible
  59. // with integer editor scales.
  60. const bool upsample = !Math::is_equal_approx(Math::round(p_scale), p_scale);
  61. Error err = ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], p_scale, upsample, p_convert_colors);
  62. ERR_FAIL_COND_V_MSG(err != OK, Ref<ImageTexture>(), "Failed generating icon, unsupported or invalid SVG data in editor theme.");
  63. if (p_saturation != 1.0) {
  64. img->adjust_bcs(1.0, 1.0, p_saturation);
  65. }
  66. #else
  67. // If the SVG module is disabled, we can't really display the UI well, but at least we won't crash.
  68. // 16 pixels is used as it's the most common base size for Godot icons.
  69. img = Image::create_empty(16 * p_scale, 16 * p_scale, false, Image::FORMAT_RGBA8);
  70. #endif
  71. return ImageTexture::create_from_image(img);
  72. }
  73. float get_gizmo_handle_scale(const String &p_gizmo_handle_name, float p_gizmo_handle_scale) {
  74. if (p_gizmo_handle_scale > 1.0f) {
  75. // The names of the icons that require additional scaling.
  76. static HashSet<StringName> gizmo_to_scale;
  77. if (gizmo_to_scale.is_empty()) {
  78. gizmo_to_scale.insert("EditorHandle");
  79. gizmo_to_scale.insert("EditorHandleAdd");
  80. gizmo_to_scale.insert("EditorHandleDisabled");
  81. gizmo_to_scale.insert("EditorCurveHandle");
  82. gizmo_to_scale.insert("EditorPathSharpHandle");
  83. gizmo_to_scale.insert("EditorPathSmoothHandle");
  84. }
  85. if (gizmo_to_scale.has(p_gizmo_handle_name)) {
  86. return EDSCALE * p_gizmo_handle_scale;
  87. }
  88. }
  89. return EDSCALE;
  90. }
  91. void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p_icon_saturation, int p_thumb_size, float p_gizmo_handle_scale) {
  92. // Before we register the icons, we adjust their colors and saturation.
  93. // Most icons follow the standard rules for color conversion to follow the editor
  94. // theme's polarity (dark/light). We also adjust the saturation for most icons,
  95. // following the editor setting.
  96. // Some icons are excluded from this conversion, and instead use the configured
  97. // accent color to replace their innate accent color to match the editor theme.
  98. // And then some icons are completely excluded from the conversion.
  99. // Standard color conversion map.
  100. HashMap<Color, Color> color_conversion_map;
  101. // Icons by default are set up for the dark theme, so if the theme is light,
  102. // we apply the dark-to-light color conversion map.
  103. if (!p_dark_theme) {
  104. for (KeyValue<Color, Color> &E : EditorColorMap::get_color_conversion_map()) {
  105. color_conversion_map[E.key] = E.value;
  106. }
  107. }
  108. // These colors should be converted even if we are using a dark theme.
  109. const Color error_color = p_theme->get_color(SNAME("error_color"), EditorStringName(Editor));
  110. const Color success_color = p_theme->get_color(SNAME("success_color"), EditorStringName(Editor));
  111. const Color warning_color = p_theme->get_color(SNAME("warning_color"), EditorStringName(Editor));
  112. color_conversion_map[Color::html("#ff5f5f")] = error_color;
  113. color_conversion_map[Color::html("#5fff97")] = success_color;
  114. color_conversion_map[Color::html("#ffdd65")] = warning_color;
  115. // The names of the icons to exclude from the standard color conversion.
  116. HashSet<StringName> conversion_exceptions = EditorColorMap::get_color_conversion_exceptions();
  117. // The names of the icons to exclude when adjusting for saturation.
  118. HashSet<StringName> saturation_exceptions;
  119. saturation_exceptions.insert("DefaultProjectIcon");
  120. saturation_exceptions.insert("Godot");
  121. saturation_exceptions.insert("Logo");
  122. // Accent color conversion map.
  123. // It is used on some icons (checkbox, radio, toggle, etc.), regardless of the dark
  124. // or light mode.
  125. HashMap<Color, Color> accent_color_map;
  126. HashSet<StringName> accent_color_icons;
  127. const Color accent_color = p_theme->get_color(SNAME("accent_color"), EditorStringName(Editor));
  128. accent_color_map[Color::html("699ce8")] = accent_color;
  129. if (accent_color.get_luminance() > 0.75) {
  130. accent_color_map[Color::html("ffffff")] = Color(0.2, 0.2, 0.2);
  131. }
  132. accent_color_icons.insert("GuiChecked");
  133. accent_color_icons.insert("GuiRadioChecked");
  134. accent_color_icons.insert("GuiIndeterminate");
  135. accent_color_icons.insert("GuiToggleOn");
  136. accent_color_icons.insert("GuiToggleOnMirrored");
  137. accent_color_icons.insert("PlayOverlay");
  138. // Generate icons.
  139. {
  140. for (int i = 0; i < editor_icons_count; i++) {
  141. Ref<ImageTexture> icon;
  142. const String &editor_icon_name = editor_icons_names[i];
  143. if (accent_color_icons.has(editor_icon_name)) {
  144. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), 1.0, accent_color_map);
  145. } else {
  146. float saturation = p_icon_saturation;
  147. if (saturation_exceptions.has(editor_icon_name)) {
  148. saturation = 1.0;
  149. }
  150. if (conversion_exceptions.has(editor_icon_name)) {
  151. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation);
  152. } else {
  153. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map);
  154. }
  155. }
  156. p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), icon);
  157. }
  158. }
  159. // Generate thumbnail icons with the given thumbnail size.
  160. // See editor\icons\editor_icons_builders.py for the code that determines which icons are thumbnails.
  161. if (p_thumb_size >= 64) {
  162. const float scale = (float)p_thumb_size / 64.0 * EDSCALE;
  163. for (int i = 0; i < editor_bg_thumbs_count; i++) {
  164. const int index = editor_bg_thumbs_indices[i];
  165. Ref<ImageTexture> icon;
  166. if (accent_color_icons.has(editor_icons_names[index])) {
  167. icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
  168. } else {
  169. float saturation = p_icon_saturation;
  170. if (saturation_exceptions.has(editor_icons_names[index])) {
  171. saturation = 1.0;
  172. }
  173. if (conversion_exceptions.has(editor_icons_names[index])) {
  174. icon = editor_generate_icon(index, scale, saturation);
  175. } else {
  176. icon = editor_generate_icon(index, scale, saturation, color_conversion_map);
  177. }
  178. }
  179. p_theme->set_icon(editor_icons_names[index], EditorStringName(EditorIcons), icon);
  180. }
  181. } else {
  182. const float scale = (float)p_thumb_size / 32.0 * EDSCALE;
  183. for (int i = 0; i < editor_md_thumbs_count; i++) {
  184. const int index = editor_md_thumbs_indices[i];
  185. Ref<ImageTexture> icon;
  186. if (accent_color_icons.has(editor_icons_names[index])) {
  187. icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
  188. } else {
  189. float saturation = p_icon_saturation;
  190. if (saturation_exceptions.has(editor_icons_names[index])) {
  191. saturation = 1.0;
  192. }
  193. if (conversion_exceptions.has(editor_icons_names[index])) {
  194. icon = editor_generate_icon(index, scale, saturation);
  195. } else {
  196. icon = editor_generate_icon(index, scale, saturation, color_conversion_map);
  197. }
  198. }
  199. p_theme->set_icon(editor_icons_names[index], EditorStringName(EditorIcons), icon);
  200. }
  201. }
  202. }
  203. void editor_copy_icons(const Ref<Theme> &p_theme, const Ref<Theme> &p_old_theme) {
  204. for (int i = 0; i < editor_icons_count; i++) {
  205. p_theme->set_icon(editor_icons_names[i], EditorStringName(EditorIcons), p_old_theme->get_icon(editor_icons_names[i], EditorStringName(EditorIcons)));
  206. }
  207. }
  208. // Returns the SVG code for the default project icon.
  209. String get_default_project_icon() {
  210. // FIXME: This icon can probably be predefined in editor_icons.gen.h so we don't have to look up.
  211. for (int i = 0; i < editor_icons_count; i++) {
  212. if (strcmp(editor_icons_names[i], "DefaultProjectIcon") == 0) {
  213. return String(editor_icons_sources[i]);
  214. }
  215. }
  216. return String();
  217. }