-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
theme_utils.gd
84 lines (70 loc) · 3.97 KB
/
theme_utils.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
## Provides functions to scale the theme resource and theme properties.
## The editor scale is used to scale the theme.
@tool
## Gets and scales the font_size theme override of the input text_node using the editor scale.
## Adds a font size override to text_node directly.
static func scale_font_size(text_node: Control) -> void:
var editor_scale := EditorInterface.get_editor_scale()
var title_font_size: int = text_node.get_theme_font_size("font_size")
text_node.add_theme_font_size_override("font_size", title_font_size * editor_scale)
## Gets and scales the margins of the input margin_container using the editor scale.
## Adds a theme constant override for each margin property directly.
static func scale_margin_container_margins(margin_container: MarginContainer) -> void:
var editor_scale := EditorInterface.get_editor_scale()
for property in ["margin_left", "margin_right", "margin_top", "margin_bottom"]:
var margin: int = margin_container.get_theme_constant(property)
margin_container.add_theme_constant_override(property, margin * editor_scale)
## Returns a new theme object, a deep copy of theme_resource, with properties scaled
## using the editor scale.
## Making a deep copy ensures values don't get scaled and saved when working on an addon's
## user interface.
static func generate_scaled_theme(theme_resource: Theme) -> Theme:
var new_theme = theme_resource.duplicate(true)
var editor_scale := EditorInterface.get_editor_scale()
# Scale font sizes
# We take two measures into account when scaling the interface. Users may have changed their
# editor scale, but they can also change the main font size without changing the editor scale.
# If they changed the main font size, we increase the size of every font accordingly.
var editor_settings := EditorInterface.get_editor_settings()
var main_font_size := editor_settings.get_setting("interface/editor/main_font_size")
var base_font_size := max(new_theme.default_font_size, main_font_size)
var size_difference: int = max(main_font_size - new_theme.default_font_size, 0)
new_theme.default_font_size = base_font_size * editor_scale
var theme_types := Array(new_theme.get_font_size_type_list()) + ["TitleLabel"]
for theme_type in theme_types:
for font_size_property in new_theme.get_font_size_list(theme_type):
var font_size: int = (
new_theme.get_font_size(font_size_property, theme_type) + size_difference
)
var new_font_size: int = font_size * editor_scale
new_theme.set_font_size(font_size_property, theme_type, new_font_size)
# Scale margins
for theme_type in new_theme.get_constant_type_list():
for constant in new_theme.get_constant_list(theme_type):
var constant_value: int = new_theme.get_constant(constant, theme_type)
var new_value: int = constant_value * editor_scale
new_theme.set_constant(theme_type, constant, new_value)
for stylebox_type in new_theme.get_stylebox_type_list():
for stylebox_name in new_theme.get_stylebox_list(stylebox_type):
var stylebox: StyleBox = new_theme.get_stylebox(stylebox_name, stylebox_type)
if stylebox is StyleBoxFlat:
stylebox.border_width_bottom *= editor_scale
stylebox.border_width_left *= editor_scale
stylebox.border_width_right *= editor_scale
stylebox.border_width_top *= editor_scale
stylebox.corner_radius_bottom_left *= editor_scale
stylebox.corner_radius_bottom_right *= editor_scale
stylebox.corner_radius_top_left *= editor_scale
stylebox.corner_radius_top_right *= editor_scale
stylebox.shadow_offset *= editor_scale
stylebox.shadow_size *= editor_scale
if stylebox is StyleBoxFlat or stylebox is StyleBoxTexture:
stylebox.content_margin_left *= editor_scale
stylebox.content_margin_right *= editor_scale
stylebox.content_margin_top *= editor_scale
stylebox.content_margin_bottom *= editor_scale
stylebox.expand_margin_left *= editor_scale
stylebox.expand_margin_right *= editor_scale
stylebox.expand_margin_top *= editor_scale
stylebox.expand_margin_bottom *= editor_scale
return new_theme