ShapeVector.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 _SHAPE_VECTOR_H_
  23. #define _SHAPE_VECTOR_H_
  24. #ifndef _SCENE_OBJECT_H_
  25. #include "2d/sceneobject/SceneObject.h"
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. class ShapeVector : public SceneObject
  29. {
  30. typedef SceneObject Parent;
  31. protected:
  32. ColorF mLineColor;
  33. ColorF mFillColor;
  34. bool mFillMode;
  35. Vector<Vector2> mPolygonBasisList; ///< Polygon Basis List.
  36. Vector<Vector2> mPolygonLocalList; ///< Polygon Local List.
  37. bool mIsCircle;
  38. F32 mCircleRadius;
  39. bool mFlipX;
  40. bool mFlipY;
  41. public:
  42. ShapeVector();
  43. ~ShapeVector();
  44. static void initPersistFields();
  45. /// Polygon Configuration.
  46. void setPolyPrimitive( const U32 polyVertexCount );
  47. void setPolyCustom( const U32 polyVertexCount, const char* pCustomPolygon );
  48. U32 getPolyVertexCount( void ) { return U32(mPolygonBasisList.size()); };
  49. inline const Vector2* getPolyBasis( void ) const { return &(mPolygonBasisList[0]); };
  50. const char* getPoly( void );
  51. const char* getWorldPoly( void );
  52. inline void setLineColor( const ColorF& linecolor ) { mLineColor = linecolor; }
  53. inline const ColorF& getLineColor( void ) const { return mLineColor; }
  54. inline void setLineAlpha( const F32 alpha ) { mLineColor.alpha = alpha; }
  55. inline void setFillColor( const ColorF& fillcolor ) { mFillColor = fillcolor; }
  56. inline const ColorF& getFillColor( void ) const { return mFillColor; }
  57. inline void setFillAlpha( const F32 alpha ) { mFillColor.alpha = alpha; }
  58. inline void setFillMode( const bool fillMode ) { mFillMode = fillMode; }
  59. inline bool getFillMode( void ) const { return mFillMode; }
  60. inline void setIsCircle( const bool isCircle ) { mIsCircle = isCircle; }
  61. inline bool getIsCircle( void ) const { return mIsCircle; }
  62. inline void setCircleRadius( const F32 circleRadius ) { mCircleRadius = circleRadius; }
  63. inline F32 getCircleRadius ( void ) const { return mCircleRadius; }
  64. Vector2 getBoxFromPoints( void );
  65. /// Internal Crunchers.
  66. void generateLocalPoly( void );
  67. void renderCircleShape(Vector2 position, F32 radius, const bool wireFrame);
  68. void renderPolygonShape(U32 vertexCount, const bool wireFrame);
  69. /// Render flipping.
  70. inline void setFlip( const bool flipX, const bool flipY ) { mFlipX = flipX; mFlipY = flipY; generateLocalPoly(); }
  71. inline void setFlipX( const bool flipX ) { setFlip( flipX, mFlipY ); }
  72. inline void setFlipY( const bool flipY ) { setFlip( mFlipX, flipY ); }
  73. inline bool getFlipX(void) const { return mFlipX; }
  74. inline bool getFlipY(void) const { return mFlipY; }
  75. virtual void setSize( const Vector2& size );
  76. /// Core.
  77. virtual bool onAdd();
  78. virtual void onRemove();
  79. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  80. virtual bool validRender( void ) const { return (mPolygonLocalList.size() > 0 || mIsCircle); }
  81. virtual bool shouldRender( void ) const { return true; }
  82. /// Render batching.
  83. virtual bool isBatchRendered( void ) { return false; }
  84. /// Clone support
  85. void copyTo(SimObject* obj);
  86. /// Declare Console Object.
  87. DECLARE_CONOBJECT(ShapeVector);
  88. protected:
  89. static bool setPolyList(void* obj, const char* data)
  90. {
  91. const U32 count = Utility::mGetStringElementCount(data) >> 1;
  92. static_cast<ShapeVector*>(obj)->setPolyCustom(count, data);
  93. return false;
  94. }
  95. static bool writePolyList( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mPolygonBasisList.size() > 0; }
  96. static bool writeLineColor( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mLineColor != ColorF(1.0f,1.0f,1.0f,1.0f); }
  97. static bool writeFillColor( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mFillColor != ColorF(0.5f,0.5f,0.5f,1.0f); }
  98. static bool writeFillMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mFillMode == true; }
  99. static bool writeIsCircle( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mIsCircle == true; }
  100. static bool writeCircleRadius( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mCircleRadius != 1; }
  101. };
  102. #endif // _SHAPE_VECTOR_H_