platformWindow.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _WINDOWMANAGER_PLATFORMWINDOW_H_
  23. #define _WINDOWMANAGER_PLATFORMWINDOW_H_
  24. #include "math/mRect.h"
  25. #include "core/util/journal/journaledSignal.h"
  26. #include "core/util/safeDelete.h"
  27. #include "windowManager/platformCursorController.h"
  28. #include "windowManager/windowInputGenerator.h"
  29. //forward decl's
  30. class PlatformWindowManager;
  31. class GFXDevice;
  32. struct GFXVideoMode;
  33. class GFXWindowTarget;
  34. class IProcessInput;
  35. /// Abstract representation of a native OS window.
  36. ///
  37. /// Every windowing system has its own representations and conventions as
  38. /// regards the windows on-screen. In order to provide Torque with means for
  39. /// interfacing with multiple windows, tracking their state, etc. we provide
  40. /// this interface.
  41. ///
  42. /// This interface also allows the app to access the render target for the
  43. /// window it represents, as well as control mode switches, get mode info,
  44. /// and so on.
  45. ///
  46. /// @see PlatformWindowManager
  47. class PlatformWindow
  48. {
  49. friend class PlatformWindowManager;
  50. protected:
  51. /// Are we enabling IME or other keyboard input translation services,
  52. /// or concerned about raw input?
  53. bool mEnableKeyboardTranslation;
  54. /// When Torque GuiText input controls have focus they need to
  55. /// disable native OS keyboard accelerator translation.
  56. bool mEnableAccelerators;
  57. /// Minimum allowed size for this window. When possible, we will communicate
  58. /// this to the OS.
  59. Point2I mMinimumSize;
  60. /// When the resize is locked, this will be used as both minimum and maximum window size
  61. Point2I mLockedSize;
  62. /// When this is true, resizing is locked
  63. bool mResizeLocked;
  64. /// Is Idle?
  65. bool mIsBackground;
  66. /// Cursor Controller for this Window
  67. PlatformCursorController *mCursorController;
  68. /// An opaque ID used to resolve references to this Window
  69. WindowId mWindowId;
  70. /// Window Mouse/Key input Controller for this Window
  71. WindowInputGenerator *mWindowInputGenerator;
  72. /// Suppress device resets
  73. bool mSuppressReset;
  74. /// Offscreen Render
  75. bool mOffscreenRender;
  76. /// This is set as part of the canvas being shown, and flags that the windows should render as normal from now on.
  77. // Basically a flag that lets the window manager know that we've handled the splash screen, and to operate as normal.
  78. bool mDisplayWindow;
  79. /// Protected constructor so that the win
  80. PlatformWindow()
  81. {
  82. mIsBackground = false; // This could be toggled to true to prefer performance.
  83. mMinimumSize.set(0,0);
  84. mLockedSize.set(0,0);
  85. mResizeLocked = false;
  86. mEnableKeyboardTranslation = false;
  87. mEnableAccelerators = true;
  88. mCursorController = NULL;
  89. mSuppressReset = false;
  90. mOffscreenRender = false;
  91. mDisplayWindow = false;
  92. // This controller maps window input (Mouse/Keyboard) to a generic input consumer
  93. mWindowInputGenerator = new WindowInputGenerator( this );
  94. }
  95. public:
  96. /// To get rid of a window, just delete it. Make sure the GFXDevice is
  97. /// done with it first!
  98. virtual ~PlatformWindow()
  99. {
  100. SAFE_DELETE( mCursorController );
  101. SAFE_DELETE( mWindowInputGenerator );
  102. }
  103. /// Get the WindowController associated with this window
  104. virtual void setInputController( IProcessInput *controller ) { if( mWindowInputGenerator ) mWindowInputGenerator->setInputController( controller ); };
  105. WindowInputGenerator* getInputGenerator() const { return mWindowInputGenerator; }
  106. /// Get the ID that uniquely identifies this window in the context of its
  107. /// window manager.
  108. virtual WindowId getWindowId() { return 0; };
  109. enum WindowSystem
  110. {
  111. WindowSystem_Unknown = 0,
  112. WindowSystem_Windows,
  113. WindowSystem_X11,
  114. };
  115. virtual void* getSystemWindow(const WindowSystem system) { return NULL; }
  116. /// Set the flag that determines whether to suppress a GFXDevice reset
  117. inline void setSuppressReset(bool suppress) { mSuppressReset = suppress; };
  118. /// @name GFX State Management
  119. ///
  120. /// @{
  121. /// Return a pointer to the GFX device this window is bound to. A GFX
  122. /// device may use many windows, but a window can only be used by a
  123. /// single GFX device.
  124. virtual GFXDevice *getGFXDevice()=0;
  125. /// Return a pointer to this window's render target.
  126. ///
  127. /// By setting window targets from different windows, we can effect
  128. /// rendering to multiple windows from a single device.
  129. virtual GFXWindowTarget *getGFXTarget()=0;
  130. /// Set the video mode for this window.
  131. virtual void setVideoMode(const GFXVideoMode &mode)=0;
  132. /// Get our current video mode - if the window has been resized, it will
  133. /// reflect this.
  134. virtual const GFXVideoMode &getVideoMode()=0;
  135. /// If we're fullscreen, this function returns us to desktop mode.
  136. ///
  137. /// This will be either the last mode that we had that was not
  138. /// fullscreen, or the equivalent mode, windowed.
  139. virtual bool clearFullscreen()=0;
  140. /// @return true if this window is fullscreen, false otherwise.
  141. virtual bool isFullscreen()=0;
  142. /// Acquire the entire screen
  143. void setFullscreen(const bool fullscreen);
  144. /// Set Idle State (Background)
  145. ///
  146. /// This is called to put a window into idle state, which causes it's
  147. /// rendering priority to be toned down to prefer performance
  148. virtual void setBackground( bool val ) { mIsBackground = val; };
  149. /// Get Idle State (Background)
  150. ///
  151. /// This is called to poll the window as to it's idle state.
  152. virtual bool getBackground() { return mIsBackground; };
  153. /// Set whether this window is intended for offscreen rendering
  154. /// An offscreen window will never be shown or lose focus
  155. virtual void setOffscreenRender(bool val ) { mOffscreenRender = val; };
  156. /// Set whether this window is intended for offscreen rendering
  157. ///
  158. /// This is called to poll the window as to it's idle state.
  159. virtual bool getOffscreenRender() { return mOffscreenRender; };
  160. /// Set whether this window is should display as normal
  161. virtual void setDisplayWindow(bool val ) { mDisplayWindow = val; };
  162. /// Set Focused State (Foreground)
  163. ///
  164. /// Claim OS input focus for this window
  165. virtual void setFocus() { }
  166. /// @}
  167. /// @name Caption
  168. ///
  169. /// @{
  170. /// Set the window's caption.
  171. virtual bool setCaption(const char *cap)=0;
  172. /// Get the window's caption.
  173. virtual const char *getCaption()=0;
  174. /// @}
  175. /// @name Visibility
  176. ///
  177. /// Control how the window is displayed
  178. /// @{
  179. /// Minimize the window on screen
  180. virtual void minimize()=0;
  181. /// Maximize the window on screen
  182. virtual void maximize()=0;
  183. /// Hide the window on screen
  184. virtual void hide()=0;
  185. /// Show the window on screen
  186. virtual void show()=0;
  187. /// Destroy the window on screen
  188. virtual void close()=0;
  189. /// Restore the window from a Maximized or Minimized state
  190. virtual void restore()=0;
  191. /// @}
  192. /// @name Window Bounds
  193. ///
  194. /// @{
  195. /// The Client Rectangle or "Render Area" of a window is the area that
  196. /// is occupied by a given client that is rendering to that window.
  197. /// This does not include the area occupied by a title-bar, menu,
  198. /// borders or other non-client elements.
  199. /// @{
  200. /// Set the Client Area Extent (Resolution) of this window
  201. virtual void setClientExtent( const Point2I newExtent ) = 0;
  202. /// Get the Client Area Extent (Resolution) of this window
  203. virtual const Point2I getClientExtent() = 0;
  204. /// @}
  205. /// The bounds of a Window are defined as the entire area occupied by
  206. /// that Window. This includes the area needed for a title-bar, menu,
  207. /// borders, and other non-client elements.
  208. ///
  209. /// @{
  210. /// Resize the window to have some new bounds.
  211. virtual void setBounds( const RectI &newBounds ) = 0;
  212. /// Get the position and size (fullscreen windows are always at (0,0)).
  213. virtual const RectI getBounds() const = 0;
  214. /// @}
  215. /// The Position of a window is always in relation to the very upper left
  216. /// of the window. This means that saying setPosition at 0,0 will put the
  217. /// position of the window title-bar (if one exists) at 0,0 and the Client
  218. /// area will be offset from that point by the space needed for the Non-Client
  219. /// area.
  220. /// @{
  221. /// Set the position of this window
  222. virtual void setPosition( const Point2I newPosition ) = 0;
  223. /// Get the position of this window
  224. virtual const Point2I getPosition() = 0;
  225. virtual void centerWindow() {};
  226. /// Resize the window to have a new size (but be in the same position).
  227. virtual bool setSize(const Point2I &newSize)=0;
  228. /// @}
  229. /// @name Coordinate Space Conversion
  230. /// @{
  231. /// Convert the coordinate given in this window space to screen coordinates.
  232. virtual Point2I clientToScreen( const Point2I& point ) = 0;
  233. /// Convert the given screen coordinates to coordinates in this window space.
  234. virtual Point2I screenToClient( const Point2I& point ) = 0;
  235. /// @}
  236. /// @name Windowed state
  237. ///
  238. /// This is only really meaningful if the window is not fullscreen.
  239. ///
  240. /// @{
  241. /// Returns true if the window is instantiated in the OS.
  242. virtual bool isOpen() = 0;
  243. /// Returns true if the window is visible.
  244. virtual bool isVisible() = 0;
  245. /// Returns true if the window has input focus
  246. virtual bool isFocused() = 0;
  247. /// Returns true if the window is minimized
  248. virtual bool isMinimized() = 0;
  249. /// Returns true if the window is maximized
  250. virtual bool isMaximized() = 0;
  251. /// @name Keyboard Translation
  252. ///
  253. /// When keyboard translation is on, keypress events that correspond to character input
  254. /// should be send as character input events rather than as raw key events *except* if
  255. /// shouldNotTranslate() returns true for a specific keypress event. This enables the
  256. /// platform layer to perform platform-specific character input mapping.
  257. ///
  258. /// @{
  259. /// Set if relevant keypress events should be translated into character input events.
  260. virtual void setKeyboardTranslation(const bool enabled)
  261. {
  262. mEnableKeyboardTranslation = enabled;
  263. }
  264. /// Returns true if keyboard translation is enabled.
  265. virtual bool getKeyboardTranslation() const
  266. {
  267. return mEnableKeyboardTranslation;
  268. }
  269. /// Returns true if the given keypress event should not be translated.
  270. virtual bool shouldNotTranslate( U32 modifiers, U32 keyCode ) const;
  271. /// @}
  272. /// Used to disable native OS keyboard accelerators.
  273. virtual void setAcceleratorsEnabled(const bool enabled)
  274. {
  275. mEnableAccelerators = enabled;
  276. }
  277. /// Returns true if native OS keyboard accelerators are enabled.
  278. virtual bool getAcceleratorsEnabled() const
  279. {
  280. return mEnableAccelerators;
  281. }
  282. /// Sets a minimum window size. We'll work with the OS to prevent user
  283. /// from sizing the window to less than this. Setting to (0,0) means
  284. /// user has complete freedom of resize.
  285. virtual void setMinimumWindowSize(Point2I minSize)
  286. {
  287. mMinimumSize = minSize;
  288. }
  289. /// Returns the current minimum window size for this window.
  290. virtual Point2I getMinimumWindowSize()
  291. {
  292. return mMinimumSize;
  293. }
  294. /// Locks/unlocks window resizing
  295. virtual void lockSize(bool locked)
  296. {
  297. mResizeLocked = locked;
  298. if (mResizeLocked)
  299. mLockedSize = getBounds().extent;
  300. }
  301. /// Returns true if the window size is locked
  302. virtual bool isSizeLocked()
  303. {
  304. return mResizeLocked;
  305. }
  306. /// Returns the locked window size
  307. virtual Point2I getLockedSize()
  308. {
  309. return mLockedSize;
  310. }
  311. /// @}
  312. /// @name Window Cursor
  313. ///
  314. /// Accessors to control a windows cursor shape and visibility
  315. ///
  316. /// @{
  317. /// Get the CursorController that this window owns.
  318. virtual PlatformCursorController *getCursorController() { return mCursorController; };
  319. /// Set the cursor position based on logical coordinates from the upper-right corner
  320. ///
  321. /// @param x The X position of the cursor
  322. /// @param y The Y position of the cursor
  323. virtual void setCursorPosition(S32 x, S32 y)
  324. {
  325. if( mCursorController != NULL )
  326. mCursorController->setCursorPosition(x,y);
  327. }
  328. /// Get the cursor position based on logical coordinates from the upper-right corner
  329. ///
  330. /// @param point A reference to a Point2I to store the coordinates
  331. virtual void getCursorPosition( Point2I &point )
  332. {
  333. if( mCursorController != NULL )
  334. mCursorController->getCursorPosition(point);
  335. }
  336. /// Set the cursor visibility on this window
  337. ///
  338. /// @param visible Whether the cursor should be visible or not
  339. virtual void setCursorVisible(bool visible)
  340. {
  341. if( mCursorController != NULL )
  342. mCursorController->setCursorVisible(visible);
  343. }
  344. /// Get the cursor visibility on this window
  345. ///
  346. /// @return true if the cursor is visible or false if it's hidden
  347. virtual bool isCursorVisible()
  348. {
  349. if( mCursorController != NULL )
  350. return mCursorController->isCursorVisible();
  351. return false;
  352. }
  353. /// Lock the mouse to this window.
  354. ///
  355. /// When this is set, the mouse will always be returned to the center
  356. /// of the client area after every mouse event. The mouse will also be
  357. /// hidden while it is locked.
  358. ///
  359. /// The mouse cannot be moved out of the bounds of the window, but the
  360. /// window may lose focus (for instance by an alt-tab or other event).
  361. /// While the window lacks focus, no mouse events will be reported.
  362. virtual void setMouseLocked( bool enable )=0;
  363. /// Is the mouse locked ?
  364. virtual bool isMouseLocked() const = 0;
  365. /// Should the mouse be locked at the next opportunity ?
  366. ///
  367. /// This flag is set to the current state of the mouse lock
  368. /// on a window, to specify the preferred lock status of the
  369. /// mouse in a platform window.
  370. ///
  371. /// This is important for situations where a call is made
  372. /// to setMouseLocked, and the window is not in a state that
  373. /// it can be cleanly locked. Take for example if it was called
  374. /// while the window is in the background, then it is not appropriate
  375. /// to lock the window, but rather the window should query this
  376. /// state at it's next opportunity and lock the mouse if requested.
  377. virtual bool shouldLockMouse() const = 0;
  378. /// @}
  379. virtual PlatformWindow * getNextWindow() const = 0;
  380. /// @name Event Handlers
  381. ///
  382. /// Various events that this window receives. These are all subclasses of
  383. /// JournaledSignal, so you can subscribe to them and receive notifications
  384. /// per the documentation for that class.
  385. ///
  386. /// @{
  387. ///
  388. AppEvent appEvent;
  389. MouseEvent mouseEvent;
  390. MouseWheelEvent wheelEvent;
  391. ButtonEvent buttonEvent;
  392. LinearEvent linearEvent;
  393. KeyEvent keyEvent;
  394. CharEvent charEvent;
  395. DisplayEvent displayEvent;
  396. ResizeEvent resizeEvent;
  397. IdleEvent idleEvent;
  398. /// @}
  399. /// Get the platform specific object needed to create or attach an accelerated
  400. /// graohics drawing context on or to the window
  401. /// Win32 D3D and OpenGL typically needs an HWND
  402. /// Mac Cocoa OpenGL typically needs an NSOpenGLView
  403. /// Mac Carbon OpenGL typically needs a WindowRef
  404. ///
  405. virtual void* getPlatformDrawable() const = 0;
  406. protected:
  407. virtual void _setFullscreen(const bool fullScreen) {};
  408. };
  409. #endif