-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSurface.cs
152 lines (133 loc) · 4.28 KB
/
Surface.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
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>
/// A static 2D image that can be rendered with a render context without causing interference to future render calls.
/// </summary>
public abstract class Surface : IDisposable
{
/// <summary>
/// Renders the surface to an area on the given context.
/// </summary>
public abstract void Render(Rectangle Area, GUIRenderContext Context);
/// <summary>
/// Creates a control that displays this surface.
/// </summary>
public SurfaceControl CreateControl()
{
return new SurfaceControl(this);
}
/// <summary>
/// Called when the surface is disposed. The surface may not be used after this.
/// </summary>
public virtual void OnDispose()
{
}
/// <summary>
/// Creates a texture surface representation of this surface.
/// </summary>
public TextureSurface AsTexture(int Width, int Height)
{
return TextureSurface.Create(this, Width, Height);
}
public void Dispose()
{
this.OnDispose();
}
}
/// <summary>
/// A surface that renders a 3D scene.
/// </summary>
public abstract class Render3DSurface : Surface
{
/// <summary>
/// Sets up the projection matrix for the 3D scene.
/// </summary>
public abstract void SetupProjection(Point Viewsize);
/// <summary>
/// Renders the scene for the syrface. This may use calls outside of a GUI render context, as long as it
/// resets the GL state to how it was before the render.
/// </summary>
public abstract void RenderScene();
public sealed override void Render(Rectangle Area, GUIRenderContext Context)
{
Context.PushTranslate(Area.Location);
Context.Draw3D(this.SetupProjection, this.RenderScene, Area.Size);
Context.Pop();
}
}
/// <summary>
/// A surface with a fixed a size.
/// </summary>
public abstract class FixedSurface : Surface
{
public override void Render(Rectangle Area, GUIRenderContext Context)
{
Point tsize = Area.Size;
Point csize = this.Size;
if (tsize.X > csize.X || tsize.Y > csize.Y)
{
Context.PushClip(Area);
this.Render(Area.Location, Context);
Context.Pop();
}
else
{
this.Render(Area.Location, Context);
}
}
/// <summary>
/// Creates a resizable surface by aliging this fixed surface in the resizable area.
/// </summary>
public AlignSurface WithAlign(Align Horizontal, Align Vertical)
{
return new AlignSurface(this, Horizontal, Vertical);
}
/// <summary>
/// Creates a texture surface representation of this surface.
/// </summary>
public TextureSurface AsTexture()
{
return TextureSurface.Create(this);
}
/// <summary>
/// Gets the size of the surface.
/// </summary>
public abstract Point Size { get; }
/// <summary>
/// Renders the surface to a context with the given point at the upper-left corner of the rendered surface.
/// </summary>
public abstract void Render(Point Point, GUIRenderContext Context);
}
/// <summary>
/// A control that draws a resizable surface.
/// </summary>
public class SurfaceControl : Control
{
public SurfaceControl(Surface Surface)
{
this._Surface = Surface;
}
/// <summary>
/// Gets the surface drawn by the surface control.
/// </summary>
public Surface Surface
{
get
{
return this._Surface;
}
}
public override void Render(GUIRenderContext Context)
{
Context.DrawSurface(this._Surface, new Rectangle(this.Size));
}
private Surface _Surface;
}
}