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

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

209 {
210  _BSFResources.reset();
211 
212  // GUI is very picky about delete order
213  if(WindowsGui) {
214  WindowsGui->Release();
215  WindowsGui.reset();
216  }
217 
218  if(MouseCaptured) {
219 
220  SDL_SetRelativeMouseMode(SDL_FALSE);
221  }
222 
223  // Un fullscreen to make sure nothing is screwed up
224  if(SDL_GetWindowFlags(SDLWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) {
225 
226  LOG_INFO("Window: unfullscreened before quit");
227  SDL_SetWindowFullscreen(SDLWindow, 0);
228  }
229 
230  // Report that the window is now closed //
231  Logger::Get()->Info(
232  "Window: closing window(" + Convert::ToString(GetWindowNumber()) + ")");
233 
234  // Close the window //
235  // OWindow->destroy();
236 
237  TertiaryReceiver.reset();
238 
239  int windowsafter = --OpenWindowCount;
240 
241  // Report close to graphics
242  const bool isPrimary = Engine::Get()->GetGraphics()->UnRegisterWindow(*this);
243  if(!isPrimary) {
244  BSFWindow->destroy();
245  }
246 
247  BSFWindow.reset();
248 
249  if(windowsafter == 0) {
250 
251  Logger::Get()->Info("Window: all windows have been closed, "
252  "should quit soon");
253  }
254 
255  if(isPrimary) {
256  // It was the primary window, don't destroy it yet
257  SDL_HideWindow(SDLWindow);
258  } else {
259 
260  SDL_DestroyWindow(SDLWindow);
261  }
262 
263  SDLWindow = nullptr;
264 }
#define LOG_INFO(x)
Definition: Define.h:88
DLLEXPORT void Info(const std::string &data) override
Definition: Logger.cpp:164
Graphics * GetGraphics()
Definition: Engine.h:179
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
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85
bool UnRegisterWindow(Window &window)
Called just before a window is destroyed. This needs to stop rendering to it.
Definition: Graphics.cpp:494

Member Function Documentation

◆ _CheckMouseVisibilityStates()

void Leviathan::Window::_CheckMouseVisibilityStates ( )
protected

Definition at line 1066 of file Window.cpp.

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

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

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

349 {
350  if(SDLWindow) {
351  SDL_RaiseWindow(SDLWindow);
352  }
353 }

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

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

◆ 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:896

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

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

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

919 {
920  SDL_SysWMinfo wmInfo;
921  SDL_VERSION(&wmInfo.version);
922  if(!SDL_GetWindowWMInfo(SDLWindow, &wmInfo)) {
923 
924  LOG_FATAL("Window: GetNativeHandle: failed to retrieve wm info");
925 #ifdef _WIN32
926  return 0;
927 #else
928  return -1;
929 #endif //_WIN32
930  }
931 #if defined(_WIN32)
932  return wmInfo.info.win.window;
933 #else
934 #if defined(__linux)
935  return wmInfo.info.x11.window;
936 #else
937 #error Fix this for mac
938 #endif
939 #endif
940 }
#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 861 of file Window.cpp.

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

◆ GetPosition()

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

Definition at line 901 of file Window.cpp.

902 {
903  SDL_GetWindowPosition(SDLWindow, &x, &y);
904 }

◆ GetRelativeMouse()

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

Definition at line 823 of file Window.cpp.

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

◆ GetSDLID()

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

Definition at line 906 of file Window.cpp.

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

◆ GetSize()

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

Definition at line 896 of file Window.cpp.

897 {
898  SDL_GetWindowSize(SDLWindow, &width, &height);
899 }

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

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

◆ GetVsync()

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

Definition at line 966 of file Window.cpp.

967 {
968  DEBUG_BREAK;
969  return false;
970  // return OWindow->isVSyncEnabled();
971 }

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

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

◆ InjectKeyDown()

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

Definition at line 1204 of file Window.cpp.

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

1237 {
1238  if(!InputGatherStarted)
1240 
1241  // Send to CEF if GUI is active //
1242  int mouseX;
1243  int mouseY;
1244  GetRelativeMouse(mouseX, mouseY);
1245  DoGUILayerInputPass(event, false, false, mouseX, mouseY);
1246 
1247  // This should always be passed here //
1248  GetInputController()->OnInputGet(event.key.keysym.sym, SpecialKeyModifiers, false);
1249 }
DLLEXPORT void _StartGatherInput()
Detects state of modifier keys. Called whenever input is injected and is stored until InputEnd is cal...
Definition: Window.cpp:988
bool DoGUILayerInputPass(const SDL_Event &sdlevent, bool down, bool textinput, int mousex, int mousey)
Definition: Window.cpp:1085
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:823
virtual DLLEXPORT void OnInputGet(int32_t key, int specialmodifiers, bool down)
Called when the GUI didn'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 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, true);
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:988
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:978

◆ InjectMouseButtonUp()

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

Definition at line 1171 of file Window.cpp.

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

◆ InjectMouseMove()

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

Definition at line 1099 of file Window.cpp.

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

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

1122 {
1123  if(!InputGatherStarted)
1125 
1126  if(!MouseCaptured) {
1127 
1128  int mouseX;
1129  int mouseY;
1130  GetRelativeMouse(mouseX, mouseY);
1131  // TODO: allow configuring if mouse wheel is considered a key
1132  auto* receiver = GetGUIEventReceiver(GUI::INPUT_EVENT_TYPE::Scroll, mouseX, mouseY);
1133 
1134  if(receiver) {
1135 
1136  receiver->OnMouseWheel(event);
1137 
1138  } else {
1139 
1140  int x = event.wheel.x;
1141  int y = event.wheel.y;
1142 
1143  if(SDL_MOUSEWHEEL_FLIPPED == event.wheel.direction) {
1144  y *= -1;
1145  } else {
1146  x *= -1;
1147  }
1148 
1149  GetInputController()->OnScroll(x, y, SpecialKeyModifiers);
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:988
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:978
DLLEXPORT void GetRelativeMouse(int &x, int &y) const
Definition: Window.cpp:823
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 1042 of file Window.cpp.

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

◆ IsMouseOutsideWindowClientArea()

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

Definition at line 879 of file Window.cpp.

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

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

267 {
268  if(LinkedWorld == world)
269  return;
270 
271  if(LinkedWorld) {
272 
273  LinkedWorld->OnUnLinkedFromWindow(this, Engine::Get()->GetGraphics());
274  } else {
275  _BSFResources->WindowCamera->getViewport()->setTarget(nullptr);
276  }
277 
278  LinkedWorld = world;
279 
280  if(LinkedWorld) {
281 
282  LinkedWorld->OnLinkToWindow(this, Engine::Get()->GetGraphics());
283  } else {
284 
285  _BSFResources->WindowCamera->getViewport()->setTarget(BSFWindow);
286  }
287 }
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ OnFocusChange()

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

Definition at line 328 of file Window.cpp.

329 {
330  if(Focused == focused)
331  return;
332 
333  LOG_INFO("Focus change in Window");
334 
335  // Update mouse //
336  Focused = focused;
338 
339  WindowsGui->OnFocusChanged(focused);
340 
341  if(!Focused && MouseCaptured) {
342 
343  LOG_WRITE("TODO: We need to force GUI on to stop mouse capture");
344  LOG_FATAL("Not implemented unfocus when mouse capture is on");
345  }
346 }
#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:1066

◆ OnResize()

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

Definition at line 312 of file Window.cpp.

313 {
314  if(DoingResize)
315  return;
316 
317  // Notify bsf
318  bs::gCoreThread().queueCommand([coreWindow = BSFWindow->getCore()]() {
319  coreWindow->_notifyWindowEvent(bs::WindowEventType::Resized);
320  });
321 
322  BSFWindow->_onExternalResize(width, height);
323 
324  // Send to GUI //
325  WindowsGui->OnResize();
326 }

◆ Render()

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

This function uses the LinkObjects function objects.

Definition at line 300 of file Window.cpp.

301 {
302  if(LinkedWorld)
303  LinkedWorld->Render(mspassed, tick, timeintick);
304 
305  // Update GUI before each frame //
306  WindowsGui->Render();
307 
308  // BSF does it's own rendering handling
309  return true;
310 }

◆ SaveScreenShot()

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

Definition at line 957 of file Window.cpp.

958 {
959  LOG_INFO("bsf doesn't support screenshots: "
960  "https://discourse.bsframework.io/t/taking-a-screenshot/487");
961  // uses render target's capability to save it's contents //
962  // DEBUG_BREAK;
963  // GetOgreWindow()->writeContentsToTimestampedFile(filename, "_window1.png");
964 }
#define LOG_INFO(x)
Definition: Define.h:88

◆ SetCustomInputController()

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

Overwrites the default InputController with a custom one.

Definition at line 973 of file Window.cpp.

974 {
975  TertiaryReceiver = controller;
976 }

◆ SetHideCursor()

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

Definition at line 784 of file Window.cpp.

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

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

◆ SetMouseToCenter()

DLLEXPORT void Leviathan::Window::SetMouseToCenter ( )

Definition at line 816 of file Window.cpp.

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

◆ Tick()

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

Called by Engine.

Definition at line 294 of file Window.cpp.

295 {
296  // pass to GUI //
297  WindowsGui->GuiTick(mspassed);
298 }

◆ UnlinkAll()

DLLEXPORT void Leviathan::Window::UnlinkAll ( )

Definition at line 289 of file Window.cpp.

290 {
291  LinkObjects(nullptr);
292 }
DLLEXPORT void LinkObjects(std::shared_ptr< GameWorld > world)
This function also updates the camera aspect ratio.
Definition: Window.cpp:266

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