-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathBordered.cs
More file actions
204 lines (160 loc) · 5.7 KB
/
Copy pathBordered.cs
File metadata and controls
204 lines (160 loc) · 5.7 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
using Intersect.Client.Framework.GenericClasses;
using Intersect.Client.Framework.Graphics;
namespace Intersect.Client.Framework.Gwen.Skin.Texturing;
public partial struct SubRect
{
public float[] Uv;
}
/// <summary>
/// 3x3 texture grid.
/// </summary>
public partial struct Bordered : IEquatable<Bordered>, IAtlasDrawable
{
private readonly float mX;
private readonly float mY;
public static implicit operator FivePatch(Bordered bordered)
{
return new FivePatch(
bordered._texture,
(int)bordered.mX,
(int)bordered.mY,
(int)bordered._width,
(int)bordered._height,
bordered._margin,
default
);
}
private IGameTexture _texture;
private readonly SubRect[] _subRects;
private Margin _margin;
private float _width;
private float _height;
public Margin Margin => _margin;
public Bordered(
IGameTexture texture,
float x,
float y,
float w,
float h,
Margin inMargin
) : this()
{
mX = x;
mY = y;
_subRects = new SubRect[9];
for (var i = 0; i < _subRects.Length; i++)
{
_subRects[i].Uv = new float[4];
}
Init(texture, x, y, w, h, inMargin);
}
void DrawRect(Renderer.Base render, int i, int x, int y, int w, int h, Color clr)
{
render.DrawTexturedRect(
_texture, new Rectangle(x, y, w, h), clr, _subRects[i].Uv[0], _subRects[i].Uv[1], _subRects[i].Uv[2],
_subRects[i].Uv[3]
);
}
void SetRect(int num, float x, float y, float w, float h)
{
if (_texture == null)
{
return;
}
float texw = _texture.Width;
float texh = _texture.Height;
//x -= 1.0f;
//y -= 1.0f;
_subRects[num].Uv[0] = x / texw;
_subRects[num].Uv[1] = y / texh;
_subRects[num].Uv[2] = (x + w) / texw;
_subRects[num].Uv[3] = (y + h) / texh;
// rects[num].uv[0] += 1.0f / m_Texture->width;
// rects[num].uv[1] += 1.0f / m_Texture->width;
}
private void Init(
IGameTexture texture,
float x,
float y,
float w,
float h,
Margin inMargin,
float drawMarginScale = 1.0f
)
{
_texture = texture;
_margin = inMargin;
SetRect(0, x, y, _margin.Left, _margin.Top);
SetRect(1, x + _margin.Left, y, w - _margin.Left - _margin.Right, _margin.Top);
SetRect(2, x + w - _margin.Right, y, _margin.Right, _margin.Top);
SetRect(3, x, y + _margin.Top, _margin.Left, h - _margin.Top - _margin.Bottom);
SetRect(
4,
x + _margin.Left,
y + _margin.Top,
w - _margin.Left - _margin.Right,
h - _margin.Top - _margin.Bottom
);
SetRect(5, x + w - _margin.Right, y + _margin.Top, _margin.Right, h - _margin.Top - _margin.Bottom);
SetRect(6, x, y + h - _margin.Bottom, _margin.Left, _margin.Bottom);
SetRect(7, x + _margin.Left, y + h - _margin.Bottom, w - _margin.Left - _margin.Right, _margin.Bottom);
SetRect(8, x + w - _margin.Right, y + h - _margin.Bottom, _margin.Right, _margin.Bottom);
_width = w;
_height = h;
}
// can't have this as default param
/*public void Draw(Renderer.Base render, Rectangle r, Color col )
{
Draw(render, r, Color.Red);
}*/
public void Draw(Renderer.Base render, Rectangle r, Color col)
{
if (_texture == null)
{
return;
}
render.DrawColor = col;
if (r.Width < _width && r.Height < _height)
{
render.DrawTexturedRect(
_texture, r, col, _subRects[0].Uv[0], _subRects[0].Uv[1], _subRects[8].Uv[2], _subRects[8].Uv[3]
);
return;
}
DrawRect(render, 0, r.X, r.Y, _margin.Left, _margin.Top, col);
DrawRect(render, 1, r.X + _margin.Left, r.Y, r.Width - _margin.Left - _margin.Right, _margin.Top, col);
DrawRect(render, 2, r.X + r.Width - _margin.Right, r.Y, _margin.Right, _margin.Top, col);
DrawRect(render, 3, r.X, r.Y + _margin.Top, _margin.Left, r.Height - _margin.Top - _margin.Bottom, col);
DrawRect(
render, 4, r.X + _margin.Left, r.Y + _margin.Top, r.Width - _margin.Left - _margin.Right,
r.Height - _margin.Top - _margin.Bottom, col
);
DrawRect(
render, 5, r.X + r.Width - _margin.Right, r.Y + _margin.Top, _margin.Right,
r.Height - _margin.Top - _margin.Bottom, col
);
DrawRect(render, 6, r.X, r.Y + r.Height - _margin.Bottom, _margin.Left, _margin.Bottom, col);
DrawRect(
render, 7, r.X + _margin.Left, r.Y + r.Height - _margin.Bottom, r.Width - _margin.Left - _margin.Right,
_margin.Bottom, col
);
DrawRect(
render, 8, r.X + r.Width - _margin.Right, r.Y + r.Height - _margin.Bottom, _margin.Right,
_margin.Bottom, col
);
}
public bool Equals(Bordered other)
{
return (_texture?.Equals(other._texture) ?? false) && _subRects.Equals(other._subRects) && _margin.Equals(other._margin) && _width.Equals(other._width) && _height.Equals(other._height);
}
public override bool Equals(object? obj)
{
return obj is Bordered other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(_texture, _subRects, _margin, _width, _height);
}
public static bool operator ==(Bordered lhs, Bordered rhs) => lhs.Equals(rhs);
public static bool operator !=(Bordered lhs, Bordered rhs) => !lhs.Equals(rhs);
}