guiSpriteCtrl.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _GUISPRITECTRL_H_
  23. #include "2d/gui/guiSpriteCtrl.h"
  24. #endif
  25. #ifndef _CONSOLE_H_
  26. #include "console/console.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _DGL_H_
  32. #include "graphics/dgl.h"
  33. #endif
  34. #include "guiSpriteCtrl_ScriptBindings.h"
  35. //-----------------------------------------------------------------------------
  36. IMPLEMENT_CONOBJECT(GuiSpriteCtrl);
  37. //-----------------------------------------------------------------------------
  38. GuiSpriteCtrl::GuiSpriteCtrl( void ) :
  39. mImageAssetId(StringTable->EmptyString),
  40. mImageFrameId(0),
  41. mNamedImageFrameId(StringTable-> EmptyString),
  42. mAnimationAssetId(StringTable->EmptyString)
  43. {
  44. // Set to self ticking.
  45. mSelfTick = true;
  46. // Default to static provider.
  47. mStaticProvider = true;
  48. }
  49. //-----------------------------------------------------------------------------
  50. GuiSpriteCtrl::~GuiSpriteCtrl()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. void GuiSpriteCtrl::initPersistFields()
  55. {
  56. // Call parent.
  57. Parent::initPersistFields();
  58. addProtectedField("Image", TypeAssetId, Offset(mImageAssetId, GuiSpriteCtrl), &setImage, &defaultProtectedGetFn, &writeImage, "The image asset Id used for the image.");
  59. addProtectedField("Frame", TypeS32, Offset(mImageFrameId, GuiSpriteCtrl), &setImageFrame, &defaultProtectedGetFn, &writeImageFrame, "The image frame used for the image.");
  60. addProtectedField("NamedFrame", TypeString, Offset(mNamedImageFrameId, GuiSpriteCtrl), &setNamedImageFrame, &defaultProtectedGetFn, &writeNamedImageFrame, "The named image frame used for the image");
  61. addProtectedField("Animation", TypeAssetId, Offset(mAnimationAssetId, GuiSpriteCtrl), &setAnimation, &defaultProtectedGetFn, &writeAnimation, "The animation to use.");
  62. }
  63. //------------------------------------------------------------------------------
  64. void GuiSpriteCtrl::copyTo(SimObject* object)
  65. {
  66. // Call to parent.
  67. Parent::copyTo(object);
  68. // Cast to control.
  69. GuiSpriteCtrl* pGuiSpriteCtrl = static_cast<GuiSpriteCtrl*>(object);
  70. // Sanity!
  71. AssertFatal(pGuiSpriteCtrl != NULL, "GuiSpriteCtrl::copyTo() - Object is not the correct type.");
  72. // Copy asset fields.
  73. if ( mImageAssetId != StringTable->EmptyString )
  74. {
  75. if ( !isUsingNamedImageFrame() )
  76. pGuiSpriteCtrl->setImage( getImage(), getImageFrame() );
  77. else
  78. pGuiSpriteCtrl->setImage( getImage(), getNamedImageFrame() );
  79. }
  80. else if ( mAnimationAssetId != StringTable->EmptyString )
  81. {
  82. pGuiSpriteCtrl->setAnimation( getAnimation() );
  83. }
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool GuiSpriteCtrl::onWake()
  87. {
  88. // Call parent.
  89. if (!Parent::onWake())
  90. return false;
  91. // Are we in static mode?
  92. if ( mImageAssetId != StringTable->EmptyString )
  93. {
  94. if ( mNamedImageFrameId != StringTable->EmptyString)
  95. {
  96. // Set the image asset and named frame
  97. ImageFrameProvider::setImage( mImageAssetId, mNamedImageFrameId );
  98. }
  99. else
  100. {
  101. // Set image asset and numerical frame.
  102. ImageFrameProvider::setImage( mImageAssetId, mImageFrameId );
  103. }
  104. }
  105. else if ( mAnimationAssetId != StringTable->EmptyString )
  106. {
  107. // Play animation asset.
  108. ImageFrameProvider::setAnimation( mAnimationAssetId );
  109. }
  110. else
  111. {
  112. // Not good, so warn.
  113. Con::warnf("GuiSpriteCtrl::onWake() - No Image or Animation Asset defined.");
  114. }
  115. return true;
  116. }
  117. //-----------------------------------------------------------------------------
  118. void GuiSpriteCtrl::onSleep()
  119. {
  120. // Clear assets.
  121. ImageFrameProvider::clearAssets();
  122. // Call parent.
  123. Parent::onSleep();
  124. }
  125. //-----------------------------------------------------------------------------
  126. bool GuiSpriteCtrl::setImage( const char* pImageAssetId, const U32 frame )
  127. {
  128. // Sanity!
  129. AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
  130. // Reset animation.
  131. if ( mAnimationAssetId != StringTable->EmptyString )
  132. mAnimationAssetId = StringTable->EmptyString;
  133. // Fetch the asset Id.
  134. if ( mImageAssetId != pImageAssetId )
  135. mImageAssetId = StringTable->insert(pImageAssetId);
  136. // Set the image frame if the image asset was set.
  137. if ( mImageAssetId != StringTable->EmptyString )
  138. setImageFrame(frame);
  139. // Finish if not awake.
  140. if ( !isAwake() )
  141. return true;
  142. // Call parent.
  143. if ( !ImageFrameProvider::setImage(pImageAssetId, frame) )
  144. return false;
  145. // Update control.
  146. setUpdate();
  147. return true;
  148. }
  149. //-----------------------------------------------------------------------------
  150. bool GuiSpriteCtrl::setImage( const char* pImageAssetId, const char* pNamedFrame )
  151. {
  152. // Sanity!
  153. AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
  154. // Reset animation.
  155. if ( mAnimationAssetId != StringTable->EmptyString )
  156. mAnimationAssetId = StringTable->EmptyString;
  157. // Fetch the asset Id.
  158. if ( mImageAssetId != pImageAssetId )
  159. mImageAssetId = StringTable->insert(pImageAssetId);
  160. // Set the image frame if the image asset was set.
  161. if ( mImageAssetId != StringTable->EmptyString )
  162. setNamedImageFrame(pNamedFrame);
  163. // Finish if not awake.
  164. if ( !isAwake() )
  165. return true;
  166. // Call parent.
  167. if ( !ImageFrameProvider::setImage(pImageAssetId, pNamedFrame) )
  168. return false;
  169. // Update control.
  170. setUpdate();
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. bool GuiSpriteCtrl::setImageFrame( const U32 frame )
  175. {
  176. // Check Existing Image.
  177. if ( mImageAssetId == StringTable->EmptyString )
  178. {
  179. // Warn.
  180. Con::warnf("GuiSpriteCtrl::setImageFrame() - Cannot set frame without existing asset Id.");
  181. // Return Here.
  182. return false;
  183. }
  184. // Set frame.
  185. mImageFrameId = frame;
  186. // Finish if not awake.
  187. if ( !isAwake() )
  188. return true;
  189. // Call parent.
  190. if ( !ImageFrameProvider::setImageFrame(frame) )
  191. return false;
  192. // Update control.
  193. setUpdate();
  194. return true;
  195. }
  196. //-----------------------------------------------------------------------------
  197. bool GuiSpriteCtrl::setNamedImageFrame( const char* pNamedFrame )
  198. {
  199. // Check Existing Image.
  200. if ( mImageAssetId == StringTable->EmptyString )
  201. {
  202. // Warn.
  203. Con::warnf("GuiSpriteCtrl::setNamedImageFrame() - Cannot set named frame without existing asset Id.");
  204. // Return Here.
  205. return false;
  206. }
  207. // Set named frame.
  208. mNamedImageFrameId = StringTable->insert(pNamedFrame);
  209. // Finish if not awake.
  210. if ( !isAwake() )
  211. return true;
  212. // Call parent.
  213. if ( !ImageFrameProvider::setNamedImageFrame(pNamedFrame) )
  214. return false;
  215. // Update control.
  216. setUpdate();
  217. return true;
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
  221. {
  222. // Sanity!
  223. AssertFatal( pAnimationAssetId != NULL, "Cannot use a NULL asset Id." );
  224. // Reset the image asset Id.
  225. if ( mImageAssetId != StringTable->EmptyString )
  226. mImageAssetId = StringTable->EmptyString;
  227. // Fetch the asset Id.
  228. if ( mAnimationAssetId != pAnimationAssetId )
  229. mAnimationAssetId = StringTable->insert(pAnimationAssetId);
  230. // Finish if not awake.
  231. if ( !isAwake() )
  232. return true;
  233. // Play animation asset if it's valid.
  234. if ( mAnimationAssetId != StringTable->EmptyString )
  235. ImageFrameProvider::setAnimation( mAnimationAssetId );
  236. return true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. void GuiSpriteCtrl::onRender( Point2I offset, const RectI &updateRect)
  240. {
  241. // Call parent.
  242. ImageFrameProvider::renderGui( *this, offset, updateRect );
  243. }
  244. //------------------------------------------------------------------------------
  245. void GuiSpriteCtrl::onAnimationEnd( void )
  246. {
  247. // Clear assets.
  248. ImageFrameProvider::clearAssets();
  249. }