SceneWindow.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _SCENE_WINDOW_H_
  23. #define _SCENE_WINDOW_H_
  24. #ifndef _GUICONTROL_H_
  25. #include "gui/guiControl.h"
  26. #endif
  27. #ifndef _VECTOR_H_
  28. #include "collection/vector.h"
  29. #endif
  30. #ifndef _SCENE_H_
  31. #include "2d/scene/Scene.h"
  32. #endif
  33. #ifndef _VECTOR2_H_
  34. #include "2d/core/Vector2.h"
  35. #endif
  36. #ifndef _UTILITY_H_
  37. #include "2d/core/Utility.h"
  38. #endif
  39. class GuiSceneScrollCtrl;
  40. class SceneWindow : public GuiControl, public virtual Tickable
  41. {
  42. typedef GuiControl Parent;
  43. public:
  44. virtual bool onWake();
  45. virtual void onSleep();
  46. virtual void setControlThumbProfile(GuiControlProfile* prof);
  47. virtual void setControlTrackProfile(GuiControlProfile* prof);
  48. virtual void setControlArrowProfile(GuiControlProfile* prof);
  49. /// Camera View.
  50. struct CameraView
  51. {
  52. // Source.
  53. RectF mSourceArea;
  54. F32 mCameraZoom;
  55. F32 mCameraAngle;
  56. // Destination.
  57. RectF mDestinationArea;
  58. Point2F mSceneMin;
  59. Point2F mSceneMax;
  60. Point2F mSceneWindowScale;
  61. CameraView()
  62. {
  63. mSourceArea = RectF(0.0f, 0.0f, 10.0f, 10.0f);
  64. mCameraZoom = 1.0f;
  65. mCameraAngle = 0.0f;
  66. mDestinationArea = RectF(0.0f, 0.0f, 10.0f, 10.0f);
  67. mSceneMin = Point2F(0.0f, 0.0f);
  68. mSceneMax = Point2F(10.0f, 10.0f);
  69. mSceneWindowScale = Point2F(1.0f, 1.0f);
  70. }
  71. };
  72. private:
  73. /// Cameras.
  74. CameraView mCameraCurrent, mCameraSource, mCameraTarget;
  75. // Camera Interpolation.
  76. Vector<CameraView> mCameraQueue;
  77. S32 mMaxQueueItems;
  78. F32 mCameraTransitionTime;
  79. F32 mPreCameraTime;
  80. F32 mPostCameraTime;
  81. F32 mRenderCameraTime;
  82. F32 mCurrentCameraTime;
  83. bool mMovingCamera;
  84. /// Tick Properties.
  85. Point2F mPreTickPosition;
  86. Point2F mPostTickPosition;
  87. /// Background color.
  88. ColorF mBackgroundColor;
  89. bool mUseBackgroundColor;
  90. /// Camera Attachment.
  91. bool mCameraMounted;
  92. SceneObject* mpMountedTo;
  93. Vector2 mMountOffset;
  94. U32 mMountToID;
  95. F32 mMountForce;
  96. bool mMountAngle;
  97. /// Camera Scroll bar
  98. bool mShowScrollBar;
  99. GuiSceneScrollCtrl* mScrollBar;
  100. bool mMouseWheelScrolls;
  101. /// View Limit.
  102. bool mViewLimitActive;
  103. Vector2 mViewLimitMin;
  104. Vector2 mViewLimitMax;
  105. Vector2 mViewLimitArea;
  106. /// Camera Shaking.
  107. bool mCameraShaking;
  108. F32 mShakeLife;
  109. F32 mCurrentShake;
  110. F32 mShakeRamp;
  111. Vector2 mCameraShakeOffset;
  112. /// Misc.
  113. Scene* mpScene;
  114. S32 mLastRenderTime;
  115. bool mLockMouse;
  116. bool mWindowDirty;
  117. // Input Events.
  118. bool mUseWindowInputEvents;
  119. bool mUseObjectInputEvents;
  120. U32 mInputEventGroupMaskFilter;
  121. U32 mInputEventLayerMaskFilter;
  122. bool mInputEventInvisibleFilter;
  123. typeWorldQueryResultVector mInputEventQuery;
  124. typeSceneObjectVector mInputEventEntering;
  125. typeSceneObjectVector mInputEventLeaving;
  126. SimSet mInputEventWatching;
  127. SimSet mInputListeners;
  128. /// Render Masks.
  129. U32 mRenderLayerMask;
  130. U32 mRenderGroupMask;
  131. //Audio Listener
  132. bool mProcessAudioListener;
  133. char mDebugText[256];
  134. /// Handling Input Events.
  135. bool dispatchInputEvent( StringTableEntry name, const GuiEvent& event );
  136. bool sendWindowInputEvent( StringTableEntry name, const GuiEvent& event );
  137. bool sendObjectInputEvent( StringTableEntry, const GuiEvent& event );
  138. void calculateCameraView( CameraView* pCameraView );
  139. //Standard Scrolling settings
  140. bool mUseConstantHeightThumb;
  141. S32 mScrollBarThickness;
  142. bool mShowArrowButtons;
  143. GuiControlProfile *mThumbProfile;
  144. GuiControlProfile *mTrackProfile;
  145. GuiControlProfile *mArrowProfile;
  146. public:
  147. /// Camera Interpolation Mode.
  148. enum CameraInterpolationMode
  149. {
  150. INVALID_INTERPOLATION_MODE,
  151. LINEAR, ///< Standard Linear.
  152. SIGMOID ///< Slow Start / Slow Stop.
  153. } mCameraInterpolationMode;
  154. SceneWindow();
  155. virtual ~SceneWindow();
  156. virtual bool onAdd();
  157. virtual void onRemove();
  158. /// Initialization.
  159. virtual void setScene( Scene* pScene );
  160. virtual void resetScene( void );
  161. inline void setRenderGroups( const U32 groupMask) { mRenderGroupMask = groupMask; }
  162. inline void setRenderLayers( const U32 layerMask) { mRenderLayerMask = layerMask; }
  163. inline void setRenderMasks( const U32 layerMask,const U32 groupMask ) { mRenderLayerMask = layerMask; mRenderGroupMask = groupMask; }
  164. inline U32 getRenderLayerMask( void ) { return mRenderLayerMask; }
  165. inline U32 getRenderGroupMask( void ) { return mRenderGroupMask; }
  166. /// Get scene.
  167. inline Scene* getScene( void ) const { return mpScene; };
  168. /// Mouse.
  169. void setLockMouse( bool lockStatus ) { mLockMouse = lockStatus; };
  170. bool getLockMouse( void ) { return mLockMouse; };
  171. Vector2 getMousePosition( void );
  172. void setMousePosition( const Vector2& mousePosition );
  173. /// Background color.
  174. inline void setBackgroundColor( const ColorF& backgroundColor ) { mBackgroundColor = backgroundColor; }
  175. inline const ColorF& getBackgroundColor( void ) const { return mBackgroundColor; }
  176. inline void setUseBackgroundColor( const bool useBackgroundColor ) { mUseBackgroundColor = useBackgroundColor; }
  177. inline bool getUseBackgroundColor( void ) const { return mUseBackgroundColor; }
  178. /// Input.
  179. void setObjectInputEventFilter( const U32 groupMask, const U32 layerMask, const bool useInvisible = false );
  180. void setObjectInputEventGroupFilter( const U32 groupMask );
  181. void setObjectInputEventLayerFilter( const U32 layerMask );
  182. void setObjectInputEventInvisibleFilter( const bool useInvisible );
  183. inline void setUseWindowInputEvents( const bool inputStatus ) { mUseWindowInputEvents = inputStatus; };
  184. inline void setUseObjectInputEvents( const bool inputStatus ) { mUseObjectInputEvents = inputStatus; };
  185. inline bool getUseWindowInputEvents( void ) const { return mUseWindowInputEvents; };
  186. inline bool getUseObjectInputEvents( void ) const { return mUseObjectInputEvents; };
  187. inline void clearWatchedInputEvents( void ) { mInputEventWatching.clear(); }
  188. inline void removeFromInputEventPick(SceneObject* pSceneObject ) { mInputEventWatching.removeObject((SimObject*)pSceneObject); }
  189. void addInputListener( SimObject* pSimObject );
  190. void removeInputListener( SimObject* pSimObject );
  191. /// Coordinate Conversion.
  192. void windowToScenePoint( const Vector2& srcPoint, Vector2& dstPoint ) const;
  193. void sceneToWindowPoint( const Vector2& srcPoint, Vector2& dstPoint ) const;
  194. /// Mounting.
  195. void mount( SceneObject* pSceneObject, const Vector2& mountOffset, const F32 mountForce, const bool sendToMount, const bool mountAngle );
  196. void dismount( void );
  197. void dismountMe( SceneObject* pSceneObject );
  198. void calculateCameraMount( const F32 elapsedTime );
  199. void interpolateCameraMount( const F32 timeDelta );
  200. /// View Limit.
  201. void setViewLimitOn( const Vector2& limitMin, const Vector2& limitMax );
  202. inline void setViewLimitOff( void ) { mViewLimitActive = false; };
  203. inline bool isViewLimitOn( void ) const { return mViewLimitActive; }
  204. inline Vector2 getViewLimitMin( void ) const { return mViewLimitMin; }
  205. inline Vector2 getViewLimitMax( void ) const { return mViewLimitMax; }
  206. void clampCameraViewLimit( void );
  207. void setShowScrollBar(bool setting);
  208. inline bool getShowScrollBar() { return mShowScrollBar; }
  209. void updateScrollBar();
  210. inline void setMouseWheelScrolls(bool setting) { mMouseWheelScrolls = setting; }
  211. inline bool getMouseWheelScrolls() { return mMouseWheelScrolls; }
  212. /// Tick Processing.
  213. void zeroCameraTime( void );
  214. void resetTickCameraTime( void );
  215. void updateTickCameraTime( void );
  216. void resetTickCameraPosition( void );
  217. virtual void interpolateTick( F32 delta );
  218. virtual void processTick();
  219. virtual void advanceTime( F32 timeDelta ) {};
  220. /// Camera,
  221. virtual void setCameraPosition( const Vector2& position );
  222. inline Vector2 getCameraPosition( void ) const { return mCameraCurrent.mSourceArea.centre(); }
  223. void setCameraSize( const Vector2& size );
  224. inline Vector2 getCameraSize( void ) const { return Vector2( mCameraCurrent.mSourceArea.extent ); }
  225. virtual void setCameraArea( const RectF& cameraWindow );
  226. inline RectF getCameraArea( void ) const { return mCameraCurrent.mSourceArea; }
  227. void setCameraZoom( const F32 zoomFactor );
  228. inline F32 getCameraZoom( void ) const { return mCameraCurrent.mCameraZoom; }
  229. void setCameraAngle( const F32 cameraAngle );
  230. inline F32 getCameraAngle( void ) const { return mRadToDeg(mCameraCurrent.mCameraAngle); }
  231. /// Target Camera.
  232. virtual void setTargetCameraPosition( const Vector2& position );
  233. inline Vector2 getTargetCameraPosition( void ) const { return mCameraTarget.mSourceArea.centre(); }
  234. void setTargetCameraSize( const Vector2& size );
  235. inline Vector2 getTargetCameraSize( void ) const { return Vector2( mCameraTarget.mSourceArea.extent ); }
  236. virtual void setTargetCameraArea( const RectF& cameraWindow );
  237. inline RectF getTargetCameraArea( void ) const { return mCameraTarget.mSourceArea; }
  238. void setTargetCameraZoom( const F32 zoomFactor );
  239. inline F32 getTargetCameraZoom( void ) const { return mCameraTarget.mCameraZoom; }
  240. void setTargetCameraAngle( const F32 cameraAngle );
  241. inline F32 getTargetCameraAngle( void ) const { return mRadToDeg(mCameraTarget.mCameraAngle); }
  242. /// Camera Interpolation Time/Mode.
  243. void setCameraInterpolationTime( const F32 interpolationTime );
  244. void setCameraInterpolationMode( const CameraInterpolationMode interpolationMode );
  245. /// Audio Listener
  246. void setProcessAudioListener(bool mval);
  247. /// Camera Movement.
  248. void startCameraMove( const F32 interpolationTime );
  249. void stopCameraMove( void );
  250. void completeCameraMove( void );
  251. void undoCameraMove( const F32 interpolationTime );
  252. F32 interpolate( F32 from, F32 to, F32 delta );
  253. void updateCamera( void );
  254. inline Vector2 getCameraRenderPosition( void ) { calculateCameraView( &mCameraCurrent ); return mCameraCurrent.mDestinationArea.centre(); }
  255. inline RectF getCameraRenderArea( void ) { calculateCameraView( &mCameraCurrent ); return mCameraCurrent.mDestinationArea; }
  256. inline const Vector2 getCameraWindowScale( void ) const { return mCameraCurrent.mSceneWindowScale; }
  257. inline F32 getCameraInterpolationTime( void ) { return mCameraTransitionTime; }
  258. inline const CameraView& getCamera(void) const { return mCameraCurrent; }
  259. inline const Vector2& getCameraShake(void) const { return mCameraShakeOffset; }
  260. inline bool isCameraMounted( void ) const { return mCameraMounted; }
  261. inline bool isCameraMoving( void ) const { return mMovingCamera; }
  262. /// Audio Listener
  263. inline bool isProcessAudioListener(void) const { return mProcessAudioListener; }
  264. /// Camera Shake.
  265. void startCameraShake( const F32 magnitude, const F32 time );
  266. void stopCameraShake( void );
  267. static void initPersistFields();
  268. /// GuiControl
  269. virtual void resize(const Point2I &newPosition, const Point2I &newExtent);
  270. virtual void onRender( Point2I offset, const RectI& updateRect );
  271. virtual void onTouchEnter( const GuiEvent& event );
  272. virtual void onTouchLeave( const GuiEvent& event );
  273. virtual void onTouchDown( const GuiEvent& event );
  274. virtual void onTouchUp( const GuiEvent& event );
  275. virtual void onTouchMove( const GuiEvent& event );
  276. virtual void onTouchDragged( const GuiEvent& event );
  277. virtual void onMiddleMouseDown(const GuiEvent &event);
  278. virtual void onMiddleMouseUp(const GuiEvent &event);
  279. virtual void onMiddleMouseDragged(const GuiEvent &event);
  280. virtual void onRightMouseDown( const GuiEvent& event );
  281. virtual void onRightMouseUp( const GuiEvent& event );
  282. virtual void onRightMouseDragged( const GuiEvent& event );
  283. virtual void onMouseWheelDown( const GuiEvent &event );
  284. virtual void onMouseWheelUp( const GuiEvent &event );
  285. void setUseConstantThumbHeight(const bool setting);
  286. void setScrollBarThickness(const S32 thickness);
  287. void setShowArrowButtons(const bool setting);
  288. void renderMetricsOverlay( Point2I offset, const RectI& updateRect );
  289. static CameraInterpolationMode getInterpolationModeEnum(const char* label);
  290. /// Declare Console Object.
  291. DECLARE_CONOBJECT(SceneWindow);
  292. protected:
  293. static bool writeLockMouse( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mLockMouse == true; }
  294. static bool writeUseWindowInputEvents( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseWindowInputEvents == false; }
  295. static bool writeUseObjectInputEvents( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseObjectInputEvents == true; }
  296. static bool writeBackgroundColor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseBackgroundColor == true; }
  297. static bool writeUseBackgroundColor(void* obj, StringTableEntry pFieldName) { return static_cast<SceneWindow*>(obj)->mUseBackgroundColor == true; }
  298. static bool writeScrollSettingFn(void* obj, StringTableEntry pFieldName) { return static_cast<SceneWindow*>(obj)->mShowScrollBar == true; }
  299. static bool setConstantThumbFn(void* obj, const char* data) { SceneWindow* ctrl = static_cast<SceneWindow*>(obj); ctrl->setUseConstantThumbHeight(dAtob(data)); return false; }
  300. static bool setScrollBarThicknessFn(void* obj, const char* data) { SceneWindow* ctrl = static_cast<SceneWindow*>(obj); ctrl->setScrollBarThickness(dAtoi(data)); return false; }
  301. static bool setShowArrowButtonsFn(void* obj, const char* data) { SceneWindow* ctrl = static_cast<SceneWindow*>(obj); ctrl->setShowArrowButtons(dAtob(data)); return false; }
  302. static bool setThumbProfileFn(void* obj, const char* data)
  303. {
  304. SceneWindow* ctrl = static_cast<SceneWindow*>(obj);
  305. SimObject* simObj = Sim::findObject(data);
  306. if (simObj)
  307. {
  308. GuiControlProfile* profile = dynamic_cast<GuiControlProfile*>(simObj);
  309. if(profile)
  310. {
  311. ctrl->setControlThumbProfile(profile);
  312. return false;
  313. }
  314. }
  315. Con::warnf("SceneWindow::setThumbProfileFn - Unable to find requested profile.");
  316. return true;
  317. }
  318. static bool setTrackProfileFn(void* obj, const char* data)
  319. {
  320. SceneWindow* ctrl = static_cast<SceneWindow*>(obj);
  321. SimObject* simObj = Sim::findObject(data);
  322. if (simObj)
  323. {
  324. GuiControlProfile* profile = dynamic_cast<GuiControlProfile*>(simObj);
  325. if (profile)
  326. {
  327. ctrl->setControlTrackProfile(profile);
  328. return false;
  329. }
  330. }
  331. Con::warnf("SceneWindow::setTrackProfileFn - Unable to find requested profile.");
  332. return true;
  333. }
  334. static bool setArrowProfileFn(void* obj, const char* data)
  335. {
  336. SceneWindow* ctrl = static_cast<SceneWindow*>(obj);
  337. SimObject* simObj = Sim::findObject(data);
  338. if (simObj)
  339. {
  340. GuiControlProfile* profile = dynamic_cast<GuiControlProfile*>(simObj);
  341. if (profile)
  342. {
  343. ctrl->setControlArrowProfile(profile);
  344. return false;
  345. }
  346. }
  347. Con::warnf("SceneWindow::setArrowProfileFn - Unable to find requested profile.");
  348. return true;
  349. }
  350. };
  351. #endif // _SCENE_WINDOW_H_