editor_icons.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/dpi_texture.h"
  36. #include "scene/resources/image_texture.h"
  37. #include "modules/svg/image_loader_svg.h"
  38. void editor_configure_icons(bool p_dark_icon_and_font) {
  39. if (p_dark_icon_and_font) {
  40. ImageLoaderSVG::set_forced_color_map(HashMap<Color, Color>());
  41. } else {
  42. ImageLoaderSVG::set_forced_color_map(EditorColorMap::get_color_conversion_map());
  43. }
  44. }
  45. // See also `generate_icon()` in `scene/theme/default_theme.cpp`.
  46. Ref<DPITexture> editor_generate_icon(int p_index, float p_scale, float p_saturation, const Dictionary &p_convert_colors = Dictionary()) {
  47. return DPITexture::create_from_string(editor_icons_sources[p_index], p_scale, p_saturation, p_convert_colors);
  48. }
  49. float get_gizmo_handle_scale(const String &p_gizmo_handle_name, float p_gizmo_handle_scale) {
  50. if (p_gizmo_handle_scale > 1.0f) {
  51. // The names of the icons that require additional scaling.
  52. static HashSet<StringName> gizmo_to_scale;
  53. if (gizmo_to_scale.is_empty()) {
  54. gizmo_to_scale.insert("EditorHandle");
  55. gizmo_to_scale.insert("EditorHandleAdd");
  56. gizmo_to_scale.insert("EditorHandleDisabled");
  57. gizmo_to_scale.insert("EditorCurveHandle");
  58. gizmo_to_scale.insert("EditorPathSharpHandle");
  59. gizmo_to_scale.insert("EditorPathSmoothHandle");
  60. gizmo_to_scale.insert("EditorControlAnchor");
  61. }
  62. if (gizmo_to_scale.has(p_gizmo_handle_name)) {
  63. return EDSCALE * p_gizmo_handle_scale;
  64. }
  65. }
  66. return EDSCALE;
  67. }
  68. 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) {
  69. // Before we register the icons, we adjust their colors and saturation.
  70. // Most icons follow the standard rules for color conversion to follow the editor
  71. // theme's polarity (dark/light). We also adjust the saturation for most icons,
  72. // following the editor setting.
  73. // Some icons are excluded from this conversion, and instead use the configured
  74. // accent color to replace their innate accent color to match the editor theme.
  75. // And then some icons are completely excluded from the conversion.
  76. // Standard color conversion map.
  77. Dictionary color_conversion_map_light;
  78. Dictionary color_conversion_map_dark;
  79. // Icons by default are set up for the dark theme, so if the theme is light,
  80. // we apply the dark-to-light color conversion map.
  81. for (KeyValue<Color, Color> &E : EditorColorMap::get_color_conversion_map()) {
  82. color_conversion_map_light[E.key] = E.value;
  83. }
  84. // These colors should be converted even if we are using a dark theme.
  85. const Color error_color = p_theme->get_color(SNAME("error_color"), EditorStringName(Editor));
  86. const Color success_color = p_theme->get_color(SNAME("success_color"), EditorStringName(Editor));
  87. const Color warning_color = p_theme->get_color(SNAME("warning_color"), EditorStringName(Editor));
  88. color_conversion_map_dark[Color::html("#ff5f5f")] = error_color;
  89. color_conversion_map_dark[Color::html("#5fff97")] = success_color;
  90. color_conversion_map_dark[Color::html("#ffdd65")] = warning_color;
  91. color_conversion_map_light[Color::html("#ff5f5f")] = error_color;
  92. color_conversion_map_light[Color::html("#5fff97")] = success_color;
  93. color_conversion_map_light[Color::html("#ffdd65")] = warning_color;
  94. Dictionary color_conversion_map = p_dark_theme ? color_conversion_map_dark : color_conversion_map_light;
  95. // The names of the icons to exclude from the standard color conversion.
  96. HashSet<StringName> conversion_exceptions = EditorColorMap::get_color_conversion_exceptions();
  97. // The names of the icons to exclude when adjusting for saturation.
  98. HashSet<StringName> saturation_exceptions;
  99. saturation_exceptions.insert("DefaultProjectIcon");
  100. saturation_exceptions.insert("Godot");
  101. saturation_exceptions.insert("Logo");
  102. // Accent color conversion map.
  103. // It is used on some icons (checkbox, radio, toggle, etc.), regardless of the dark
  104. // or light mode.
  105. Dictionary accent_color_map;
  106. HashSet<StringName> accent_color_icons;
  107. const Color accent_color = p_theme->get_color(SNAME("accent_color"), EditorStringName(Editor));
  108. accent_color_map[Color::html("699ce8")] = accent_color;
  109. if (accent_color.get_luminance() > 0.75) {
  110. accent_color_map[Color::html("ffffff")] = Color(0.2, 0.2, 0.2);
  111. }
  112. accent_color_icons.insert("GuiChecked");
  113. accent_color_icons.insert("GuiRadioChecked");
  114. accent_color_icons.insert("GuiIndeterminate");
  115. accent_color_icons.insert("GuiToggleOn");
  116. accent_color_icons.insert("GuiToggleOnMirrored");
  117. accent_color_icons.insert("PlayOverlay");
  118. // Generate icons.
  119. {
  120. for (int i = 0; i < editor_icons_count; i++) {
  121. const String &editor_icon_name = editor_icons_names[i];
  122. Ref<DPITexture> icon;
  123. if (accent_color_icons.has(editor_icon_name)) {
  124. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), 1.0, accent_color_map);
  125. } else {
  126. float saturation = p_icon_saturation;
  127. if (saturation_exceptions.has(editor_icon_name)) {
  128. saturation = 1.0;
  129. }
  130. if (conversion_exceptions.has(editor_icon_name)) {
  131. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation);
  132. } else {
  133. icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map);
  134. }
  135. }
  136. p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), icon);
  137. }
  138. }
  139. // Generate thumbnail icons with the given thumbnail size.
  140. // See editor\icons\editor_icons_builders.py for the code that determines which icons are thumbnails.
  141. if (p_thumb_size >= 64) {
  142. const float scale = (float)p_thumb_size / 64.0 * EDSCALE;
  143. for (int i = 0; i < editor_bg_thumbs_count; i++) {
  144. const int index = editor_bg_thumbs_indices[i];
  145. Ref<DPITexture> icon;
  146. if (accent_color_icons.has(editor_icons_names[index])) {
  147. icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
  148. } else {
  149. float saturation = p_icon_saturation;
  150. if (saturation_exceptions.has(editor_icons_names[index])) {
  151. saturation = 1.0;
  152. }
  153. if (conversion_exceptions.has(editor_icons_names[index])) {
  154. icon = editor_generate_icon(index, scale, saturation);
  155. } else {
  156. icon = editor_generate_icon(index, scale, saturation, color_conversion_map);
  157. }
  158. }
  159. p_theme->set_icon(editor_icons_names[index], EditorStringName(EditorIcons), icon);
  160. }
  161. } else {
  162. const float scale = (float)p_thumb_size / 32.0 * EDSCALE;
  163. for (int i = 0; i < editor_md_thumbs_count; i++) {
  164. const int index = editor_md_thumbs_indices[i];
  165. Ref<DPITexture> 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. }
  182. }
  183. void editor_copy_icons(const Ref<Theme> &p_theme, const Ref<Theme> &p_old_theme) {
  184. for (int i = 0; i < editor_icons_count; i++) {
  185. p_theme->set_icon(editor_icons_names[i], EditorStringName(EditorIcons), p_old_theme->get_icon(editor_icons_names[i], EditorStringName(EditorIcons)));
  186. }
  187. }
  188. // Returns the SVG code for the default project icon.
  189. String get_default_project_icon() {
  190. // FIXME: This icon can probably be predefined in editor_icons.gen.h so we don't have to look up.
  191. for (int i = 0; i < editor_icons_count; i++) {
  192. if (strcmp(editor_icons_names[i], "DefaultProjectIcon") == 0) {
  193. return String(editor_icons_sources[i]);
  194. }
  195. }
  196. return String();
  197. }