forked from moonheart08/Content.AL.UIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseStyle.cs
More file actions
246 lines (204 loc) · 8.5 KB
/
Copy pathBaseStyle.cs
File metadata and controls
246 lines (204 loc) · 8.5 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.AL.UIKit.Sheets;
using Content.AL.UIKit.Styleboxes;
using Content.AL.UIKit.Widgets;
using JetBrains.Annotations;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Reflection;
using Robust.Shared.Sandboxing;
using Robust.Shared.Utility;
using static Robust.Client.UserInterface.StylesheetHelpers;
using static Content.AL.UIKit.ALStylesheetHelpers;
namespace Content.AL.UIKit;
[PublicAPI]
public abstract class BaseStyle
{
[Dependency] public readonly IUserInterfaceManager UserInterface = default!;
[Dependency] public readonly IReflectionManager Reflection = default!;
[Dependency] public readonly ISandboxHelper SandboxHelper = default!;
public abstract string FileRoot { get; }
/// <summary>
/// A five color, intense palette used for primary colors.
/// </summary>
public abstract Color[] PrimaryPalette { get; }
/// <summary>
/// A five color, dull palette used for secondary colors i.e. window backgrounds.
/// </summary>
public abstract Color[] SecondaryPalette { get; }
public readonly IResourceCache ResourceCache;
#region Textures
public Texture[] PanelBackgroundTextures;
public StyleBoxTexture[] PanelBackgrounds;
public Texture[] ButtonBackgroundTextures;
public StyleBoxTexture[] ButtonBackgrounds;
public Texture[] ButtonPositiveBackgroundTextures;
public StyleBoxTexture[] ButtonPositiveBackgrounds;
public Texture[] ButtonNegativeBackgroundTextures;
public StyleBoxTexture[] ButtonNegativeBackgrounds;
public StyleBox[] PrimarySolidBackgrounds;
public StyleBox[] SecondarySolidBackgrounds;
public virtual int PanelMargin => 3;
#endregion
#region Fonts
public virtual int BaseFontSize => 12;
public virtual FontStack Font => DefaultFonts.NotoSans;
public virtual FontStack DisplayFont => DefaultFonts.NotoSansDisplay;
public virtual FontStack MonoFont => DefaultFonts.NotoMono;
#endregion
public StyleRule[] BaseRules { get; }
[SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
public BaseStyle(IResourceCache resCache)
{
IoCManager.InjectDependencies(this);
ResourceCache = resCache;
/* Panel textures */
PrimarySolidBackgrounds = PrimaryPalette.Select(x => (StyleBox)new StyleBoxFlat(x)).ToArray();
SecondarySolidBackgrounds = SecondaryPalette.Select(x => (StyleBox)new StyleBoxFlat(x)).ToArray();
(PanelBackgroundTextures, PanelBackgrounds, _)
= LoadIndefiniteNinePatchSet($"{FileRoot}/panel_bg_{{0}}.png", PanelMargin);
(ButtonBackgroundTextures, ButtonBackgrounds, _)
= LoadIndefiniteNinePatchSet($"{FileRoot}/button_bg_{{0}}.png", PanelMargin);
(ButtonPositiveBackgroundTextures, ButtonPositiveBackgrounds, _)
= LoadIndefiniteNinePatchSet($"{FileRoot}/button_positive_bg_{{0}}.png", PanelMargin);
(ButtonNegativeBackgroundTextures, ButtonNegativeBackgrounds, _)
= LoadIndefiniteNinePatchSet($"{FileRoot}/button_negative_bg_{{0}}.png", PanelMargin);
BaseRules = new StyleRule[]
{
Element().Prop(ALStyleConsts.BackgroundPanelStyleboxes, PanelBackgrounds),
Element().Prop(ALStyleConsts.PrimaryPalette, PrimaryPalette),
Element().Prop(ALStyleConsts.SecondaryPalette, SecondaryPalette),
/* TEXT AND FONTS */
Element().Prop(ALStyleConsts.Font, Font.GetFont(BaseFontSize)),
Element().Class(ALStyleConsts.Bold)
.Prop(ALStyleConsts.Font, Font.GetFont(BaseFontSize, FontStack.FontKind.Bold)),
Element().Prop(ALStyleConsts.FontColor, SecondaryPalette[0]),
/* Horizontal and vertical bars */
E<HBar>().Prop(ALStyleConsts.StyleBox, SecondarySolidBackgrounds[0]),
/* Sliders */
E<Slider>()
.Prop(Slider.StylePropertyBackground, LoadTexture($"{FileRoot}/slider_fill.png").ToPatchStyleBox(14).Modulate("#141111FF"))
.Prop(Slider.StylePropertyForeground, LoadTexture($"{FileRoot}/slider_outline.png").ToPatchStyleBox(14).Modulate(SecondaryPalette[4]))
.Prop(Slider.StylePropertyGrabber, LoadTexture($"{FileRoot}/slider_grabber.png").ToPatchStyleBox(14).Zoom(2))
.Prop(Slider.StylePropertyFill, LoadTexture($"{FileRoot}/slider_fill.png").ToPatchStyleBox(14).Modulate(PrimaryPalette[1])),
E<TextureButton>().ParentOf(E<TextureRect>())
.Prop(TextureRect.StylePropertyTextureStretch, TextureRect.StretchMode.KeepCentered)
}
// Load all the stylesheet pieces.
.Concat(GetRulesFromAttribute<StylesheetAttribute>()).ToArray();
}
#region Texture loading utilities
public bool TryLoadTexture(string target, [NotNullWhen(true)] out Texture? texture)
{
var success = ResourceCache.TryGetResource<TextureResource>(new ResPath(target), out var res);
texture = null;
if (res is null)
return success;
texture = res;
return success;
}
public Texture LoadTexture(string target)
{
return ResourceCache.GetTexture(target);
}
public Texture[] LoadTextureSet(string target, int amount)
{
var tx = new Texture[amount];
for (var i = 0; i < amount; i++)
{
tx[i] = ResourceCache.GetTexture(string.Format(target, i));
}
return tx;
}
public (Texture[] textures, int found) LoadIndefiniteTextureSet(string target)
{
var tx = new List<Texture>();
var i = 0;
while (ResourceCache.ContentFileExists(string.Format(target, i)))
{
tx.Add(ResourceCache.GetTexture(string.Format(target, i)));
i++;
}
return (tx.ToArray(), i);
}
public (Texture[] textures, StyleBoxTexture[] boxes, int found) LoadIndefiniteNinePatchSet(string target, int margin)
{
var (textures, found) = LoadIndefiniteTextureSet(target);
return (textures, textures.ToPatchStyleBoxes(margin), found);
}
#endregion
#region Subsheets
public List<BaseSubsheet> GetFromAttribute<T>()
where T: Attribute
{
var tys = Reflection.FindTypesWithAttribute<T>();
var outp = new List<BaseSubsheet>();
foreach (var t in tys)
{
var value = (BaseSubsheet) SandboxHelper.CreateInstance(t);
outp.Add(value);
}
return outp;
}
public StyleRule[] GetRules(List<BaseSubsheet> sheets)
{
var outp = new List<StyleRule>();
foreach (var sheet in sheets)
{
outp.AddRange(sheet.GetRules(this));
}
return outp.ToArray();
}
public StyleRule[] GetRulesFromAttribute<T>()
where T: Attribute
{
return GetRules(GetFromAttribute<T>());
}
#endregion
public abstract Stylesheet Stylesheet { get; }
}
public static class StyleExtensions
{
public static StyleBoxTexture ToPatchStyleBox(this Texture texture, int margin)
{
var box = new StyleBoxTexture {Texture = texture};
box.SetPatchMargin(StyleBox.Margin.All, margin);
return box;
}
public static StyleBox Modulate(this StyleBoxTexture box, Color color)
{
var n = new StyleBoxTexture(box);
n.Modulate = color;
return n;
}
public static StyleBox Modulate(this StyleBoxTexture box, string color)
{
return box.Modulate(Color.FromHex(color));
}
public static StyleBox MinSize(this StyleBox box, Vector2i size)
{
box.ContentMarginBottomOverride = size.Y;
box.ContentMarginTopOverride = size.Y;
box.ContentMarginLeftOverride = size.X;
box.ContentMarginRightOverride = size.X;
return box;
}
public static StyleBox Zoom(this StyleBox box, float amount)
{
return new StyleBoxScaled(box, amount);
}
public static StyleBoxTexture[] ToPatchStyleBoxes(this Texture[] textures, int margin)
{
return textures.Select(x =>
{
var box = new StyleBoxTexture {Texture = x};
box.SetPatchMargin(StyleBox.Margin.All, margin);
return box;
}).ToArray();
}
}