platformWindow.h 16 KB

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