ShapeVector.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. #include "graphics/dgl.h"
  23. #include "console/consoleTypes.h"
  24. #include "2d/core/Utility.h"
  25. #include "ShapeVector.h"
  26. // Script bindings.
  27. #include "ShapeVector_ScriptBinding.h"
  28. //----------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT(ShapeVector);
  30. //----------------------------------------------------------------------------
  31. ShapeVector::ShapeVector() :
  32. mLineColor(ColorF(1.0f,1.0f,1.0f,1.0f)),
  33. mFillColor(ColorF(0.5f,0.5f,0.5f,1.0f)),
  34. mFillMode(false),
  35. mPolygonScale( 1.0f, 1.0f ),
  36. mIsCircle(false),
  37. mCircleRadius(1.0f),
  38. mFlipX(false),
  39. mFlipY(false)
  40. {
  41. // Set Vector Associations.
  42. VECTOR_SET_ASSOCIATION( mPolygonBasisList );
  43. VECTOR_SET_ASSOCIATION( mPolygonLocalList );
  44. // Use a static body by default.
  45. mBodyDefinition.type = b2_staticBody;
  46. }
  47. //----------------------------------------------------------------------------
  48. ShapeVector::~ShapeVector()
  49. {
  50. }
  51. //----------------------------------------------------------------------------
  52. void ShapeVector::initPersistFields()
  53. {
  54. addProtectedField("PolyList", TypePoint2FVector, Offset(mPolygonBasisList, ShapeVector), &setPolyList, &defaultProtectedGetFn, &writePolyList, "");
  55. addProtectedField("LineColor", TypeColorF, Offset(mLineColor, ShapeVector), &setLineColor, &defaultProtectedGetFn, &writeLineColor, "");
  56. addProtectedField("FillColor", TypeColorF, Offset(mFillColor, ShapeVector), &setFillColor, &defaultProtectedGetFn, &writeFillColor, "");
  57. addProtectedField("FillMode", TypeBool, Offset(mFillMode, ShapeVector), &setFillMode, &defaultProtectedGetFn, &writeFillMode, "");
  58. addProtectedField("IsCircle", TypeBool, Offset(mIsCircle, ShapeVector), &setIsCircle, &defaultProtectedGetFn, &writeIsCircle, "");
  59. addProtectedField("CircleRadius", TypeF32, Offset(mCircleRadius, ShapeVector), &setCircleRadius, &defaultProtectedGetFn, &writeCircleRadius, "");
  60. Parent::initPersistFields();
  61. }
  62. //----------------------------------------------------------------------------
  63. void ShapeVector::copyTo(SimObject* obj)
  64. {
  65. Parent::copyTo(obj);
  66. AssertFatal(dynamic_cast<ShapeVector*>(obj), "ShapeVector::copyTo() - Object is not the correct type.");
  67. ShapeVector* object = static_cast<ShapeVector*>(obj);
  68. // Copy fields
  69. object->mFillMode = mFillMode;
  70. object->mFillColor = mFillColor;
  71. object->mLineColor = mLineColor;
  72. object->mIsCircle = mIsCircle;
  73. object->mCircleRadius = mCircleRadius;
  74. object->mFlipX = mFlipX;
  75. object->mFlipY = mFlipY;
  76. if (getPolyVertexCount() > 0)
  77. object->setPolyCustom(mPolygonBasisList.size(), getPoly());
  78. }
  79. //----------------------------------------------------------------------------
  80. bool ShapeVector::onAdd()
  81. {
  82. // Call Parent.
  83. if(!Parent::onAdd())
  84. return false;
  85. // Return Okay.
  86. return true;
  87. }
  88. //----------------------------------------------------------------------------
  89. void ShapeVector::onRemove()
  90. {
  91. // Call Parent.
  92. Parent::onRemove();
  93. }
  94. //----------------------------------------------------------------------------
  95. void ShapeVector::sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
  96. {
  97. // Fetch Vertex Count.
  98. const U32 vertexCount = mPolygonLocalList.size();
  99. // Finish if not vertices.
  100. if ( vertexCount == 0 && !mIsCircle)
  101. return;
  102. // Disable Texturing.
  103. glDisable ( GL_TEXTURE_2D );
  104. // Save Model-view.
  105. glMatrixMode(GL_MODELVIEW);
  106. glPushMatrix();
  107. // Fetch Position/Rotation.
  108. const Vector2 position = getRenderPosition();
  109. // Set Blend Options.
  110. setBlendOptions();
  111. if (mIsCircle)
  112. {
  113. glRotatef( mRadToDeg(getRenderAngle()), 0.0f, 0.0f, 1.0f );
  114. renderCircleShape(position, mCircleRadius);
  115. }
  116. else
  117. {
  118. // Move into Vector-Space.
  119. glTranslatef( position.x, position.y, 0.0f );
  120. glRotatef( mRadToDeg(getRenderAngle()), 0.0f, 0.0f, 1.0f );
  121. renderPolygonShape(vertexCount);
  122. }
  123. // Restore Colour.
  124. glColor4f( 1,1,1,1 );
  125. // Restore Matrix.
  126. glPopMatrix();
  127. }
  128. void ShapeVector::renderCircleShape(Vector2 position, F32 radius)
  129. {
  130. if (mFillMode)
  131. {
  132. const float32 k_segments = 32.0f;
  133. const float32 k_increment = 2.0f * b2_pi / k_segments;
  134. float32 theta = 0.0f;
  135. glEnable(GL_BLEND);
  136. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  137. glColor4f(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
  138. glBegin(GL_TRIANGLE_FAN);
  139. for (int32 i = 0; i < k_segments; ++i)
  140. {
  141. Vector2 v = position + radius * Vector2(cosf(theta), sinf(theta));
  142. glVertex2f(v.x, v.y);
  143. theta += k_increment;
  144. }
  145. glEnd();
  146. glDisable(GL_BLEND);
  147. theta = 0.0f;
  148. glColor4f(mLineColor.red, mLineColor.green, mLineColor.blue, 1.0f);
  149. glBegin(GL_LINE_LOOP);
  150. for (int32 i = 0; i < k_segments; ++i)
  151. {
  152. Vector2 v = position + radius * Vector2(cosf(theta), sinf(theta));
  153. glVertex2f(v.x, v.y);
  154. theta += k_increment;
  155. }
  156. glEnd();
  157. }
  158. else
  159. {
  160. const float32 k_segments = 36.0f;
  161. const float32 k_increment = 2.0f * b2_pi / k_segments;
  162. float32 theta = 0.0f;
  163. glColor4f(mLineColor.red, mLineColor.green, mLineColor.blue, mLineColor.alpha);
  164. glBegin(GL_LINE_LOOP);
  165. for (int32 i = 0; i < k_segments; ++i)
  166. {
  167. Vector2 v = position + radius * Vector2(cosf(theta), sinf(theta));
  168. glVertex2f(v.x, v.y);
  169. theta += k_increment;
  170. }
  171. glEnd();
  172. }
  173. }
  174. void ShapeVector::renderPolygonShape(U32 vertexCount)
  175. {
  176. #ifdef TORQUE_OS_IOS
  177. // Fill Mode?
  178. if ( mFillMode )
  179. {
  180. // Yes, so set polygon mode to FILL.
  181. //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  182. // Set Fill Colour.
  183. glColor4f( (GLfloat)mFillColor.red, (GLfloat)mFillColor.green, (GLfloat)mFillColor.blue, (GLfloat)mFillColor.alpha );
  184. GLfloat vert1[] = {//get first vert and make triangles based off of this one
  185. (GLfloat)(mPolygonLocalList[0].x),
  186. (GLfloat)(mPolygonLocalList[0].y),
  187. };
  188. GLfloat prevVert[] = {
  189. (GLfloat)(mPolygonLocalList[1].x),
  190. (GLfloat)(mPolygonLocalList[1].y),
  191. };
  192. // Draw Object.
  193. for ( U32 n = 2; n < vertexCount; n++ )
  194. {
  195. //glVertex2fv ( (GLfloat*)&(mPolygonLocalList[n]) );
  196. GLfloat vertex[] = {
  197. vert1[0], vert1[1],
  198. (GLfloat)(mPolygonLocalList[n].x), (GLfloat)(mPolygonLocalList[n].y),
  199. prevVert[0], prevVert[1],
  200. };
  201. glVertexPointer(2, GL_FLOAT, 0, vertex );
  202. glDrawArrays(GL_TRIANGLES, 0, 3 );
  203. prevVert[0] = (GLfloat)(mPolygonLocalList[n].x);//save the current one's for nxt time
  204. prevVert[1] = (GLfloat)(mPolygonLocalList[n].y);
  205. }
  206. //glDrawArrays(GL_TRIANGLES, 0, vertexCount);
  207. //glEnd();
  208. }
  209. // Set Line Colour.
  210. glColor4f(mLineColor.red, mLineColor.green, mLineColor.blue, mLineColor.alpha );
  211. for ( U32 n = 1; n <= vertexCount; n++ )
  212. {
  213. GLfloat verts[] = {
  214. (GLfloat)(mPolygonLocalList[n - 1].x),
  215. (GLfloat)(mPolygonLocalList[n - 1].y),
  216. (GLfloat)(mPolygonLocalList[n == vertexCount ? 0 : n].x),
  217. (GLfloat)(mPolygonLocalList[n == vertexCount ? 0 : n].y),
  218. };
  219. glVertexPointer(2, GL_FLOAT, 0, verts );
  220. glDrawArrays(GL_LINE_LOOP, 0, 2);//draw last two
  221. }
  222. #else
  223. // Fill Mode?
  224. if ( mFillMode )
  225. {
  226. // Yes, so set polygon mode to FILL.
  227. glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  228. // Set Fill Colour.
  229. glColor4fv( (GLfloat*)&mFillColor );
  230. // Draw Object.
  231. glBegin( GL_POLYGON );
  232. for ( U32 n = 0; n < vertexCount; n++ )
  233. {
  234. glVertex2fv ( (GLfloat*)&(mPolygonLocalList[n]) );
  235. }
  236. glEnd();
  237. }
  238. // Set Line Colour.
  239. glColor4fv( (GLfloat*)&mLineColor );
  240. // Draw Object.
  241. glBegin(GL_LINES);
  242. for ( U32 n = 1; n <= vertexCount; n++ )
  243. {
  244. glVertex2fv ( (GLfloat*)&(mPolygonLocalList[n - 1]) );
  245. glVertex2fv ( (GLfloat*)&(mPolygonLocalList[n == vertexCount ? 0 : n]) );
  246. }
  247. glEnd();
  248. #endif
  249. }
  250. //----------------------------------------------------------------------------
  251. void ShapeVector::setSize( const Vector2& size )
  252. {
  253. Vector2 difference = size - mSize;
  254. // Call Parent.
  255. Parent::setSize( size );
  256. if (mIsCircle)
  257. {
  258. F32 total = difference.x + difference.y;
  259. mCircleRadius += total;
  260. }
  261. else
  262. {
  263. // Generate Local Polygon.
  264. generateLocalPoly();
  265. }
  266. }
  267. //----------------------------------------------------------------------------
  268. void ShapeVector::setPolyScale( const Vector2& scale )
  269. {
  270. // Check Scales.
  271. if ( scale.x <= 0.0f || scale.y <= 0.0f )
  272. {
  273. Con::warnf("ShapeVector::setPolyScale() - Polygon Scales must be greater than zero! '%g,%g'.", scale.x, scale.y);
  274. return;
  275. }
  276. // Check Scales.
  277. if ( scale.x > 1.0f || scale.y > 1.0f )
  278. {
  279. Con::warnf("ShapeVector::setPolyScale() - Polygon Scales cannot be greater than one! '%g,%g'.", scale.x, scale.y);
  280. return;
  281. }
  282. // Set Polygon Scale.
  283. mPolygonScale = scale;
  284. // Generation Local Poly.
  285. generateLocalPoly();
  286. }
  287. //----------------------------------------------------------------------------
  288. void ShapeVector::setPolyPrimitive( const U32 polyVertexCount )
  289. {
  290. // Check it's not zero!
  291. if ( polyVertexCount == 0 )
  292. {
  293. // Warn.
  294. Con::warnf("ShapeVector::setPolyPrimitive() - Vertex count must be greater than zero!");
  295. // Finish Here.
  296. return;
  297. }
  298. // Clear Polygon List.
  299. mPolygonBasisList.clear();
  300. mPolygonBasisList.setSize( polyVertexCount );
  301. // Point?
  302. if ( polyVertexCount == 1 )
  303. {
  304. // Set Polygon Point.
  305. mPolygonBasisList[0].Set(0.0f, 0.0f);
  306. }
  307. // Special-Case Quad?
  308. else if ( polyVertexCount == 4 )
  309. {
  310. // Yes, so set Quad.
  311. mPolygonBasisList[0].Set(-1.0f, -1.0f);
  312. mPolygonBasisList[1].Set(+1.0f, -1.0f);
  313. mPolygonBasisList[2].Set(+1.0f, +1.0f);
  314. mPolygonBasisList[3].Set(-1.0f, +1.0f);
  315. }
  316. else
  317. {
  318. // No, so calculate Regular (Primitive) Polygon Stepping.
  319. //
  320. // NOTE:- The polygon sits on an circle that subscribes the interior
  321. // of the collision box.
  322. F32 angle = M_PI_F / polyVertexCount;
  323. const F32 angleStep = M_2PI_F / polyVertexCount;
  324. // Calculate Polygon.
  325. for ( U32 n = 0; n < polyVertexCount; n++ )
  326. {
  327. // Calculate Angle.
  328. angle += angleStep;
  329. // Store Polygon Vertex.
  330. mPolygonBasisList[n].Set(mCos(angle), mSin(angle));
  331. }
  332. }
  333. // Generation Local Poly.
  334. generateLocalPoly();
  335. }
  336. //----------------------------------------------------------------------------
  337. void ShapeVector::setPolyCustom( const U32 polyVertexCount, const char* pCustomPolygon )
  338. {
  339. // Validate Polygon.
  340. if ( polyVertexCount < 1 )
  341. {
  342. // Warn.
  343. Con::warnf("ShapeVector::setPolyCustom() - Vertex count must be greater than zero!");
  344. return;
  345. }
  346. // Fetch Custom Polygon Value Count.
  347. const U32 customCount = Utility::mGetStringElementCount(pCustomPolygon);
  348. // Validate Polygon Custom Length.
  349. if ( customCount != polyVertexCount*2 )
  350. {
  351. // Warn.
  352. Con::warnf("ShapeVector::setPolyCustom() - Invalid Custom Polygon Items '%d'; expected '%d'!", customCount, polyVertexCount*2 );
  353. return;
  354. }
  355. //// Validate Polygon Vertices.
  356. //for ( U32 n = 0; n < customCount; n+=2 )
  357. //{
  358. // // Fetch Coordinate.
  359. // const Vector2 coord = Utility::mGetStringElementVector(pCustomPolygon, n);
  360. // // Check Range.
  361. // if ( coord.x < -1.0f || coord.x > 1.0f || coord.y < -1.0f || coord.y > 1.0f )
  362. // {
  363. // // Warn.
  364. // Con::warnf("ShapeVector::setPolyCustom() - Invalid Polygon Coordinate range; Must be -1 to +1! '(%g,%g)'", coord.x, coord.y );
  365. // return;
  366. // }
  367. //}
  368. // Clear Polygon Basis List.
  369. mPolygonBasisList.clear();
  370. mPolygonBasisList.setSize( polyVertexCount );
  371. // Validate Polygon Vertices.
  372. for ( U32 n = 0; n < polyVertexCount; n++ )
  373. {
  374. // Fetch Coordinate.
  375. const F32 x = dAtof(Utility::mGetStringElement(pCustomPolygon, n*2));
  376. const F32 y = dAtof(Utility::mGetStringElement(pCustomPolygon, n*2+1));
  377. // Store Polygon Vertex.
  378. mPolygonBasisList[n].Set(x, y);
  379. }
  380. // Generation Local Poly.
  381. generateLocalPoly();
  382. }
  383. //----------------------------------------------------------------------------
  384. const char* ShapeVector::getPoly( void )
  385. {
  386. // Get Collision Polygon.
  387. const Vector2* pPoly = (getPolyVertexCount() > 0) ? getPolyBasis() : NULL;
  388. // Set Max Buffer Size.
  389. const U32 maxBufferSize = getPolyVertexCount() * 18 + 1;
  390. // Get Return Buffer.
  391. char* pReturnBuffer = Con::getReturnBuffer( maxBufferSize );
  392. // Check Buffer.
  393. if( !pReturnBuffer )
  394. {
  395. // Warn.
  396. Con::printf("ShapeVector::getPoly() - Unable to allocate buffer!");
  397. // Exit.
  398. return NULL;
  399. }
  400. // Set Buffer Counter.
  401. U32 bufferCount = 0;
  402. // Add Polygon Edges.
  403. for ( U32 n = 0; n < getPolyVertexCount(); n++ )
  404. {
  405. // Output Object ID.
  406. bufferCount += dSprintf( pReturnBuffer + bufferCount, maxBufferSize-bufferCount, "%0.5f %0.5f ", pPoly[n].x, pPoly[n].y );
  407. // Finish early if we run out of buffer space.
  408. if ( bufferCount >= maxBufferSize )
  409. {
  410. // Warn.
  411. Con::warnf("ShapeVector::getPoly() - Error writing to buffer!");
  412. break;
  413. }
  414. }
  415. // Return Buffer.
  416. return pReturnBuffer;
  417. }
  418. //----------------------------------------------------------------------------
  419. const char* ShapeVector::getWorldPoly( void )
  420. {
  421. // Get the object space polygon
  422. //const Vector2* pPoly = (getPolyVertexCount() > 0) ? getPolyBasis() : NULL;
  423. // Set the max buffer size
  424. const U32 maxBufferSize = getPolyVertexCount() * 18 + 1;
  425. // Get the return buffer.
  426. char* pReturnBuffer = Con::getReturnBuffer( maxBufferSize );
  427. // Check the buffer.
  428. if( !pReturnBuffer )
  429. {
  430. // Warn.
  431. Con::printf("ShapeVector::getWorldPoly() - Unable to allocate buffer!");
  432. // Exit.
  433. return NULL;
  434. }
  435. // Set Buffer Counter.
  436. U32 bufferCount = 0;
  437. // Add Polygon Edges.
  438. for ( U32 n = 0; n < getPolyVertexCount(); n++ )
  439. {
  440. // Convert the poly point to a world coordinate
  441. Vector2 worldPoint = getWorldPoint(mPolygonLocalList[n]);
  442. // Output the point
  443. bufferCount += dSprintf( pReturnBuffer + bufferCount, maxBufferSize-bufferCount, "%0.5f %0.5f ", worldPoint.x, worldPoint.y );
  444. // Finish early if we run out of buffer space.
  445. if ( bufferCount >= maxBufferSize )
  446. {
  447. // Warn.
  448. Con::warnf("ShapeVector::getWorldPoly() - Error writing to buffer!");
  449. break;
  450. }
  451. }
  452. // Return Buffer.
  453. return pReturnBuffer;
  454. }
  455. //----------------------------------------------------------------------------
  456. void ShapeVector::generateLocalPoly( void )
  457. {
  458. // Fetch Polygon Vertex Count.
  459. const U32 polyVertexCount = mPolygonBasisList.size();
  460. // Process Collision Polygon (if we've got one).
  461. if ( polyVertexCount > 0 )
  462. {
  463. // Clear Polygon List.
  464. mPolygonLocalList.clear();
  465. mPolygonLocalList.setSize( polyVertexCount );
  466. // Fetch Half Size.
  467. const Vector2 halfSize = getHalfSize();
  468. // Calculate Polygon Half-Size.
  469. const Vector2 polyHalfSize( halfSize.x * mPolygonScale.x, halfSize.y * mPolygonScale.y );
  470. // Scale/Orientate Polygon.
  471. for ( U32 n = 0; n < polyVertexCount; n++ )
  472. {
  473. // Fetch Polygon Basis.
  474. Vector2 polyVertex = mPolygonBasisList[n];
  475. // Scale.
  476. polyVertex.Set( polyVertex.x * mSize.x * (mFlipX ? -1.0f : 1.0f),
  477. polyVertex.y * mSize.y * (mFlipY ? -1.0f : 1.0f));
  478. // Set Vertex.
  479. mPolygonLocalList[n] = polyVertex;
  480. }
  481. }
  482. }
  483. //----------------------------------------------------------------------------
  484. void ShapeVector::setLineColorString( const char* lineColour )
  485. {
  486. // Calculate Element Count.
  487. const U32 elementCount = Utility::mGetStringElementCount( lineColour );
  488. // Check we've got enough arguments.
  489. if ( elementCount < 3 )
  490. {
  491. Con::warnf("ShapeVector::setLineColourString() - Invalid Number of Elements! (%s)", lineColour);
  492. return;
  493. }
  494. // Calculate Red, Green and Blue.
  495. const F32 red = dAtof(Utility::mGetStringElement( lineColour, 0 ));
  496. const F32 green = dAtof(Utility::mGetStringElement( lineColour, 1 ));
  497. const F32 blue = dAtof(Utility::mGetStringElement( lineColour, 2 ));
  498. // Set Alpha (if specified).
  499. F32 alpha;
  500. if ( elementCount >= 4 )
  501. alpha = dAtof(Utility::mGetStringElement( lineColour, 3 ));
  502. else alpha = 1.0f;
  503. // Set Line Colour.
  504. setLineColor( ColorF(red, green, blue, alpha) );
  505. }
  506. //----------------------------------------------------------------------------
  507. void ShapeVector::setLineColor( const ColorF& lineColour )
  508. {
  509. // Set Line Colour.
  510. mLineColor = lineColour;
  511. }
  512. //----------------------------------------------------------------------------
  513. const char* ShapeVector::getLineColor()
  514. {
  515. // Get Return Buffer.
  516. char* pReturnBuffer = Con::getReturnBuffer( 64 );
  517. dSprintf( pReturnBuffer, 64, "%0.5f %0.5f %0.5f %0.5f", mLineColor.red, mLineColor.green,
  518. mLineColor.blue, mLineColor.alpha);
  519. return pReturnBuffer;
  520. }
  521. //----------------------------------------------------------------------------
  522. void ShapeVector::setLineAlpha( const F32 alpha )
  523. {
  524. // Set Line Alpha.
  525. mLineColor.alpha = alpha;
  526. }
  527. //----------------------------------------------------------------------------
  528. void ShapeVector::setFillColorString( const char* fillColour )
  529. {
  530. // Calculate Element Count.
  531. const U32 elementCount = Utility::mGetStringElementCount( fillColour );
  532. // Check we've got enough arguments.
  533. if ( elementCount < 3 )
  534. {
  535. Con::warnf("ShapeVector::setFillColourString() - Invalid Number of Elements! (%s)", fillColour);
  536. return;
  537. }
  538. // Calculate Red, Green and Blue.
  539. const F32 red = dAtof(Utility::mGetStringElement( fillColour, 0 ));
  540. const F32 green = dAtof(Utility::mGetStringElement( fillColour, 1 ));
  541. const F32 blue = dAtof(Utility::mGetStringElement( fillColour, 2 ));
  542. // Set Alpha (if specified).
  543. F32 alpha;
  544. if ( elementCount >= 4 )
  545. alpha = dAtof(Utility::mGetStringElement( fillColour, 3 ));
  546. else alpha = 1.0f;
  547. // Set Fill Colour.
  548. setFillColor( ColorF(red, green, blue, alpha) );
  549. }
  550. //----------------------------------------------------------------------------
  551. void ShapeVector::setFillColor( const ColorF& fillColour )
  552. {
  553. // Set Fill Colour.
  554. mFillColor = fillColour;
  555. }
  556. //----------------------------------------------------------------------------
  557. const char* ShapeVector::getFillColor()
  558. {
  559. // Get Return Buffer.
  560. char* pReturnBuffer = Con::getReturnBuffer( 64 );
  561. dSprintf( pReturnBuffer, 64, "%0.5f %0.5f %0.5f %0.5f", mFillColor.red, mFillColor.green,
  562. mFillColor.blue, mFillColor.alpha);
  563. return pReturnBuffer;
  564. }
  565. //----------------------------------------------------------------------------
  566. void ShapeVector::setFillAlpha( const F32 alpha )
  567. {
  568. // Set Fill Alpha.
  569. mFillColor.alpha = alpha;
  570. }
  571. //----------------------------------------------------------------------------
  572. void ShapeVector::setFillMode( const bool fillMode )
  573. {
  574. // Set Fill Mode.
  575. mFillMode = fillMode;
  576. }
  577. //----------------------------------------------------------------------------
  578. bool ShapeVector::getFillMode()
  579. {
  580. return mFillMode;
  581. }
  582. //----------------------------------------------------------------------------
  583. void ShapeVector::setIsCircle( const bool isCircle )
  584. {
  585. // Set Fill Mode.
  586. mIsCircle = isCircle;
  587. }
  588. //----------------------------------------------------------------------------
  589. bool ShapeVector::getIsCircle()
  590. {
  591. return mIsCircle;
  592. }
  593. //----------------------------------------------------------------------------
  594. void ShapeVector::setCircleRadius( const F32 circleRadius )
  595. {
  596. // Set Fill Mode.
  597. mCircleRadius = circleRadius;
  598. }
  599. //----------------------------------------------------------------------------
  600. F32 ShapeVector::getCircleRadius()
  601. {
  602. return mCircleRadius;
  603. }
  604. //----------------------------------------------------------------------------
  605. Vector2 ShapeVector::getBoxFromPoints()
  606. {
  607. Vector2 box(1.0f, 1.0f);
  608. // Fetch Polygon Vertex Count.
  609. const U32 polyVertexCount = mPolygonBasisList.size();
  610. F32 minX = 0;
  611. F32 minY = 0;
  612. F32 maxX = 0;
  613. F32 maxY = 0;
  614. // Process Collision Polygon (if we've got one).
  615. if ( polyVertexCount > 0 )
  616. {
  617. // Scale/Orientate Polygon.
  618. for ( U32 n = 0; n < polyVertexCount; n++ )
  619. {
  620. // Fetch Polygon Basis.
  621. Vector2 polyVertex = mPolygonBasisList[n];
  622. if (polyVertex.x > maxX)
  623. maxX = polyVertex.x;
  624. else if (polyVertex.x < minX)
  625. minX = polyVertex.x;
  626. if (polyVertex.y > maxY)
  627. maxY = polyVertex.y;
  628. else if (polyVertex.y < minY)
  629. minY = polyVertex.y;
  630. }
  631. }
  632. box.x = maxX - minX;
  633. box.y = maxY - minY;
  634. return box;
  635. }