Leviathan  0.8.0.0
Leviathan game engine
Leviathan::Window Class Reference

#include <Window.h>

Classes

struct  BSFResources
 

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 unsigned long 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 const auto & GetBSFWindow () 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 34 of file Window.h.

Constructor & Destructor Documentation

◆ Window()

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

Definition at line 53 of file Window.cpp.

53  :
54  CurrentCursor(nullptr, nullptr), CurrentCursorImage(nullptr, nullptr),
55  ID(IDFactory::GetID())
56 {
57  // Create window //
58 
59  const WindowDataDetails& WData = windowproperties->GetWindowDetails();
60 
61  // TODO: FSAA (if MSAA is disabled)
62 
63  // // set some rendering specific parameters //
64  // Ogre::NameValuePairList WParams;
65 
66  // // variables //
67  // Ogre::String fsaastr = Convert::ToString(WData.FSAA);
68 
69  // // Context needs to be reused for multiple windows
70  // if(OpenWindowCount > 0) {
71 
72  // LOG_INFO("Window: using existing GLX context for creating");
73 
74  // WParams["currentGLContext"] = "true";
75 
76  // // Needs to be forced off to not cause issues like vsyncing each window separately
77  // and
78  // // dropping to "monitor refresh rate / window count" fps
79  // WParams["vsync"] = "false";
80 
81  // } else {
82  // WParams["vsync"] = WData.VSync ? "true" : "false";
83  // }
84 
85  // WParams["FSAA"] = fsaastr;
86  // WParams["gamma"] = WData.UseGamma ? "true" : "false";
87 
88  // Ogre::String wcaption = WData.Title;
89 
90  int extraFlags = 0;
91 
92  if(WData.FullScreen == "no" || WData.FullScreen == "0") {
93  } else if(WData.FullScreen == "fullscreendesktop") {
94  extraFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
95  } else if(WData.FullScreen == "fullscreenvideomode") {
96  extraFlags |= SDL_WINDOW_FULLSCREEN;
97  } else {
98 
99  LOG_ERROR("Window: invalid fullscreen value: " + WData.FullScreen);
100  }
101 
102  // TODO: On Apple's OS X you must set the NSHighResolutionCapable
103  // Info.plist property to YES, otherwise you will not receive a
104  // High DPI OpenGL
105  // canvas. https://wiki.libsdl.org/SDL_CreateWindow
106 
107  SDLWindow = SDL_CreateWindow(WData.Title.c_str(),
108  SDL_WINDOWPOS_UNDEFINED_DISPLAY(WData.DisplayNumber),
109  SDL_WINDOWPOS_UNDEFINED_DISPLAY(WData.DisplayNumber), WData.Width, WData.Height,
110  // This seems to cause issues on Windows
111  // SDL_WINDOW_OPENGL |
112  SDL_WINDOW_RESIZABLE | extraFlags);
113 
114  // SDL_WINDOW_FULLSCREEN_DESKTOP works so much better than
115  // SDL_WINDOW_FULLSCREEN so it should be always used
116 
117  // SDL_WINDOW_BORDERLESS
118  // SDL_WINDOWPOS_UNDEFINED_DISPLAY(x)
119  // SDL_WINDOWPOS_CENTERED_DISPLAY(x)
120 
121  if(!SDLWindow) {
122 
123  LOG_FATAL("SDL Window creation failed, error: " + std::string(SDL_GetError()));
124  }
125 
126  // SDL_GLContext glContext = SDL_GL_CreateContext(SDLWindow);
127 
128  SDL_SysWMinfo wmInfo;
129  SDL_VERSION(&wmInfo.version);
130  if(!SDL_GetWindowWMInfo(SDLWindow, &wmInfo)) {
131 
132  LOG_FATAL("Window: created sdl window failed to retrieve info");
133  }
134 
135  BSFWindow = windowcreater->RegisterCreatedWindow(*this);
136 
137  if(!BSFWindow) {
138  throw Exception("Failed to create bsf window");
139  }
140 
141  ++OpenWindowCount;
142 
143  // Store this window's number
144  WindowNumber = ++TotalCreatedWindows;
145 
146 
147 #ifdef _WIN32
148  // Fetch the windows handle from SDL //
149  HWND ourHWND = wmInfo.info.win.window;
150 
151  // apply style settings (mainly ICON) //
152  if(ourHWND) {
153 
154  WData.ApplyIconToHandle(ourHWND);
155 
156  } else {
157  LOG_WARNING("Window: failed to get window HWND for styling");
158  }
159 #else
160  // \todo linux icon
161 #endif
162  // tmpwindow->setDeactivateOnFocusChange(false);
163 
164  // set the new window to be active //
165  // tmpwindow->setActive(true);
166  Focused = true;
167 
168  _BSFResources = std::make_unique<BSFResources>();
169 
170  // Create and attach our camera to the window
171  {
172  _BSFResources->WindowSceneObject = bs::SceneObject::create("window no world camera");
173  _BSFResources->WindowCamera =
174  _BSFResources->WindowSceneObject->addComponent<bs::CCamera>();
175  _BSFResources->WindowCamera->getViewport()->setClearColorValue(Float4(0, 0, 0, 1));
176  _BSFResources->WindowSceneObject->setPosition(Float3(40.0f, 30.0f, 230.0f));
177  _BSFResources->WindowSceneObject->lookAt(Float3(0, 0, 0));
178 
179  _BSFResources->WindowCamera->getViewport()->setTarget(BSFWindow);
180  }
181 
182  // create GUI //
183  WindowsGui = std::make_unique<GUI::GuiManager>();
184  if(!WindowsGui) {
185  // TODO: the window must be destroyed here
186  DEBUG_BREAK;
187  throw NULLPtr("cannot create GUI manager instance");
188  }
189 
190  if(!WindowsGui->Init(windowcreater, this)) {
191 
192  LOG_ERROR("Window: Gui init failed");
193  throw NULLPtr("invalid GUI manager");
194  }
195 
196  // create receiver interface //
197  TertiaryReceiver = std::shared_ptr<InputController>(new InputController());
198 
199  // TODO: this needs to be only used when a text box etc. is used
200  // But that is quite hard to detect
201  SDL_StartTextInput();
202 
203  // cursor on top of window isn't hidden //
204  ApplicationWantCursorState = false;
205 }
#define LOG_ERROR(x)
Definition: Define.h:90
#define LOG_FATAL(x)
Definition: Define.h:92
static int GetID()
Definition: IDFactory.h:17
#define LOG_WARNING(x)
Definition: Define.h:89

◆ ~Window()

DLLEXPORT Leviathan::Window::~Window ( )

Definition at line 207 of file Window.cpp.

208 {
209  _BSFResources.reset();
210 
211  // GUI is very picky about delete order
212  if(WindowsGui) {
213  WindowsGui->Release();
214  WindowsGui.reset();
215  }
216 
217  if(MouseCaptured) {
218 
219  SDL_SetRelativeMouseMode(SDL_FALSE);
220  }
221 
222  // Un fullscreen to make sure nothing is screwed up
223  if(SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) {
224 
225  LOG_INFO("Window: unfullscreened before quit");
226  SDL_SetWindowFullscreen(SDLWindow, 0);
227  }
228 
229  // Report that the window is now closed //
230  Logger::Get()->Info(
231  "Window: closing window(" + Convert::ToString(GetWindowNumber()) + ")");
232 
233  // Close the window //
234  // OWindow->destroy();
235 
236  TertiaryReceiver.reset();
237 
238  int windowsafter = --OpenWindowCount;
239 
240  BSFWindow->destroy();
241  BSFWindow.reset();
242 
243  if(windowsafter == 0) {
244 
245  Logger::Get()->Info("Window: all windows have been closed, "
246  "should quit soon");
247  }
248 
249  // LOG_WRITE("TODO: check why calling SDL_DestroyWindow crashes in Ogre "
250  // "GLX plugin uninstall");
251  SDL_DestroyWindow(SDLWindow);
252  // SDL_HideWindow(SDLWindow);
253  SDLWindow = nullptr;
254 }
#define LOG_INFO(x)
Definition: Define.h:88
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
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:173

Member Function Documentation

◆ _CheckMouseVisibilityStates()

void Leviathan::Window::_CheckMouseVisibilityStates ( )
protected

Definition at line 1062 of file Window.cpp.

1063 {
1064  const bool outsideArea = IsMouseOutsideWindowClientArea();
1065 
1066  // force cursor visible check (if outside client area or mouse is unfocused on the
1067  // window)
1068  if(outsideArea || !Focused) {
1069 
1070  ForceMouseVisible = true;
1071 
1072  } else {
1073 
1074  ForceMouseVisible = false;
1075  }
1076 
1077  // update cursor state //
1078  SetHideCursor(ApplicationWantCursorState);
1079 }
DLLEXPORT void SetHideCursor(bool toset)
Definition: Window.cpp:782
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:877

◆ _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 984 of file Window.cpp.

985 {
986  // Quit if window closed //
987  if(!SDLWindow) {
988 
989  LOG_WARNING("Window: GatherInput: window is closed");
990  return;
991  }
992 
993  InputGatherStarted = true;
994 
996 
997  SpecialKeyModifiers = 0;
998  CEFSpecialKeyModifiers = EVENTFLAG_NONE;
999 
1000  const auto mods = SDL_GetModState();
1001 
1002  if(mods & KMOD_CTRL) {
1003  SpecialKeyModifiers |= KEYSPECIAL_CTRL;
1004  CEFSpecialKeyModifiers |= EVENTFLAG_CONTROL_DOWN;
1005  }
1006  if(mods & KMOD_ALT) {
1007  SpecialKeyModifiers |= KEYSPECIAL_ALT;
1008  CEFSpecialKeyModifiers |= EVENTFLAG_ALT_DOWN;
1009  }
1010  if(mods & KMOD_SHIFT) {
1011  SpecialKeyModifiers |= KEYSPECIAL_SHIFT;
1012  CEFSpecialKeyModifiers |= EVENTFLAG_SHIFT_DOWN;
1013  }
1014  if(mods & KMOD_CAPS) {
1015  SpecialKeyModifiers |= KEYSPECIAL_CAPS;
1016  CEFSpecialKeyModifiers |= EVENTFLAG_CAPS_LOCK_ON;
1017  }
1018  if(mods & KMOD_GUI) {
1019  SpecialKeyModifiers |= KEYSPECIAL_SUPER;
1020  }
1021 
1022  // TODO: EVENTFLAG_NUM_LOCK_ON;
1023 
1024  // TODO: fix mouse capture
1025  // // Handle mouse capture
1026  // if(MouseCaptured && Focused) {
1027 
1028  // // get mouse relative to window center //
1029  // int xmoved = 0, ymoved = 0;
1030 
1031  // SDL_GetRelativeMouseState(&xmoved, &ymoved);
1032 
1033  // // Pass input //
1034  // GetInputController()->SendMouseMovement(xmoved, ymoved);
1035  // }
1036 }
#define LOG_WARNING(x)
Definition: Define.h:89
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 346 of file Window.cpp.

347 {
348  if(SDLWindow) {
349  SDL_RaiseWindow(SDLWindow);
350  }
351 }

◆ 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 1081 of file Window.cpp.

1083 {
1084  // Find active gui view that wants the event
1085  auto* receiver = GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Keypress, mousex, mousey);
1086 
1087  // Don't pass to GUI
1088  if(!receiver)
1089  return false;
1090 
1091  return receiver->OnReceiveKeyInput(sdlevent, down, textinput, mousex, mousey,
1092  SpecialKeyModifiers, CEFSpecialKeyModifiers);
1093 }
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:974

◆ GetAspectRatio()

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

Definition at line 77 of file Window.h.

78  {
79  int32_t width, height;
80  GetSize(width, height);
81 
82  return (static_cast<float>(width)) / height;
83  }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:894

◆ GetBSFWindow()

DLLEXPORT const auto& Leviathan::Window::GetBSFWindow ( ) const
inline

Definition at line 166 of file Window.h.

167  {
168  return BSFWindow;
169  }

◆ GetGlobalWindowCount()

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

Returns how many windows are open

See also
OpenWindowCount

Definition at line 180 of file Window.h.

181  {
182  return OpenWindowCount;
183  }

◆ 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 974 of file Window.cpp.

975 {
976  // Don't pass to GUI if mouse capture is enabled
977  if(MouseCaptured)
978  return nullptr;
979 
980  auto layer = WindowsGui->GetTargetLayerForInput(type, mousex, mousey);
981  return layer;
982 }

◆ GetInputController()

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

Definition at line 160 of file Window.h.

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

◆ GetNativeHandle()

DLLEXPORT unsigned long Leviathan::Window::GetNativeHandle ( ) const

Definition at line 915 of file Window.cpp.

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

◆ 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 859 of file Window.cpp.

860 {
861  int xInt, yInt;
862  GetRelativeMouse(xInt, yInt);
863 
864  int32_t width, height;
865  GetSize(width, height);
866 
867  if(width == 0 || height == 0) {
868  x = 0.5f;
869  y = 0.5f;
870  return;
871  }
872 
873  x = static_cast<float>(xInt) / width;
874  y = static_cast<float>(yInt) / height;
875 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:894
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:821

◆ GetPosition()

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

Definition at line 899 of file Window.cpp.

900 {
901  SDL_GetWindowPosition(SDLWindow, &x, &y);
902 }

◆ GetRelativeMouse()

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

Definition at line 821 of file Window.cpp.

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

◆ GetSDLID()

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

Definition at line 904 of file Window.cpp.

905 {
906  if(!SDLWindow)
907  return -1;
908 
909  return SDL_GetWindowID(SDLWindow);
910 }

◆ GetSize()

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

Definition at line 894 of file Window.cpp.

895 {
896  SDL_GetWindowSize(SDLWindow, &width, &height);
897 }

◆ 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 843 of file Window.cpp.

844 {
845  if(!SDLWindow)
846  return;
847 
848  int globalX, globalY;
849  SDL_GetGlobalMouseState(&globalX, &globalY);
850 
851  int windowX, windowY;
852  SDL_GetWindowPosition(SDLWindow, &windowX, &windowY);
853 
854  x = globalX - windowX;
855  y = globalY - windowY;
856 }

◆ GetVsync()

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

Definition at line 962 of file Window.cpp.

963 {
964  DEBUG_BREAK;
965  return false;
966  // return OWindow->isVSyncEnabled();
967 }

◆ 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 173 of file Window.h.

174  {
175  return WindowNumber;
176  }

◆ InjectCodePoint()

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

Injects text.

Definition at line 1184 of file Window.cpp.

1185 {
1186  if(!InputGatherStarted)
1188 
1189  // Try to pass to CEF //
1190  int mouseX;
1191  int mouseY;
1192  GetRelativeMouse(mouseX, mouseY);
1193 
1194  if(!DoGUILayerInputPass(event, true, true, mouseX, mouseY)) {
1195 
1196  // GUI didn't want it
1197  }
1198 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1081
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:821

◆ InjectKeyDown()

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

Definition at line 1200 of file Window.cpp.

1201 {
1202  if(!InputGatherStarted)
1204 
1205  bool SentToController = false;
1206 
1207  // Try to pass to CEF //
1208  int mouseX;
1209  int mouseY;
1210  GetRelativeMouse(mouseX, mouseY);
1211 
1212  const auto handled = DoGUILayerInputPass(event, true, false, mouseX, mouseY);
1213 
1214  if(!handled) {
1215 
1216  // CEF didn't want it
1217  // Then try disabling collections //
1218  // LOG_WRITE("TODO: check is a text box active");
1219  // if(!OwningWindow->GetGui()->ProcessKeyDown(sdlkey, SpecialKeyModifiers)) {
1220 
1221  // Finally send to a controller //
1222  SentToController = true;
1223  GetInputController()->OnInputGet(event.key.keysym.sym, SpecialKeyModifiers, true);
1224  // }
1225  }
1226 
1227  if(!SentToController) {
1228  GetInputController()->OnBlockedInput(event.key.keysym.sym, SpecialKeyModifiers, true);
1229  }
1230 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1081
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:821
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 1232 of file Window.cpp.

1233 {
1234  if(!InputGatherStarted)
1236 
1237  // Send to CEF if GUI is active //
1238  int mouseX;
1239  int mouseY;
1240  GetRelativeMouse(mouseX, mouseY);
1241  DoGUILayerInputPass(event, false, false, mouseX, mouseY);
1242 
1243  // This should always be passed here //
1244  GetInputController()->OnInputGet(event.key.keysym.sym, SpecialKeyModifiers, false);
1245 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1081
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:821
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 1150 of file Window.cpp.

1151 {
1152  if(!InputGatherStarted)
1154 
1155  if(!MouseCaptured) {
1156 
1157  auto* receiver =
1158  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.button.x, event.button.y);
1159 
1160  if(receiver) {
1161 
1162  receiver->OnMouseButton(event, true);
1163  }
1164  }
1165 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
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:974

◆ InjectMouseButtonUp()

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

Definition at line 1167 of file Window.cpp.

1168 {
1169  if(!InputGatherStarted)
1171 
1172  if(!MouseCaptured) {
1173 
1174  auto* receiver =
1175  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.button.x, event.button.y);
1176 
1177  if(receiver) {
1178 
1179  receiver->OnMouseButton(event, false);
1180  }
1181  }
1182 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
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:974

◆ InjectMouseMove()

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

Definition at line 1095 of file Window.cpp.

1096 {
1097  if(!InputGatherStarted)
1099 
1100  // Only pass this data if we aren't going to pass our own captured mouse //
1101  if(!MouseCaptured) {
1102 
1103  auto* receiver =
1104  GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Other, event.motion.x, event.motion.y);
1105 
1106  if(receiver) {
1107  // TODO: IsMouseOutsideWindowClientArea needs to be
1108  // updated (for CEF GuiViews) once we support multiple
1109  // browsers in a single window
1110  receiver->OnMouseMove(event, IsMouseOutsideWindowClientArea());
1111  }
1112  }
1113 
1115 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
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:974
void _CheckMouseVisibilityStates()
Definition: Window.cpp:1062
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:877

◆ 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 1117 of file Window.cpp.

1118 {
1119  if(!InputGatherStarted)
1121 
1122  if(!MouseCaptured) {
1123 
1124  int mouseX;
1125  int mouseY;
1126  GetRelativeMouse(mouseX, mouseY);
1127  // TODO: allow configuring if mouse wheel is considered a key
1128  auto* receiver = GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Scroll, mouseX, mouseY);
1129 
1130  if(receiver) {
1131 
1132  receiver->OnMouseWheel(event);
1133 
1134  } else {
1135 
1136  int x = event.wheel.x;
1137  int y = event.wheel.y;
1138 
1139  if(SDL_MOUSEWHEEL_FLIPPED == event.wheel.direction) {
1140  y *= -1;
1141  } else {
1142  x *= -1;
1143  }
1144 
1145  GetInputController()->OnScroll(x, y, SpecialKeyModifiers);
1146  }
1147  }
1148 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:984
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:974
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:821
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 1038 of file Window.cpp.

1039 {
1040  // Initial mouse position
1041  if(!InitialMousePositionSent) {
1043 
1044  InitialMousePositionSent = true;
1045 
1046  // Build a mouse move from the current position and send it
1047  int x;
1048  int y;
1049  GetRelativeMouse(x, y);
1050 
1051  SDL_Event event;
1052 
1053  event.motion.x = x;
1054  event.motion.y = y;
1055  InjectMouseMove(event);
1056  }
1057  }
1058 
1059  InputGatherStarted = false;
1060 }
DLLEXPORT void InjectMouseMove(const SDL_Event &event)
Definition: Window.cpp:1095
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:821
DLLEXPORT bool IsMouseOutsideWindowClientArea() const
Definition: Window.cpp:877

◆ IsMouseOutsideWindowClientArea()

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

Definition at line 877 of file Window.cpp.

878 {
879  int X, Y;
881 
882  int32_t width, height;
883  GetSize(width, height);
884 
885  // check the coordinates //
886 
887  if(X < 0 || Y < 0 || X > width || Y > height) {
888  return true;
889  }
890 
891  return false;
892 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:894
DLLEXPORT void GetUnclampedRelativeMouse(int &x, int &y) const
Definition: Window.cpp:843

◆ 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 256 of file Window.cpp.

257 {
258  if(LinkedWorld == world)
259  return;
260 
261  if(LinkedWorld) {
262 
263  LinkedWorld->OnUnLinkedFromWindow(this, Engine::Get()->GetGraphics());
264  } else {
265  _BSFResources->WindowCamera->getViewport()->setTarget(nullptr);
266  }
267 
268  LinkedWorld = world;
269 
270  if(LinkedWorld) {
271 
272  LinkedWorld->OnLinkToWindow(this, Engine::Get()->GetGraphics());
273  } else {
274 
275  _BSFResources->WindowCamera->getViewport()->setTarget(BSFWindow);
276  }
277 }
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ OnFocusChange()

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

Definition at line 326 of file Window.cpp.

327 {
328  if(Focused == focused)
329  return;
330 
331  LOG_INFO("Focus change in Window");
332 
333  // Update mouse //
334  Focused = focused;
336 
337  WindowsGui->OnFocusChanged(focused);
338 
339  if(!Focused && MouseCaptured) {
340 
341  LOG_WRITE("TODO: We need to force GUI on to stop mouse capture");
342  LOG_FATAL("Not implemented unfocus when mouse capture is on");
343  }
344 }
#define LOG_INFO(x)
Definition: Define.h:88
#define LOG_FATAL(x)
Definition: Define.h:92
#define LOG_WRITE(x)
Definition: Define.h:91
void _CheckMouseVisibilityStates()
Definition: Window.cpp:1062

◆ OnResize()

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

Definition at line 302 of file Window.cpp.

303 {
304  if(DoingResize)
305  return;
306 
307 // Notify bsf
308 // TODO: something better, this is very fiddly
309 #ifdef _WIN32
310  LOG_ERROR("Resizing doesn't work at all on Windows, see: "
311  "https://github.com/GameFoundry/bsf/issues/382");
312 #else
313  BSFWindow->resize(width, height);
314  LOG_WARNING("Resizing is fiddly, see: "
315  "https://github.com/GameFoundry/bsf/issues/382");
316 #endif
317 
318  // These don't work
319  // BSFWindow->_notifyWindowEvent(bs::WindowEventType::Resized);
320  // BSFWindow->_windowMovedOrResized();
321 
322  // Send to GUI //
323  WindowsGui->OnResize();
324 }
#define LOG_ERROR(x)
Definition: Define.h:90
#define LOG_WARNING(x)
Definition: Define.h:89

◆ Render()

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

This function uses the LinkObjects function objects.

Definition at line 290 of file Window.cpp.

291 {
292  if(LinkedWorld)
293  LinkedWorld->Render(mspassed, tick, timeintick);
294 
295  // Update GUI before each frame //
296  WindowsGui->Render();
297 
298  // BSF does it's own rendering handling
299  return true;
300 }

◆ SaveScreenShot()

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

Definition at line 955 of file Window.cpp.

956 {
957  // uses render target's capability to save it's contents //
958  DEBUG_BREAK;
959  // GetOgreWindow()->writeContentsToTimestampedFile(filename, "_window1.png");
960 }

◆ SetCustomInputController()

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

Overwrites the default InputController with a custom one.

Definition at line 969 of file Window.cpp.

970 {
971  TertiaryReceiver = controller;
972 }

◆ SetHideCursor()

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

Definition at line 782 of file Window.cpp.

783 {
784  ApplicationWantCursorState = toset;
785 
786  if(!ApplicationWantCursorState || ForceMouseVisible) {
787  // show cursor //
788  if(!CursorState) {
789  CursorState = true;
790  Logger::Get()->Info("Showing cursor");
791 
792  // Don't do anything if window is not valid anymore //
793  if(!SDLWindow)
794  return;
795 
796  SDL_ShowCursor(SDL_ENABLE);
797  }
798  } else {
799  // hide cursor //
800  if(CursorState) {
801 
802  CursorState = false;
803  Logger::Get()->Info("Hiding cursor");
804 
805  // Don't do anything if window is not valid anymore //
806  if(!SDLWindow)
807  return;
808 
809  SDL_ShowCursor(SDL_DISABLE);
810  }
811  }
812 }
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 745 of file Window.cpp.

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

◆ SetMouseToCenter()

DLLEXPORT void Leviathan::Window::SetMouseToCenter ( )

Definition at line 814 of file Window.cpp.

815 {
816  int32_t width, height;
817  GetSize(width, height);
818  SDL_WarpMouseInWindow(SDLWindow, width / 2, height / 2);
819 }
DLLEXPORT void GetSize(int32_t &width, int32_t &height) const
Definition: Window.cpp:894

◆ Tick()

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

Called by Engine.

Definition at line 284 of file Window.cpp.

285 {
286  // pass to GUI //
287  WindowsGui->GuiTick(mspassed);
288 }

◆ UnlinkAll()

DLLEXPORT void Leviathan::Window::UnlinkAll ( )

Definition at line 279 of file Window.cpp.

280 {
281  LinkObjects(nullptr);
282 }
DLLEXPORT void LinkObjects(std::shared_ptr< GameWorld > world)
This function also updates the camera aspect ratio.
Definition: Window.cpp:256

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