editor_icons.cpp 11 KB

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