This repository was archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathWindow.cs
More file actions
354 lines (301 loc) · 10.3 KB
/
Copy pathWindow.cs
File metadata and controls
354 lines (301 loc) · 10.3 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Numerics;
namespace Foster.Framework;
/// <summary>
/// A Window.
///
/// Screen Coordinates may be different on each platform.
/// For example, on Windows High DPI displays, this is always 1-1 with
/// the pixel size of the Window. On MacOS Retina displays, this is
/// usually 1-2 with the pixel size of the Window.
///
/// The Window is only able to be Rendered to during is OnRender callback. Attempting
/// to render to the Window outside of that will throw an exception.
/// </summary>
public sealed class Window : RenderTarget
{
public abstract class Platform
{
protected internal abstract IntPtr Pointer { get; }
protected internal abstract Point2 Position { get; set; }
protected internal abstract Point2 Size { get; set; }
protected internal abstract Point2 RenderSize { get; }
protected internal abstract Vector2 ContentScale { get; }
protected internal abstract bool Opened { get; }
protected internal abstract string Title { get; set; }
protected internal abstract bool Bordered { get; set; }
protected internal abstract bool Resizable { get; set; }
protected internal abstract bool Fullscreen { get; set; }
protected internal abstract bool Visible { get; set; }
protected internal abstract bool VSync { get; set; }
protected internal abstract bool Focused { get; }
protected internal abstract Vector2 Mouse { get; }
protected internal abstract Vector2 ScreenMouse { get; }
protected internal abstract bool MouseOver { get; }
protected internal abstract void Focus();
protected internal abstract void Present();
protected internal abstract void Close();
protected internal Action? OnFocus;
protected internal Action? OnResize;
protected internal Action? OnClose;
protected internal Action? OnCloseRequested;
}
/// <summary>
/// A reference to the internal platform implementation of the Window
/// </summary>
public readonly Platform Implementation;
/// <summary>
/// A pointer to the underlying OS Window
/// </summary>
public IntPtr NativePointer => Implementation.Pointer;
/// <summary>
/// Position of the Window, in Screen coordinates. Setting the Position will toggle off Fullscreen.
/// </summary>
public Point2 Position
{
get => Implementation.Position;
set
{
if (Implementation.Fullscreen)
Implementation.Fullscreen = false;
Implementation.Position = value;
}
}
/// <summary>
/// The size of the Window, in Screen coordinates. Setting the Size will toggle off Fullscreen.
/// </summary>
public Point2 Size
{
get => Implementation.Size;
set
{
if (Implementation.Fullscreen)
Implementation.Fullscreen = false;
Implementation.Size = value;
}
}
/// <summary>
/// The X position of the Window, in Screen coordinates. Setting the Position will toggle off Fullscreen.
/// </summary>
public int X
{
get => Position.X;
set => Position = new Point2(value, Y);
}
/// <summary>
/// The X position of the Window, in Screen coordinates. Setting the Position will toggle off Fullscreen.
/// </summary>
public int Y
{
get => Position.Y;
set => Position = new Point2(X, value);
}
/// <summary>
/// The Width of the Window, in Screen coordinates. Setting the Width will toggle off Fullscreen.
/// </summary>
public int Width
{
get => Size.X;
set => Size = new Point2(value, Size.Y);
}
/// <summary>
/// The Height of the Window, in Screen coordinates. Setting the Height will toggle off Fullscreen.
/// </summary>
public int Height
{
get => Size.Y;
set => Size = new Point2(Size.X, value);
}
/// <summary>
/// The Window bounds, in Screen coordinates. Setting the Bounds will toggle off Fullscreen.
/// </summary>
public RectInt Bounds
{
get
{
var position = Position;
var size = Size;
return new RectInt(position.X, position.Y, size.X, size.Y);
}
set
{
Size = value.Size;
Position = value.Position;
}
}
/// <summary>
/// The Render Size of the Window, in Pixels
/// </summary>
public Point2 RenderSize => Implementation.RenderSize;
/// <summary>
/// The Render Width of the Window, in Pixels
/// </summary>
public override int RenderWidth => Implementation.RenderSize.X;
/// <summary>
/// The Render Height of the Window, in Pixels
/// </summary>
public override int RenderHeight => Implementation.RenderSize.Y;
/// <summary>
/// The drawable bounds of the Window, in Pixels
/// </summary>
public RectInt RenderBounds => new RectInt(0, 0, Implementation.RenderSize.X, Implementation.RenderSize.Y);
/// <summary>
/// The scale of the Render size compared to the Window size
/// On Windows and Linux this is always 1.
/// On MacOS Retina displays this is 2.
/// </summary>
public Vector2 RenderScale => new Vector2(Implementation.RenderSize.X / (float)Width, Implementation.RenderSize.Y / (float)Height);
/// <summary>
/// The Content Scale of the Window
/// On High DPI displays this may be larger than 1
/// Use this to appropriately scale UI
/// </summary>
public Vector2 ContentScale => Implementation.ContentScale;
/// <summary>
/// A callback when the Window is redrawn
/// </summary>
public Action<Window>? OnRender;
/// <summary>
/// A callback when the Window is resized by the user
/// </summary>
public Action<Window>? OnResize;
/// <summary>
/// A callback when the Window is focused
/// </summary>
public Action<Window>? OnFocus;
/// <summary>
/// A callback when the Window is about to close
/// </summary>
public Action<Window>? OnClose;
/// <summary>
/// A callback when the Window has been requested to be closed (ex. by pressing the Close menu button).
/// By default this calls Window.Close()
/// </summary>
public Action<Window>? OnCloseRequested;
/// <summary>
/// Gets or Sets the Title of this Window
/// </summary>
public string Title
{
get => Implementation.Title;
set => Implementation.Title = value;
}
/// <summary>
/// Gets if the Window is currently Open
/// </summary>
public bool Opened => Implementation.Opened;
/// <summary>
/// Gets or Sets whether the Window has a Border
/// </summary>
public bool Bordered
{
get => Implementation.Bordered;
set => Implementation.Bordered = value;
}
/// <summary>
/// Gets or Sets whether the Window is resizable by the user
/// </summary>
public bool Resizable
{
get => Implementation.Resizable;
set => Implementation.Resizable = value;
}
/// <summary>
/// Gets or Sets whether the Window is in Fullscreen Mode
/// </summary>
public bool Fullscreen
{
get => Implementation.Fullscreen;
set => Implementation.Fullscreen = value;
}
/// <summary>
/// Gets or Sets whether the Window is Visible to the user
/// </summary>
public bool Visible
{
get => Implementation.Visible;
set => Implementation.Visible = value;
}
/// <summary>
/// Gets or Sets whether the Window synchronizes the vertical redraw
/// </summary>
public bool VSync
{
get => Implementation.VSync;
set => Implementation.VSync = value;
}
/// <summary>
/// Whether this is the currently focused Window
/// </summary>
public bool Focused => Implementation.Focused;
/// <summary>
/// The Mouse position relative to the top-left of the Window, in Screen coordinates
/// </summary>
public Vector2 Mouse => Implementation.Mouse;
/// <summary>
/// The position of the Mouse in pixels, relative to the top-left of the Window
/// </summary>
public Vector2 RenderMouse => Implementation.Mouse * RenderScale;
/// <summary>
/// The position of the mouse relative to the top-left of the Screen, in Screen coordinates
/// </summary>
public Vector2 ScreenMouse => Implementation.ScreenMouse;
/// <summary>
/// Whether the mouse is currently over this Window
/// </summary>
public bool MouseOver => Implementation.MouseOver;
public Window(string title, int width, int height, WindowFlags flags = WindowFlags.ScaleToMonitor)
: this(App.System, title, width, height, flags)
{
}
public Window(System system, string title, int width, int height, WindowFlags flags = WindowFlags.ScaleToMonitor)
{
system.windows.Add(this);
// create implementation object
Implementation = system.CreateWindow(title, width, height, flags);
Implementation.OnFocus = () => OnFocus?.Invoke(this);
Implementation.OnResize = () => OnResize?.Invoke(this);
Implementation.OnClose = () =>
{
OnClose?.Invoke(this);
system.windows.Remove(this);
};
Implementation.OnCloseRequested = () => OnCloseRequested?.Invoke(this);
// default close request to... close the window!
OnCloseRequested = (window) => window.Close();
}
public void Focus()
{
Implementation.Focus();
}
/// <summary>
/// Renders the Window. Call Present afterwards to display the rendered contents
/// </summary>
internal void Render()
{
// The Window Target is only allowed to be rendered to during this call
// it greatly simplifies the various states for the Graphics Module
lock (this)
{
Renderable = true;
App.Modules.BeforeRenderWindow(this);
OnRender?.Invoke(this);
App.Modules.AfterRenderWindow(this);
Renderable = false;
}
}
/// <summary>
/// Presents the drawn contents of the Window
/// </summary>
internal void Present()
{
Implementation.Present();
}
/// <summary>
/// Closes the Window
/// </summary>
public void Close()
{
Implementation.Close();
}
}