Leviathan  0.8.0.0
Leviathan game engine
Window.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Common/ThreadSafe.h"
7 #include "Common/Types.h"
8 
9 #ifdef _WIN32
10 #include "WindowsInclude.h"
11 #endif
12 
13 #include "bsfCore/RenderAPI/BsRenderWindow.h"
14 
15 #include <atomic>
16 #include <memory>
17 
18 struct SDL_Window;
19 struct SDL_Keysym;
20 union SDL_Event;
21 struct SDL_Cursor;
22 struct SDL_Surface;
23 
24 namespace Leviathan {
25 
26 namespace GUI {
27 class Layer;
28 enum class INPUT_EVENT_TYPE : int;
29 } // namespace GUI
30 
34 class Window {
35  struct BSFResources;
36 
37 public:
39  DLLEXPORT Window(Graphics* windowcreater, AppDef* windowproperties);
40  DLLEXPORT ~Window();
41 
42 
44  DLLEXPORT void Tick(int mspassed);
45 
47  DLLEXPORT bool Render(int mspassed, int tick, int timeintick);
48 
50  DLLEXPORT void LinkObjects(std::shared_ptr<GameWorld> world);
51 
52  DLLEXPORT void UnlinkAll();
53 
55  DLLEXPORT bool SetMouseCapture(bool state);
56 
58  DLLEXPORT void BringToFront();
59 
60  // Input function //
61 
62  // graphics related //
63  // DLLEXPORT float GetViewportAspectRatio();
64  DLLEXPORT void SaveScreenShot(const std::string& filename);
65 
66  DLLEXPORT void OnResize(int width, int height);
67 
68  DLLEXPORT void OnFocusChange(bool focused);
69 
70 
73  DLLEXPORT void SetCustomInputController(std::shared_ptr<InputController> controller);
74 
75  // ------------------------------------ //
76  // Size properties and mouse handling
77  DLLEXPORT inline float GetAspectRatio() const
78  {
79  int32_t width, height;
80  GetSize(width, height);
81 
82  return (static_cast<float>(width)) / height;
83  }
84 
85  DLLEXPORT void GetSize(int32_t& width, int32_t& height) const;
86 
87  DLLEXPORT void GetPosition(int32_t& x, int32_t& y) const;
88 
89 
90  DLLEXPORT void SetHideCursor(bool toset);
91 #ifdef __linux
92  DLLEXPORT void SetX11Cursor(int cursor, int retrycount = 10);
93 #endif //__linux
94 #ifdef _WIN32
95  DLLEXPORT void SetWinCursor(HCURSOR cursor);
96 #endif //_WIN32
97 
99  DLLEXPORT void GetRelativeMouse(int& x, int& y) const;
100 
103  DLLEXPORT void GetUnclampedRelativeMouse(int& x, int& y) const;
104 
106  DLLEXPORT void GetNormalizedRelativeMouse(float& x, float& y) const;
107  DLLEXPORT void SetMouseToCenter();
108  DLLEXPORT bool IsMouseOutsideWindowClientArea() const;
109 
113  // DLLEXPORT Int2 TranslateClientPointToScreenPoint(const Int2 &point) const;
114 
115  // ------------------------------------ //
116  // Input handling
117 
121  DLLEXPORT void InputEnd();
122 
123  DLLEXPORT uint32_t GetSDLID() const;
124 
125 #ifdef _WIN32
126  DLLEXPORT HWND GetNativeHandle() const;
127 #else
128  DLLEXPORT unsigned long GetNativeHandle() const;
129 #endif //_WIN32
130 
131 #if defined(__linux)
132  DLLEXPORT unsigned long GetWindowXDisplay() const;
133 #endif
134 
138  {
139  return Focused;
140  }
141 
142 
143  // Key press callbacks
144  DLLEXPORT void InjectMouseMove(const SDL_Event& event);
146  DLLEXPORT void InjectMouseWheel(const SDL_Event& event);
147  DLLEXPORT void InjectMouseButtonDown(const SDL_Event& event);
148  DLLEXPORT void InjectMouseButtonUp(const SDL_Event& event);
150  DLLEXPORT void InjectCodePoint(const SDL_Event& event);
151  DLLEXPORT void InjectKeyDown(const SDL_Event& event);
152  DLLEXPORT void InjectKeyUp(const SDL_Event& event);
153 
154  // ------------------------------------ //
155  // Various random get functions
157  {
158  return WindowsGui.get();
159  }
161  {
162  return TertiaryReceiver.get();
163  }
164  DLLEXPORT inline bool GetVsync() const;
165 
166  DLLEXPORT inline const auto& GetBSFWindow() const
167  {
168  return BSFWindow;
169  }
170 
173  DLLEXPORT inline int GetWindowNumber() const
174  {
175  return WindowNumber;
176  }
177 
181  {
182  return OpenWindowCount;
183  }
184 
185  // ------------------------------------ //
186  // Input helpers
187 
189 
190 
191 protected:
195  DLLEXPORT void _StartGatherInput();
196 
197  void _CheckMouseVisibilityStates();
198 
204  bool DoGUILayerInputPass(
205  const SDL_Event& sdlevent, bool down, bool textinput, int mousex, int mousey);
206 
215  GUI::Layer* GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey);
216 
217 #ifdef __linux
218  DLLEXPORT void MakeX11CursorPermanent();
223 #endif //__linux
224 
225 private:
227  SDL_Window* SDLWindow = nullptr;
228 
229  bs::SPtr<bs::RenderWindow> BSFWindow;
230 
234  // boost::intrusive_ptr<GUI::View> inputreceiver = nullptr;
235 
238  // bool InputProcessedByCEF;
239 
240  // this is updated every time input is gathered //
241  int SpecialKeyModifiers = 0;
243  int CEFSpecialKeyModifiers = 0;
246  bool InputGatherStarted = false;
247 
250  bool InitialMousePositionSent = false;
251 
252  bool Focused = true;
253 
255  bool ApplicationWantCursorState;
258  bool ForceMouseVisible = false;
260  bool CursorState = true;
261 
262  bool MouseCaptured = false;
263 
265  std::unique_ptr<SDL_Cursor, void (*)(SDL_Cursor*)> CurrentCursor;
266  std::unique_ptr<SDL_Surface, void (*)(SDL_Surface*)> CurrentCursorImage;
267 
268 #ifdef __linux
269  int WantedX11Cursor = 0;
271 #endif //__linux
272 
273  std::shared_ptr<InputController> TertiaryReceiver;
274  std::unique_ptr<GUI::GuiManager> WindowsGui;
275 
277  int ID;
278 
280  // bool InputStarted = false;
281 
282  std::shared_ptr<GameWorld> LinkedWorld;
283 
285  static std::atomic<int> TotalCreatedWindows;
286  static std::atomic<int> OpenWindowCount;
287 
289  int WindowNumber;
290 
291  bool MouseCaptureState = false;
292 
294  std::unique_ptr<BSFResources> _BSFResources;
295 
298  bool DoingResize = false;
299 
301  static Window* InputCapturer;
302 };
303 } // namespace Leviathan
static DLLEXPORT int GetGlobalWindowCount()
Definition: Window.h:180
DLLEXPORT bool IsWindowFocused() const
Returns whether this window is focused.
Definition: Window.h:137
DLLEXPORT const auto & GetBSFWindow() const
Definition: Window.h:166
Manages registering multiple InputReceiver objects to a window to easily control where input is sent...
GUI controller for a Window.
Definition: GuiManager.h:20
Base class for WidgetLayer and View (browser containers / CEF) to add to a GuiManager.
Definition: GuiLayer.h:18
DLLEXPORT float GetAspectRatio() const
Definition: Window.h:77
DLLEXPORT int GetWindowNumber() const
Definition: Window.h:173
DLLEXPORT GUI::GuiManager * GetGui()
Definition: Window.h:156
#define DLLEXPORT
Definition: Include.h:84
DLLEXPORT InputController * GetInputController()
Definition: Window.h:160
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
unsigned int uint32_t
Definition: core.h:40