guiCanvas.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 _GUICANVAS_H_
  23. #define _GUICANVAS_H_
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. #ifndef _GUICONTROL_H_
  28. #include "gui/core/guiControl.h"
  29. #endif
  30. #ifndef _PLATFORMINPUT_H_
  31. #include "platform/platformInput.h"
  32. #endif
  33. #include "component/interfaces/IProcessInput.h"
  34. #include "windowManager/platformWindowMgr.h"
  35. #include "gfx/gfxFence.h"
  36. #ifdef TORQUE_DEMO_PURCHASE
  37. #ifndef _PURCHASESCREEN_H_
  38. #include "demo/purchase/purchaseScreen.h"
  39. #endif
  40. #endif
  41. /// A canvas on which rendering occurs.
  42. ///
  43. ///
  44. /// @section GuiCanvas_contents What a GUICanvas Can Contain...
  45. ///
  46. /// @subsection GuiCanvas_content_contentcontrol Content Control
  47. /// A content control is the top level GuiControl for a screen. This GuiControl
  48. /// will be the parent control for all other GuiControls on that particular
  49. /// screen.
  50. ///
  51. /// @subsection GuiCanvas_content_dialogs Dialogs
  52. ///
  53. /// A dialog is essentially another screen, only it gets overlaid on top of the
  54. /// current content control, and all input goes to the dialog. This is most akin
  55. /// to the "Open File" dialog box found in most operating systems. When you
  56. /// choose to open a file, and the "Open File" dialog pops up, you can no longer
  57. /// send input to the application, and must complete or cancel the open file
  58. /// request. Torque keeps track of layers of dialogs. The dialog with the highest
  59. /// layer is on top and will get all the input, unless the dialog is
  60. /// modeless, which is a profile option.
  61. ///
  62. /// @see GuiControlProfile
  63. ///
  64. /// @section GuiCanvas_dirty Dirty Rectangles
  65. ///
  66. /// The GuiCanvas is based on dirty regions.
  67. ///
  68. /// Every frame the canvas paints only the areas of the canvas that are 'dirty'
  69. /// or need updating. In most cases, this only is the area under the mouse cursor.
  70. /// This is why if you look in guiCanvas.cc the call to glClear is commented out.
  71. /// If you want a really good idea of what exactly dirty regions are and how they
  72. /// work, un-comment that glClear line in the renderFrame method of guiCanvas.cc
  73. ///
  74. /// What you will see is a black screen, except in the dirty regions, where the
  75. /// screen will be painted normally. If you are making an animated GuiControl
  76. /// you need to add your control to the dirty areas of the canvas.
  77. ///
  78. class GuiCanvas : public GuiControl, public IProcessInput
  79. {
  80. protected:
  81. typedef GuiControl Parent;
  82. /// @name Rendering
  83. /// @{
  84. RectI mOldUpdateRects[2];
  85. RectI mCurUpdateRect;
  86. U32 mLastRenderMs;
  87. /// @}
  88. /// @name Cursor Properties
  89. /// @{
  90. bool mCursorEnabled;
  91. bool mShowCursor;
  92. bool mRenderFront;
  93. Point2F mCursorPt; ///< Current cursor position in local coordinates.
  94. Point2I mLastCursorPt;
  95. GuiCursor *mDefaultCursor;
  96. GuiCursor *mLastCursor;
  97. bool mLastCursorEnabled;
  98. bool mForceMouseToGUI;
  99. bool mClampTorqueCursor;
  100. bool mAlwaysHandleMouseButtons;
  101. bool mDisplayWindow;
  102. /// @}
  103. /// @name Mouse Input
  104. /// @{
  105. SimObjectPtr<GuiControl> mMouseCapturedControl; ///< All mouse events will go to this ctrl only
  106. SimObjectPtr<GuiControl> mMouseControl; ///< the control the mouse was last seen in unless some other one captured it
  107. bool mMouseControlClicked; ///< whether the current ctrl has been clicked - used by helpctrl
  108. U32 mPrevMouseTime; ///< this determines how long the mouse has been in the same control
  109. bool mMouseButtonDown; ///< Flag to determine if the button is depressed
  110. bool mMouseRightButtonDown; ///< bool to determine if the right button is depressed
  111. bool mMouseMiddleButtonDown; ///< Middle button flag
  112. GuiEvent mLastEvent;
  113. U8 mLastMouseClickCount;
  114. S32 mLastMouseDownTime;
  115. bool mLeftMouseLast;
  116. bool mMiddleMouseLast;
  117. bool mRightMouseLast;
  118. Point2F mMouseDownPoint;
  119. /// Processes keyboard input events. Helper method for processInputEvent
  120. ///
  121. /// \param inputEvent Information on the input even to be processed.
  122. /// \return True if the event was handled or false if it was not.
  123. virtual bool processKeyboardEvent(InputEventInfo &inputEvent);
  124. /// Processes mouse input events. Helper method for processInputEvent
  125. ///
  126. /// \param inputEvent Information on the input even to be processed.
  127. /// \return True if the event was handled or false if it was not.
  128. virtual bool processMouseEvent(InputEventInfo &inputEvent);
  129. /// Processes gamepad input events. Helper method for processInputEvent
  130. ///
  131. /// \param inputEvent Information on the input even to be processed.
  132. /// \return True if the event was handled or false if it was not.
  133. virtual bool processGamepadEvent(InputEventInfo &inputEvent);
  134. virtual void findMouseControl(const GuiEvent &event);
  135. virtual void refreshMouseControl();
  136. /// @}
  137. /// @name Keyboard Input
  138. /// @{
  139. /// Accelerator key map
  140. struct AccKeyMap
  141. {
  142. GuiControl *ctrl;
  143. U32 index;
  144. U32 keyCode;
  145. U32 modifier;
  146. };
  147. Vector <AccKeyMap> mAcceleratorMap;
  148. //for tooltip rendering
  149. U32 mHoverControlStart;
  150. GuiControl* mHoverControl;
  151. Point2I mHoverPosition;
  152. bool mHoverPositionSet;
  153. U32 mHoverLeftControlTime;
  154. /// @}
  155. // Internal event handling callbacks for use with PlatformWindow.
  156. void handleResize (WindowId did, S32 width, S32 height);
  157. void handleAppEvent (WindowId did, S32 event);
  158. void handlePaintEvent (WindowId did);
  159. PlatformWindow *mPlatformWindow;
  160. GFXFence **mFences;
  161. S32 mNextFenceIdx;
  162. S32 mNumFences;
  163. static bool setProtectedNumFences( void *object, const char *index, const char *data );
  164. virtual void setupFences();
  165. void checkLockMouseMove( const GuiEvent& event );
  166. GuiControl *mMenuBarCtrl;
  167. public:
  168. DECLARE_CONOBJECT(GuiCanvas);
  169. DECLARE_CATEGORY( "Gui Core" );
  170. GuiCanvas();
  171. virtual ~GuiCanvas();
  172. virtual bool onAdd();
  173. virtual void onRemove();
  174. void setMenuBar(SimObject *obj);
  175. static void initPersistFields();
  176. /// @name Rendering methods
  177. ///
  178. /// @{
  179. /// Repaints the dirty regions of the canvas
  180. /// @param preRenderOnly If set to true, only the onPreRender methods of all the GuiControls will be called
  181. /// @param bufferSwap If set to true, it will swap buffers at the end. This is to support canvas-subclassing.
  182. virtual void renderFrame(bool preRenderOnly, bool bufferSwap = true);
  183. /// Repaints the canvas by calling the platform window display event.
  184. virtual void paint();
  185. /// Repaints the canvas skipping rendering if the target time
  186. /// has not yet elapsed.
  187. /// @param elapsedMS The time since the last frame.
  188. virtual void repaint(U32 elapsedMS);
  189. /// This signal is triggered at the beginning and end of each render frame
  190. ///
  191. /// @param beginFrame true at the beginning of the frame, false at the end
  192. ///
  193. typedef Signal <void ( bool beginFrame )> GuiCanvasFrameSignal;
  194. static GuiCanvasFrameSignal& getGuiCanvasFrameSignal();
  195. /// Adds a dirty area to the canvas so it will be updated on the next frame
  196. /// @param pos Screen-coordinates of the upper-left hand corner of the dirty area
  197. /// @param ext Width/height of the dirty area
  198. virtual void addUpdateRegion(Point2I pos, Point2I ext);
  199. /// Resets the update regions so that the next call to renderFrame will
  200. /// repaint the whole canvas
  201. virtual void resetUpdateRegions();
  202. /// Resizes the content control to match the canvas size.
  203. void maintainSizing();
  204. /// This builds a rectangle which encompasses all of the dirty regions to be
  205. /// repainted
  206. /// @param updateUnion (out) Rectangle which surrounds all dirty areas
  207. virtual void buildUpdateUnion(RectI *updateUnion);
  208. /// This will swap the buffers at the end of renderFrame. It was added for canvas
  209. /// sub-classes in case they wanted to do some custom code before the buffer
  210. /// flip occured.
  211. virtual void swapBuffers();
  212. /// @}
  213. /// @name Canvas Content Management
  214. /// @{
  215. /// This returns the PlatformWindow owned by this Canvas
  216. virtual PlatformWindow *getPlatformWindow()
  217. {
  218. return mPlatformWindow;
  219. }
  220. /// This sets the content control to something different
  221. /// @param gui New content control
  222. virtual void setContentControl(GuiControl *gui);
  223. /// Returns the content control
  224. virtual GuiControl *getContentControl();
  225. /// Adds a dialog control onto the stack of dialogs
  226. /// @param gui Dialog to add
  227. /// @param layer Layer to put dialog on
  228. /// @param center Center dialog on canvas.
  229. virtual void pushDialogControl(GuiControl *gui, S32 layer = 0, bool center = false);
  230. /// Removes a specific layer of dialogs
  231. /// @param layer Layer to pop off from
  232. virtual void popDialogControl(S32 layer = 0);
  233. /// Removes a specific dialog control
  234. /// @param gui Dialog to remove from the dialog stack
  235. virtual void popDialogControl(GuiControl *gui);
  236. ///@}
  237. /// This turns on/off front-buffer rendering
  238. /// @param front True if all rendering should be done to the front buffer
  239. virtual void setRenderFront(bool front) { mRenderFront = front; }
  240. /// @name Cursor commands
  241. /// A cursor can be on, but not be shown. If a cursor is not on, than it does not
  242. /// process input.
  243. /// @{
  244. /// Sets the cursor for the canvas.
  245. /// @param cursor New cursor to use.
  246. virtual void setCursor(GuiCursor *cursor);
  247. S32 mCursorChanged;
  248. /// Returns true if the cursor is on.
  249. virtual bool isCursorON() { return mCursorEnabled; }
  250. /// Sets if mouse events should be passed to the GUI even if the cursor is off.
  251. /// @param onOff True if events should be passed to the GUI if the cursor is off
  252. virtual void setForceMouseToGUI(bool onOff);
  253. /// Sets if the Torque cursor should be clamped to the window.
  254. /// @param onOff True if the Torque cursor should be clamped against the window
  255. virtual void setClampTorqueCursor(bool onOff);
  256. /// Returns if the Torque cursor is clamped to the window
  257. virtual bool getClampTorqueCursor() { return mClampTorqueCursor; }
  258. /// Turns the cursor on or off.
  259. /// @param onOff True if the cursor should be on.
  260. virtual void setCursorON(bool onOff);
  261. /// Sets the position of the cursor
  262. /// @param pt Point, in screenspace for the cursor
  263. virtual void setCursorPos(const Point2I &pt);
  264. /// Returns the point, in screenspace, at which the cursor is located.
  265. virtual Point2I getCursorPos();
  266. /// Enable/disable rendering of the cursor.
  267. /// @param state True if we should render cursor
  268. virtual void showCursor(bool state);
  269. /// Returns true if the cursor is being rendered.
  270. virtual bool isCursorShown();
  271. /// @}
  272. ///used by the tooltip resource
  273. Point2I getCursorExtent() { return mDefaultCursor->getExtent(); }
  274. /// @name Input Processing
  275. /// @{
  276. /// Processes an input event
  277. /// @see InputEvent
  278. /// @param event Input event to process
  279. virtual bool processInputEvent(InputEventInfo &inputEvent);
  280. /// @}
  281. /// @name Mouse Methods
  282. /// @{
  283. /// When a control gets the mouse lock this means that that control gets
  284. /// ALL mouse input and no other control receives any input.
  285. /// @param lockingControl Control to lock mouse to
  286. virtual void mouseLock(GuiControl *lockingControl);
  287. /// Unlocks the mouse from a control
  288. /// @param lockingControl Control to unlock from
  289. virtual void mouseUnlock(GuiControl *lockingControl);
  290. /// Returns the control which the mouse is over
  291. virtual GuiControl* getMouseControl() { return mMouseControl; }
  292. /// Returns the control which the mouse is locked to if any
  293. virtual GuiControl* getMouseLockedControl() { return mMouseCapturedControl; }
  294. /// Returns true if the left mouse button is down
  295. virtual bool mouseButtonDown(void) { return mMouseButtonDown; }
  296. /// Returns true if the right mouse button is down
  297. virtual bool mouseRightButtonDown(void) { return mMouseRightButtonDown; }
  298. /// @}
  299. /// @name Mouse input methods
  300. /// These events process the events before passing them down to the
  301. /// controls they effect. This allows for things such as the input
  302. /// locking and such.
  303. ///
  304. /// Each of these methods corresponds to the action in it's method name
  305. /// and processes the GuiEvent passed as a parameter
  306. /// @{
  307. virtual void rootMouseUp(const GuiEvent &event);
  308. virtual void rootMouseDown(const GuiEvent &event);
  309. virtual void rootMouseMove(const GuiEvent &event);
  310. virtual void rootMouseDragged(const GuiEvent &event);
  311. virtual void rootRightMouseDown(const GuiEvent &event);
  312. virtual void rootRightMouseUp(const GuiEvent &event);
  313. virtual void rootRightMouseDragged(const GuiEvent &event);
  314. virtual void rootMiddleMouseDown(const GuiEvent &event);
  315. virtual void rootMiddleMouseUp(const GuiEvent &event);
  316. virtual void rootMiddleMouseDragged(const GuiEvent &event);
  317. virtual bool rootMouseWheelUp(const GuiEvent &event);
  318. virtual bool rootMouseWheelDown(const GuiEvent &event);
  319. /// @}
  320. /// @name Keyboard input methods
  321. /// First responders
  322. ///
  323. /// A first responder is a the GuiControl which responds first to input events
  324. /// before passing them off for further processing.
  325. /// @{
  326. /// Moves the first responder to the next tabable controle
  327. virtual bool tabNext(void);
  328. /// Moves the first responder to the previous tabable control
  329. virtual bool tabPrev(void);
  330. /// Setups a keyboard accelerator which maps to a GuiControl.
  331. ///
  332. /// @param ctrl GuiControl to map to.
  333. /// @param index
  334. /// @param keyCode Key code.
  335. /// @param modifier Shift, ctrl, etc.
  336. virtual void addAcceleratorKey(GuiControl *ctrl, U32 index, U32 keyCode, U32 modifier);
  337. /// Sets the first responder.
  338. /// @param firstResponder Control to designate as first responder
  339. virtual void setFirstResponder(GuiControl *firstResponder);
  340. /// This is used to toggle processing of native OS accelerators, not
  341. /// to be confused with the Torque accelerator key system, to keep them
  342. /// from swallowing up keystrokes. Both GuiTextEditCtrl and GuiTextPadCtrl
  343. /// use this method.
  344. virtual void setNativeAcceleratorsEnabled( bool enabled );
  345. /// @}
  346. ///
  347. virtual Point2I getWindowSize();
  348. virtual void enableKeyboardTranslation();
  349. virtual void disableKeyboardTranslation();
  350. virtual void setWindowTitle(const char *newTitle);
  351. private:
  352. static const U32 MAX_GAMEPADS = 4; ///< The maximum number of supported gamepads
  353. #ifdef TORQUE_DEMO_PURCHASE
  354. private:
  355. PurchaseScreen* mPurchaseScreen;
  356. U32 mLastPurchaseHideTime;
  357. public:
  358. void showPurchaseScreen(bool show, bool startBlocker, const char* location, bool doExit);
  359. void updatePurchaseScreen(const char* value);
  360. #endif
  361. #ifdef TORQUE_DEMO_TIMEOUT
  362. private:
  363. void checkTimeOut();
  364. #endif
  365. };
  366. #endif