Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/themes/editor_icons.cpp
9896 views
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_theme) {
43
if (p_dark_theme) {
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 used in native menus.
108
HashSet<StringName> native_menu_icons;
109
native_menu_icons.insert("HelpSearch");
110
native_menu_icons.insert("ActionCopy");
111
native_menu_icons.insert("Heart");
112
native_menu_icons.insert("PackedScene");
113
native_menu_icons.insert("FileAccess");
114
native_menu_icons.insert("Folder");
115
native_menu_icons.insert("AnimationTrackList");
116
native_menu_icons.insert("Object");
117
native_menu_icons.insert("History");
118
119
// The names of the icons to exclude from the standard color conversion.
120
HashSet<StringName> conversion_exceptions = EditorColorMap::get_color_conversion_exceptions();
121
122
// The names of the icons to exclude when adjusting for saturation.
123
HashSet<StringName> saturation_exceptions;
124
saturation_exceptions.insert("DefaultProjectIcon");
125
saturation_exceptions.insert("Godot");
126
saturation_exceptions.insert("Logo");
127
128
// Accent color conversion map.
129
// It is used on some icons (checkbox, radio, toggle, etc.), regardless of the dark
130
// or light mode.
131
Dictionary accent_color_map;
132
HashSet<StringName> accent_color_icons;
133
134
const Color accent_color = p_theme->get_color(SNAME("accent_color"), EditorStringName(Editor));
135
accent_color_map[Color::html("699ce8")] = accent_color;
136
if (accent_color.get_luminance() > 0.75) {
137
accent_color_map[Color::html("ffffff")] = Color(0.2, 0.2, 0.2);
138
}
139
140
accent_color_icons.insert("GuiChecked");
141
accent_color_icons.insert("GuiRadioChecked");
142
accent_color_icons.insert("GuiIndeterminate");
143
accent_color_icons.insert("GuiToggleOn");
144
accent_color_icons.insert("GuiToggleOnMirrored");
145
accent_color_icons.insert("PlayOverlay");
146
147
// Generate icons.
148
{
149
for (int i = 0; i < editor_icons_count; i++) {
150
const String &editor_icon_name = editor_icons_names[i];
151
if (native_menu_icons.has(editor_icon_name)) {
152
float saturation = p_icon_saturation;
153
if (saturation_exceptions.has(editor_icon_name)) {
154
saturation = 1.0;
155
}
156
157
Ref<DPITexture> icon_dark = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_dark);
158
Ref<DPITexture> icon_light = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_light);
159
160
p_theme->set_icon(editor_icon_name + "Dark", EditorStringName(EditorIcons), icon_dark);
161
p_theme->set_icon(editor_icon_name + "Light", EditorStringName(EditorIcons), icon_light);
162
p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), p_dark_theme ? icon_dark : icon_light);
163
} else {
164
Ref<DPITexture> icon;
165
if (accent_color_icons.has(editor_icon_name)) {
166
icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), 1.0, accent_color_map);
167
} else {
168
float saturation = p_icon_saturation;
169
if (saturation_exceptions.has(editor_icon_name)) {
170
saturation = 1.0;
171
}
172
173
if (conversion_exceptions.has(editor_icon_name)) {
174
icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation);
175
} else {
176
icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map);
177
}
178
}
179
180
p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), icon);
181
}
182
}
183
}
184
185
// Generate thumbnail icons with the given thumbnail size.
186
// See editor\icons\editor_icons_builders.py for the code that determines which icons are thumbnails.
187
if (p_thumb_size >= 64) {
188
const float scale = (float)p_thumb_size / 64.0 * EDSCALE;
189
for (int i = 0; i < editor_bg_thumbs_count; i++) {
190
const int index = editor_bg_thumbs_indices[i];
191
Ref<DPITexture> icon;
192
193
if (accent_color_icons.has(editor_icons_names[index])) {
194
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
195
} else {
196
float saturation = p_icon_saturation;
197
if (saturation_exceptions.has(editor_icons_names[index])) {
198
saturation = 1.0;
199
}
200
201
if (conversion_exceptions.has(editor_icons_names[index])) {
202
icon = editor_generate_icon(index, scale, saturation);
203
} else {
204
icon = editor_generate_icon(index, scale, saturation, color_conversion_map);
205
}
206
}
207
208
p_theme->set_icon(editor_icons_names[index], EditorStringName(EditorIcons), icon);
209
}
210
} else {
211
const float scale = (float)p_thumb_size / 32.0 * EDSCALE;
212
for (int i = 0; i < editor_md_thumbs_count; i++) {
213
const int index = editor_md_thumbs_indices[i];
214
Ref<DPITexture> icon;
215
216
if (accent_color_icons.has(editor_icons_names[index])) {
217
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
218
} else {
219
float saturation = p_icon_saturation;
220
if (saturation_exceptions.has(editor_icons_names[index])) {
221
saturation = 1.0;
222
}
223
224
if (conversion_exceptions.has(editor_icons_names[index])) {
225
icon = editor_generate_icon(index, scale, saturation);
226
} else {
227
icon = editor_generate_icon(index, scale, saturation, color_conversion_map);
228
}
229
}
230
231
p_theme->set_icon(editor_icons_names[index], EditorStringName(EditorIcons), icon);
232
}
233
}
234
}
235
236
void editor_copy_icons(const Ref<Theme> &p_theme, const Ref<Theme> &p_old_theme) {
237
for (int i = 0; i < editor_icons_count; i++) {
238
p_theme->set_icon(editor_icons_names[i], EditorStringName(EditorIcons), p_old_theme->get_icon(editor_icons_names[i], EditorStringName(EditorIcons)));
239
}
240
}
241
242
// Returns the SVG code for the default project icon.
243
String get_default_project_icon() {
244
// FIXME: This icon can probably be predefined in editor_icons.gen.h so we don't have to look up.
245
for (int i = 0; i < editor_icons_count; i++) {
246
if (strcmp(editor_icons_names[i], "DefaultProjectIcon") == 0) {
247
return String(editor_icons_sources[i]);
248
}
249
}
250
return String();
251
}
252
253