-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSkin.cs
311 lines (273 loc) · 10.7 KB
/
Skin.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace OpenTKGUI
{
/// <summary>
/// An image containing many related, flexible, subimages that can be used to draw controls.
/// </summary>
public class Skin
{
public Skin(Bitmap Source)
{
this._Width = Source.Width;
this._Height = Source.Height;
int id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
GL.TexEnv(TextureEnvTarget.TextureEnv,
TextureEnvParameter.TextureEnvMode,
(float)TextureEnvMode.Modulate);
BitmapData bd = Source.LockBits(
new System.Drawing.Rectangle(0, 0, Source.Width, Source.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D,
0, PixelInternalFormat.Rgba,
Source.Width, Source.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bd.Scan0);
Source.UnlockBits(bd);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
this._Texture = id;
}
/// <summary>
/// Gets the default skin.
/// </summary>
public static Skin Default
{
get
{
if (_Default == null)
{
_Default = new Skin(Res.SkinDefault);
}
return _Default;
}
}
private static Skin _Default;
/// <summary>
/// Gets a skin surface for the given region in the skin. No stretching or resizing is applied to the surface.
/// </summary>
public SkinSurface GetSurface(SkinArea Rect)
{
List<SkinSurface.Stop> xstops = new List<SkinSurface.Stop>();
List<SkinSurface.Stop> ystops = new List<SkinSurface.Stop>();
xstops.Add(new SkinSurface.Stop(Rect.X, 0.0));
xstops.Add(new SkinSurface.Stop(Rect.X + Rect.Width, Rect.Width));
ystops.Add(new SkinSurface.Stop(Rect.Y, 0.0));
ystops.Add(new SkinSurface.Stop(Rect.Y + Rect.Height, Rect.Height));
return this.GetSurface(xstops, ystops);
}
/// <summary>
/// Gets a skin surface for the given region in the skin. The surface will be stretched at the midline to get the target size.
/// </summary>
public SkinSurface GetSurface(SkinArea Rect, Point TargetSize)
{
return this.GetSurface(Rect, Rect.Width / 2, Rect.Height / 2, TargetSize);
}
/// <summary>
/// Gets a surface that displays by stretching the given rectangle at its midline.
/// </summary>
public Surface GetStretchableSurface(SkinArea Rect)
{
return new _StretchableSurface(this, Rect, Rect.Width / 2, Rect.Height / 2);
}
/// <summary>
/// Gets a surface that displays by stretching on the defined lines (relative to the source rectangle).
/// </summary>
public Surface GetStretchableSurface(SkinArea Rect, int XStretchLine, int YStretchLine)
{
return new _StretchableSurface(this, Rect, XStretchLine, YStretchLine);
}
/// <summary>
/// Gets a skin surface for the given region in the skin. The surface will be stretched at the stretch lines to get the target size.
/// </summary>
public SkinSurface GetSurface(SkinArea Rect, int XStretchLine, int YStretchLine, Point TargetSize)
{
List<SkinSurface.Stop> xstops = new List<SkinSurface.Stop>();
List<SkinSurface.Stop> ystops = new List<SkinSurface.Stop>();
xstops.Add(new SkinSurface.Stop(Rect.X, 0.0));
if (TargetSize.X > Rect.Width)
{
xstops.Add(new SkinSurface.Stop(Rect.X + XStretchLine - 1, XStretchLine - 1));
xstops.Add(new SkinSurface.Stop(Rect.X + XStretchLine + 1, TargetSize.X + XStretchLine - Rect.Width + 1));
}
xstops.Add(new SkinSurface.Stop(Rect.X + Rect.Width, TargetSize.X));
ystops.Add(new SkinSurface.Stop(Rect.Y, 0.0));
if (TargetSize.Y > Rect.Height)
{
ystops.Add(new SkinSurface.Stop(Rect.Y + YStretchLine - 1, YStretchLine - 1));
ystops.Add(new SkinSurface.Stop(Rect.Y + YStretchLine + 1, TargetSize.Y + YStretchLine - Rect.Height + 1));
}
ystops.Add(new SkinSurface.Stop(Rect.Y + Rect.Height, TargetSize.Y));
return this.GetSurface(xstops, ystops);
}
/// <summary>
/// Gets a skin surface for the given region in the skin. Stretching and resizing is to be manually specified with stops. Lists
/// must be discarded after being supplied.
/// </summary>
public SkinSurface GetSurface(List<SkinSurface.Stop> XStops, List<SkinSurface.Stop> YStops)
{
return new SkinSurface(this, XStops, YStops);
}
/// <summary>
/// A surface from a skin that can be stretched.
/// </summary>
private class _StretchableSurface : Surface
{
public _StretchableSurface(Skin Skin, SkinArea Rect, int XStretchLine, int YStretchLine)
{
this.Skin = Skin;
this.Rect = Rect;
this.XStretchLine = XStretchLine;
this.YStretchLine = YStretchLine;
}
public override void Render(Rectangle Area, GUIRenderContext Context)
{
Context.DrawSurface(Skin.GetSurface(this.Rect, this.XStretchLine, this.YStretchLine, Area.Size), Area.Location);
}
public Skin Skin;
public SkinArea Rect;
public int XStretchLine;
public int YStretchLine;
}
/// <summary>
/// Gets the id for the texture of the skin.
/// </summary>
public int Texture
{
get
{
return this._Texture;
}
}
/// <summary>
/// Gets the width of the texture for the skin.
/// </summary>
public int Width
{
get
{
return this._Width;
}
}
/// <summary>
/// Gets the height of the texture for the skin.
/// </summary>
public int Height
{
get
{
return this._Height;
}
}
private int _Width;
private int _Height;
private int _Texture;
}
/// <summary>
/// A source area for a skin.
/// </summary>
public struct SkinArea
{
public SkinArea(int X, int Y, int Width, int Height)
{
this.X = X;
this.Y = Y;
this.Width = Width;
this.Height = Height;
}
public int X;
public int Y;
public int Width;
public int Height;
}
/// <summary>
/// A surface that renders part of a skin, with optional modifications.
/// </summary>
public class SkinSurface : FixedSurface
{
internal SkinSurface(Skin Skin, List<Stop> XStops, List<Stop> YStops)
{
_MultiplyTextureOffsets(XStops, 1.0 / (double)Skin.Width);
_MultiplyTextureOffsets(YStops, 1.0 / (double)Skin.Height);
this._Skin = Skin;
this._XStops = XStops;
this._YStops = YStops;
}
public override void Render(Point Offset, GUIRenderContext Context)
{
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, this._Skin.Texture);
GL.Color4(1.0, 1.0, 1.0, 1.0);
int xs = this._XStops.Count - 1;
int ys = this._YStops.Count - 1;
GL.Begin(BeginMode.Quads);
for (int tx = 0; tx < xs; tx++)
{
for (int ty = 0; ty < ys; ty++)
{
double x = this._XStops[tx].RenderOffset + Offset.X;
double y = this._YStops[ty].RenderOffset + Offset.Y;
double xx = this._XStops[tx + 1].RenderOffset + Offset.X;
double yy = this._YStops[ty + 1].RenderOffset + Offset.Y;
double u = this._XStops[tx].TextureOffset;
double v = this._YStops[ty].TextureOffset;
double uu = this._XStops[tx + 1].TextureOffset;
double vv = this._YStops[ty + 1].TextureOffset;
GL.TexCoord2(u, v); GL.Vertex2(x, y);
GL.TexCoord2(uu, v); GL.Vertex2(xx, y);
GL.TexCoord2(uu, vv); GL.Vertex2(xx, yy);
GL.TexCoord2(u, vv); GL.Vertex2(x, yy);
}
}
GL.End();
}
/// <summary>
/// Applies the texture offsets in a list of stops by applying an offset and a multipler.
/// </summary>
private static void _MultiplyTextureOffsets(List<Stop> Stops, double Multipler)
{
for (int t = 0; t < Stops.Count; t++)
{
Stop st = Stops[t];
Stops[t] = new Stop(st.TextureOffset * Multipler, st.RenderOffset);
}
}
/// <summary>
/// Represents a line on an axis that correlates an offset when rendered to an offset from the source texture. Using multiple stops can create
/// stretching effects.
/// </summary>
public struct Stop
{
public Stop(double TextureOffset, double RenderOffset)
{
this.TextureOffset = TextureOffset;
this.RenderOffset = RenderOffset;
}
/// <summary>
/// The offset of the stop from the source texture region.
/// </summary>
public double TextureOffset;
/// <summary>
/// The offset of the stop from the begining of the rendered surface.
/// </summary>
public double RenderOffset;
}
public override Point Size
{
get
{
return new Point(
this._XStops[this._XStops.Count - 1].RenderOffset,
this._YStops[this._XStops.Count - 1].RenderOffset);
}
}
private Skin _Skin;
private List<Stop> _XStops;
private List<Stop> _YStops;
}
}