Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Window Class Reference

#include <Window.h>

Public Member Functions

DLLEXPORT Window (Graphics *windowcreater, AppDef *windowproperties)
 
DLLEXPORT ~Window ()
 
DLLEXPORT void Tick (int mspassed)
 Called by Engine. More...
 
DLLEXPORT bool Render (int mspassed, int tick, int timeintick)
 This function uses the LinkObjects function objects. More...
 
DLLEXPORT void LinkObjects (std::shared_ptr< GameWorld > world)
 This function also updates the camera aspect ratio. More...
 
DLLEXPORT void UnlinkAll ()
 
DLLEXPORT bool SetMouseCapture (bool state)
 
DLLEXPORT void BringToFront ()
 Tries to bring this window to front. More...
 
DLLEXPORT void SaveScreenShot (const std::string &filename)
 
DLLEXPORT void OnResize (int width, int height)
 
DLLEXPORT void OnFocusChange (bool focused)
 
DLLEXPORT void SetCustomInputController (std::shared_ptr< InputController > controller)
 Overwrites the default InputController with a custom one. More...
 
DLLEXPORT float GetAspectRatio () const
 
DLLEXPORT void GetSize (int32_t &width, int32_t &height) const
 
DLLEXPORT void GetPosition (int32_t &x, int32_t &y) const
 
DLLEXPORT void SetHideCursor (bool toset)
 
DLLEXPORT void GetRelativeMouse (int &x, int &y) const
 
DLLEXPORT void GetUnclampedRelativeMouse (int &x, int &y) const
 
DLLEXPORT void GetNormalizedRelativeMouse (float &x, float &y) const
 
DLLEXPORT void SetMouseToCenter ()
 
DLLEXPORT bool IsMouseOutsideWindowClientArea () const
 
DLLEXPORT void InputEnd ()
 Translates a client space coordinate to screen coordinate. More...
 
DLLEXPORT uint32_t GetSDLID () const
 
DLLEXPORT uint32_t GetNativeHandle () const
 
DLLEXPORT bool IsWindowFocused () const
 Returns whether this window is focused. More...
 
DLLEXPORT void InjectMouseMove (const SDL_Event &event)
 
DLLEXPORT void InjectMouseWheel (const SDL_Event &event)
 
DLLEXPORT void InjectMouseButtonDown (const SDL_Event &event)
 
DLLEXPORT void InjectMouseButtonUp (const SDL_Event &event)
 
DLLEXPORT void InjectCodePoint (const SDL_Event &event)
 Injects text. More...
 
DLLEXPORT void InjectKeyDown (const SDL_Event &event)
 
DLLEXPORT void InjectKeyUp (const SDL_Event &event)
 
DLLEXPORT GUI::GuiManagerGetGui ()
 
DLLEXPORT InputControllerGetInputController ()
 
DLLEXPORT bool GetVsync () const
 
DLLEXPORT Ogre::RenderWindow * GetOgreWindow () const
 
DLLEXPORT int GetWindowNumber () const
 

Static Public Member Functions

static DLLEXPORT int GetGlobalWindowCount ()
 

Protected Member Functions

DLLEXPORT void _StartGatherInput ()
 Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is called. More...
 
void _CheckMouseVisibilityStates ()
 
bool DoGUILayerInputPass (const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
 
GUI::LayerGetGUIEventReceiver (GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
 Retrieves the active gui object that is going to receive an event. More...
 

Detailed Description

window class

Todo:

Implement global lock for input handling

This should be handled through shared_ptr

Definition at line 40 of file Window.h.

Constructor & Destructor Documentation

◆ Window()

DLLEXPORT Leviathan::Window::Window ( Graphics windowcreater,
AppDef windowproperties 
)
Exceptions
InvalidArgumentif creation fails

Definition at line 49 of file Window.cpp.

49  :
50  CurrentCursor(nullptr, nullptr), CurrentCursorImage(nullptr, nullptr),
51  ID(IDFactory::GetID())
52 {
53  // Create window //
54 
55  const WindowDataDetails& WData = windowproperties->GetWindowDetails();
56 
57  // set some rendering specific parameters //
58  Ogre::NameValuePairList WParams;
59 
60  // variables //
61  Ogre::String fsaastr = Convert::ToString(WData.FSAA);
62 
63  // Context needs to be reused for multiple windows
64  if(OpenWindowCount > 0) {
65 
66  LOG_INFO("Window: using existing GLX context for creating");
67 
68  WParams["currentGLContext"] = "true";
69 
70  // Needs to be forced off to not cause issues like vsyncing each window separately and
71  // dropping to "monitor refresh rate / window count" fps
72  WParams["vsync"] = "false";
73 
74  } else {
75  WParams["vsync"] = WData.VSync ? "true" : "false";
76  }
77 
78  WParams["FSAA"] = fsaastr;
79  WParams["gamma"] = WData.UseGamma ? "true" : "false";
80 
81  Ogre::String wcaption = WData.Title;
82 
83  int extraFlags = 0;
84 
85  if(WData.FullScreen == "no" || WData.FullScreen == "0") {
86  } else if(WData.FullScreen == "fullscreendesktop") {
87  extraFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
88  } else if(WData.FullScreen == "fullscreenvideomode") {
89  extraFlags |= SDL_WINDOW_FULLSCREEN;
90  } else {
91 
92  LOG_ERROR("Window: invalid fullscreen value: " + WData.FullScreen);
93  }
94 
95  // TODO: On Apple's OS X you must set the NSHighResolutionCapable
96  // Info.plist property to YES, otherwise you will not receive a
97  // High DPI OpenGL
98  // canvas. https://wiki.libsdl.org/SDL_CreateWindow
99 
100  SDL_Window* sdlWindow = SDL_CreateWindow(WData.Title.c_str(),
101  SDL_WINDOWPOS_UNDEFINED_DISPLAY(WData.DisplayNumber),
102  SDL_WINDOWPOS_UNDEFINED_DISPLAY(WData.DisplayNumber), WData.Width, WData.Height,
103  // This seems to cause issues on Windows
104  // SDL_WINDOW_OPENGL |
105  SDL_WINDOW_RESIZABLE | extraFlags);
106 
107  // SDL_WINDOW_FULLSCREEN_DESKTOP works so much better than
108  // SDL_WINDOW_FULLSCREEN so it should be always used
109 
110  // SDL_WINDOW_BORDERLESS
111  // SDL_WINDOWPOS_UNDEFINED_DISPLAY(x)
112  // SDL_WINDOWPOS_CENTERED_DISPLAY(x)
113 
114  if(!sdlWindow) {
115 
116  LOG_FATAL("SDL Window creation failed, error: " + std::string(SDL_GetError()));
117  }
118 
119  // SDL_GLContext glContext = SDL_GL_CreateContext(sdlWindow);
120 
121  SDL_SysWMinfo wmInfo;
122  SDL_VERSION(&wmInfo.version);
123  if(!SDL_GetWindowWMInfo(sdlWindow, &wmInfo)) {
124 
125  LOG_FATAL("Window: created sdl window failed to retrieve info");
126  }
127 
128 #ifdef _WIN32
129  auto winHandle = reinterpret_cast<size_t>(wmInfo.info.win.window);
130  // WParams["parentWindowHandle"] = Ogre::StringConverter::toString(winHandle);
131  // This seems to be the right name on windows
132  WParams["externalWindowHandle"] = Ogre::StringConverter::toString(winHandle);
133  // externalWindowHandle
134 #else
135  WParams["parentWindowHandle"] =
136  Ogre::StringConverter::toString((unsigned long)wmInfo.info.x11.display) + ":" +
137  Ogre::StringConverter::toString(
138  (unsigned int)XDefaultScreen(wmInfo.info.x11.display)) +
139  ":" + Ogre::StringConverter::toString((unsigned long)wmInfo.info.x11.window);
140 
141 #endif
142 
143  Ogre::RenderWindow* tmpwindow;
144 
145  try {
146  tmpwindow = windowcreater->GetOgreRoot()->createRenderWindow(
147  wcaption, WData.Width, WData.Height, false, &WParams);
148  } catch(const Ogre::RenderingAPIException& e) {
149 
150  LOG_ERROR("Failed to create Ogre window, exception: " + std::string(e.what()));
151  throw;
152  }
153 
154  int windowsafter = ++OpenWindowCount;
155 
156  // Do some first window initialization //
157  if(windowsafter == 1) {
158 
159  // Notify engine to register threads to work with Ogre //
161 
162  // Hlms is needed to parse scripts etc.
163  windowcreater->_LoadOgreHLMS();
164 
166  }
167 
168  // Store this window's number
169  WindowNumber = ++TotalCreatedWindows;
170 
171  OWindow = tmpwindow;
172 
173 #ifdef _WIN32
174  // Fetch the windows handle from SDL //
175  HWND ourHWND = wmInfo.info.win.window;
176 
177  // apply style settings (mainly ICON) //
178  if(ourHWND) {
179 
180  WData.ApplyIconToHandle(ourHWND);
181 
182  } else {
183  LOG_WARNING("Window: failed to get window HWND for styling");
184  }
185 #else
186  // \todo linux icon
187 #endif
188  tmpwindow->setDeactivateOnFocusChange(false);
189 
190  // set the new window to be active //
191  tmpwindow->setActive(true);
192  Focused = true;
193 
194  // create GUI //
195  WindowsGui = std::make_unique<GUI::GuiManager>();
196  if(!WindowsGui) {
197  // TODO: the window must be destroyed here
198  DEBUG_BREAK;
199  throw NULLPtr("cannot create GUI manager instance");
200  }
201 
202  if(!WindowsGui->Init(windowcreater, this)) {
203 
204  LOG_ERROR("Window: Gui init failed");
205  throw NULLPtr("invalid GUI manager");
206  }
207 
208  // create receiver interface //
209  TertiaryReceiver = std::shared_ptr<InputController>(new InputController());
210 
211  SDLWindow = sdlWindow;
212 
213  // TODO: this needs to be only used when a text box etc. is used
214  // But that is quite hard to detect
215  SDL_StartTextInput();
216 
217  // cursor on top of window isn't hidden //
218  ApplicationWantCursorState = false;
219 }
void _NotifyThreadsRegisterOgre()
Definition: Engine.cpp:1235
#define LOG_INFO(x)
Definition: Define.h:82
#define LOG_ERROR(x)
Definition: Define.h:84
#define LOG_FATAL(x)
Definition: Define.h:86
static int GetID()
Definition: IDFactory.h:17
#define LOG_WARNING(x)
Definition: Define.h:83
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Engine * GetEngine()
Definition: Engine.cpp:79
static DLLEXPORT void RegisterOGREResourceGroups(bool testload=false)
Definition: FileSystem.cpp:937

◆ ~Window()

DLLEXPORT Leviathan::Window::~Window ( )

Definition at line 221 of file Window.cpp.

222 {
223  // GUI is very picky about delete order
224  if(WindowsGui) {
225  WindowsGui->Release();
226  WindowsGui.reset();
227  }
228 
229  if(MouseCaptured) {
230 
231  SDL_SetRelativeMouseMode(SDL_FALSE);
232  }
233 
234  // Un fullscreen to make sure nothing is screwed up
235  if(SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) {
236 
237  LOG_INFO("Window: unfullscreened before quit");
238  SDL_SetWindowFullscreen(SDLWindow, 0);
239  }
240 
241  // Report that the window is now closed //
242  Logger::Get()->Info(
243  "Window: closing window(" + Convert::ToString(GetWindowNumber()) + ")");
244 
245  // Close the window //
246  OWindow->destroy();
247 
248  TertiaryReceiver.reset();
249 
250  int windowsafter = --OpenWindowCount;
251 
252  if(windowsafter == 0) {
253 
254  Logger::Get()->Info("Window: all windows have been closed, "
255  "should quit soon");
256  }
257 
258  LOG_WRITE("TODO: check why calling SDL_DestroyWindow crashes in Ogre "
259  "GLX plugin uninstall");
260  // SDL_DestroyWindow(SDLWindow);
261  SDL_HideWindow(SDLWindow);
262  SDLWindow = nullptr;
263 }
#define LOG_INFO(x)
Definition: Define.h:82
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
#define LOG_WRITE(x)
Definition: Define.h:85
static std::string ToString(const T &val)
Definition: Convert.h:72
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
DLLEXPORT int GetWindowNumber() const
Definition: Window.h:172

Member Function Documentation

◆ _CheckMouseVisibilityStates()

void Leviathan::Window::_CheckMouseVisibilityStates ( )
protected

Definition at line 1049 of file Window.cpp.

1050 {
1051  const bool outsideArea = IsMouseOutsideWindowClientArea();
1052 
1053  // force cursor visible check (if outside client area or mouse is unfocused on the
1054  // window)
1055  if(outsideArea || !Focused) {
1056 
1057  ForceMouseVisible = true;
1058 
1059  } else {
1060 
1061  ForceMouseVisible = false;
1062  }
1063 
1064  // update cursor state //
1065  SetHideCursor(ApplicationWantCursorState);
1066 }
DLLEXPORT void SetHideCursor(bool toset)
Definition: Window.cpp:786
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:882

◆ _StartGatherInput()

DLLEXPORT void Leviathan::Window::_StartGatherInput ( )
protected

Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is called.

Todo:
Move to KeyMapping.cpp
Todo:
Fix mouse capture

Definition at line 971 of file Window.cpp.

972 {
973  // Quit if window closed //
974  if(!SDLWindow) {
975 
976  LOG_WARNING("Window: GatherInput: window is closed");
977  return;
978  }
979 
980  InputGatherStarted = true;
981 
983 
984  SpecialKeyModifiers = 0;
985  CEFSpecialKeyModifiers = EVENTFLAG_NONE;
986 
987  const auto mods = SDL_GetModState();
988 
989  if(mods & KMOD_CTRL) {
990  SpecialKeyModifiers |= KEYSPECIAL_CTRL;
991  CEFSpecialKeyModifiers |= EVENTFLAG_CONTROL_DOWN;
992  }
993  if(mods & KMOD_ALT) {
994  SpecialKeyModifiers |= KEYSPECIAL_ALT;
995  CEFSpecialKeyModifiers |= EVENTFLAG_ALT_DOWN;
996  }
997  if(mods & KMOD_SHIFT) {
998  SpecialKeyModifiers |= KEYSPECIAL_SHIFT;
999  CEFSpecialKeyModifiers |= EVENTFLAG_SHIFT_DOWN;
1000  }
1001  if(mods & KMOD_CAPS) {
1002  SpecialKeyModifiers |= KEYSPECIAL_CAPS;
1003  CEFSpecialKeyModifiers |= EVENTFLAG_CAPS_LOCK_ON;
1004  }
1005  if(mods & KMOD_GUI) {
1006  SpecialKeyModifiers |= KEYSPECIAL_SUPER;
1007  }
1008 
1009  // TODO: EVENTFLAG_NUM_LOCK_ON;
1010 
1011  // TODO: fix mouse capture
1012  // // Handle mouse capture
1013  // if(MouseCaptured && Focused) {
1014 
1015  // // get mouse relative to window center //
1016  // int xmoved = 0, ymoved = 0;
1017 
1018  // SDL_GetRelativeMouseState(&xmoved, &ymoved);
1019 
1020  // // Pass input //
1021  // GetInputController()->SendMouseMovement(xmoved, ymoved);
1022  // }
1023 }
#define LOG_WARNING(x)
Definition: Define.h:83
virtual DLLEXPORT void StartInputGather()
DLLEXPORT InputController * GetInputController()
Definition: Window.h:160

◆ BringToFront()

DLLEXPORT void Leviathan::Window::BringToFront ( )

Tries to bring this window to front.

Definition at line 350 of file Window.cpp.

351 {
352  if(SDLWindow) {
353  SDL_RaiseWindow(SDLWindow);
354  }
355 }

◆ DoGUILayerInputPass()

bool Leviathan::Window::DoGUILayerInputPass ( const SDL_Event &  sdlevent,
bool  down,
bool  textinput,
int  mousex,
int  mousey 
)
protected
Returns
True if the event was passed to the GUI

It is difficult to get the events back (from CEF) in a guaranteed time so we use GUI_INPUT_MODE to only send events to the GUI when they wouldn't be used by any player character controllers or similar

Definition at line 1068 of file Window.cpp.

1070 {
1071  // Find active gui view that wants the event
1072  auto* receiver = GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Keypress, mousex, mousey);
1073 
1074  // Don't pass to GUI
1075  if(!receiver)
1076  return false;
1077 
1078  return receiver->OnReceiveKeyInput(sdlevent, down, textinput, mousex, mousey,
1079  SpecialKeyModifiers, CEFSpecialKeyModifiers);
1080 }
GUI::Layer * GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
Retrieves the active gui object that is going to receive an event.
Definition: Window.cpp:961

◆ GetAspectRatio()

DLLEXPORT float Leviathan::Window::GetAspectRatio ( ) const
inline

Definition at line 81 of file Window.h.

82  {
83  int32_t width, height;
84  GetSize(width, height);
85 
86  return (static_cast<float>(width)) / height;
87  }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:899

◆ GetGlobalWindowCount()

static DLLEXPORT int Leviathan::Window::GetGlobalWindowCount ( )
inlinestatic

Returns how many windows are open

See also
OpenWindowCount

Definition at line 179 of file Window.h.

180  {
181  return OpenWindowCount;
182  }

◆ GetGui()

DLLEXPORT GUI::GuiManager* Leviathan::Window::GetGui ( )
inline

Definition at line 156 of file Window.h.

157  {
158  return WindowsGui.get();
159  }

◆ GetGUIEventReceiver()

GUI::Layer * Leviathan::Window::GetGUIEventReceiver ( GUI::INPUT_EVENT_TYPE  type,
int  mousex,
int  mousey 
)
protected

Retrieves the active gui object that is going to receive an event.

Parameters
typeIf Keypress then only the currently input receiving window (controlled by GUI_INPUT_MODE) is returned if there is one. For mouse events the gui object under the cursor position is returned (unless mouse capture is on then nothing is returned even if iskeypress is true) If Scroll then this is a mouse scroll event and a GUI::Layer that has a scrollable element is returned in every case except when the mouse is captured (or the Layer has None as the input mode)

Definition at line 961 of file Window.cpp.

962 {
963  // Don't pass to GUI if mouse capture is enabled
964  if(MouseCaptured)
965  return nullptr;
966 
967  auto layer = WindowsGui->GetTargetLayerForInput(type, mousex, mousey);
968  return layer;
969 }

◆ GetInputController()

DLLEXPORT InputController* Leviathan::Window::GetInputController ( )
inline

Definition at line 160 of file Window.h.

161  {
162  return TertiaryReceiver.get();
163  }

◆ GetNativeHandle()

DLLEXPORT uint32_t Leviathan::Window::GetNativeHandle ( ) const

Definition at line 920 of file Window.cpp.

922 {
923  SDL_SysWMinfo wmInfo;
924  SDL_VERSION(&wmInfo.version);
925  if(!SDL_GetWindowWMInfo(SDLWindow, &wmInfo)) {
926 
927  LOG_FATAL("Window: GetNativeHandle: failed to retrieve wm info");
928 #ifdef _WIN32
929  return 0;
930 #else
931  return -1;
932 #endif //_WIN32
933  }
934 #if defined(_WIN32)
935  return wmInfo.info.win.window;
936 #else
937 #if defined(__linux)
938  return wmInfo.info.x11.window;
939 #else
940 #error Fix this for mac
941 #endif
942 #endif
943 }
#define LOG_FATAL(x)
Definition: Define.h:86

◆ GetNormalizedRelativeMouse()

DLLEXPORT void Leviathan::Window::GetNormalizedRelativeMouse ( float &  x,
float &  y 
) const
Returns
The normalized mouse x and y positions (in range [0, 1])

Definition at line 863 of file Window.cpp.

864 {
865 
866  int xInt, yInt;
867  GetRelativeMouse(xInt, yInt);
868 
869  int32_t width, height;
870  GetSize(width, height);
871 
872  if(width == 0 || height == 0) {
873  x = 0.5f;
874  y = 0.5f;
875  return;
876  }
877 
878  x = static_cast<float>(xInt) / width;
879  y = static_cast<float>(yInt) / height;
880 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:899
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825

◆ GetOgreWindow()

DLLEXPORT Ogre::RenderWindow* Leviathan::Window::GetOgreWindow ( ) const
inline

Definition at line 165 of file Window.h.

166  {
167  return OWindow;
168  }

◆ GetPosition()

DLLEXPORT void Leviathan::Window::GetPosition ( int32_t &  x,
int32_t &  y 
) const

Definition at line 904 of file Window.cpp.

905 {
906  SDL_GetWindowPosition(SDLWindow, &x, &y);
907 }

◆ GetRelativeMouse()

DLLEXPORT void Leviathan::Window::GetRelativeMouse ( int &  x,
int &  y 
) const
Note
This is clamped to range [0, width / height]

Definition at line 825 of file Window.cpp.

826 {
827  if(!SDLWindow)
828  return;
829 
830  int globalX, globalY;
831  SDL_GetGlobalMouseState(&globalX, &globalY);
832 
833  int windowX, windowY;
834  SDL_GetWindowPosition(SDLWindow, &windowX, &windowY);
835 
836 
837  globalX -= windowX;
838  globalY -= windowY;
839 
840  int32_t width, height;
841  GetSize(width, height);
842 
843  x = std::clamp(globalX, 0, width);
844  y = std::clamp(globalY, 0, height);
845 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:899

◆ GetSDLID()

DLLEXPORT uint32_t Leviathan::Window::GetSDLID ( ) const

Definition at line 909 of file Window.cpp.

910 {
911  if(!SDLWindow)
912  return -1;
913 
914  return SDL_GetWindowID(SDLWindow);
915 }

◆ GetSize()

DLLEXPORT void Leviathan::Window::GetSize ( int32_t &  width,
int32_t &  height 
) const

Definition at line 899 of file Window.cpp.

900 {
901  SDL_GetWindowSize(SDLWindow, &width, &height);
902 }

◆ GetUnclampedRelativeMouse()

DLLEXPORT void Leviathan::Window::GetUnclampedRelativeMouse ( int &  x,
int &  y 
) const

Unclamped version of GetRelativeMouse can return negative coordinates if the mouse is left of this window

Definition at line 847 of file Window.cpp.

848 {
849  if(!SDLWindow)
850  return;
851 
852  int globalX, globalY;
853  SDL_GetGlobalMouseState(&globalX, &globalY);
854 
855  int windowX, windowY;
856  SDL_GetWindowPosition(SDLWindow, &windowX, &windowY);
857 
858  x = globalX - windowX;
859  y = globalY - windowY;
860 }

◆ GetVsync()

DLLEXPORT bool Leviathan::Window::GetVsync ( ) const
inline

Definition at line 951 of file Window.cpp.

952 {
953  return OWindow->isVSyncEnabled();
954 }

◆ GetWindowNumber()

DLLEXPORT int Leviathan::Window::GetWindowNumber ( ) const
inline

Returns this windows creation number

Note
This is quaranteed to be unique among all windows

Definition at line 172 of file Window.h.

173  {
174  return WindowNumber;
175  }

◆ InjectCodePoint()

DLLEXPORT void Leviathan::Window::InjectCodePoint ( const SDL_Event &  event)

Injects text.

Definition at line 1171 of file Window.cpp.

1172 {
1173  if(!InputGatherStarted)
1175 
1176  // Try to pass to CEF //
1177  int mouseX;
1178  int mouseY;
1179  GetRelativeMouse(mouseX, mouseY);
1180 
1181  if(!DoGUILayerInputPass(event, true, true, mouseX, mouseY)) {
1182 
1183  // GUI didn't want it
1184  }
1185 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1068
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825

◆ InjectKeyDown()

DLLEXPORT void Leviathan::Window::InjectKeyDown ( const SDL_Event &  event)

Definition at line 1187 of file Window.cpp.

1188 {
1189  if(!InputGatherStarted)
1191 
1192  bool SentToController = false;
1193 
1194  // Try to pass to CEF //
1195  int mouseX;
1196  int mouseY;
1197  GetRelativeMouse(mouseX, mouseY);
1198 
1199  const auto handled = DoGUILayerInputPass(event, true, false, mouseX, mouseY);
1200 
1201  if(!handled) {
1202 
1203  // CEF didn't want it
1204  // Then try disabling collections //
1205  // LOG_WRITE("TODO: check is a text box active");
1206  // if(!OwningWindow->GetGui()->ProcessKeyDown(sdlkey, SpecialKeyModifiers)) {
1207 
1208  // Finally send to a controller //
1209  SentToController = true;
1210  GetInputController()->OnInputGet(event.key.keysym.sym, SpecialKeyModifiers, true);
1211  // }
1212  }
1213 
1214  if(!SentToController) {
1215  GetInputController()->OnBlockedInput(event.key.keysym.sym, SpecialKeyModifiers, true);
1216  }
1217 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1068
virtual DLLEXPORT void OnBlockedInput(int32_t key, int specialmodifiers, bool down)
This is called when input is not received.
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825
virtual DLLEXPORT void OnInputGet(int32_t key, int specialmodifiers, bool down)
Called when the GUI didn&#39;t eat the whole keypress (it only ate half of the A-press) ...
DLLEXPORT InputController * GetInputController()
Definition: Window.h:160

◆ InjectKeyUp()

DLLEXPORT void Leviathan::Window::InjectKeyUp ( const SDL_Event &  event)

Definition at line 1219 of file Window.cpp.

1220 {
1221  if(!InputGatherStarted)
1223 
1224  // Send to CEF if GUI is active //
1225  int mouseX;
1226  int mouseY;
1227  GetRelativeMouse(mouseX, mouseY);
1228  DoGUILayerInputPass(event, false, false, mouseX, mouseY);
1229 
1230  // This should always be passed here //
1231  GetInputController()->OnInputGet(event.key.keysym.sym, SpecialKeyModifiers, false);
1232 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1068
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825
virtual DLLEXPORT void OnInputGet(int32_t key, int specialmodifiers, bool down)
Called when the GUI didn&#39;t eat the whole keypress (it only ate half of the A-press) ...
DLLEXPORT InputController * GetInputController()
Definition: Window.h:160

◆ InjectMouseButtonDown()

DLLEXPORT void Leviathan::Window::InjectMouseButtonDown ( const SDL_Event &  event)

Definition at line 1137 of file Window.cpp.

1138 {
1139  if(!InputGatherStarted)
1141 
1142  if(!MouseCaptured) {
1143 
1144  auto* receiver =
1145  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.button.x, event.button.y);
1146 
1147  if(receiver) {
1148 
1149  receiver->OnMouseButton(event, true);
1150  }
1151  }
1152 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
GUI::Layer * GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
Retrieves the active gui object that is going to receive an event.
Definition: Window.cpp:961

◆ InjectMouseButtonUp()

DLLEXPORT void Leviathan::Window::InjectMouseButtonUp ( const SDL_Event &  event)

Definition at line 1154 of file Window.cpp.

1155 {
1156  if(!InputGatherStarted)
1158 
1159  if(!MouseCaptured) {
1160 
1161  auto* receiver =
1162  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.button.x, event.button.y);
1163 
1164  if(receiver) {
1165 
1166  receiver->OnMouseButton(event, false);
1167  }
1168  }
1169 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
GUI::Layer * GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
Retrieves the active gui object that is going to receive an event.
Definition: Window.cpp:961

◆ InjectMouseMove()

DLLEXPORT void Leviathan::Window::InjectMouseMove ( const SDL_Event &  event)

Definition at line 1082 of file Window.cpp.

1083 {
1084  if(!InputGatherStarted)
1086 
1087  // Only pass this data if we aren't going to pass our own captured mouse //
1088  if(!MouseCaptured) {
1089 
1090  auto* receiver =
1091  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.motion.x, event.motion.y);
1092 
1093  if(receiver) {
1094  // TODO: IsMouseOutsideWindowClientArea needs to be
1095  // updated (for CEF GuiViews) once we support multiple
1096  // browsers in a single window
1097  receiver->OnMouseMove(event, IsMouseOutsideWindowClientArea());
1098  }
1099  }
1100 
1102 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
GUI::Layer * GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
Retrieves the active gui object that is going to receive an event.
Definition: Window.cpp:961
void _CheckMouseVisibilityStates()
Definition: Window.cpp:1049
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:882

◆ InjectMouseWheel()

DLLEXPORT void Leviathan::Window::InjectMouseWheel ( const SDL_Event &  event)
Todo:
allow configuring if mouse wheel is considered a key (for Gameplay input mode)

Definition at line 1104 of file Window.cpp.

1105 {
1106  if(!InputGatherStarted)
1108 
1109  if(!MouseCaptured) {
1110 
1111  int mouseX;
1112  int mouseY;
1113  GetRelativeMouse(mouseX, mouseY);
1114  // TODO: allow configuring if mouse wheel is considered a key
1115  auto* receiver = GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Scroll, mouseX, mouseY);
1116 
1117  if(receiver) {
1118 
1119  receiver->OnMouseWheel(event);
1120 
1121  } else {
1122 
1123  int x = event.wheel.x;
1124  int y = event.wheel.y;
1125 
1126  if(SDL_MOUSEWHEEL_FLIPPED == event.wheel.direction) {
1127  y *= -1;
1128  } else {
1129  x *= -1;
1130  }
1131 
1132  GetInputController()->OnScroll(x, y, SpecialKeyModifiers);
1133  }
1134  }
1135 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:971
GUI::Layer * GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE type, int mousex, int mousey)
Retrieves the active gui object that is going to receive an event.
Definition: Window.cpp:961
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825
DLLEXPORT InputController * GetInputController()
Definition: Window.h:160
virtual DLLEXPORT void OnScroll(int x, int y, int modifiers)

◆ InputEnd()

DLLEXPORT void Leviathan::Window::InputEnd ( )

Translates a client space coordinate to screen coordinate.

Exceptions
ExceptionNotFoundIf the window is not found (the internal translate fails)
Note
Doesn't work on linux, returns the input point Called by the engine event loop after key events are sent

This is used to reset modifier key states

Definition at line 1025 of file Window.cpp.

1026 {
1027  // Initial mouse position
1028  if(!InitialMousePositionSent) {
1030 
1031  InitialMousePositionSent = true;
1032 
1033  // Build a mouse move from the current position and send it
1034  int x;
1035  int y;
1036  GetRelativeMouse(x, y);
1037 
1038  SDL_Event event;
1039 
1040  event.motion.x = x;
1041  event.motion.y = y;
1042  InjectMouseMove(event);
1043  }
1044  }
1045 
1046  InputGatherStarted = false;
1047 }
DLLEXPORT void InjectMouseMove(const SDL_Event &event)
Definition: Window.cpp:1082
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:825
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:882

◆ IsMouseOutsideWindowClientArea()

DLLEXPORT bool Leviathan::Window::IsMouseOutsideWindowClientArea ( ) const

Definition at line 882 of file Window.cpp.

883 {
884  int X, Y;
886 
887  int32_t width, height;
888  GetSize(width, height);
889 
890  // check the coordinates //
891 
892  if(X < 0 || Y < 0 || X > width || Y > height) {
893  return true;
894  }
895 
896  return false;
897 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:899
DLLEXPORT void GetUnclampedRelativeMouse(int &x, int &y) const
Definition: Window.cpp:847

◆ IsWindowFocused()

DLLEXPORT bool Leviathan::Window::IsWindowFocused ( ) const
inline

Returns whether this window is focused.

Returns
True when the window has focus

Definition at line 137 of file Window.h.

138  {
139  return Focused;
140  }

◆ LinkObjects()

DLLEXPORT void Leviathan::Window::LinkObjects ( std::shared_ptr< GameWorld world)

This function also updates the camera aspect ratio.

Definition at line 265 of file Window.cpp.

266 {
267  if(LinkedWorld == world)
268  return;
269 
270  if(LinkedWorld) {
271 
272  LinkedWorld->OnUnLinkedFromWindow(this, Graphics::Get()->GetOgreRoot());
273  }
274 
275  LinkedWorld = world;
276 
277  if(LinkedWorld) {
278 
279  LinkedWorld->OnLinkToWindow(this, Graphics::Get()->GetOgreRoot());
280  }
281 }
static DLLEXPORT Graphics * Get()
Definition: Graphics.cpp:65

◆ OnFocusChange()

DLLEXPORT void Leviathan::Window::OnFocusChange ( bool  focused)

Definition at line 330 of file Window.cpp.

331 {
332  if(Focused == focused)
333  return;
334 
335  LOG_INFO("Focus change in Window");
336 
337  // Update mouse //
338  Focused = focused;
340 
341  WindowsGui->OnFocusChanged(focused);
342 
343  if(!Focused && MouseCaptured) {
344 
345  LOG_WRITE("TODO: We need to force GUI on to stop mouse capture");
346  LOG_FATAL("Not implemented unfocus when mouse capture is on");
347  }
348 }
#define LOG_INFO(x)
Definition: Define.h:82
#define LOG_FATAL(x)
Definition: Define.h:86
#define LOG_WRITE(x)
Definition: Define.h:85
void _CheckMouseVisibilityStates()
Definition: Window.cpp:1049

◆ OnResize()

DLLEXPORT void Leviathan::Window::OnResize ( int  width,
int  height 
)

Definition at line 317 of file Window.cpp.

318 {
319 // Notify Ogre //
320 // This causes issues on Windows
321 #ifdef __linux__
322  GetOgreWindow()->resize(width, height);
323 #endif
324  GetOgreWindow()->windowMovedOrResized();
325 
326  // send to GUI //
327  WindowsGui->OnResize();
328 }
DLLEXPORT Ogre::RenderWindow * GetOgreWindow() const
Definition: Window.h:165

◆ Render()

DLLEXPORT bool Leviathan::Window::Render ( int  mspassed,
int  tick,
int  timeintick 
)

This function uses the LinkObjects function objects.

Definition at line 294 of file Window.cpp.

295 {
296  if(LinkedWorld)
297  LinkedWorld->Render(mspassed, tick, timeintick);
298 
299  // Update GUI before each frame //
300  WindowsGui->Render();
301 
302  // update window //
303  Ogre::RenderWindow* tmpwindow = GetOgreWindow();
304 
305  // finish rendering the window //
306 
307  // We don't actually want to swap buffers here because the Engine
308  // method that called us will call Ogre::Root::renderOneFrame
309  // after Render has been called on all active windows, so if we
310  // have this call here we may do double swap depending on the
311  // drivers.
312  // tmpwindow->swapBuffers();
313 
314  return true;
315 }
DLLEXPORT Ogre::RenderWindow * GetOgreWindow() const
Definition: Window.h:165

◆ SaveScreenShot()

DLLEXPORT void Leviathan::Window::SaveScreenShot ( const std::string &  filename)

Definition at line 945 of file Window.cpp.

946 {
947  // uses render target's capability to save it's contents //
948  GetOgreWindow()->writeContentsToTimestampedFile(filename, "_window1.png");
949 }
DLLEXPORT Ogre::RenderWindow * GetOgreWindow() const
Definition: Window.h:165

◆ SetCustomInputController()

DLLEXPORT void Leviathan::Window::SetCustomInputController ( std::shared_ptr< InputController controller)

Overwrites the default InputController with a custom one.

Definition at line 956 of file Window.cpp.

957 {
958  TertiaryReceiver = controller;
959 }

◆ SetHideCursor()

DLLEXPORT void Leviathan::Window::SetHideCursor ( bool  toset)

Definition at line 786 of file Window.cpp.

787 {
788  ApplicationWantCursorState = toset;
789 
790  if(!ApplicationWantCursorState || ForceMouseVisible) {
791  // show cursor //
792  if(!CursorState) {
793  CursorState = true;
794  Logger::Get()->Info("Showing cursor");
795 
796  // Don't do anything if window is not valid anymore //
797  if(!SDLWindow)
798  return;
799 
800  SDL_ShowCursor(SDL_ENABLE);
801  }
802  } else {
803  // hide cursor //
804  if(CursorState) {
805 
806  CursorState = false;
807  Logger::Get()->Info("Hiding cursor");
808 
809  // Don't do anything if window is not valid anymore //
810  if(!SDLWindow)
811  return;
812 
813  SDL_ShowCursor(SDL_DISABLE);
814  }
815  }
816 }
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106

◆ SetMouseCapture()

DLLEXPORT bool Leviathan::Window::SetMouseCapture ( bool  state)
Returns
true if succeeds, false if another window has input

Definition at line 749 of file Window.cpp.

750 {
751  if(MouseCaptureState == state)
752  return true;
753 
754  MouseCaptureState = state;
755 
756  // handle changing state //
757  if(!MouseCaptureState) {
758 
759  // set mouse visible and disable capturing //
760  SDL_SetRelativeMouseMode(SDL_FALSE);
761  // reset pointer to indicate that this object no longer captures mouse to this
762  // window
763  // //
764  InputCapturer = nullptr;
765 
766  } else {
767 
768  if(InputCapturer != this && InputCapturer != nullptr) {
769  // another window has input //
770  MouseCaptureState = false;
771  return false;
772  }
773 
774  SDL_SetRelativeMouseMode(SDL_TRUE);
775 
776  // hide mouse and tell window to capture //
777  // DisplayWindow->SetMouseToCenter();
778 
779  // set static ptr to this //
780  InputCapturer = this;
781  }
782  return true;
783 }

◆ SetMouseToCenter()

DLLEXPORT void Leviathan::Window::SetMouseToCenter ( )

Definition at line 818 of file Window.cpp.

819 {
820  int32_t width, height;
821  GetSize(width, height);
822  SDL_WarpMouseInWindow(SDLWindow, width / 2, height / 2);
823 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:899

◆ Tick()

DLLEXPORT void Leviathan::Window::Tick ( int  mspassed)

Called by Engine.

Definition at line 288 of file Window.cpp.

289 {
290  // pass to GUI //
291  WindowsGui->GuiTick(mspassed);
292 }

◆ UnlinkAll()

DLLEXPORT void Leviathan::Window::UnlinkAll ( )

Definition at line 283 of file Window.cpp.

284 {
285  LinkObjects(nullptr);
286 }
DLLEXPORT void LinkObjects(std::shared_ptr< GameWorld > world)
This function also updates the camera aspect ratio.
Definition: Window.cpp:265

The documentation for this class was generated from the following files: