guiSpriteCtrl.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. mAnimationAssetId( StringTable->EmptyString )
  41. {
  42. // Set to self ticking.
  43. mSelfTick = true;
  44. }
  45. //-----------------------------------------------------------------------------
  46. GuiSpriteCtrl::~GuiSpriteCtrl()
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. void GuiSpriteCtrl::initPersistFields()
  51. {
  52. // Call parent.
  53. Parent::initPersistFields();
  54. addProtectedField( "Image", TypeAssetId, Offset(mImageAssetId, GuiSpriteCtrl), &setImage, &getImage, &writeImage, "The image asset Id used for the image." );
  55. addProtectedField( "Frame", TypeS32, Offset(mImageFrame, GuiSpriteCtrl), &setImageFrame, &defaultProtectedGetFn, &writeImageFrame, "The image frame used for the image." );
  56. addProtectedField( "Animation", TypeAssetId, Offset(mAnimationAssetId, GuiSpriteCtrl), &setAnimation, &getAnimation, &writeAnimation, "The animation to use.");
  57. }
  58. //-----------------------------------------------------------------------------
  59. bool GuiSpriteCtrl::onWake()
  60. {
  61. // Call parent.
  62. if (!Parent::onWake())
  63. return false;
  64. // Are we in static mode?
  65. if ( mImageAssetId != StringTable->EmptyString )
  66. {
  67. // Set image asset.
  68. ImageFrameProvider::setImage( mImageAssetId );
  69. }
  70. else if ( mAnimationAssetId != StringTable->EmptyString )
  71. {
  72. // Play animation asset.
  73. ImageFrameProvider::setAnimation( mAnimationAssetId );
  74. }
  75. return true;
  76. }
  77. //-----------------------------------------------------------------------------
  78. void GuiSpriteCtrl::onSleep()
  79. {
  80. // Clear assets.
  81. ImageFrameProvider::clearAssets();
  82. // Call parent.
  83. Parent::onSleep();
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool GuiSpriteCtrl::setImage( const char* pImageAssetId )
  87. {
  88. // Sanity!
  89. AssertFatal( pImageAssetId != NULL, "Cannot use a NULL asset Id." );
  90. // Reset animation.
  91. mAnimationAssetId = StringTable->EmptyString;
  92. // Fetch the asset Id.
  93. mImageAssetId = StringTable->insert(pImageAssetId);
  94. // Reset image frame.
  95. mImageFrame = 0;
  96. // Finish if not awake.
  97. if ( !isAwake() )
  98. return true;
  99. // Call parent.
  100. if ( !ImageFrameProvider::setImage( pImageAssetId, mImageFrame ) )
  101. return false;
  102. // Update control.
  103. setUpdate();
  104. return true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool GuiSpriteCtrl::setImageFrame( const U32 frame )
  108. {
  109. // Call parent.
  110. if ( !ImageFrameProvider::setImageFrame( frame ) )
  111. return false;
  112. // Update control.
  113. setUpdate();
  114. return true;
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool GuiSpriteCtrl::setAnimation( const char* pAnimationAssetId )
  118. {
  119. // Sanity!
  120. AssertFatal( pAnimationAssetId != NULL, "Cannot use a NULL asset Id." );
  121. // Fetch the asset Id.
  122. mAnimationAssetId = StringTable->insert(pAnimationAssetId);
  123. // Reset the image asset Id.
  124. mImageAssetId = StringTable->EmptyString;
  125. // Finish if not awake.
  126. if ( !isAwake() )
  127. return true;
  128. // Play animation asset if it's valid.
  129. if ( mAnimationAssetId != StringTable->EmptyString )
  130. ImageFrameProvider::setAnimation( mAnimationAssetId );
  131. return true;
  132. }
  133. //-----------------------------------------------------------------------------
  134. void GuiSpriteCtrl::onRender( Point2I offset, const RectI &updateRect)
  135. {
  136. // Call parent.
  137. ImageFrameProvider::renderGui( *this, offset, updateRect );
  138. }
  139. //------------------------------------------------------------------------------
  140. void GuiSpriteCtrl::onAnimationEnd( void )
  141. {
  142. // Clear assets.
  143. ImageFrameProvider::clearAssets();
  144. }