-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameControl.cs
235 lines (195 loc) · 6.91 KB
/
GameControl.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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Input;
using OpenTK.Platform;
using KeyPressEventArgs = OpenTK.KeyPressEventArgs;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
namespace engenious.WinForms
{
public class GameControl : UserControl, IRenderingSurface
{
private readonly Thread _updateThread;
/// <inheritdoc />
public GameControl()
{
CursorVisible = true;
UpdateFrequency = 0.0;
SetStyle(ControlStyles.UserPaint, true);
Application.Idle += Application_Idle;
}
public double UpdateFrequency { get; set; }
private void Application_Idle(object sender, EventArgs e)
{
var elapsed = _stopwatch.ElapsedMilliseconds / 1000.0;
if (elapsed <= UpdateFrequency)
return;
_stopwatch.Restart();
// TODO: better render handling
_updateFrame?.Invoke(this, new FrameEventArgs(elapsed));
_renderFrame?.Invoke(this, new FrameEventArgs(elapsed));
}
private Stopwatch _stopwatch = new Stopwatch();
/// <inheritdoc />
public Point PointToScreen(Point pt) => base.PointToScreen(new System.Drawing.Point(pt.X, pt.Y));
/// <inheritdoc />
public Point PointToClient(Point pt) => base.PointToClient(new System.Drawing.Point(pt.X, pt.Y));
/// <inheritdoc />
public new Rectangle ClientRectangle
{
get => new Rectangle(base.ClientRectangle.X,base.ClientRectangle.Y,base.ClientRectangle.Width,base.ClientRectangle.Height);
set
{
var diffX = Location.X - base.ClientRectangle.X;
var diffY = Location.Y - base.ClientRectangle.Y;
Location = new System.Drawing.Point(value.X + diffX, value.Y + diffY);
ClientSize = new System.Drawing.Size(value.Width, value.Height);
}
}
/// <inheritdoc />
public new Size ClientSize
{
get => new Size(base.ClientSize.Width, base.ClientSize.Height);
set
{
base.ClientSize = new System.Drawing.Size(value.Width,value.Height);
}
}
private bool _cursorVisible;
/// <inheritdoc />
public bool CursorVisible
{
get => _cursorVisible;
set
{
_cursorVisible = value;
if (value)
Cursor.Show();
else
Cursor.Hide();
}
}
public IWindowInfo WindowInfo { get; internal set; }
private event EventHandler<FrameEventArgs> _renderFrame;
event EventHandler<FrameEventArgs> IControlInternals.RenderFrame
{
add => _renderFrame += value;
remove => _renderFrame -= value;
}
private event EventHandler<FrameEventArgs> _updateFrame;
event EventHandler<FrameEventArgs> IControlInternals.UpdateFrame
{
add => _updateFrame += value;
remove => _updateFrame -= value;
}
private double _lastElapsed = Environment.TickCount;
/// <inheritdoc />
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
/// <inheritdoc />
protected override void OnPaint(PaintEventArgs e)
{
}
private Control GetParent(Control current)
{
while (true)
{
if (current == null) return null;
if (current is Form) return current;
current = current.Parent;
}
}
private Form ParentWindow
{
get => (Form)GetParent(this);
}
private event EventHandler<CancelEventArgs> _closing;
event EventHandler<CancelEventArgs> IControlInternals.Closing
{
add => _closing += value;
remove => _closing -= value;
}
private void ParentWindowOnClosing(object sender, CancelEventArgs e)
{
_closing?.Invoke(this, e);
}
private event EventHandler<EventArgs> _focusChanged;
event EventHandler<EventArgs> IControlInternals.FocusedChanged
{
add => _focusChanged += value;
remove => _focusChanged -= value;
}
/// <inheritdoc />
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
_focusChanged?.Invoke(this, e);
}
/// <inheritdoc />
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
_focusChanged?.Invoke(this, e);
}
private event EventHandler<EventArgs> _resize;
event EventHandler<EventArgs> IControlInternals.Resize
{
add => _resize += value;
remove => _resize -= value;
}
/// <inheritdoc />
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
_resize?.Invoke(this, EventArgs.Empty);
Invalidate();
}
private event EventHandler<EventArgs> _load;
event EventHandler<EventArgs> IControlInternals.Load
{
add => _load += value;
remove => _load -= value;
}
/// <inheritdoc />
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (ParentWindow != null)
ParentWindow.Closing += ParentWindowOnClosing;
else
HandleDestroyed += (sender, args) => ParentWindowOnClosing(this, new CancelEventArgs(false));
_load?.Invoke(this, EventArgs.Empty);
_stopwatch.Start();
}
private event EventHandler<KeyPressEventArgs> _keyPress;
event EventHandler<KeyPressEventArgs> IControlInternals.KeyPress
{
add => _keyPress += value;
remove => _keyPress-= value;
}
/// <inheritdoc />
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
_keyPress?.Invoke(this, new KeyPressEventArgs(e.KeyChar));
}
private event EventHandler<MouseWheelEventArgs> _mouseWheel;
event EventHandler<MouseWheelEventArgs> IControlInternals.MouseWheel
{
add => _mouseWheel += value;
remove => _mouseWheel -= value;
}
private int _wheelPos;
/// <inheritdoc />
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
_wheelPos += e.Delta;
_mouseWheel?.Invoke(this, new MouseWheelEventArgs(e.X, e.Y, _wheelPos, e.Delta));
}
}
}