forked from dzamkov/OpenTKGUI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUIRenderContext.cs
249 lines (225 loc) · 8.13 KB
/
GUIRenderContext.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
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace OpenTKGUI
{
/// <summary>
/// Context given to a control for render that prevents controls from interfering with each other by setting GL states directly.
/// </summary>
public class GUIRenderContext
{
public GUIRenderContext(Point ViewSize)
{
this._Effects = new Stack<_Effect>();
this._ViewSize = ViewSize;
}
/// <summary>
/// Sets up the render context on the current GL context.
/// </summary>
public void Setup()
{
GL.Scale(2.0, -2.0, 1.0);
GL.Translate(-0.5, -0.5, 0.0);
GL.Scale(1.0 / (double)this._ViewSize.X, 1.0 / (double)this._ViewSize.Y, 1.0);
GL.Enable(EnableCap.Blend);
GL.BlendEquation(BlendEquationMode.FuncAdd);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
}
/// <summary>
/// Draws a solid-colored rectangle at the specified location, with the specified color.
/// </summary>
public void DrawSolid(Color Color, Rectangle Rectangle)
{
GL.Disable(EnableCap.Texture2D);
GL.Color4(Color);
double x = Rectangle.Location.X;
double y = Rectangle.Location.Y;
double xx = x + Rectangle.Size.X;
double yy = y + Rectangle.Size.Y;
GL.Begin(BeginMode.Quads);
GL.Vertex2(x, y);
GL.Vertex2(xx, y);
GL.Vertex2(xx, yy);
GL.Vertex2(x, yy);
GL.End();
}
/// <summary>
/// Draws a textured rectangle at the specified location.
/// </summary>
/// <param name="Modulate">A color to multiply the texture by.</param>
public void DrawTexture(int Texture, Color Modulate, Rectangle Rectangle)
{
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, Texture);
GL.Color4(Modulate);
double x = Rectangle.Location.X;
double y = Rectangle.Location.Y;
double xx = x + Rectangle.Size.X;
double yy = y + Rectangle.Size.Y;
GL.Begin(BeginMode.Quads);
GL.TexCoord2(0f, 0f); GL.Vertex2(x, y);
GL.TexCoord2(1f, 0f); GL.Vertex2(xx, y);
GL.TexCoord2(1f, 1f); GL.Vertex2(xx, yy);
GL.TexCoord2(0f, 1f); GL.Vertex2(x, yy);
GL.End();
}
public void DrawTexture(int Texture, Rectangle Rectangle)
{
this.DrawTexture(Texture, Color.RGB(1.0, 1.0, 1.0), Rectangle);
}
/// <summary>
/// Draws a surface with the specified offset.
/// </summary>
public void DrawSurface(Surface Surface, Point Offset)
{
Surface.Render(Offset, this);
}
public void DrawSurface(Surface Surface)
{
Surface.Render(this);
}
/// <summary>
/// Draws text to the specified location.
/// </summary>
public void DrawText(Color Color, TextSample Sample, Point TopLeft)
{
Sample.Render(Color, TopLeft);
}
/// <summary>
/// Draws text to the specified region, overriding its layout parameters.
/// </summary>
public void DrawText(Color Color, TextSample Sample, Rectangle Region, TextAlign HorizontalAlign, TextAlign VerticalAlign)
{
double x = Region.Location.X;
double y = Region.Location.Y;
switch (HorizontalAlign)
{
case TextAlign.Center: x += Region.Size.X / 2.0 - Sample.Size.X / 2.0; break;
case TextAlign.Right: x += Region.Size.X - Sample.Size.X; break;
}
switch (VerticalAlign)
{
case TextAlign.Center: y += Region.Size.Y / 2.0 - Sample.Size.Y / 2.0; break;
case TextAlign.Bottom: y += Region.Size.Y - Sample.Size.Y; break;
}
this.DrawText(Color, Sample, new Point(x, y));
}
/// <summary>
/// Draws text to the specified region using the layout parameters it was supplied with.
/// </summary>
public void DrawText(Color Color, TextSample Sample, Rectangle Region)
{
this.DrawText(Color, Sample, Region, Sample.HorizontalAlign, Sample.VerticalAlign);
}
/// <summary>
/// Pushes an effect on the stack that will cause all rendering not the specified region defined by a rectangle to be
/// ignored. The rectangle is in the current coordinate space of the context.
/// </summary>
public void PushClip(Rectangle Clip)
{
if (this._TopTranslate != null)
{
Clip.Location += this._TopTranslate.Offset;
}
if (this._TopClip == null)
{
GL.Enable(EnableCap.ScissorTest);
}
else
{
Clip = Rectangle.Intersection(Clip, this._TopClip.Rectangle);
}
_ClipEffect ce = new _ClipEffect()
{
Previous = this._TopClip,
Rectangle = Clip
};
ce.Apply(this._ViewSize.Y);
this._Effects.Push(this._TopClip = ce);
}
/// <summary>
/// Pushes an effect that translates the coordinate space by the specified amount.
/// </summary>
public void PushTranslate(Point Offset)
{
GL.Translate(Offset.X, Offset.Y, 0.0);
if (this._TopTranslate != null)
{
Offset += this._TopTranslate.Offset;
}
_TranslateEffect te = new _TranslateEffect()
{
Offset = Offset,
Previous = this._TopTranslate
};
this._Effects.Push(this._TopTranslate = te);
}
/// <summary>
/// Undoes the most recent command/effect given to the context.
/// </summary>
public void Pop()
{
_Effect e = this._Effects.Pop();
// Remove clip effect
_ClipEffect ce = e as _ClipEffect;
if (ce != null)
{
this._TopClip = ce.Previous;
if (this._TopClip == null)
{
GL.Disable(EnableCap.ScissorTest);
}
else
{
this._TopClip.Apply(this._ViewSize.Y);
}
}
// Remove translate effect
_TranslateEffect te = e as _TranslateEffect;
if (te != null)
{
this._TopTranslate = te.Previous;
if (this._TopTranslate != null)
{
Point noffset = this._TopTranslate.Offset - te.Offset;
GL.Translate(noffset.X, noffset.Y, 0.0);
}
else
{
GL.Translate(-te.Offset.X, -te.Offset.Y, 0.0);
}
}
}
private class _Effect
{
}
private class _ClipEffect : _Effect
{
public Rectangle Rectangle;
public _ClipEffect Previous;
public void Apply(double ViewHeight)
{
if (this.Rectangle.Size.X < 0.0 || this.Rectangle.Size.Y < 0.0)
{
this.Rectangle.Size = new Point(0.0, 0.0);
}
GL.Scissor(
(int)this.Rectangle.Location.X,
(int)(ViewHeight - this.Rectangle.Location.Y - this.Rectangle.Size.Y),
(int)this.Rectangle.Size.X,
(int)this.Rectangle.Size.Y);
}
}
private class _TranslateEffect : _Effect
{
public Point Offset;
public _TranslateEffect Previous;
}
private Point _ViewSize;
private _TranslateEffect _TopTranslate;
private _ClipEffect _TopClip;
private Stack<_Effect> _Effects;
}
}