platformWindow.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. // This controller maps window input (Mouse/Keyboard) to a generic input consumer
  90. mWindowInputGenerator = new WindowInputGenerator( this );
  91. mSuppressReset = false;
  92. mOffscreenRender = false;
  93. mDisplayWindow = false;
  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. /// Get the ID that uniquely identifies this window in the context of its
  106. /// window manager.
  107. virtual WindowId getWindowId() { return 0; };
  108. /// Set the flag that determines whether to suppress a GFXDevice reset
  109. inline void setSuppressReset(bool suppress) { mSuppressReset = suppress; };
  110. /// @name GFX State Management
  111. ///
  112. /// @{
  113. /// Return a pointer to the GFX device this window is bound to. A GFX
  114. /// device may use many windows, but a window can only be used by a
  115. /// single GFX device.
  116. virtual GFXDevice *getGFXDevice()=0;
  117. /// Return a pointer to this window's render target.
  118. ///
  119. /// By setting window targets from different windows, we can effect
  120. /// rendering to multiple windows from a single device.
  121. virtual GFXWindowTarget *getGFXTarget()=0;
  122. /// Set the video mode for this window.
  123. virtual void setVideoMode(const GFXVideoMode &mode)=0;
  124. /// Get our current video mode - if the window has been resized, it will
  125. /// reflect this.
  126. virtual const GFXVideoMode &getVideoMode()=0;
  127. /// If we're fullscreen, this function returns us to desktop mode.
  128. ///
  129. /// This will be either the last mode that we had that was not
  130. /// fullscreen, or the equivalent mode, windowed.
  131. virtual bool clearFullscreen()=0;
  132. /// @return true if this window is fullscreen, false otherwise.
  133. virtual bool isFullscreen()=0;
  134. /// Acquire the entire screen
  135. void setFullscreen(const bool fullscreen);
  136. /// Set Idle State (Background)
  137. ///
  138. /// This is called to put a window into idle state, which causes it's
  139. /// rendering priority to be toned down to prefer performance
  140. virtual void setBackground( bool val ) { mIsBackground = val; };
  141. /// Get Idle State (Background)
  142. ///
  143. /// This is called to poll the window as to it's idle state.
  144. virtual bool getBackground() { return mIsBackground; };
  145. /// Set whether this window is intended for offscreen rendering
  146. /// An offscreen window will never be shown or lose focus
  147. virtual void setOffscreenRender(bool val ) { mOffscreenRender = val; };
  148. /// Set whether this window is intended for offscreen rendering
  149. ///
  150. /// This is called to poll the window as to it's idle state.
  151. virtual bool getOffscreenRender() { return mOffscreenRender; };
  152. /// Set whether this window is should display as normal
  153. virtual void setDisplayWindow(bool val ) { mDisplayWindow = val; };
  154. /// Set Focused State (Foreground)
  155. ///
  156. /// Claim OS input focus for this window
  157. virtual void setFocus() { }
  158. /// @}
  159. /// @name Caption
  160. ///
  161. /// @{
  162. /// Set the window's caption.
  163. virtual bool setCaption(const char *cap)=0;
  164. /// Get the window's caption.
  165. virtual const char *getCaption()=0;
  166. /// @}
  167. /// @name Visibility
  168. ///
  169. /// Control how the window is displayed
  170. /// @{
  171. /// Minimize the window on screen
  172. virtual void minimize()=0;
  173. /// Maximize the window on screen
  174. virtual void maximize()=0;
  175. /// Hide the window on screen
  176. virtual void hide()=0;
  177. /// Show the window on screen
  178. virtual void show()=0;
  179. /// Destroy the window on screen
  180. virtual void close()=0;
  181. /// Restore the window from a Maximized or Minimized state
  182. virtual void restore()=0;
  183. /// @}
  184. /// @name Window Bounds
  185. ///
  186. /// @{
  187. /// The Client Rectangle or "Render Area" of a window is the area that
  188. /// is occupied by a given client that is rendering to that window.
  189. /// This does not include the area occupied by a title-bar, menu,
  190. /// borders or other non-client elements.
  191. /// @{
  192. /// Set the Client Area Extent (Resolution) of this window
  193. virtual void setClientExtent( const Point2I newExtent ) = 0;
  194. /// Get the Client Area Extent (Resolution) of this window
  195. virtual const Point2I getClientExtent() = 0;
  196. /// @}
  197. /// The bounds of a Window are defined as the entire area occupied by
  198. /// that Window. This includes the area needed for a title-bar, menu,
  199. /// borders, and other non-client elements.
  200. ///
  201. /// @{
  202. /// Resize the window to have some new bounds.
  203. virtual void setBounds( const RectI &newBounds ) = 0;
  204. /// Get the position and size (fullscreen windows are always at (0,0)).
  205. virtual const RectI getBounds() const = 0;
  206. /// @}
  207. /// The Position of a window is always in relation to the very upper left
  208. /// of the window. This means that saying setPosition at 0,0 will put the
  209. /// position of the window title-bar (if one exists) at 0,0 and the Client
  210. /// area will be offset from that point by the space needed for the Non-Client
  211. /// area.
  212. /// @{
  213. /// Set the position of this window
  214. virtual void setPosition( const Point2I newPosition ) = 0;
  215. /// Get the position of this window
  216. virtual const Point2I getPosition() = 0;
  217. virtual void centerWindow() {};
  218. /// Resize the window to have a new size (but be in the same position).
  219. virtual bool setSize(const Point2I &newSize)=0;
  220. /// @}
  221. /// @name Coordinate Space Conversion
  222. /// @{
  223. /// Convert the coordinate given in this window space to screen coordinates.
  224. virtual Point2I clientToScreen( const Point2I& point ) = 0;
  225. /// Convert the given screen coordinates to coordinates in this window space.
  226. virtual Point2I screenToClient( const Point2I& point ) = 0;
  227. /// @}
  228. /// @name Windowed state
  229. ///
  230. /// This is only really meaningful if the window is not fullscreen.
  231. ///
  232. /// @{
  233. /// Returns true if the window is instantiated in the OS.
  234. virtual bool isOpen() = 0;
  235. /// Returns true if the window is visible.
  236. virtual bool isVisible() = 0;
  237. /// Returns true if the window has input focus
  238. virtual bool isFocused() = 0;
  239. /// Returns true if the window is minimized
  240. virtual bool isMinimized() = 0;
  241. /// Returns true if the window is maximized
  242. virtual bool isMaximized() = 0;
  243. /// @name Keyboard Translation
  244. ///
  245. /// When keyboard translation is on, keypress events that correspond to character input
  246. /// should be send as character input events rather than as raw key events *except* if
  247. /// shouldNotTranslate() returns true for a specific keypress event. This enables the
  248. /// platform layer to perform platform-specific character input mapping.
  249. ///
  250. /// @{
  251. /// Set if relevant keypress events should be translated into character input events.
  252. virtual void setKeyboardTranslation(const bool enabled)
  253. {
  254. mEnableKeyboardTranslation = enabled;
  255. }
  256. /// Returns true if keyboard translation is enabled.
  257. virtual bool getKeyboardTranslation() const
  258. {
  259. return mEnableKeyboardTranslation;
  260. }
  261. /// Returns true if the given keypress event should not be translated.
  262. virtual bool shouldNotTranslate( U32 modifiers, U32 keyCode ) const;
  263. /// @}
  264. /// Used to disable native OS keyboard accelerators.
  265. virtual void setAcceleratorsEnabled(const bool enabled)
  266. {
  267. mEnableAccelerators = enabled;
  268. }
  269. /// Returns true if native OS keyboard accelerators are enabled.
  270. virtual bool getAcceleratorsEnabled() const
  271. {
  272. return mEnableAccelerators;
  273. }
  274. /// Sets a minimum window size. We'll work with the OS to prevent user
  275. /// from sizing the window to less than this. Setting to (0,0) means
  276. /// user has complete freedom of resize.
  277. virtual void setMinimumWindowSize(Point2I minSize)
  278. {
  279. mMinimumSize = minSize;
  280. }
  281. /// Returns the current minimum window size for this window.
  282. virtual Point2I getMinimumWindowSize()
  283. {
  284. return mMinimumSize;
  285. }
  286. /// Locks/unlocks window resizing
  287. virtual void lockSize(bool locked)
  288. {
  289. mResizeLocked = locked;
  290. if (mResizeLocked)
  291. mLockedSize = getBounds().extent;
  292. }
  293. /// Returns true if the window size is locked
  294. virtual bool isSizeLocked()
  295. {
  296. return mResizeLocked;
  297. }
  298. /// Returns the locked window size
  299. virtual Point2I getLockedSize()
  300. {
  301. return mLockedSize;
  302. }
  303. /// @}
  304. /// @name Window Cursor
  305. ///
  306. /// Accessors to control a windows cursor shape and visibility
  307. ///
  308. /// @{
  309. /// Get the CursorController that this window owns.
  310. virtual PlatformCursorController *getCursorController() { return mCursorController; };
  311. /// Set the cursor position based on logical coordinates from the upper-right corner
  312. ///
  313. /// @param x The X position of the cursor
  314. /// @param y The Y position of the cursor
  315. virtual void setCursorPosition(S32 x, S32 y)
  316. {
  317. if( mCursorController != NULL )
  318. mCursorController->setCursorPosition(x,y);
  319. }
  320. /// Get the cursor position based on logical coordinates from the upper-right corner
  321. ///
  322. /// @param point A reference to a Point2I to store the coordinates
  323. virtual void getCursorPosition( Point2I &point )
  324. {
  325. if( mCursorController != NULL )
  326. mCursorController->getCursorPosition(point);
  327. }
  328. /// Set the cursor visibility on this window
  329. ///
  330. /// @param visible Whether the cursor should be visible or not
  331. virtual void setCursorVisible(bool visible)
  332. {
  333. if( mCursorController != NULL )
  334. mCursorController->setCursorVisible(visible);
  335. }
  336. /// Get the cursor visibility on this window
  337. ///
  338. /// @return true if the cursor is visible or false if it's hidden
  339. virtual bool isCursorVisible()
  340. {
  341. if( mCursorController != NULL )
  342. return mCursorController->isCursorVisible();
  343. return false;
  344. }
  345. /// Lock the mouse to this window.
  346. ///
  347. /// When this is set, the mouse will always be returned to the center
  348. /// of the client area after every mouse event. The mouse will also be
  349. /// hidden while it is locked.
  350. ///
  351. /// The mouse cannot be moved out of the bounds of the window, but the
  352. /// window may lose focus (for instance by an alt-tab or other event).
  353. /// While the window lacks focus, no mouse events will be reported.
  354. virtual void setMouseLocked( bool enable )=0;
  355. /// Is the mouse locked ?
  356. virtual bool isMouseLocked() const = 0;
  357. /// Should the mouse be locked at the next opportunity ?
  358. ///
  359. /// This flag is set to the current state of the mouse lock
  360. /// on a window, to specify the preferred lock status of the
  361. /// mouse in a platform window.
  362. ///
  363. /// This is important for situations where a call is made
  364. /// to setMouseLocked, and the window is not in a state that
  365. /// it can be cleanly locked. Take for example if it was called
  366. /// while the window is in the background, then it is not appropriate
  367. /// to lock the window, but rather the window should query this
  368. /// state at it's next opportunity and lock the mouse if requested.
  369. virtual bool shouldLockMouse() const = 0;
  370. /// @}
  371. virtual PlatformWindow * getNextWindow() const = 0;
  372. /// @name Event Handlers
  373. ///
  374. /// Various events that this window receives. These are all subclasses of
  375. /// JournaledSignal, so you can subscribe to them and receive notifications
  376. /// per the documentation for that class.
  377. ///
  378. /// @{
  379. ///
  380. AppEvent appEvent;
  381. MouseEvent mouseEvent;
  382. MouseWheelEvent wheelEvent;
  383. ButtonEvent buttonEvent;
  384. LinearEvent linearEvent;
  385. KeyEvent keyEvent;
  386. CharEvent charEvent;
  387. DisplayEvent displayEvent;
  388. ResizeEvent resizeEvent;
  389. IdleEvent idleEvent;
  390. /// @}
  391. /// Get the platform specific object needed to create or attach an accelerated
  392. /// graohics drawing context on or to the window
  393. /// Win32 D3D and OpenGL typically needs an HWND
  394. /// Mac Cocoa OpenGL typically needs an NSOpenGLView
  395. /// Mac Carbon OpenGL typically needs a WindowRef
  396. ///
  397. virtual void* getPlatformDrawable() const = 0;
  398. protected:
  399. virtual void _setFullscreen(const bool fullScreen) {};
  400. };
  401. #endif