Debian Codegraph @

editor/themes/editor_icons.cpp

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