Plasma Engine  2.0
Loading...
Searching...
No Matches
Window.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Math/Rect.h>
5#include <Foundation/Math/Size.h>
6#include <Foundation/Math/Vec2.h>
7#include <Foundation/Strings/String.h>
8#include <Foundation/Types/UniquePtr.h>
9
10class plOpenDdlWriter;
11class plOpenDdlReader;
13
14// Include the proper Input implementation to use
15#if PL_ENABLED(PL_SUPPORTS_GLFW)
16# include <Core/System/Implementation/glfw/InputDevice_glfw.h>
17#elif PL_ENABLED(PL_PLATFORM_WINDOWS_DESKTOP)
18# include <Core/System/Implementation/Win/InputDevice_win32.h>
19#elif PL_ENABLED(PL_PLATFORM_WINDOWS_UWP)
20# include <Core/System/Implementation/uwp/InputDevice_uwp.h>
21#elif PL_ENABLED(PL_PLATFORM_ANDROID)
22# include <Core/System/Implementation/android/InputDevice_android.h>
23#else
24# include <Core/System/Implementation/null/InputDevice_null.h>
25#endif
26
27// Currently the following scenarios are possible
28// - Windows native implementation, using HWND
29// - GLFW on windows, using GLFWWindow* internally and HWND to pass windows around
30// - GLFW / XCB on linux. Runtime uses GLFWWindow*. Editor uses xcb-window. Tagged union is passed around as window handle.
31
32#if PL_ENABLED(PL_SUPPORTS_GLFW)
33
34extern "C"
35{
36 typedef struct GLFWwindow GLFWwindow;
37}
38
39# if PL_ENABLED(PL_PLATFORM_WINDOWS_DESKTOP)
40# include <Foundation/Basics/Platform/Win/MinWindows.h>
41using plWindowHandle = plMinWindows::HWND;
42using plWindowInternalHandle = GLFWwindow*;
43# define INVALID_WINDOW_HANDLE_VALUE (plWindowHandle)(0)
44# define INVALID_INTERNAL_WINDOW_HANDLE_VALUE nullptr
45# elif PL_ENABLED(PL_PLATFORM_LINUX)
46
47extern "C"
48{
49 typedef struct xcb_connection_t xcb_connection_t;
50}
51
52struct plXcbWindowHandle
53{
54 xcb_connection_t* m_pConnection;
55 plUInt32 m_Window;
56};
57
58struct plWindowHandle
59{
60 enum class Type
61 {
62 Invalid = 0,
63 GLFW = 1, // Used by the runtime
64 XCB = 2 // Used by the editor
65 };
66
67 Type type;
68 union
69 {
70 GLFWwindow* glfwWindow;
71 plXcbWindowHandle xcbWindow;
72 };
73
74 bool operator==(plWindowHandle& rhs)
75 {
76 if (type != rhs.type)
77 return false;
78
79 if (type == Type::GLFW)
80 {
81 return glfwWindow == rhs.glfwWindow;
82 }
83 else
84 {
85 // We don't compare the connection because we only want to know if we reference the same window.
86 return xcbWindow.m_Window == rhs.xcbWindow.m_Window;
87 }
88 }
89};
90
91using plWindowInternalHandle = plWindowHandle;
92# define INVALID_WINDOW_HANDLE_VALUE \
93 plWindowHandle {}
94# else
95using plWindowHandle = GLFWwindow*;
96using plWindowInternalHandle = GLFWwindow*;
97# define INVALID_WINDOW_HANDLE_VALUE (GLFWwindow*)(0)
98# endif
99
100#elif PL_ENABLED(PL_PLATFORM_WINDOWS_DESKTOP)
101
102# include <Foundation/Basics/Platform/Win/MinWindows.h>
103using plWindowHandle = plMinWindows::HWND;
104using plWindowInternalHandle = plWindowHandle;
105# define INVALID_WINDOW_HANDLE_VALUE (plWindowHandle)(0)
106
107#elif PL_ENABLED(PL_PLATFORM_WINDOWS_UWP)
108
109using plWindowHandle = IUnknown*;
110using plWindowInternalHandle = plWindowHandle;
111# define INVALID_WINDOW_HANDLE_VALUE nullptr
112
113#elif PL_ENABLED(PL_PLATFORM_ANDROID)
114
115struct ANativeWindow;
116using plWindowHandle = ANativeWindow*;
117using plWindowInternalHandle = plWindowHandle;
118# define INVALID_WINDOW_HANDLE_VALUE nullptr
119
120#else
121
122using plWindowHandle = void*;
123using plWindowInternalHandle = plWindowHandle;
124# define INVALID_WINDOW_HANDLE_VALUE nullptr
125
126#endif
127
128#ifndef INVALID_INTERNAL_WINDOW_HANDLE_VALUE
129# define INVALID_INTERNAL_WINDOW_HANDLE_VALUE INVALID_WINDOW_HANDLE_VALUE
130#endif
131
133class PL_CORE_DLL plWindowBase
134{
135public:
136 virtual ~plWindowBase() = default;
137
138 virtual plSizeU32 GetClientAreaSize() const = 0;
139 virtual plWindowHandle GetNativeWindowHandle() const = 0;
140
145 virtual bool IsFullscreenWindow(bool bOnlyProperFullscreenMode = false) const = 0;
146
149 virtual bool IsVisible() const = 0;
150
151 virtual void ProcessWindowMessages() = 0;
152
153 virtual void AddReference() = 0;
154 virtual void RemoveReference() = 0;
155};
156
158struct PL_CORE_DLL plWindowMode
159{
160 using StorageType = plUInt8;
161
162 enum Enum
163 {
166 FullscreenBorderlessNativeResolution,
168 FullscreenFixedResolution,
170
171 Default = WindowFixedResolution
172 };
173
175 static constexpr bool IsFullscreen(Enum e) { return e == FullscreenBorderlessNativeResolution || e == FullscreenFixedResolution; }
176};
177
179struct PL_CORE_DLL plWindowCreationDesc
180{
188 plResult AdjustWindowSizeAndPosition();
189
191 void SaveToDDL(plOpenDdlWriter& ref_writer);
192
194 plResult SaveToDDL(plStringView sFile);
195
197 void LoadFromDDL(const plOpenDdlReaderElement* pParentElement);
198
200 plResult LoadFromDDL(plStringView sFile);
201
202
204 plString m_Title = "plEngine";
205
208
211 plInt8 m_iMonitor = -1;
212
214 plVec2I32 m_Position = plVec2I32(0x80000000, 0x80000000); // Magic number on windows that positions the window at a 'good default position'
215
217 plSizeU32 m_Resolution = plSizeU32(1280, 720);
218
221 plUInt8 m_uiWindowNumber = 0;
222
225 bool m_bClipMouseCursor = true;
226
229 bool m_bShowMouseCursor = false;
230
232 bool m_bSetForegroundOnInit = true;
233
235 bool m_bCenterWindowOnDisplay = true;
236};
237
243class PL_CORE_DLL plWindow : public plWindowBase
244{
245public:
250 plWindow();
251
253 virtual ~plWindow();
254
256 inline const plWindowCreationDesc& GetCreationDescription() const { return m_CreationDescription; }
257
259 virtual plSizeU32 GetClientAreaSize() const override { return m_CreationDescription.m_Resolution; }
260
262 virtual plWindowHandle GetNativeWindowHandle() const override;
263
267 virtual bool IsFullscreenWindow(bool bOnlyProperFullscreenMode = false) const override
268 {
269 if (bOnlyProperFullscreenMode)
270 return m_CreationDescription.m_WindowMode == plWindowMode::FullscreenFixedResolution;
271
272 return plWindowMode::IsFullscreen(m_CreationDescription.m_WindowMode);
273 }
274
275 virtual bool IsVisible() const override { return m_bVisible; }
276
277 virtual void AddReference() override { m_iReferenceCount.Increment(); }
278 virtual void RemoveReference() override { m_iReferenceCount.Decrement(); }
279
280
284 virtual void ProcessWindowMessages() override;
285
291 plResult Initialize();
292
301 plResult Initialize(const plWindowCreationDesc& creationDescription)
302 {
303 m_CreationDescription = creationDescription;
304 return Initialize();
305 }
306
308 inline bool IsInitialized() const { return m_bInitialized; }
309
311 plResult Destroy();
312
315 plResult Resize(const plSizeU32& newWindowSize);
316
322 virtual void OnResize(const plSizeU32& newWindowSize);
323
325 virtual void OnWindowMove(const plInt32 iNewPosX, const plInt32 iNewPosY) {}
326
328 virtual void OnFocus(bool bHasFocus) {}
329
331 virtual void OnVisibleChange(bool bVisible) { m_bVisible = bVisible; }
332
334 virtual void OnClickClose() {}
335
336
337#if PL_ENABLED(PL_PLATFORM_WINDOWS)
346 virtual void OnWindowMessage(plMinWindows::HWND hWnd, plMinWindows::UINT msg, plMinWindows::WPARAM wparam, plMinWindows::LPARAM lparam);
347
348#elif PL_ENABLED(PL_PLATFORM_OSX)
349
350#elif PL_ENABLED(PL_PLATFORM_LINUX)
351
352#elif PL_ENABLED(PL_PLATFORM_ANDROID)
353
354#else
355# error "Missing code for plWindow on this platform!"
356#endif
357
359 plStandardInputDevice* GetInputDevice() const { return m_pInputDevice.Borrow(); }
360
364 static plUInt8 GetNextUnusedWindowNumber();
365
366protected:
370
371private:
372 bool m_bInitialized = false;
373 bool m_bVisible = true;
374
376
377 mutable plWindowInternalHandle m_hWindowHandle = plWindowInternalHandle();
378
379#if PL_ENABLED(PL_SUPPORTS_GLFW)
380 static void IconifyCallback(GLFWwindow* window, int iconified);
381 static void SizeCallback(GLFWwindow* window, int width, int height);
382 static void PositionCallback(GLFWwindow* window, int xpos, int ypos);
383 static void CloseCallback(GLFWwindow* window);
384 static void FocusCallback(GLFWwindow* window, int focused);
385 static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
386 static void CharacterCallback(GLFWwindow* window, unsigned int codepoint);
387 static void CursorPositionCallback(GLFWwindow* window, double xpos, double ypos);
388 static void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
389 static void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
390#endif
391
393 static plUInt8 s_uiNextUnusedWindowNumber;
394 plAtomicInteger32 m_iReferenceCount = 0;
395};
Represents a single 'object' in a DDL document, e.g. either a custom type or a primitives list.
Definition OpenDdlReader.h:11
An OpenDDL reader parses an entire DDL document and creates an in-memory representation of the docume...
Definition OpenDdlReader.h:118
The base class for OpenDDL writers.
Definition OpenDdlWriter.h:13
Android standard input device.
Definition InputDevice_android.h:10
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
Base class of all window classes that have a client area and a native window handle.
Definition Window.h:134
virtual bool IsFullscreenWindow(bool bOnlyProperFullscreenMode=false) const =0
Whether the window is a fullscreen window or should be one - some platforms may enforce this via the ...
virtual bool IsVisible() const =0
Whether the window can potentially be seen by the user. Windows that are minimized or hidden are not ...
A simple abstraction for platform specific window creation.
Definition Window.h:244
virtual bool IsFullscreenWindow(bool bOnlyProperFullscreenMode=false) const override
Returns whether the window covers an entire monitor.
Definition Window.h:267
virtual void OnClickClose()
Called when the close button of the window is clicked. Does nothing by default.
Definition Window.h:334
const plWindowCreationDesc & GetCreationDescription() const
Returns the currently active description struct.
Definition Window.h:256
virtual void OnWindowMove(const plInt32 iNewPosX, const plInt32 iNewPosY)
Called when the window position is changed. Not possible on all OSes.
Definition Window.h:325
virtual plSizeU32 GetClientAreaSize() const override
Returns the size of the client area / ie. the window resolution.
Definition Window.h:259
plResult Initialize(const plWindowCreationDesc &creationDescription)
Creates a new platform specific window with the given settings.
Definition Window.h:301
virtual void OnVisibleChange(bool bVisible)
Called when the window gets focus or loses focus.
Definition Window.h:331
virtual void OnFocus(bool bHasFocus)
Called when the window gets focus or loses focus.
Definition Window.h:328
plWindowCreationDesc m_CreationDescription
Definition Window.h:369
virtual bool IsVisible() const override
Whether the window can potentially be seen by the user. Windows that are minimized or hidden are not ...
Definition Window.h:275
plStandardInputDevice * GetInputDevice() const
Returns the input device that is attached to this window and typically provides mouse / keyboard inpu...
Definition Window.h:359
bool IsInitialized() const
Gets if the window is up and running.
Definition Window.h:308
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Parameters for creating a window, such as position and resolution.
Definition Window.h:180
plEnum< plWindowMode > m_WindowMode
Defines how the window size is determined.
Definition Window.h:207
Determines how the position and resolution for a window are picked.
Definition Window.h:159
static constexpr bool IsFullscreen(Enum e)
Returns whether the window covers an entire monitor. This includes borderless windows and proper full...
Definition Window.h:175
Enum
Definition Window.h:163
@ WindowResizable
The resolution and size are what the user picked and will not be changed. Allows window resizing by t...
Definition Window.h:165
@ FullscreenFixedResolution
Definition Window.h:168
@ WindowFixedResolution
The resolution and size are what the user picked and will not be changed. The window will not be resi...
Definition Window.h:164