SceneWindow.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. //-----------------------------------------------------------------------------
  40. class SceneWindow : public GuiControl, public virtual Tickable
  41. {
  42. typedef GuiControl Parent;
  43. public:
  44. /// Camera View.
  45. struct CameraView
  46. {
  47. // Source.
  48. RectF mSourceArea;
  49. F32 mCameraZoom;
  50. F32 mCameraAngle;
  51. // Destination.
  52. RectF mDestinationArea;
  53. Point2F mSceneMin;
  54. Point2F mSceneMax;
  55. Point2F mSceneWindowScale;
  56. CameraView()
  57. {
  58. mSourceArea = RectF(0.0f, 0.0f, 10.0f, 10.0f);
  59. mCameraZoom = 1.0f;
  60. mCameraAngle = 0.0f;
  61. mDestinationArea = RectF(0.0f, 0.0f, 10.0f, 10.0f);
  62. mSceneMin = Point2F(0.0f, 0.0f);
  63. mSceneMax = Point2F(10.0f, 10.0f);
  64. mSceneWindowScale = Point2F(1.0f, 1.0f);
  65. }
  66. };
  67. private:
  68. /// Cameras.
  69. CameraView mCameraCurrent, mCameraSource, mCameraTarget;
  70. // Camera Interpolation.
  71. Vector<CameraView> mCameraQueue;
  72. S32 mMaxQueueItems;
  73. F32 mCameraTransitionTime;
  74. F32 mPreCameraTime;
  75. F32 mPostCameraTime;
  76. F32 mRenderCameraTime;
  77. F32 mCurrentCameraTime;
  78. bool mMovingCamera;
  79. /// Tick Properties.
  80. Point2F mPreTickPosition;
  81. Point2F mPostTickPosition;
  82. /// Background color.
  83. ColorF mBackgroundColor;
  84. bool mUseBackgroundColor;
  85. /// Camera Attachment.
  86. bool mCameraMounted;
  87. SceneObject* mpMountedTo;
  88. Vector2 mMountOffset;
  89. U32 mMountToID;
  90. F32 mMountForce;
  91. bool mMountAngle;
  92. /// View Limit.
  93. bool mViewLimitActive;
  94. Vector2 mViewLimitMin;
  95. Vector2 mViewLimitMax;
  96. Vector2 mViewLimitArea;
  97. /// Camera Shaking.
  98. bool mCameraShaking;
  99. F32 mShakeLife;
  100. F32 mCurrentShake;
  101. F32 mShakeRamp;
  102. Vector2 mCameraShakeOffset;
  103. /// Misc.
  104. Scene* mpScene;
  105. S32 mLastRenderTime;
  106. bool mLockMouse;
  107. bool mWindowDirty;
  108. // Input Events.
  109. bool mUseWindowInputEvents;
  110. bool mUseObjectInputEvents;
  111. U32 mInputEventGroupMaskFilter;
  112. U32 mInputEventLayerMaskFilter;
  113. bool mInputEventInvisibleFilter;
  114. typeWorldQueryResultVector mInputEventQuery;
  115. typeSceneObjectVector mInputEventEntering;
  116. typeSceneObjectVector mInputEventLeaving;
  117. SimSet mInputEventWatching;
  118. SimSet mInputListeners;
  119. /// Render Masks.
  120. U32 mRenderLayerMask;
  121. U32 mRenderGroupMask;
  122. char mDebugText[256];
  123. /// Handling Input Events.
  124. void dispatchInputEvent( StringTableEntry name, const GuiEvent& event );
  125. void sendWindowInputEvent( StringTableEntry name, const GuiEvent& event );
  126. void sendObjectInputEvent( StringTableEntry, const GuiEvent& event );
  127. void calculateCameraView( CameraView* pCameraView );
  128. public:
  129. /// Camera Interpolation Mode.
  130. enum CameraInterpolationMode
  131. {
  132. INVALID_INTERPOLATION_MODE,
  133. LINEAR, ///< Standard Linear.
  134. SIGMOID ///< Slow Start / Slow Stop.
  135. } mCameraInterpolationMode;
  136. SceneWindow();
  137. virtual ~SceneWindow();
  138. virtual bool onAdd();
  139. virtual void onRemove();
  140. /// Initialization.
  141. virtual void setScene( Scene* pScene );
  142. virtual void resetScene( void );
  143. inline void setRenderGroups( const U32 groupMask) { mRenderGroupMask = groupMask; }
  144. inline void setRenderLayers( const U32 layerMask) { mRenderLayerMask = layerMask; }
  145. inline void setRenderMasks( const U32 layerMask,const U32 groupMask ) { mRenderLayerMask = layerMask; mRenderGroupMask = groupMask; }
  146. inline U32 getRenderLayerMask( void ) { return mRenderLayerMask; }
  147. inline U32 getRenderGroupMask( void ) { return mRenderGroupMask; }
  148. /// Get scene.
  149. inline Scene* getScene( void ) const { return mpScene; };
  150. /// Mouse.
  151. void setLockMouse( bool lockStatus ) { mLockMouse = lockStatus; };
  152. bool getLockMouse( void ) { return mLockMouse; };
  153. Vector2 getMousePosition( void );
  154. void setMousePosition( const Vector2& mousePosition );
  155. /// Background color.
  156. inline void setBackgroundColor( const ColorF& backgroundColor ) { mBackgroundColor = backgroundColor; }
  157. inline const ColorF& getBackgroundColor( void ) const { return mBackgroundColor; }
  158. inline void setUseBackgroundColor( const bool useBackgroundColor ) { mUseBackgroundColor = useBackgroundColor; }
  159. inline bool getUseBackgroundColor( void ) const { return mUseBackgroundColor; }
  160. /// Input.
  161. void setObjectInputEventFilter( const U32 groupMask, const U32 layerMask, const bool useInvisible = false );
  162. void setObjectInputEventGroupFilter( const U32 groupMask );
  163. void setObjectInputEventLayerFilter( const U32 layerMask );
  164. void setObjectInputEventInvisibleFilter( const bool useInvisible );
  165. inline void setUseWindowInputEvents( const bool inputStatus ) { mUseWindowInputEvents = inputStatus; };
  166. inline void setUseObjectInputEvents( const bool inputStatus ) { mUseObjectInputEvents = inputStatus; };
  167. inline bool getUseWindowInputEvents( void ) const { return mUseWindowInputEvents; };
  168. inline bool getUseObjectInputEvents( void ) const { return mUseObjectInputEvents; };
  169. inline void clearWatchedInputEvents( void ) { mInputEventWatching.clear(); }
  170. inline void removeFromInputEventPick(SceneObject* pSceneObject ) { mInputEventWatching.removeObject((SimObject*)pSceneObject); }
  171. void addInputListener( SimObject* pSimObject );
  172. void removeInputListener( SimObject* pSimObject );
  173. /// Coordinate Conversion.
  174. void windowToScenePoint( const Vector2& srcPoint, Vector2& dstPoint ) const;
  175. void sceneToWindowPoint( const Vector2& srcPoint, Vector2& dstPoint ) const;
  176. /// Mounting.
  177. void mount( SceneObject* pSceneObject, const Vector2& mountOffset, const F32 mountForce, const bool sendToMount, const bool mountAngle );
  178. void dismount( void );
  179. void dismountMe( SceneObject* pSceneObject );
  180. void calculateCameraMount( const F32 elapsedTime );
  181. void interpolateCameraMount( const F32 timeDelta );
  182. /// View Limit.
  183. void setViewLimitOn( const Vector2& limitMin, const Vector2& limitMax );
  184. inline void setViewLimitOff( void ) { mViewLimitActive = false; };
  185. inline bool isViewLimitOn( void ) const { return mViewLimitActive; }
  186. inline Vector2 getViewLimitMin( void ) const { return mViewLimitMin; }
  187. inline Vector2 getViewLimitMax( void ) const { return mViewLimitMax; }
  188. inline void clampCameraViewLimit( void );
  189. /// Tick Processing.
  190. void zeroCameraTime( void );
  191. void resetTickCameraTime( void );
  192. void updateTickCameraTime( void );
  193. void resetTickCameraPosition( void );
  194. virtual void interpolateTick( F32 delta );
  195. virtual void processTick();
  196. virtual void advanceTime( F32 timeDelta ) {};
  197. /// Camera,
  198. virtual void setCameraPosition( const Vector2& position );
  199. inline Vector2 getCameraPosition( void ) const { return mCameraCurrent.mSourceArea.centre(); }
  200. void setCameraSize( const Vector2& size );
  201. inline Vector2 getCameraSize( void ) const { return Vector2( mCameraCurrent.mSourceArea.extent ); }
  202. virtual void setCameraArea( const RectF& cameraWindow );
  203. inline RectF getCameraArea( void ) const { return mCameraCurrent.mSourceArea; }
  204. void setCameraZoom( const F32 zoomFactor );
  205. inline F32 getCameraZoom( void ) const { return mCameraCurrent.mCameraZoom; }
  206. void setCameraAngle( const F32 cameraAngle );
  207. inline F32 getCameraAngle( void ) const { return mRadToDeg(mCameraCurrent.mCameraAngle); }
  208. /// Target Camera.
  209. virtual void setTargetCameraPosition( const Vector2& position );
  210. inline Vector2 getTargetCameraPosition( void ) const { return mCameraTarget.mSourceArea.centre(); }
  211. void setTargetCameraSize( const Vector2& size );
  212. inline Vector2 getTargetCameraSize( void ) const { return Vector2( mCameraTarget.mSourceArea.extent ); }
  213. virtual void setTargetCameraArea( const RectF& cameraWindow );
  214. inline RectF getTargetCameraArea( void ) const { return mCameraTarget.mSourceArea; }
  215. void setTargetCameraZoom( const F32 zoomFactor );
  216. inline F32 getTargetCameraZoom( void ) const { return mCameraTarget.mCameraZoom; }
  217. void setTargetCameraAngle( const F32 cameraAngle );
  218. inline F32 getTargetCameraAngle( void ) const { return mRadToDeg(mCameraTarget.mCameraAngle); }
  219. /// Camera Interpolation Time/Mode.
  220. void setCameraInterpolationTime( const F32 interpolationTime );
  221. void setCameraInterpolationMode( const CameraInterpolationMode interpolationMode );
  222. /// Camera Movement.
  223. void startCameraMove( const F32 interpolationTime );
  224. void stopCameraMove( void );
  225. void completeCameraMove( void );
  226. void undoCameraMove( const F32 interpolationTime );
  227. F32 interpolate( F32 from, F32 to, F32 delta );
  228. void updateCamera( void );
  229. inline Vector2 getCameraRenderPosition( void ) { calculateCameraView( &mCameraCurrent ); return mCameraCurrent.mDestinationArea.centre(); }
  230. inline RectF getCameraRenderArea( void ) { calculateCameraView( &mCameraCurrent ); return mCameraCurrent.mDestinationArea; }
  231. inline const Vector2 getCameraWindowScale( void ) const { return mCameraCurrent.mSceneWindowScale; }
  232. inline F32 getCameraInterpolationTime( void ) { return mCameraTransitionTime; }
  233. inline const CameraView& getCamera(void) const { return mCameraCurrent; }
  234. inline const Vector2& getCameraShake(void) const { return mCameraShakeOffset; }
  235. inline bool isCameraMounted( void ) const { return mCameraMounted; }
  236. inline bool isCameraMoving( void ) const { return mMovingCamera; }
  237. /// Camera Shake.
  238. void startCameraShake( const F32 magnitude, const F32 time );
  239. void stopCameraShake( void );
  240. static void initPersistFields();
  241. /// GuiControl
  242. virtual void resize(const Point2I &newPosition, const Point2I &newExtent);
  243. virtual void onRender( Point2I offset, const RectI& updateRect );
  244. virtual void onMouseEnter( const GuiEvent& event );
  245. virtual void onMouseLeave( const GuiEvent& event );
  246. virtual void onMouseDown( const GuiEvent& event );
  247. virtual void onMouseUp( const GuiEvent& event );
  248. virtual void onMouseMove( const GuiEvent& event );
  249. virtual void onMouseDragged( const GuiEvent& event );
  250. virtual void onMiddleMouseDown(const GuiEvent &event);
  251. virtual void onMiddleMouseUp(const GuiEvent &event);
  252. virtual void onMiddleMouseDragged(const GuiEvent &event);
  253. virtual void onRightMouseDown( const GuiEvent& event );
  254. virtual void onRightMouseUp( const GuiEvent& event );
  255. virtual void onRightMouseDragged( const GuiEvent& event );
  256. virtual bool onMouseWheelDown( const GuiEvent &event );
  257. virtual bool onMouseWheelUp( const GuiEvent &event );
  258. void renderMetricsOverlay( Point2I offset, const RectI& updateRect );
  259. static CameraInterpolationMode getInterpolationModeEnum(const char* label);
  260. /// Declare Console Object.
  261. DECLARE_CONOBJECT(SceneWindow);
  262. protected:
  263. static bool writeLockMouse( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mLockMouse == true; }
  264. static bool writeUseWindowInputEvents( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseWindowInputEvents == false; }
  265. static bool writeUseObjectInputEvents( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseObjectInputEvents == true; }
  266. static bool writeBackgroundColor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseBackgroundColor == true; }
  267. static bool writeUseBackgroundColor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneWindow*>(obj)->mUseBackgroundColor == true; }
  268. };
  269. #endif // _SCENE_WINDOW_H_