-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHostWindow.cs
179 lines (156 loc) · 4.7 KB
/
HostWindow.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
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace OpenTKGUI
{
/// <summary>
/// A window hosting a TKGUI Panel.
/// </summary>
public class HostWindow : GameWindow
{
public HostWindow(BuildControlHandler BuildControl, string Title)
: this(BuildControl, Title, 640, 480)
{
}
public HostWindow(BuildControlHandler BuildControl, string Title, int Width, int Height)
: this(Title, Width, Height)
{
this._Control = BuildControl();
}
public HostWindow(string Title, int Width, int Height)
: base(Width, Height, GraphicsMode.Default, Title, GameWindowFlags.Default)
{
this._KeyboardState = new WindowKeyboardState(this);
this._MouseState = new WindowMouseState(this);
}
/// <summary>
/// Gets or sets the control displayed by the host window. This can not be null during an update or render.
/// </summary>
public Control Control
{
get
{
return this._Control;
}
set
{
this._Control = value;
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.ClearColor(Color.RGB(1.0, 1.0, 1.0));
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GUIRenderContext rc = new GUIRenderContext(this.ViewSize);
rc.Setup();
this._Control.Render(rc);
this.SwapBuffers();
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
this._MouseState.Update();
this._Control.Update(new _GUIContext(this), e.Time);
this._KeyboardState.PostUpdate();
}
/// <summary>
/// The top-level gui context when using a host window.
/// </summary>
private sealed class _GUIContext : GUIContext
{
public _GUIContext(HostWindow Window)
{
this.Window = Window;
}
public override Control KeyboardFocus
{
get
{
return this.Window._KeyboardFocus;
}
set
{
this.Window._KeyboardFocus = value;
}
}
public override Control MouseFocus
{
get
{
return this.Window._MouseFocus;
}
set
{
this.Window._MouseFocus = value;
}
}
public override MouseState MouseState
{
get
{
return Window._MouseState;
}
}
public override KeyboardState KeyboardState
{
get
{
return Window._KeyboardState;
}
}
public HostWindow Window;
}
/// <summary>
/// Gets the size of the area rendered on this window.
/// </summary>
public Point ViewSize
{
get
{
return new Point((double)this.Width, (double)this.Height);
}
}
protected override void OnResize(EventArgs e)
{
if (this._Control != null)
{
GL.Viewport(0, 0, this.Width, this.Height);
this._Control.ForceResize(this.ViewSize);
}
}
/// <summary>
/// Gets the keyboard state for the window.
/// </summary>
public KeyboardState KeyboardState
{
get
{
return this._KeyboardState;
}
}
/// <summary>
/// Gets the mouse state for the window.
/// </summary>
public MouseState MouseState
{
get
{
return this._MouseState;
}
}
private WindowKeyboardState _KeyboardState;
private WindowMouseState _MouseState;
private Control _MouseFocus;
private Control _KeyboardFocus;
private Control _Control;
}
/// <summary>
/// A function that creates a control hierarchy when controls are needed. This allows the construction of controls to be delayed until
/// the graphics context is set up.
/// </summary>
public delegate Control BuildControlHandler();
}