ShapeVector.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. Vector2 mPolygonScale; ///< Polygon Scale.
  36. Vector<Vector2> mPolygonBasisList; ///< Polygon Basis List.
  37. Vector<Vector2> mPolygonLocalList; ///< Polygon Local List.
  38. bool mIsCircle;
  39. F32 mCircleRadius;
  40. bool mFlipX;
  41. bool mFlipY;
  42. public:
  43. ShapeVector();
  44. ~ShapeVector();
  45. static void initPersistFields();
  46. /// Polygon Configuration.
  47. void setPolyScale( const Vector2& scale );
  48. void setPolyPrimitive( const U32 polyVertexCount );
  49. void setPolyCustom( const U32 polyVertexCount, const char* pCustomPolygon );
  50. /// Rendering Attributes.
  51. void setLineColorString( const char* lineColour );
  52. void setLineColor( const ColorF& lineColour );
  53. void setLineAlpha( const F32 alpha );
  54. void setFillColorString( const char* fillColour );
  55. void setFillColor( const ColorF& fillColour );
  56. void setFillAlpha( const F32 alpha );
  57. void setFillMode( const bool fillMode );
  58. void setIsCircle( const bool isCircle );
  59. void setCircleRadius( F32 radius );
  60. /// Retrieval.
  61. U32 getPolyVertexCount( void ) { return U32(mPolygonBasisList.size()); };
  62. const char* getPoly( void );
  63. const char* getWorldPoly( void );
  64. inline const Vector2* getPolyBasis( void ) const { return &(mPolygonBasisList[0]); };
  65. bool getFillMode( void );
  66. const char* getLineColor( void );
  67. const char* getFillColor( void );
  68. bool getIsCircle( void );
  69. F32 getCircleRadius ( void );
  70. Vector2 getBoxFromPoints( void );
  71. /// Internal Crunchers.
  72. void generateLocalPoly( void );
  73. void renderCircleShape(Vector2 position, F32 radius);
  74. void renderPolygonShape(U32 vertexCount);
  75. /// Render flipping.
  76. inline void setFlip( const bool flipX, const bool flipY ) { mFlipX = flipX; mFlipY = flipY; generateLocalPoly(); }
  77. inline void setFlipX( const bool flipX ) { setFlip( flipX, mFlipY ); }
  78. inline void setFlipY( const bool flipY ) { setFlip( mFlipX, flipY ); }
  79. inline bool getFlipX(void) const { return mFlipX; }
  80. inline bool getFlipY(void) const { return mFlipY; }
  81. virtual void setSize( const Vector2& size );
  82. /// Core.
  83. virtual bool onAdd();
  84. virtual void onRemove();
  85. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  86. virtual bool validRender( void ) const { return (mPolygonLocalList.size() > 0 || mIsCircle); }
  87. virtual bool shouldRender( void ) const { return true; }
  88. /// Render batching.
  89. virtual bool isBatchRendered( void ) { return false; }
  90. /// Clone support
  91. void copyTo(SimObject* obj);
  92. /// Declare Console Object.
  93. DECLARE_CONOBJECT(ShapeVector);
  94. protected:
  95. static bool setPolyList(void* obj, const char* data)
  96. {
  97. //Vector2 poly[b2_maxPolygonVertices];
  98. U32 count = Utility::mGetStringElementCount(data) >> 1;
  99. //for (U32 i = 0; i < count; i++)
  100. // poly[i] = Utility::mGetStringElementVector(data, i * 2);
  101. static_cast<ShapeVector*>(obj)->setPolyCustom(count, data);
  102. return false;
  103. }
  104. static bool writePolyList( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mPolygonBasisList.size() > 0; }
  105. static bool setLineColor(void* obj, const char* data) { static_cast<ShapeVector*>(obj)->setLineColorString(data); return false; }
  106. static bool writeLineColor( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mLineColor != ColorF(1.0f,1.0f,1.0f,1.0f); }
  107. static bool setFillColor(void* obj, const char* data) { static_cast<ShapeVector*>(obj)->setFillColorString(data); return false; }
  108. static bool writeFillColor( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mFillColor != ColorF(0.5f,0.5f,0.5f,1.0f); }
  109. static bool setFillMode(void* obj, const char* data) { static_cast<ShapeVector*>(obj)->setFillMode(dAtob(data)); return false; }
  110. static bool writeFillMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mFillMode == true; }
  111. static bool setIsCircle(void* obj, const char* data) { static_cast<ShapeVector*>(obj)->setIsCircle(dAtob(data)); return false; }
  112. static bool writeIsCircle( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mIsCircle == true; }
  113. static bool setCircleRadius(void* obj, const char* data) { static_cast<ShapeVector*>(obj)->setCircleRadius(dAtof(data)); return false; }
  114. static bool writeCircleRadius( void* obj, StringTableEntry pFieldName ) { return static_cast<ShapeVector*>(obj)->mIsCircle != 1; }
  115. };
  116. #endif // _SHAPE_VECTOR_H_