ShapeVector_ScriptBinding.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. ConsoleMethod(ShapeVector, setPolyScale, void, 3, 3, "(widthScale / [heightScale]) - Sets the polygon scale.\n"
  23. "@param width/heightScale The scale values of the given polygon. If no height is specified, the widthScale value is repeated.\n"
  24. "@return No return value.")
  25. {
  26. // Calculate Element Count.
  27. const U32 elementCount = Utility::mGetStringElementCount( argv[2] );
  28. // Check Parameters.
  29. if ( elementCount < 1 )
  30. {
  31. Con::warnf("ShapeVector::setPolyScale() - Invalid number of parameters!");
  32. return;
  33. }
  34. // Fetch Width Scale.
  35. Vector2 scale;
  36. scale.x = dAtof(Utility::mGetStringElement(argv[2],0));
  37. // Use Fixed-Aspect for Scale if Height-Scale not specified.
  38. if ( elementCount == 2 )
  39. {
  40. // Specified Height Scale.
  41. scale.y = dAtof(Utility::mGetStringElement(argv[2],1));
  42. }
  43. else
  44. {
  45. // Fixed-Aspect Scale.
  46. scale.y = scale.x;
  47. }
  48. // Set Polygon Scale.
  49. object->setPolyScale( scale );
  50. }
  51. //----------------------------------------------------------------------------
  52. ConsoleMethod(ShapeVector, setPolyPrimitive, void, 3, 3, "(vertexCount) Sets a regular polygon primitive.\n"
  53. "@return No return value.")
  54. {
  55. // Set Polygon Primitive.
  56. object->setPolyPrimitive( dAtoi(argv[2]) );
  57. }
  58. //----------------------------------------------------------------------------
  59. ConsoleMethod(ShapeVector, setPolyCustom, void, 4, 4, "(poly-count, poly-Definition$) Sets Custom Polygon.\n"
  60. "@return No return value.")
  61. {
  62. // Set Collision Poly Custom.
  63. object->setPolyCustom( dAtoi(argv[2]), argv[3] );
  64. }
  65. //----------------------------------------------------------------------------
  66. ConsoleMethod(ShapeVector, getPoly, const char*, 2, 2, "() Gets Polygon.\n"
  67. "@return (poly-Definition) The vertices of the polygon in object space.")
  68. {
  69. // Get Collision Poly Count.
  70. return object->getPoly();
  71. }
  72. //----------------------------------------------------------------------------
  73. ConsoleMethod(ShapeVector, getWorldPoly, const char*, 2, 2, "() Gets Polygon points in world space.\n"
  74. "@return (poly-Definition) The vertices of the polygon in world space.")
  75. {
  76. // Get Collision Poly Count.
  77. return object->getWorldPoly();
  78. }
  79. //----------------------------------------------------------------------------
  80. ConsoleMethod(ShapeVector, setLineColor, void, 3, 6, "(float red, float green, float blue, [float alpha = 1.0]) or ( stockColorName ) - Sets the line color."
  81. "@param red The red value.\n"
  82. "@param green The green value.\n"
  83. "@param blue The blue value.\n"
  84. "@param alpha The alpha value.\n"
  85. "@return No return Value.")
  86. {
  87. // The colors.
  88. F32 red;
  89. F32 green;
  90. F32 blue;
  91. F32 alpha = 1.0f;
  92. // Space separated.
  93. if (argc == 3 )
  94. {
  95. // Grab the element count.
  96. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  97. // Has a single argument been specified?
  98. if ( elementCount == 1 )
  99. {
  100. // Set color.
  101. Con::setData( TypeColorF, &const_cast<ColorF&>(object->getBlendColor()), 0, 1, &(argv[2]) );
  102. return;
  103. }
  104. // ("R G B [A]")
  105. if ((elementCount == 3) || (elementCount == 4))
  106. {
  107. // Extract the color.
  108. red = dAtof(Utility::mGetStringElement(argv[2], 0));
  109. green = dAtof(Utility::mGetStringElement(argv[2], 1));
  110. blue = dAtof(Utility::mGetStringElement(argv[2], 2));
  111. // Grab the alpha if it's there.
  112. if (elementCount > 3)
  113. alpha = dAtof(Utility::mGetStringElement(argv[2], 3));
  114. }
  115. // Invalid.
  116. else
  117. {
  118. Con::warnf("ShapeVector::setLineColor() - Invalid Number of parameters!");
  119. return;
  120. }
  121. }
  122. // (R, G, B)
  123. else if (argc >= 5)
  124. {
  125. red = dAtof(argv[2]);
  126. green = dAtof(argv[3]);
  127. blue = dAtof(argv[4]);
  128. // Grab the alpha if it's there.
  129. if (argc > 5)
  130. alpha = dAtof(argv[5]);
  131. }
  132. // Invalid.
  133. else
  134. {
  135. Con::warnf("ShapeVector::setLineColor() - Invalid Number of parameters!");
  136. return;
  137. }
  138. object->setLineColor( ColorF(red, green, blue, alpha) );
  139. }
  140. //----------------------------------------------------------------------------
  141. ConsoleMethod(ShapeVector, getLineColor, const char*, 2, 3, "(allowColorNames) Gets the fill color.\n"
  142. "@param allowColorNames Whether to allow stock color names to be returned or not. Optional: Defaults to false.\n"
  143. "@return (float red / float green / float blue / float alpha) The sprite blend color.")
  144. {
  145. // Get line color.
  146. ColorF color = object->getLineColor();
  147. // Fetch allow color names flag.
  148. const bool allowColorNames = (argc > 2) ? dAtob(argv[2] ) : false;
  149. // Are color names allowed?
  150. if ( allowColorNames )
  151. {
  152. // Yes, so fetch the field value.
  153. return Con::getData( TypeColorF, &color, 0 );
  154. }
  155. // No, so fetch the raw color values.
  156. return color.scriptThis();
  157. }
  158. //----------------------------------------------------------------------------
  159. ConsoleMethod(ShapeVector, setLineAlpha, void, 3, 3, "(alpha) Sets the Rendering Line Alpha (transparency).\n"
  160. "@param alpha The alpha value.\n"
  161. "@return No return value.")
  162. {
  163. // Set Line Alpha.
  164. object->setLineAlpha( dAtof(argv[2]) );
  165. }
  166. //----------------------------------------------------------------------------
  167. ConsoleMethod(ShapeVector, setFillColor, void, 3, 3, "(float red, float green, float blue, [float alpha = 1.0]) or ( stockColorName ) - Sets the fill color."
  168. "@param red The red value.\n"
  169. "@param green The green value.\n"
  170. "@param blue The blue value.\n"
  171. "@param alpha The alpha value.\n"
  172. "@return No return Value.")
  173. {
  174. // The colors.
  175. F32 red;
  176. F32 green;
  177. F32 blue;
  178. F32 alpha = 1.0f;
  179. // Space separated.
  180. if (argc == 3 )
  181. {
  182. // Grab the element count.
  183. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  184. // Has a single argument been specified?
  185. if ( elementCount == 1 )
  186. {
  187. // Set color.
  188. Con::setData( TypeColorF, &const_cast<ColorF&>(object->getBlendColor()), 0, 1, &(argv[2]) );
  189. return;
  190. }
  191. // ("R G B [A]")
  192. if ((elementCount == 3) || (elementCount == 4))
  193. {
  194. // Extract the color.
  195. red = dAtof(Utility::mGetStringElement(argv[2], 0));
  196. green = dAtof(Utility::mGetStringElement(argv[2], 1));
  197. blue = dAtof(Utility::mGetStringElement(argv[2], 2));
  198. // Grab the alpha if it's there.
  199. if (elementCount > 3)
  200. alpha = dAtof(Utility::mGetStringElement(argv[2], 3));
  201. }
  202. // Invalid.
  203. else
  204. {
  205. Con::warnf("ShapeVector::setFillColor() - Invalid Number of parameters!");
  206. return;
  207. }
  208. }
  209. // (R, G, B)
  210. else if (argc >= 5)
  211. {
  212. red = dAtof(argv[2]);
  213. green = dAtof(argv[3]);
  214. blue = dAtof(argv[4]);
  215. // Grab the alpha if it's there.
  216. if (argc > 5)
  217. alpha = dAtof(argv[5]);
  218. }
  219. // Invalid.
  220. else
  221. {
  222. Con::warnf("ShapeVector::setFillColor() - Invalid Number of parameters!");
  223. return;
  224. }
  225. object->setFillColor( ColorF(red, green, blue, alpha) );
  226. }
  227. //----------------------------------------------------------------------------
  228. ConsoleMethod(ShapeVector, getFillColor, const char*, 2, 3, "(allowColorNames) Gets the fill color.\n"
  229. "@param allowColorNames Whether to allow stock color names to be returned or not. Optional: Defaults to false.\n"
  230. "@return (float red / float green / float blue / float alpha) The sprite blend color.")
  231. {
  232. // Get line color.
  233. ColorF color = object->getFillColor();
  234. // Fetch allow color names flag.
  235. const bool allowColorNames = (argc > 2) ? dAtob(argv[2] ) : false;
  236. // Are color names allowed?
  237. if ( allowColorNames )
  238. {
  239. // Yes, so fetch the field value.
  240. return Con::getData( TypeColorF, &color, 0 );
  241. }
  242. // No, so fetch the raw color values.
  243. return color.scriptThis();
  244. }
  245. //----------------------------------------------------------------------------
  246. ConsoleMethod(ShapeVector, setFillAlpha, void, 3, 3, "(alpha) Sets the Rendering Fill Alpha (transparency).\n"
  247. "@param alpha The alpha value.\n"
  248. "@return No return value.")
  249. {
  250. // Set Fill Alpha.
  251. object->setFillAlpha( dAtof(argv[2]) );
  252. }
  253. //----------------------------------------------------------------------------
  254. ConsoleMethod(ShapeVector, setFillMode, void, 3, 3, "(fillMode?) Sets the Rendering Fill Mode.\n"
  255. "@return No return value.")
  256. {
  257. // Set Fill Mode.
  258. object->setFillMode( dAtob(argv[2]) );
  259. }
  260. //----------------------------------------------------------------------------
  261. ConsoleMethod(ShapeVector, getFillMode, bool, 2, 2, "() Gets the Rendering Fill Mode.\n"
  262. "@return The fill mode as a boolean value.")
  263. {
  264. return object->getFillMode();
  265. }
  266. //----------------------------------------------------------------------------
  267. ConsoleMethod(ShapeVector, setIsCircle, void, 3, 3, "(isCircle?) Sets whether this shape is a circle or not.\n"
  268. "@return The fill mode as a boolean value.")
  269. {
  270. object->setIsCircle(dAtob(argv[2]));
  271. }
  272. //----------------------------------------------------------------------------
  273. ConsoleMethod(ShapeVector, getIsCircle, bool, 2, 2, "() Returns whether this shape is a circle or not.\n"
  274. "@return The fill mode as a boolean value.")
  275. {
  276. return object->getIsCircle();
  277. }
  278. //----------------------------------------------------------------------------
  279. ConsoleMethod(ShapeVector, getCircleRadius, bool, 2, 2, "() Returns the radius of the shape if it is a circle.\n"
  280. "@return The fill mode as a boolean value.")
  281. {
  282. return object->getCircleRadius();
  283. }
  284. //----------------------------------------------------------------------------
  285. ConsoleMethod(ShapeVector, setCircleRadius, void, 3, 3, "(radius) Changes the radius of the shape if it is a circle.\n"
  286. "@return The fill mode as a boolean value.")
  287. {
  288. object->setCircleRadius(dAtof(argv[2]));
  289. }
  290. //-----------------------------------------------------------------------------
  291. ConsoleMethod(ShapeVector, getVertexCount, S32, 2, 2, "() Get the number of vertices on a polygon shape.\n")
  292. {
  293. return object->getPolyVertexCount();
  294. }
  295. //-----------------------------------------------------------------------------
  296. ConsoleMethod(ShapeVector, getBoxFromPoints, const char*, 2, 2, "() Get a box (\"width height\") that wraps around the poly vertices")
  297. {
  298. Vector2 box = object->getBoxFromPoints();
  299. // Create Returnable Buffer.
  300. char* pBuffer = Con::getReturnBuffer(32);
  301. // Format Buffer.
  302. dSprintf(pBuffer, 32, "%g %g", box.x, box.y);
  303. // Return box width and height.
  304. return pBuffer;
  305. }
  306. //-----------------------------------------------------------------------------
  307. ConsoleMethod(ShapeVector, setFlip, void, 4, 4, "(bool flipX, bool flipY) Sets shape flipping for each axis.\n"
  308. "@param flipX Whether or not to flip the shape along the x (horizontal) axis.\n"
  309. "@param flipY Whether or not to flip the shape along the y (vertical) axis.\n"
  310. "@return No return value.")
  311. {
  312. // Set Flip.
  313. object->setFlip( dAtob(argv[2]), dAtob(argv[3]) );
  314. }
  315. //-----------------------------------------------------------------------------
  316. ConsoleMethod(ShapeVector, getFlip, const char*, 2, 2, "() Gets the flip for each axis.\n"
  317. "@return (bool flipX/bool flipY) Whether or not the shape is flipped along the x and y axis.")
  318. {
  319. // Create Returnable Buffer.
  320. char* pBuffer = Con::getReturnBuffer(32);
  321. // Format Buffer.
  322. dSprintf(pBuffer, 32, "%d %d", object->getFlipX(), object->getFlipY());
  323. // Return Buffer.
  324. return pBuffer;
  325. }
  326. //-----------------------------------------------------------------------------
  327. ConsoleMethod(ShapeVector, setFlipX, void, 3, 3, "(bool flipX) Sets whether or not the shape is flipped horizontally.\n"
  328. "@param flipX Whether or not to flip the shape along the x (horizontal) axis."
  329. "@return No return value.")
  330. {
  331. // Set Flip.
  332. object->setFlipX( dAtob(argv[2]) );
  333. }
  334. //-----------------------------------------------------------------------------
  335. ConsoleMethod(ShapeVector, setFlipY, void, 3, 3, "(bool flipY) Sets whether or not the shape is flipped vertically.\n"
  336. "@param flipY Whether or not to flip the shape along the y (vertical) axis."
  337. "@return No return value.")
  338. {
  339. // Set Flip.
  340. object->setFlipY( dAtob(argv[2]) );
  341. }
  342. //-----------------------------------------------------------------------------
  343. ConsoleMethod(ShapeVector, getFlipX, bool, 2, 2, "() Gets whether or not the shape is flipped horizontally.\n"
  344. "@return (bool flipX) Whether or not the shape is flipped along the x axis.")
  345. {
  346. return object->getFlipX();
  347. }
  348. //-----------------------------------------------------------------------------
  349. ConsoleMethod(ShapeVector, getFlipY, bool, 2, 2, "() Gets whether or not the shape is flipped vertically."
  350. "@return (bool flipY) Whether or not the shape is flipped along the y axis.")
  351. {
  352. return object->getFlipY();
  353. }