/
GuiSkinConverters.cs
236 lines (206 loc) · 8.13 KB
/
GuiSkinConverters.cs
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
using System;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MonoGame.Extended.NuclexGui.Visuals.Flat
{
public class GuiSkinJsonConverter : JsonConverter
{
/// <summary>Width of the frame's bottom border regions</summary>
private int _bottomBorderWidth;
/// <summary>Width of the frame's left border regions</summary>
private int _leftBorderWidth;
/// <summary>Width of the frame's right border regions</summary>
private int _rightBorderWidth;
/// <summary>Width of the frame's top border regions</summary>
private int _topBorderWidth;
public override bool CanConvert(Type objectType)
{
if (objectType == typeof(Frame.Region[]))
return true;
if (objectType == typeof(Frame.Text))
return true;
return false;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
if (objectType == typeof(Frame.Region[]))
return ParseRegions(JArray.Load(reader));
if (objectType == typeof(Frame.Text))
return ParseText(JToken.Load(reader));
return (string) reader.Value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
}
private Frame.Region[] ParseRegions(JArray jArray)
{
var regions = new Frame.Region[jArray.Count];
_leftBorderWidth = 0;
_rightBorderWidth = 0;
_topBorderWidth = 0;
_bottomBorderWidth = 0;
// Detect borders
foreach (var token in jArray)
{
var hPlacement = token.Value<string>("hplacement");
var vPlacement = token.Value<string>("vplacement");
if (hPlacement == "left")
_leftBorderWidth = Math.Max(_leftBorderWidth, token.Value<int>("w"));
else
{
if (hPlacement == "right")
_rightBorderWidth = Math.Max(_rightBorderWidth, token.Value<int>("w"));
}
if (vPlacement == "top")
_topBorderWidth = Math.Max(_topBorderWidth, token.Value<int>("h"));
else
{
if (vPlacement == "bottom")
_bottomBorderWidth = Math.Max(_bottomBorderWidth, token.Value<int>("h"));
}
}
// Parse each region
for (var i = 0; i < regions.Length; i++)
regions[i] = ParseRegion(jArray[i]);
return regions;
}
private Frame.Region ParseRegion(JToken token)
{
var region = new Frame.Region();
var hAlignment = token.Value<string>("hplacement");
var vAlignment = token.Value<string>("vplacement");
var x = token.Value<int>("x");
var y = token.Value<int>("y");
var w = token.Value<int>("w");
var h = token.Value<int>("h");
// Assign trivialities
region.Source = token.Value<string>("source");
region.Id = token.Value<string>("id");
region.SourceRegion.X = x;
region.SourceRegion.Y = y;
region.SourceRegion.Width = w;
region.SourceRegion.Height = h;
// Process each region's placement and set up the unified coordinates
CalculateRegionPlacement(GetHorizontalPlacementIndex(hAlignment), w, _leftBorderWidth, _rightBorderWidth,
ref region.DestinationRegion.Location.X, ref region.DestinationRegion.Size.X);
CalculateRegionPlacement(GetVerticalPlacementIndex(vAlignment), h, _topBorderWidth, _bottomBorderWidth,
ref region.DestinationRegion.Location.Y, ref region.DestinationRegion.Size.Y);
return region;
}
/// <summary>
/// Calculates the unified coordinates a region needs to be placed at
/// </summary>
/// <param name="placementIndex">
/// Placement index indicating where in a frame the region will be located
/// </param>
/// <param name="width">Width of the region in pixels</param>
/// <param name="lowBorderWidth">
/// Width of the border on the lower end of the coordinate range
/// </param>
/// <param name="highBorderWidth">
/// Width of the border on the higher end of the coordinate range
/// </param>
/// <param name="location">
/// Receives the target location of the region in unified coordinates
/// </param>
/// <param name="size">
/// Receives the size of the region in unified coordinates
/// </param>
private void CalculateRegionPlacement(
int placementIndex, int width,
int lowBorderWidth, int highBorderWidth,
ref UniScalar location, ref UniScalar size
)
{
switch (placementIndex)
{
case -1:
{
// left or top
var gapWidth = lowBorderWidth - width;
location.Fraction = 0.0f;
location.Offset = gapWidth;
size.Fraction = 0.0f;
size.Offset = width;
break;
}
case +1:
{
// right or bottom
location.Fraction = 1.0f;
location.Offset = -highBorderWidth;
size.Fraction = 0.0f;
size.Offset = width;
break;
}
case 0:
{
// stretch
location.Fraction = 0.0f;
location.Offset = lowBorderWidth;
size.Fraction = 1.0f;
size.Offset = -(highBorderWidth + lowBorderWidth);
break;
}
}
}
/// <summary>Converts a horizontal placement string into a placement index</summary>
/// <param name="placement">String containing the horizontal placement</param>
/// <returns>A placement index that is equivalent to the provided string</returns>
private int GetHorizontalPlacementIndex(string placement)
{
switch (placement)
{
case "left":
{
return -1;
}
case "right":
{
return +1;
}
case "stretch":
default:
{
return 0;
}
}
}
/// <summary>Converts a vertical placement string into a placement index</summary>
/// <param name="placement">String containing the vertical placement</param>
/// <returns>A placement index that is equivalent to the provided string</returns>
private int GetVerticalPlacementIndex(string placement)
{
switch (placement)
{
case "top":
{
return -1;
}
case "bottom":
{
return +1;
}
case "stretch":
default:
{
return 0;
}
}
}
private Frame.Text ParseText(JToken token)
{
var text = new Frame.Text
{
Offset = new Point(token.Value<int>("xoffset"), token.Value<int>("yoffset")),
Color = ColorHelper.FromHex(token.Value<string>("color"))
};
Enum.TryParse(token.Value<string>("hplacement"), true, out text.HorizontalPlacement);
Enum.TryParse(token.Value<string>("vplacement"), true, out text.VerticalPlacement);
text.Source = token.Value<string>("font");
return text;
}
}
}