CompositeSprite_ScriptBinding.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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. ConsoleMethodGroupBeginWithDocs(CompositeSprite, SceneObject)
  23. /*! Adds a sprite at the specified logical position.
  24. You must specify the correct number of arguments for the selected layout mode.
  25. The created sprite will be automatically selected.
  26. @param a b c d e f Logical positions #1 & #2 and four additional and optional arguments.
  27. @return The batch Id of the added sprite or zero if not successful.
  28. */
  29. ConsoleMethodWithDocs(CompositeSprite, addSprite, ConsoleInt, 2, 3, ( [a] [b] [c] [d] [e] [f] ))
  30. {
  31. if ( argc == 2 )
  32. return object->addSprite( SpriteBatchItem::LogicalPosition() );
  33. else
  34. return object->addSprite( SpriteBatchItem::LogicalPosition(argv[2]) );
  35. }
  36. //-----------------------------------------------------------------------------
  37. /*! Removes the selected sprite.
  38. @return Whether the sprite was removed or not.
  39. */
  40. ConsoleMethodWithDocs(CompositeSprite, removeSprite, ConsoleBool, 2, 2, ())
  41. {
  42. return object->removeSprite();
  43. }
  44. //-----------------------------------------------------------------------------
  45. /*! Removes all sprites.
  46. @return No return value.
  47. */
  48. ConsoleMethodWithDocs(CompositeSprite, clearSprites, ConsoleVoid, 2, 2, ())
  49. {
  50. return object->clearSprites();
  51. }
  52. //-----------------------------------------------------------------------------
  53. /*! Gets a count of sprites in the composite.
  54. @return The count of sprites in the composite.
  55. */
  56. ConsoleMethodWithDocs(CompositeSprite, getSpriteCount, ConsoleInt, 2, 2, ())
  57. {
  58. return object->getSpriteCount();
  59. }
  60. //-----------------------------------------------------------------------------
  61. /*! Sets the batch layout type.
  62. The render sort mode is used when isolated batch mode is on.
  63. @param batchLayoutType 'none', 'rect' or 'iso' layout types are valid.
  64. @return No return value.
  65. */
  66. ConsoleMethodWithDocs(CompositeSprite, setBatchLayout, ConsoleVoid, 3, 3, (batchLayoutType))
  67. {
  68. // Fetch the batch layout type/
  69. CompositeSprite::BatchLayoutType batchLayoutType = CompositeSprite::getBatchLayoutTypeEnum( argv[2] );
  70. // Sanity!
  71. if ( batchLayoutType == CompositeSprite::INVALID_LAYOUT )
  72. {
  73. // Warn.
  74. Con::warnf( "CompositeSprite::setBatchLayout() - Unknown batch layout type of '%s'.", argv[2] );
  75. return;
  76. }
  77. object->setBatchLayout( batchLayoutType );
  78. }
  79. //-----------------------------------------------------------------------------
  80. /*! Gets the batch layout type.
  81. @return The batch layout type.
  82. */
  83. ConsoleMethodWithDocs(CompositeSprite, getBatchLayout, ConsoleString, 2, 2, ())
  84. {
  85. return CompositeSprite::getBatchLayoutTypeDescription( object->getBatchLayout() );
  86. }
  87. //-----------------------------------------------------------------------------
  88. /*! Sets whether the sprites are rendered, isolated from other renderings as one batch or not.
  89. When in batch isolated mode, the sprites can be optionally sorted.
  90. @return No return value.
  91. */
  92. ConsoleMethodWithDocs(CompositeSprite, setBatchIsolated, ConsoleVoid, 3, 3, (bool batchIsolated))
  93. {
  94. // Fetch batch isolated.
  95. const bool batchIsolated = dAtob(argv[2]);
  96. object->setBatchIsolated( batchIsolated );
  97. }
  98. //-----------------------------------------------------------------------------
  99. /*! Gets whether the sprites are rendered, isolated from other renderings as one batch or not.
  100. @return Whether the sprites are rendered, isolated from other renderings as one batch or not.
  101. */
  102. ConsoleMethodWithDocs(CompositeSprite, getBatchIsolated, ConsoleBool, 2, 2, ())
  103. {
  104. return object->getBatchIsolated();
  105. }
  106. //-----------------------------------------------------------------------------
  107. /*! Sets whether the sprites are culled.
  108. For sprites that are off-screen this is considerably faster during render at the expense of memory.
  109. For small composites with a few sprites, the overhead is probably not worth it.
  110. @return No return value.
  111. */
  112. ConsoleMethodWithDocs(CompositeSprite, setBatchCulling, ConsoleVoid, 3, 3, (bool batchCulling))
  113. {
  114. // Fetch batch culling..
  115. const bool batchCulling = dAtob(argv[2]);
  116. STATIC_VOID_CAST_TO(CompositeSprite, SpriteBatch, object)->setBatchCulling( batchCulling );
  117. }
  118. //-----------------------------------------------------------------------------
  119. /*! Gets whether the sprites are render culled or not
  120. @return Whether the sprites are rendered culled or not.
  121. */
  122. ConsoleMethodWithDocs(CompositeSprite, getBatchCulling, ConsoleBool, 2, 2, ())
  123. {
  124. return object->getBatchCulling();
  125. }
  126. //-----------------------------------------------------------------------------
  127. /*! Sets the batch render sort mode.
  128. The render sort mode is used when isolated batch mode is on.
  129. @return No return value.
  130. */
  131. ConsoleMethodWithDocs(CompositeSprite, setBatchSortMode, ConsoleVoid, 3, 3, (renderSortMode))
  132. {
  133. // Fetch render sort mode.
  134. SceneRenderQueue::RenderSort batchSortMode = SceneRenderQueue::getRenderSortEnum( argv[2] );
  135. // Sanity!
  136. if ( batchSortMode == SceneRenderQueue::RENDER_SORT_INVALID )
  137. {
  138. // Warn.
  139. Con::warnf( "CompositeSprite::setBatchSortMode() - Unknown batch sort mode of '%s'.", argv[2] );
  140. return;
  141. }
  142. object->setBatchSortMode( batchSortMode );
  143. }
  144. //-----------------------------------------------------------------------------
  145. /*! Gets the batch render sort mode.
  146. @return The render sort mode.
  147. */
  148. ConsoleMethodWithDocs(CompositeSprite, getBatchSortMode, ConsoleString, 2, 2, ())
  149. {
  150. return SceneRenderQueue::getRenderSortDescription( object->getBatchSortMode() );
  151. }
  152. //-----------------------------------------------------------------------------
  153. /*! Sets the stride which scales the position at which sprites are created.
  154. @param strideX The default stride of the local X axis.
  155. @param strideY The default stride of the local Y axis.
  156. @return No return value.
  157. */
  158. ConsoleMethodWithDocs(CompositeSprite, setDefaultSpriteStride, ConsoleVoid, 3, 4, (float strideX, [float strideY]]))
  159. {
  160. Vector2 stride;
  161. // Fetch element count.
  162. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  163. // ("strideX strideY")
  164. if ( (elementCount == 2) && (argc == 3) )
  165. {
  166. stride.x = dAtof(Utility::mGetStringElement(argv[2], 0));
  167. stride.y = dAtof(Utility::mGetStringElement(argv[2], 1));
  168. }
  169. // (strideX, [strideY])
  170. else if (elementCount == 1)
  171. {
  172. stride.x = dAtof(argv[2]);
  173. if (argc > 3)
  174. stride.y = dAtof(argv[3]);
  175. else
  176. stride.y = stride.x;
  177. }
  178. // Invalid
  179. else
  180. {
  181. Con::warnf("CompositeSprite::setDefaultSpriteStride() - Invalid number of parameters!");
  182. return;
  183. }
  184. object->setDefaultSpriteStride( stride );
  185. }
  186. //-----------------------------------------------------------------------------
  187. /*! Gets the stride which scales the position at which sprites are created.
  188. @return (float strideX/float strideY) The stride which scales the position at which sprites are created.
  189. */
  190. ConsoleMethodWithDocs(CompositeSprite, getDefaultSpriteStride, ConsoleString, 2, 2, ())
  191. {
  192. return object->getDefaultSpriteStride().scriptThis();
  193. }
  194. //-----------------------------------------------------------------------------
  195. /*! Sets the size at which sprites are created.
  196. @param width The default width of sprites.
  197. @param height The default height of sprites
  198. @return No return value.
  199. */
  200. ConsoleMethodWithDocs(CompositeSprite, setDefaultSpriteSize, ConsoleVoid, 3, 4, (float width, [float height]))
  201. {
  202. Vector2 size;
  203. // Fetch element count.
  204. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  205. // ("width height")
  206. if ( (elementCount == 2) && (argc == 3) )
  207. {
  208. size.x = dAtof(Utility::mGetStringElement(argv[2], 0));
  209. size.y = dAtof(Utility::mGetStringElement(argv[2], 1));
  210. }
  211. // (width, [height])
  212. else if (elementCount == 1)
  213. {
  214. size.x = dAtof(argv[2]);
  215. if (argc > 3)
  216. size.y = dAtof(argv[3]);
  217. else
  218. size.y = size.x;
  219. }
  220. // Invalid
  221. else
  222. {
  223. Con::warnf("CompositeSprite::setDefaultSpriteSize() - Invalid number of parameters!");
  224. return;
  225. }
  226. object->setDefaultSpriteSize( size );
  227. }
  228. //-----------------------------------------------------------------------------
  229. /*! Gets the size at which sprites are created.
  230. @return (float width/float height) The size at which sprites are created.
  231. */
  232. ConsoleMethodWithDocs(CompositeSprite, getDefaultSpriteSize, ConsoleString, 2, 2, ())
  233. {
  234. return object->getDefaultSpriteSize().scriptThis();
  235. }
  236. //-----------------------------------------------------------------------------
  237. /*! Sets the angle at which sprites are created.
  238. @param angle The angle at which sprites are created.
  239. @return No return value.
  240. */
  241. ConsoleMethodWithDocs(CompositeSprite, setDefaultSpriteAngle, ConsoleVoid, 3, 3, (float angle))
  242. {
  243. // Fetch angle.
  244. const F32 angle = mDegToRad( dAtof(argv[2]) );
  245. static_cast<SpriteBatch*>(object)->setDefaultSpriteAngle( angle );
  246. }
  247. //-----------------------------------------------------------------------------
  248. /*! Gets the angle at which sprites are created.
  249. @return (float angle) The angle at which sprites are created.
  250. */
  251. ConsoleMethodWithDocs(CompositeSprite, getDefaultSpriteAngle, ConsoleFloat, 3, 3, ())
  252. {
  253. return mRadToDeg( static_cast<SpriteBatch*>(object)->getDefaultSpriteAngle() );
  254. }
  255. //-----------------------------------------------------------------------------
  256. /*! Selects a sprite at the specified logical position.
  257. @param a b c d e f Logical positions #1 & #2 and four additional and optional arguments.
  258. @return Whether the sprite was selected or not.
  259. */
  260. ConsoleMethodWithDocs(CompositeSprite, selectSprite, ConsoleBool, 3, 3, ( a b [c] [d] [e] [f] ))
  261. {
  262. return object->selectSprite( SpriteBatchItem::LogicalPosition(argv[2]) );
  263. }
  264. //-----------------------------------------------------------------------------
  265. /*! Selects a sprite with the specified batch Id.
  266. @param batchId The batch Id of the sprite to select.
  267. @return Whether the sprite was selected or not.
  268. */
  269. ConsoleMethodWithDocs(CompositeSprite, selectSpriteId, ConsoleBool, 3, 3, ( int batchId ))
  270. {
  271. return object->selectSpriteId( dAtoi(argv[2]) );
  272. }
  273. //-----------------------------------------------------------------------------
  274. /*! Selects a sprite with the specified name.
  275. @param name The name of the sprite.
  276. @return Whether the sprite was selected or not.
  277. */
  278. ConsoleMethodWithDocs(CompositeSprite, selectSpriteName, ConsoleBool, 3, 3, ( name ))
  279. {
  280. return object->selectSpriteName( argv[2] );
  281. }
  282. //-----------------------------------------------------------------------------
  283. /*! Deselects any selected sprite.
  284. This is not required but can be used to stop accidental changes to sprites.
  285. @return No return value.
  286. */
  287. ConsoleMethodWithDocs(CompositeSprite, deselectSprite, ConsoleVoid, 2, 2, ())
  288. {
  289. return object->deselectSprite();
  290. }
  291. //-----------------------------------------------------------------------------
  292. /*! Checks whether a sprite is selected or not.
  293. @return Whether a sprite is selected or not.
  294. */
  295. ConsoleMethodWithDocs(CompositeSprite, isSpriteSelected, ConsoleBool, 2, 2, ())
  296. {
  297. return object->isSpriteSelected();
  298. }
  299. //-----------------------------------------------------------------------------
  300. /*! Sets the sprite image and optional frame.
  301. @param imageAssetId The image to set the sprite to.
  302. @param imageFrame The image frame of the imageAssetId to set the sprite to.
  303. @return No return value.
  304. */
  305. ConsoleMethodWithDocs(CompositeSprite, setSpriteImage, ConsoleVoid, 3, 4, (imageAssetId, [imageFrame]))
  306. {
  307. // Was a frame specified?
  308. if (argc >= 4)
  309. {
  310. // Was it a number or a string?
  311. if (!dIsalpha(*argv[3]))
  312. {
  313. // Fetch the numerical frame and set the image
  314. const U32 frame = argc >= 4 ? dAtoi(argv[3]) : 0;
  315. object->setSpriteImage( argv[2], frame );
  316. }
  317. else
  318. {
  319. // Set the image and pass the named frame string
  320. object->setSpriteImage( argv[2], argv[3] );
  321. }
  322. }
  323. else
  324. {
  325. // Frame was not specified, use default 0 and set the image
  326. const U32 frame = 0;
  327. object->setSpriteImage( argv[2], frame );
  328. }
  329. }
  330. //-----------------------------------------------------------------------------
  331. /*! Gets the sprite image.
  332. @return The sprite image.
  333. */
  334. ConsoleMethodWithDocs(CompositeSprite, getSpriteImage, ConsoleString, 2, 2, ())
  335. {
  336. return object->getSpriteImage();
  337. }
  338. //-----------------------------------------------------------------------------
  339. /*! Sets the sprite image frame.
  340. @param imageFrame The image frame to set the sprite to.
  341. @return No return value.
  342. */
  343. ConsoleMethodWithDocs(CompositeSprite, setSpriteImageFrame, ConsoleVoid, 3, 3, (int imageFrame))
  344. {
  345. // Fetch frame.
  346. const U32 frame = dAtoi(argv[2]);
  347. object->setSpriteImageFrame( frame );
  348. }
  349. //-----------------------------------------------------------------------------
  350. /*! Gets the sprite image frame.
  351. @return The sprite image frame.
  352. */
  353. ConsoleMethodWithDocs(CompositeSprite, getSpriteImageFrame, ConsoleInt, 2, 2, ())
  354. {
  355. return object->getSpriteImageFrame();
  356. }
  357. //-----------------------------------------------------------------------------
  358. /*! Sets the sprite named image frame.
  359. @param namedFrame The named image frame to set the sprite to.
  360. @return No return value.
  361. */
  362. ConsoleMethodWithDocs(CompositeSprite, setSpriteNamedImageFrame, ConsoleVoid, 3, 3, (namedFrame))
  363. {
  364. object->setSpriteNamedImageFrame( argv[2] );
  365. }
  366. //-----------------------------------------------------------------------------
  367. /*! Gets the sprite named image frame.
  368. @return The sprite named image frame.
  369. */
  370. ConsoleMethodWithDocs(CompositeSprite, getSpriteNamedImageFrame, ConsoleString, 2, 2, ())
  371. {
  372. return object->getSpriteNamedImageFrame();
  373. }
  374. //-----------------------------------------------------------------------------
  375. /*! Sets the sprite animation.
  376. @param imageAssetId The animation to set the sprite to.
  377. @return No return value.
  378. */
  379. ConsoleMethodWithDocs(CompositeSprite, setSpriteAnimation, ConsoleVoid, 3, 4, (animationAssetId))
  380. {
  381. object->setSpriteAnimation( argv[2] );
  382. }
  383. //-----------------------------------------------------------------------------
  384. /*! Gets the sprite animation.
  385. @return The sprite animation.
  386. */
  387. ConsoleMethodWithDocs(CompositeSprite, getSpriteAnimation, ConsoleString, 2, 2, ())
  388. {
  389. return object->getSpriteAnimation();
  390. }
  391. //-----------------------------------------------------------------------------
  392. /*! Sets the current animation frame for the selected sprite. IMPORTANT: this is not the image frame number used in the animation!
  393. @param frame Which frame of the animation to display
  394. @return No return value.
  395. */
  396. ConsoleMethodWithDocs(CompositeSprite, setSpriteAnimationFrame, ConsoleVoid, 3, 3, (int animationFrame))
  397. {
  398. // Fetch frame.
  399. const U32 frame = dAtoi(argv[2]);
  400. object->setSpriteAnimationFrame(frame);
  401. }
  402. //-----------------------------------------------------------------------------
  403. /*! Gets current frame index used in the animation for the selected sprite. IMPORTANT: this is not the image frame number!
  404. @return The current numerical animation frame for the selected sprite
  405. */
  406. ConsoleMethodWithDocs(CompositeSprite, getSpriteAnimationFrame, ConsoleInt, 2, 2, ())
  407. {
  408. return object->getSpriteAnimationFrame();
  409. }
  410. //-----------------------------------------------------------------------------
  411. /*! Clears any image or animation asset from the sprite.
  412. @return No return value.
  413. */
  414. ConsoleMethodWithDocs(CompositeSprite, clearSpriteAsset, ConsoleVoid, 2, 2, ())
  415. {
  416. return object->clearSpriteAsset();
  417. }
  418. //-----------------------------------------------------------------------------
  419. /*! Sets whether the sprite is visible or not.
  420. @param visible Whether the sprite is visible or not.
  421. @return No return value.
  422. */
  423. ConsoleMethodWithDocs(CompositeSprite, setSpriteVisible, ConsoleVoid, 3, 3, (bool visible))
  424. {
  425. // Fetch visible.
  426. const bool visible = dAtob(argv[2]);
  427. object->setSpriteVisible( visible );
  428. }
  429. //-----------------------------------------------------------------------------
  430. /*! Gets whether the sprite is visible or not.
  431. @return Whether the sprite is visible or not.
  432. */
  433. ConsoleMethodWithDocs(CompositeSprite, getSpriteVisible, ConsoleBool, 2, 2, ())
  434. {
  435. return object->getSpriteVisible();
  436. }
  437. //-----------------------------------------------------------------------------
  438. /*! Sets the sprites local position.
  439. @param localX The local position X.
  440. @param localY The local position Y.
  441. @return No return value.
  442. */
  443. ConsoleMethodWithDocs(CompositeSprite, setSpriteLocalPosition, ConsoleVoid, 3, 4, (float localX, float localY))
  444. {
  445. Vector2 localPosition;
  446. // Fetch element count.
  447. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  448. // ("x y")
  449. if ( (elementCount == 2) && (argc == 3) )
  450. {
  451. localPosition.x = dAtof(Utility::mGetStringElement(argv[2], 0));
  452. localPosition.y = dAtof(Utility::mGetStringElement(argv[2], 1));
  453. }
  454. // (x, y)
  455. else if ( elementCount == 1 && (argc > 3) )
  456. {
  457. localPosition.x = dAtof(argv[2]);
  458. localPosition.y = dAtof(argv[3]);
  459. }
  460. // Invalid
  461. else
  462. {
  463. Con::warnf("CompositeSprite::setSpriteLocalPosition() - Invalid number of parameters!");
  464. return;
  465. }
  466. object->setSpriteLocalPosition( localPosition );
  467. object->setSpatialDirty();
  468. }
  469. //-----------------------------------------------------------------------------
  470. /*! Gets the sprite local position.
  471. @return The sprite local position.
  472. */
  473. ConsoleMethodWithDocs(CompositeSprite, getSpriteLocalPosition, ConsoleString, 2, 2, ())
  474. {
  475. return object->getSpriteLocalPosition().scriptThis();
  476. }
  477. //-----------------------------------------------------------------------------
  478. /*! Gets the sprite logical position.
  479. @return The sprite logical position.
  480. */
  481. ConsoleMethodWithDocs(CompositeSprite, getSpriteLogicalPosition, ConsoleString, 2, 2, ())
  482. {
  483. return object->getSpriteLogicalPosition().getString();
  484. }
  485. //-----------------------------------------------------------------------------
  486. /*! Sets the sprites local angle.
  487. @param localAngle The sprite local angle.
  488. @return No return value.
  489. */
  490. ConsoleMethodWithDocs(CompositeSprite, setSpriteAngle, ConsoleVoid, 3, 3, (float localAngle))
  491. {
  492. // Fetch angle.
  493. const F32 angle = mDegToRad( dAtof(argv[2]) );
  494. object->setSpriteAngle( angle );
  495. }
  496. //-----------------------------------------------------------------------------
  497. /*! Gets the sprite local angle.
  498. @return The sprite local angle.
  499. */
  500. ConsoleMethodWithDocs(CompositeSprite, getSpriteAngle, ConsoleFloat, 2, 2, ())
  501. {
  502. return mRadToDeg( object->getSpriteAngle() );
  503. }
  504. //-----------------------------------------------------------------------------
  505. /*! Sets the sprites depth.
  506. @param depth The sprite depth.
  507. @return No return value.
  508. */
  509. ConsoleMethodWithDocs(CompositeSprite, setSpriteDepth, ConsoleVoid, 3, 3, (float depth))
  510. {
  511. // Fetch depth.
  512. const F32 depth = dAtof(argv[2]);
  513. object->setSpriteDepth( depth );
  514. }
  515. //-----------------------------------------------------------------------------
  516. /*! Gets the sprite depth.
  517. @return The sprite depth.
  518. */
  519. ConsoleMethodWithDocs(CompositeSprite, getSpriteDepth, ConsoleFloat, 2, 2, ())
  520. {
  521. return object->getSpriteDepth();
  522. }
  523. //-----------------------------------------------------------------------------
  524. /*! Sets the sprite size.
  525. @param width The sprite width.
  526. @param height The sprite height
  527. @return No return value.
  528. */
  529. ConsoleMethodWithDocs(CompositeSprite, setSpriteSize, ConsoleVoid, 3, 4, (float width, [float height]))
  530. {
  531. Vector2 size;
  532. // Fetch element count.
  533. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  534. // ("width height")
  535. if ( (elementCount == 2) && (argc == 3) )
  536. {
  537. size.x = dAtof(Utility::mGetStringElement(argv[2], 0));
  538. size.y = dAtof(Utility::mGetStringElement(argv[2], 1));
  539. }
  540. // (width, [height])
  541. else if (elementCount == 1)
  542. {
  543. size.x = dAtof(argv[2]);
  544. if (argc > 3)
  545. size.y = dAtof(argv[3]);
  546. else
  547. size.y = size.x;
  548. }
  549. // Invalid
  550. else
  551. {
  552. Con::warnf("CompositeSprite::setSpriteSize() - Invalid number of parameters!");
  553. return;
  554. }
  555. object->setSpriteSize( size );
  556. }
  557. //-----------------------------------------------------------------------------
  558. /*! Gets the sprite size.
  559. @return (float width/float height) The sprite size.
  560. */
  561. ConsoleMethodWithDocs(CompositeSprite, getSpriteSize, ConsoleString, 2, 2, ())
  562. {
  563. return object->getSpriteSize().scriptThis();
  564. }
  565. //-----------------------------------------------------------------------------
  566. /*! Sets whether the sprite is flipped along its local X axis or not.
  567. @param flipX Whether the sprite is flipped along its local X axis or not.
  568. @return No return value.
  569. */
  570. ConsoleMethodWithDocs(CompositeSprite, setSpriteFlipX, ConsoleVoid, 3, 3, (bool flipX))
  571. {
  572. // Fetch flipX.
  573. const bool flipX = dAtob(argv[2]);
  574. object->setSpriteFlipX( flipX );
  575. }
  576. //-----------------------------------------------------------------------------
  577. /*! Gets whether the sprite is flipped along its local X axis or not.
  578. @return Whether the sprite is flipped along its local X axis or not.
  579. */
  580. ConsoleMethodWithDocs(CompositeSprite, getSpriteFlipX, ConsoleBool, 2, 2, ())
  581. {
  582. return object->getSpriteFlipX();
  583. }
  584. //-----------------------------------------------------------------------------
  585. /*! Sets whether the sprite is flipped along its local Y axis or not.
  586. @param flipY Whether the sprite is flipped along its local Y axis or not.
  587. @return No return value.
  588. */
  589. ConsoleMethodWithDocs(CompositeSprite, setSpriteFlipY, ConsoleVoid, 3, 3, (bool flipY))
  590. {
  591. const bool flipY = dAtob(argv[2]);
  592. object->setSpriteFlipY( flipY );
  593. }
  594. //-----------------------------------------------------------------------------
  595. /*! Gets whether the sprite is flipped along its local Y axis or not.
  596. @return Whether the sprite is flipped along its local Y axis or not.
  597. */
  598. ConsoleMethodWithDocs(CompositeSprite, getSpriteFlipY, ConsoleBool, 2, 2, ())
  599. {
  600. return object->getSpriteFlipY();
  601. }
  602. //-----------------------------------------------------------------------------
  603. /*! Sets the sprites sort point.
  604. @param localX The local sort point X.
  605. @param localY The local sort point Y.
  606. @return No return value.
  607. */
  608. ConsoleMethodWithDocs(CompositeSprite, setSpriteSortPoint, ConsoleVoid, 3, 4, (float localX, float localY))
  609. {
  610. Vector2 sortPoint;
  611. // Fetch element count.
  612. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  613. // ("x y")
  614. if ( (elementCount == 2) && (argc == 3) )
  615. {
  616. sortPoint.x = dAtof(Utility::mGetStringElement(argv[2], 0));
  617. sortPoint.y = dAtof(Utility::mGetStringElement(argv[2], 1));
  618. }
  619. // (x, y)
  620. else if ( elementCount == 1 && (argc > 3) )
  621. {
  622. sortPoint.x = dAtof(argv[2]);
  623. sortPoint.y = dAtof(argv[3]);
  624. }
  625. // Invalid
  626. else
  627. {
  628. Con::warnf("CompositeSprite::setSpriteSortPoint() - Invalid number of parameters!");
  629. return;
  630. }
  631. object->setSpriteSortPoint( sortPoint );
  632. }
  633. //-----------------------------------------------------------------------------
  634. /*! Gets the sprite local sort point.
  635. @return The sprite local sort point.
  636. */
  637. ConsoleMethodWithDocs(CompositeSprite, getSpriteSortPoint, ConsoleString, 2, 2, ())
  638. {
  639. return object->getSpriteSortPoint().scriptThis();
  640. }
  641. //-----------------------------------------------------------------------------
  642. /*! Sets the name of the render group used to sort the sprite during rendering.
  643. @param renderGroup The name of the render group to use. Defaults to nothing.
  644. @return No return value.
  645. */
  646. ConsoleMethodWithDocs(CompositeSprite, setSpriteRenderGroup, ConsoleVoid, 3, 3, (renderGroup))
  647. {
  648. object->setSpriteRenderGroup( argv[2] );
  649. }
  650. //-----------------------------------------------------------------------------
  651. /*! Gets the name of the render group used to sort the sprite during rendering.
  652. @return The render group used to sort the object during rendering.
  653. */
  654. ConsoleMethodWithDocs(CompositeSprite, getSpriteRenderGroup, ConsoleString, 2, 2, ())
  655. {
  656. return object->getSpriteRenderGroup();
  657. }
  658. //-----------------------------------------------------------------------------
  659. /*! Sets whether sprite blending is on or not.
  660. @blendMode Whether sprite blending is on or not.
  661. @return No return Value.
  662. */
  663. ConsoleMethodWithDocs(CompositeSprite, setSpriteBlendMode, ConsoleVoid, 3, 3, (bool blendMode))
  664. {
  665. // Fetch blend mode.
  666. const bool blendMode = dAtob(argv[2]);
  667. object->setSpriteBlendMode( blendMode );
  668. }
  669. //-----------------------------------------------------------------------------
  670. /*! Gets whether sprite blending is on or not.
  671. @return (bool blendMode) Whether sprite blending is on or not.
  672. */
  673. ConsoleMethodWithDocs(CompositeSprite, getSpriteBlendMode, ConsoleBool, 2, 2, ())
  674. {
  675. return object->getSpriteBlendMode();
  676. }
  677. //-----------------------------------------------------------------------------
  678. /*! Sets the sprite source blend factor.
  679. @param srcBlend The sprite source blend factor.
  680. @return No return Value.
  681. */
  682. ConsoleMethodWithDocs(CompositeSprite, setSpriteSrcBlendFactor, ConsoleVoid, 3, 3, (srcBlend))
  683. {
  684. // Fetch source blend factor.
  685. GLenum blendFactor = SceneObject::getSrcBlendFactorEnum(argv[2]);
  686. object->setSpriteSrcBlendFactor( blendFactor );
  687. }
  688. //-----------------------------------------------------------------------------
  689. /*! Gets the sprite source blend factor.
  690. @return (srcBlend) The sprite source blend factor.
  691. */
  692. ConsoleMethodWithDocs(CompositeSprite, getSpriteSrcBlendFactor, ConsoleString, 2, 2, ())
  693. {
  694. return SceneObject::getSrcBlendFactorDescription( object->getSpriteSrcBlendFactor() );
  695. }
  696. //-----------------------------------------------------------------------------
  697. /*! Sets the sprite destination blend factor.
  698. @param dstBlend The sprite destination blend factor.
  699. @return No return Value.
  700. */
  701. ConsoleMethodWithDocs(CompositeSprite, setSpriteDstBlendFactor, ConsoleVoid, 3, 3, (dstBlend))
  702. {
  703. // Fetch destination blend factor.
  704. GLenum blendFactor = SceneObject::getDstBlendFactorEnum(argv[2]);
  705. object->setSpriteDstBlendFactor( blendFactor );
  706. }
  707. //-----------------------------------------------------------------------------
  708. /*! Gets the sprite destination blend factor.
  709. @return (dstBlend) The sprite destination blend factor.
  710. */
  711. ConsoleMethodWithDocs(CompositeSprite, getSpriteDstBlendFactor, ConsoleString, 2, 2, ())
  712. {
  713. return SceneObject::getDstBlendFactorDescription( object->getSpriteDstBlendFactor() );
  714. }
  715. //-----------------------------------------------------------------------------
  716. /*! or ( stockColorName ) - Sets the sprite blend color.
  717. @param red The red value.
  718. @param green The green value.
  719. @param blue The blue value.
  720. @param alpha The alpha value.
  721. @return No return Value.
  722. */
  723. ConsoleMethodWithDocs(CompositeSprite, setSpriteBlendColor, ConsoleVoid, 3, 6, (float red, float green, float blue, [float alpha = 1.0]))
  724. {
  725. // The colors.
  726. F32 red;
  727. F32 green;
  728. F32 blue;
  729. F32 alpha = 1.0f;
  730. // Space separated.
  731. if (argc == 3 )
  732. {
  733. // Grab the element count.
  734. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  735. // Has a single argument been specified?
  736. if ( elementCount == 1 )
  737. {
  738. // Is a sprite selected?
  739. if ( !object->isSpriteSelected() )
  740. {
  741. // No, so warn.
  742. Con::warnf("CompositeSprite::setSpriteBlendColor() - Cannot set sprite blend color as no sprite is selected." );
  743. return;
  744. }
  745. Con::setData( TypeColorF, &const_cast<ColorF&>(object->getSpriteBlendColor()), 0, 1, &(argv[2]) );
  746. return;
  747. }
  748. // ("R G B [A]")
  749. if ((elementCount == 3) || (elementCount == 4))
  750. {
  751. // Extract the color.
  752. red = dAtof(Utility::mGetStringElement(argv[2], 0));
  753. green = dAtof(Utility::mGetStringElement(argv[2], 1));
  754. blue = dAtof(Utility::mGetStringElement(argv[2], 2));
  755. // Grab the alpha if it's there.
  756. if (elementCount > 3)
  757. alpha = dAtof(Utility::mGetStringElement(argv[2], 3));
  758. }
  759. // Invalid.
  760. else
  761. {
  762. Con::warnf("SceneObject::setBlendColor() - Invalid Number of parameters!");
  763. return;
  764. }
  765. }
  766. // (R, G, B)
  767. else if (argc >= 5)
  768. {
  769. red = dAtof(argv[2]);
  770. green = dAtof(argv[3]);
  771. blue = dAtof(argv[4]);
  772. // Grab the alpha if it's there.
  773. if (argc > 5)
  774. alpha = dAtof(argv[5]);
  775. }
  776. // Invalid.
  777. else
  778. {
  779. Con::warnf("SceneObject::setBlendColor() - Invalid Number of parameters!");
  780. return;
  781. }
  782. // Set blend color.
  783. object->setSpriteBlendColor(ColorF(red, green, blue, alpha));
  784. }
  785. //-----------------------------------------------------------------------------
  786. /*! Gets the sprite blend color
  787. @param allowColorNames Whether to allow stock color names to be returned or not. Optional: Defaults to false.
  788. @return (float red / float green / float blue / float alpha) The sprite blend color.
  789. */
  790. ConsoleMethodWithDocs(CompositeSprite, getSpriteBlendColor, ConsoleString, 2, 3, (allowColorNames))
  791. {
  792. // Get Blend color.
  793. ColorF blendColor = object->getSpriteBlendColor();
  794. // Fetch allow color names flag.
  795. const bool allowColorNames = (argc > 2) ? dAtob(argv[2] ) : false;
  796. // Are color names allowed?
  797. if ( allowColorNames )
  798. {
  799. // Yes, so fetch the field value.
  800. return Con::getData( TypeColorF, &blendColor, 0 );
  801. }
  802. // No, so fetch the raw color values.
  803. return blendColor.scriptThis();
  804. }
  805. //-----------------------------------------------------------------------------
  806. /*! Sets the sprite color alpha (transparency).
  807. The alpha value specifies directly the transparency of the image. A value of 1.0 will not affect the object and a value of 0.0 will make the object completely transparent.
  808. @param alpha The alpha value.
  809. @return No return Value.
  810. */
  811. ConsoleMethodWithDocs(CompositeSprite, setSpriteBlendAlpha, ConsoleVoid, 3, 3, (float alpha))
  812. {
  813. object->setSpriteBlendAlpha( dAtof(argv[2]) );
  814. }
  815. //-----------------------------------------------------------------------------
  816. /*! Gets the sprite color alpha (transparency).
  817. @return (float alpha) The alpha value, a range from 0.0 to 1.0. Less than zero if alpha testing is disabled.
  818. */
  819. ConsoleMethodWithDocs(CompositeSprite, getSpriteBlendAlpha, ConsoleFloat, 2, 2, ())
  820. {
  821. return object->getSpriteBlendAlpha();
  822. }
  823. //-----------------------------------------------------------------------------
  824. /*! Set the sprite alpha test.
  825. @param value Numeric value of 0.0 to 1.0 to turn on alpha testing. Less than zero to disable alpha testing.
  826. @return No return Value.
  827. */
  828. ConsoleMethodWithDocs(CompositeSprite, setSpriteAlphaTest, ConsoleVoid, 3, 3, (float alpha))
  829. {
  830. object->setSpriteAlphaTest(dAtof(argv[2]));
  831. }
  832. //-----------------------------------------------------------------------------
  833. /*! Gets the sprite alpha test.
  834. @return (S32) A value of 0 to 255 if alpha testing is enabled. <0 represents disabled alpha testing.
  835. */
  836. ConsoleMethodWithDocs(CompositeSprite, getSpriteAlphaTest, ConsoleFloat, 2, 2, ())
  837. {
  838. return object->getSpriteAlphaTest();
  839. }
  840. //-----------------------------------------------------------------------------
  841. /*! Set the sprite data object.
  842. NOTE: This object will be persisted alongside the composite sprite.
  843. To clear the object you can pass an empty string.
  844. @return No return Value.
  845. */
  846. ConsoleMethodWithDocs(CompositeSprite, setSpriteDataObject, ConsoleVoid, 3, 3, (object))
  847. {
  848. object->setSpriteDataObject( Sim::findObject( argv[2] ) );
  849. }
  850. //-----------------------------------------------------------------------------
  851. /*! Gets the sprite data object.
  852. @return The sprite data object.
  853. */
  854. ConsoleMethodWithDocs(CompositeSprite, getSpriteDataObject, ConsoleString, 2, 2, ())
  855. {
  856. return object->getSpriteDataObject()->getIdString();
  857. }
  858. //-----------------------------------------------------------------------------
  859. /*! Set the sprite user data field.
  860. @param data A space separated string containing the data you wish to store.
  861. @return No return Value.
  862. */
  863. ConsoleMethodWithDocs(CompositeSprite, setSpriteUserData, ConsoleVoid, 3, 3, (data))
  864. {
  865. StringTableEntry userData = StringTable->insert(argv[2]);
  866. object->setUserData( (void*)userData );
  867. }
  868. //-----------------------------------------------------------------------------
  869. /*! Gets the sprite user data.
  870. @return The sprite user data.
  871. */
  872. ConsoleMethodWithDocs(CompositeSprite, getSpriteUserData, ConsoleString, 2, 2, ())
  873. {
  874. const char* userData = (const char*) object->getUserData();
  875. return userData;
  876. }
  877. //-----------------------------------------------------------------------------
  878. /*! Set the sprite name.
  879. This must be unique within this composite sprite instance. To clear the name you can pass an empty string.
  880. @return No return Value.
  881. */
  882. ConsoleMethodWithDocs(CompositeSprite, setSpriteName, ConsoleVoid, 3, 3, (name))
  883. {
  884. object->setSpriteName( argv[2] );
  885. }
  886. //-----------------------------------------------------------------------------
  887. /*! Gets the sprite name.
  888. @return The sprite name.
  889. */
  890. ConsoleMethodWithDocs(CompositeSprite, getSpriteName, ConsoleString, 2, 2, ())
  891. {
  892. return object->getSpriteName();
  893. }
  894. //-----------------------------------------------------------------------------
  895. /*! Gets the SpriteBatchId of the currently selected sprite.
  896. @returns The SpriteBatchId
  897. */
  898. ConsoleMethodWithDocs(CompositeSprite, getSpriteId, ConsoleInt, 2, 2, ())
  899. {
  900. return object->getSpriteId();
  901. }
  902. //-----------------------------------------------------------------------------
  903. /*! Picks sprites intersecting the specified point with optional group/layer masks.
  904. @param x/y The coordinate of the point as either (\x y\ or (x,y)
  905. @return Returns list of sprite Ids.
  906. */
  907. ConsoleMethodWithDocs(CompositeSprite, pickPoint, ConsoleString, 3, 4, (x / y ))
  908. {
  909. // Fetch sprite batch query and clear results.
  910. SpriteBatchQuery* pSpriteBatchQuery = object->getSpriteBatchQuery( true );
  911. // Is the sprite batch query available?
  912. if ( pSpriteBatchQuery == NULL )
  913. {
  914. // No, so warn.
  915. Con::warnf( "CompositeSprite::pickPoint() - Cannot pick sprites if clipping mode is off." );
  916. // Return nothing.
  917. return NULL;
  918. }
  919. // The point.
  920. Vector2 point;
  921. // The index of the first optional parameter.
  922. U32 firstArg;
  923. // Grab the number of elements in the first parameter.
  924. U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  925. // ("x y")
  926. if ((elementCount == 2) && (argc < 8))
  927. {
  928. point = Utility::mGetStringElementVector(argv[2]);
  929. firstArg = 3;
  930. }
  931. // (x, y)
  932. else if ((elementCount == 1) && (argc > 3))
  933. {
  934. point = Vector2(dAtof(argv[2]), dAtof(argv[3]));
  935. firstArg = 4;
  936. }
  937. // Invalid
  938. else
  939. {
  940. Con::warnf("CompositeSprite::pickPoint() - Invalid number of parameters!");
  941. return NULL;
  942. }
  943. // Fetch the render transform.
  944. const b2Transform& renderTransform = object->getRenderTransform();
  945. // Transform into local space.
  946. point = b2MulT( renderTransform, point );
  947. // Perform query.
  948. pSpriteBatchQuery->queryPoint( point, true );
  949. // Fetch result count.
  950. const U32 resultCount = pSpriteBatchQuery->getQueryResultsCount();
  951. // Finish if no results.
  952. if (resultCount == 0 )
  953. return NULL;
  954. // Fetch results.
  955. typeSpriteBatchQueryResultVector& queryResults = pSpriteBatchQuery->getQueryResults();
  956. // Set Max Buffer Size.
  957. const U32 maxBufferSize = 4096;
  958. // Create Returnable Buffer.
  959. char* pBuffer = Con::getReturnBuffer(maxBufferSize);
  960. // Set Buffer Counter.
  961. U32 bufferCount = 0;
  962. // Add picked sprites.
  963. for ( U32 n = 0; n < resultCount; n++ )
  964. {
  965. // Output Object ID.
  966. bufferCount += dSprintf( pBuffer + bufferCount, maxBufferSize-bufferCount, "%d ", queryResults[n].mpSpriteBatchItem->getBatchId() );
  967. // Finish early if we run out of buffer space.
  968. if ( bufferCount >= maxBufferSize )
  969. {
  970. // Warn.
  971. Con::warnf("CompositeSprite::pickPoint() - Too many items picked to return to scripts!");
  972. break;
  973. }
  974. }
  975. // Clear sprite batch query.
  976. pSpriteBatchQuery->clearQuery();
  977. // Return buffer.
  978. return pBuffer;
  979. }
  980. //-----------------------------------------------------------------------------
  981. /*! Picks sprites intersecting the specified area with optional group/layer masks.
  982. @param startx/y The coordinates of the start point as either (\x y\ or (x,y)
  983. @param endx/y The coordinates of the end point as either (\x y\ or (x,y)
  984. @return Returns list of sprite Ids.
  985. */
  986. ConsoleMethodWithDocs(CompositeSprite, pickArea, ConsoleString, 4, 6, (startx/y, endx/y ))
  987. {
  988. // Fetch sprite batch query and clear results.
  989. SpriteBatchQuery* pSpriteBatchQuery = object->getSpriteBatchQuery( true );
  990. // Is the sprite batch query available?
  991. if ( pSpriteBatchQuery == NULL )
  992. {
  993. // No, so warn.
  994. Con::warnf( "CompositeSprite::pickArea() - Cannot pick sprites if clipping mode is off." );
  995. // Return nothing.
  996. return NULL;
  997. }
  998. // Upper left and lower right bound.
  999. Vector2 v1, v2;
  1000. // The index of the first optional parameter.
  1001. U32 firstArg;
  1002. // Grab the number of elements in the first two parameters.
  1003. U32 elementCount1 = Utility::mGetStringElementCount(argv[2]);
  1004. U32 elementCount2 = 1;
  1005. if (argc > 3)
  1006. elementCount2 = Utility::mGetStringElementCount(argv[3]);
  1007. // ("x1 y1 x2 y2")
  1008. if ((elementCount1 == 4) && (argc < 9))
  1009. {
  1010. v1 = Utility::mGetStringElementVector(argv[2]);
  1011. v2 = Utility::mGetStringElementVector(argv[2], 2);
  1012. firstArg = 3;
  1013. }
  1014. // ("x1 y1", "x2 y2")
  1015. else if ((elementCount1 == 2) && (elementCount2 == 2) && (argc > 3) && (argc < 10))
  1016. {
  1017. v1 = Utility::mGetStringElementVector(argv[2]);
  1018. v2 = Utility::mGetStringElementVector(argv[3]);
  1019. firstArg = 4;
  1020. }
  1021. // (x1, y1, x2, y2)
  1022. else if (argc > 5)
  1023. {
  1024. v1 = Vector2(dAtof(argv[2]), dAtof(argv[3]));
  1025. v2 = Vector2(dAtof(argv[4]), dAtof(argv[5]));
  1026. firstArg = 6;
  1027. }
  1028. // Invalid
  1029. else
  1030. {
  1031. Con::warnf("CompositeSprite::pickArea() - Invalid number of parameters!");
  1032. return NULL;
  1033. }
  1034. // Calculate normalized AABB.
  1035. b2AABB aabb;
  1036. aabb.lowerBound.x = getMin( v1.x, v2.x );
  1037. aabb.lowerBound.y = getMin( v1.y, v2.y );
  1038. aabb.upperBound.x = getMax( v1.x, v2.x );
  1039. aabb.upperBound.y = getMax( v1.y, v2.y );
  1040. // Calculate local OOBB.
  1041. b2Vec2 localOOBB[4];
  1042. CoreMath::mAABBtoOOBB( aabb, localOOBB );
  1043. CoreMath::mCalculateInverseOOBB( localOOBB, object->getRenderTransform(), localOOBB );
  1044. // Calculate local AABB.
  1045. b2AABB localAABB;
  1046. CoreMath::mOOBBtoAABB( localOOBB, localAABB );
  1047. // Convert OOBB to a PolygonShape
  1048. b2PolygonShape oobb_polygon;
  1049. oobb_polygon.Set(localOOBB, 4);
  1050. // Perform query.
  1051. pSpriteBatchQuery->queryOOBB( localAABB, oobb_polygon, true );
  1052. // Fetch result count.
  1053. const U32 resultCount = pSpriteBatchQuery->getQueryResultsCount();
  1054. // Finish if no results.
  1055. if (resultCount == 0 )
  1056. return NULL;
  1057. // Fetch results.
  1058. typeSpriteBatchQueryResultVector& queryResults = pSpriteBatchQuery->getQueryResults();
  1059. // Set Max Buffer Size.
  1060. const U32 maxBufferSize = 4096;
  1061. // Create Returnable Buffer.
  1062. char* pBuffer = Con::getReturnBuffer(maxBufferSize);
  1063. // Set Buffer Counter.
  1064. U32 bufferCount = 0;
  1065. // Add picked objects.
  1066. for ( U32 n = 0; n < resultCount; n++ )
  1067. {
  1068. // Output Object ID.
  1069. bufferCount += dSprintf( pBuffer + bufferCount, maxBufferSize-bufferCount, "%d ", queryResults[n].mpSpriteBatchItem->getBatchId() );
  1070. // Finish early if we run out of buffer space.
  1071. if ( bufferCount >= maxBufferSize )
  1072. {
  1073. // Warn.
  1074. Con::warnf("CompositeSprite::pickArea() - Too many items picked to return to scripts!");
  1075. break;
  1076. }
  1077. }
  1078. // Clear sprite batch query.
  1079. pSpriteBatchQuery->clearQuery();
  1080. // Return buffer.
  1081. return pBuffer;
  1082. }
  1083. //-----------------------------------------------------------------------------
  1084. /*! Picks sprites intersecting the specified ray with optional group/layer masks.
  1085. @param startx/y The coordinates of the start point as either (\x y\ or (x,y)
  1086. @param endx/y The coordinates of the end point as either (\x y\ or (x,y)
  1087. @return Returns list of sprite Ids
  1088. */
  1089. ConsoleMethodWithDocs(CompositeSprite, pickRay, ConsoleString, 4, 6, (startx/y, endx/y))
  1090. {
  1091. // Fetch sprite batch query and clear results.
  1092. SpriteBatchQuery* pSpriteBatchQuery = object->getSpriteBatchQuery( true );
  1093. // Is the sprite batch query available?
  1094. if ( pSpriteBatchQuery == NULL )
  1095. {
  1096. // No, so warn.
  1097. Con::warnf( "CompositeSprite::pickRay() - Cannot pick sprites if clipping mode is off." );
  1098. // Return nothing.
  1099. return NULL;
  1100. }
  1101. // Upper left and lower right bound.
  1102. Vector2 v1, v2;
  1103. // The index of the first optional parameter.
  1104. U32 firstArg;
  1105. // Grab the number of elements in the first two parameters.
  1106. U32 elementCount1 = Utility::mGetStringElementCount(argv[2]);
  1107. U32 elementCount2 = 1;
  1108. if (argc > 3)
  1109. elementCount2 = Utility::mGetStringElementCount(argv[3]);
  1110. // ("x1 y1 x2 y2")
  1111. if ((elementCount1 == 4) && (argc < 9))
  1112. {
  1113. v1 = Utility::mGetStringElementVector(argv[2]);
  1114. v2 = Utility::mGetStringElementVector(argv[2], 2);
  1115. firstArg = 3;
  1116. }
  1117. // ("x1 y1", "x2 y2")
  1118. else if ((elementCount1 == 2) && (elementCount2 == 2) && (argc > 3) && (argc < 10))
  1119. {
  1120. v1 = Utility::mGetStringElementVector(argv[2]);
  1121. v2 = Utility::mGetStringElementVector(argv[3]);
  1122. firstArg = 4;
  1123. }
  1124. // (x1, y1, x2, y2)
  1125. else if (argc > 5)
  1126. {
  1127. v1 = Vector2(dAtof(argv[2]), dAtof(argv[3]));
  1128. v2 = Vector2(dAtof(argv[4]), dAtof(argv[5]));
  1129. firstArg = 6;
  1130. }
  1131. // Invalid
  1132. else
  1133. {
  1134. Con::warnf("CompositeSprite::pickRay() - Invalid number of parameters!");
  1135. return NULL;
  1136. }
  1137. // Fetch the render transform.
  1138. const b2Transform& renderTransform = object->getRenderTransform();
  1139. // Transform into local space.
  1140. v1 = b2MulT( renderTransform, v1 );
  1141. v2 = b2MulT( renderTransform, v2 );
  1142. // Perform query.
  1143. pSpriteBatchQuery->queryRay( v1, v2, true );
  1144. // Sanity!
  1145. AssertFatal( pSpriteBatchQuery->getIsRaycastQueryResult(), "Invalid non-ray-cast query result returned." );
  1146. // Fetch result count.
  1147. const U32 resultCount = pSpriteBatchQuery->getQueryResultsCount();
  1148. // Finish if no results.
  1149. if (resultCount == 0 )
  1150. return NULL;
  1151. // Sort ray-cast result.
  1152. pSpriteBatchQuery->sortRaycastQueryResult();
  1153. // Fetch results.
  1154. typeSpriteBatchQueryResultVector& queryResults = pSpriteBatchQuery->getQueryResults();
  1155. // Set Max Buffer Size.
  1156. const U32 maxBufferSize = 4096;
  1157. // Create Returnable Buffer.
  1158. char* pBuffer = Con::getReturnBuffer(maxBufferSize);
  1159. // Set Buffer Counter.
  1160. U32 bufferCount = 0;
  1161. // Add Picked Objects to List.
  1162. for ( U32 n = 0; n < resultCount; n++ )
  1163. {
  1164. // Output Object ID.
  1165. bufferCount += dSprintf( pBuffer + bufferCount, maxBufferSize-bufferCount, "%d ", queryResults[n].mpSpriteBatchItem->getBatchId() );
  1166. // Finish early if we run out of buffer space.
  1167. if ( bufferCount >= maxBufferSize )
  1168. {
  1169. // Warn.
  1170. Con::warnf("CompositeSprite::pickRay() - Too many items picked to return to scripts!");
  1171. break;
  1172. }
  1173. }
  1174. // Clear sprite batch query.
  1175. pSpriteBatchQuery->clearQuery();
  1176. // Return buffer.
  1177. return pBuffer;
  1178. }
  1179. ConsoleMethodGroupEndWithDocs(CompositeSprite)