-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cs
337 lines (300 loc) · 8.46 KB
/
Window.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.Collections.Generic;
using System.Numerics;
using Glfw3;
using SFML.Graphics;
namespace Tiler
{
public class KeyEventArgs : EventArgs
{
public Glfw.KeyCode Key;
public int ScanCode;
public Glfw.KeyMods Modifiers;
}
public class UnicodeInputEventArgs : EventArgs
{
public uint CodePoint;
public Glfw.KeyMods Modifiers;
}
public class MouseEventArgs : EventArgs
{
public double X;
public double Y;
}
public class MouseButtonEventArgs : EventArgs
{
public Glfw.MouseButton Button;
public Glfw.KeyMods Modifiers;
}
public class DroppedEventArgs : EventArgs
{
public string[] Paths;
}
public class WindowMovedEventArgs : EventArgs
{
public int X;
public int Y;
}
public class WindowResizedEventArgs : EventArgs
{
public int Width;
public int Height;
}
public class Window
{
private static Dictionary<Glfw.Window, Window> dictionary = new Dictionary<Glfw.Window, Window>();
#region event wrapper functions
private static void KeyCallback(Glfw.Window window, Glfw.KeyCode key, int scancode, Glfw.InputState state, Glfw.KeyMods mods)
{
var engineWindow = dictionary[window];
if (state == Glfw.InputState.Press || state == Glfw.InputState.Repeat)
{
engineWindow.KeyPressed?.Invoke(engineWindow, new KeyEventArgs()
{
Key = key,
ScanCode = scancode,
Modifiers = mods
});
}
else if (state == Glfw.InputState.Release)
{
engineWindow.KeyReleased?.Invoke(engineWindow, new KeyEventArgs()
{
Key = key,
ScanCode = scancode,
Modifiers = mods
});
}
}
private static void UnicodeCallback(Glfw.Window window, uint codepoint, Glfw.KeyMods mods)
{
var engineWindow = dictionary[window];
engineWindow.UnicodeInput?.Invoke(engineWindow, new UnicodeInputEventArgs()
{
CodePoint = codepoint,
Modifiers = mods
});
}
private static void MouseMoveCallback(Glfw.Window window, double xpos, double ypos)
{
var engineWindow = dictionary[window];
engineWindow.MouseMoved?.Invoke(engineWindow, new MouseEventArgs()
{
X = xpos,
Y = ypos
});
}
private static void MouseButtonCallback(Glfw.Window window, Glfw.MouseButton button, Glfw.InputState state, Glfw.KeyMods mods)
{
var engineWindow = dictionary[window];
if (state == Glfw.InputState.Press || state == Glfw.InputState.Repeat)
{
engineWindow.MousePressed?.Invoke(engineWindow, new MouseButtonEventArgs()
{
Button = button,
Modifiers = mods
});
}
else if (state == Glfw.InputState.Release)
{
engineWindow.MouseReleased?.Invoke(engineWindow, new MouseButtonEventArgs()
{
Button = button,
Modifiers = mods
});
}
}
private static void MouseScrollCallback(Glfw.Window window, double xpos, double ypos)
{
var engineWindow = dictionary[window];
engineWindow.MouseScrolled?.Invoke(engineWindow, new MouseEventArgs()
{
X = xpos,
Y = ypos
});
}
private static void CursorEnterCallback(Glfw.Window window, bool entered)
{
var engineWindow = dictionary[window];
(entered ? engineWindow.MouseEntered : engineWindow.MouseExited)?.Invoke(engineWindow, EventArgs.Empty);
}
private static void DropCallback(Glfw.Window window, int count, string[] paths)
{
var engineWindow = dictionary[window];
engineWindow.Dropped?.Invoke(engineWindow, new DroppedEventArgs()
{
Paths = paths
});
}
private static void WindowCloseCallback(Glfw.Window window)
{
var engineWindow = dictionary[window];
engineWindow.Closed?.Invoke(engineWindow, EventArgs.Empty);
}
private static void WindowMoveCallback(Glfw.Window window, int xpos, int ypos)
{
var engineWindow = dictionary[window];
engineWindow.Moved?.Invoke(engineWindow, new WindowMovedEventArgs()
{
X = xpos,
Y = ypos
});
}
private static void WindowResizedCallback(Glfw.Window window, int width, int height)
{
var engineWindow = dictionary[window];
/*
{
var view = new View(engineWindow.RenderWindow.GetView());
engineWindow.RenderWindow?.Dispose();
engineWindow.RenderWindow = new RenderWindow(engineWindow.NativeHandle);
engineWindow.RenderWindow.SetView(view);
}
*/
engineWindow.Resized?.Invoke(engineWindow, new WindowResizedEventArgs()
{
Width = width,
Height = height
});
}
#endregion
private Window CurrentWindow = null;
private string _title;
#region events
public event EventHandler<KeyEventArgs> KeyPressed;
public event EventHandler<KeyEventArgs> KeyReleased;
public event EventHandler<UnicodeInputEventArgs> UnicodeInput;
public event EventHandler<MouseEventArgs> MouseMoved;
public event EventHandler<MouseButtonEventArgs> MousePressed;
public event EventHandler<MouseButtonEventArgs> MouseReleased;
public event EventHandler<MouseEventArgs> MouseScrolled;
public event EventHandler MouseEntered;
public event EventHandler MouseExited;
public event EventHandler<DroppedEventArgs> Dropped;
public event EventHandler Closed;
public event EventHandler<WindowMovedEventArgs> Moved;
public event EventHandler<WindowResizedEventArgs> Resized;
#endregion
#region public api
public Glfw.Window GlfwWindow { get; private set; }
public RenderWindow RenderWindow { get; private set; }
public bool IsOpen { get => !Glfw.WindowShouldClose(GlfwWindow); }
public Vector2 Size
{
get
{
Glfw.GetWindowSize(GlfwWindow, out var x, out var y);
return new Vector2(x, y);
}
set
{
Glfw.SetWindowSize(GlfwWindow, (int)value.X, (int)value.Y);
}
}
public Vector2 Position
{
get
{
Glfw.GetWindowPos(GlfwWindow, out var x, out var y);
return new Vector2(x, y);
}
set
{
Glfw.SetWindowPos(GlfwWindow, (int)value.X, (int)value.Y);
}
}
public string Title
{
get => _title;
set
{
_title = value;
Glfw.SetWindowTitle(GlfwWindow, _title);
}
}
public IntPtr NativeHandle { get => Glfw.glfwGetWin32Window(GlfwWindow.Ptr); }
public Window(int width, int height, string title)
{
// @todo style flags
// @todo fullscreen support
// @todo monitor support
Glfw.DefaultWindowHints();
// context hints
Glfw.WindowHint(Glfw.Hint.ClientApi, Glfw.ClientApi.None);
/*
Glfw.WindowHint(Glfw.Hint.ClientApi, Glfw.ClientApi.OpenGL);
Glfw.WindowHint(Glfw.Hint.ContextVersionMajor, 4);
Glfw.WindowHint(Glfw.Hint.ContextVersionMinor, 5);
Glfw.WindowHint(Glfw.Hint.OpenglForwardCompat, true);
Glfw.WindowHint(Glfw.Hint.OpenglProfile, Glfw.OpenGLProfile.Core);
*/
Glfw.WindowHint(Glfw.Hint.Resizable, false);
_title = title;
GlfwWindow = Glfw.CreateWindow(width, height, title);
if (!GlfwWindow)
throw new Exception("Failed to create GLFW window");
RenderWindow = new RenderWindow(NativeHandle);
dictionary.Add(GlfwWindow, this);
Glfw.SetKeyCallback(GlfwWindow, Utils.Pin<Glfw.KeyFunc>(KeyCallback));
Glfw.SetCharModsCallback(GlfwWindow, Utils.Pin<Glfw.CharModsFunc>(UnicodeCallback));
Glfw.SetCursorPosCallback(GlfwWindow, Utils.Pin<Glfw.CursorPosFunc>(MouseMoveCallback));
Glfw.SetMouseButtonCallback(GlfwWindow, Utils.Pin<Glfw.MouseButtonFunc>(MouseButtonCallback));
Glfw.SetScrollCallback(GlfwWindow, Utils.Pin<Glfw.CursorPosFunc>(MouseScrollCallback));
Glfw.SetCursorEnterCallback(GlfwWindow, Utils.Pin<Glfw.CursorEnterFunc>(CursorEnterCallback));
Glfw.SetDropCallback(GlfwWindow, Utils.Pin<Glfw.DropFunc>(DropCallback));
Glfw.SetWindowCloseCallback(GlfwWindow, Utils.Pin<Glfw.WindowCloseFunc>(WindowCloseCallback));
Glfw.SetWindowPosCallback(GlfwWindow, Utils.Pin<Glfw.WindowPosFunc>(WindowMoveCallback));
Glfw.SetWindowSizeCallback(GlfwWindow, Utils.Pin<Glfw.WindowSizeFunc>(WindowResizedCallback));
}
~Window()
{
dictionary.Remove(GlfwWindow);
Glfw.DestroyWindow(GlfwWindow);
}
public void Activate()
{
if (CurrentWindow != this)
{
Glfw.MakeContextCurrent(GlfwWindow);
CurrentWindow = this;
}
}
public void Minimize()
{
Glfw.IconifyWindow(GlfwWindow);
}
public void Restore()
{
Glfw.RestoreWindow(GlfwWindow);
}
public void Maximize()
{
Glfw.MaximizeWindow(GlfwWindow);
}
public void Show()
{
Glfw.ShowWindow(GlfwWindow);
}
public void Hide()
{
Glfw.HideWindow(GlfwWindow);
}
public void Close()
{
Glfw.SetWindowShouldClose(GlfwWindow, true);
}
public void Clear()
{
RenderWindow.Clear();
}
public void Draw(Drawable drawable)
{
RenderWindow.Draw(drawable);
}
public void Display()
{
RenderWindow.Display();
}
#endregion
}
}