guiAnimBitmapCtrl.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gui/controls/guiAnimBitmapCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. IMPLEMENT_CONOBJECT(guiAnimBitmapCtrl);
  30. IMPLEMENT_CALLBACK(guiAnimBitmapCtrl, onLoop, void, (),
  31. (), "triggered when a loop completes");
  32. IMPLEMENT_CALLBACK(guiAnimBitmapCtrl, onCompleted, void, (),
  33. (), "triggered when an animation completes");
  34. IMPLEMENT_CALLBACK(guiAnimBitmapCtrl, onFrame, void, (S32 frameIndex, S32 frame),
  35. (frameIndex, frame), "triggered when a frame increments");
  36. guiAnimBitmapCtrl::guiAnimBitmapCtrl(void)
  37. {
  38. mAnimTexTiling = Point2I::One;
  39. mAnimTexFramesString = NULL;
  40. mAnimTexFrames.clear();
  41. mNumFrames = 0;
  42. mCurFrameIndex = 0;
  43. mFramesPerSec = 60;
  44. mAnimateTexture = false;
  45. mFrameTime = PlatformTimer::create();
  46. mLoop = true;
  47. mPlay = true;
  48. mReverse = false;
  49. mFinished = false;
  50. }
  51. guiAnimBitmapCtrl::~guiAnimBitmapCtrl(void)
  52. {
  53. mAnimTexFrames.clear();
  54. }
  55. void guiAnimBitmapCtrl::initPersistFields()
  56. {
  57. addField("AnimTexTiling", TYPEID< Point2I >(), Offset(mAnimTexTiling, guiAnimBitmapCtrl),
  58. "@brief The number of frames, in rows and columns stored in textureName "
  59. "(when animateTexture is true).\n\n"
  60. "A maximum of 256 frames can be stored in a single texture when using "
  61. "mAnimTexTiling. Value should be \"NumColumns NumRows\", for example \"4 4\".");
  62. addProtectedField("AnimTexFrames", TYPEID< StringTableEntry >(), Offset(mAnimTexFramesString, guiAnimBitmapCtrl), &ptSetFrameRanges, &defaultProtectedGetFn,
  63. "@brief A list of frames and/or frame ranges to use for particle "
  64. "animation if animateTexture is true.\n\n"
  65. "Each frame token must be separated by whitespace. A frame token must be "
  66. "a positive integer frame number or a range of frame numbers separated "
  67. "with a '-'. The range separator, '-', cannot have any whitspace around "
  68. "it.\n\n"
  69. "Ranges can be specified to move through the frames in reverse as well "
  70. "as forward (eg. 19-14). Frame numbers exceeding the number of tiles will "
  71. "wrap.\n"
  72. "@tsexample\n"
  73. "mAnimTexFrames = \"0-16 20 19 18 17 31-21\";\n"
  74. "@endtsexample\n");
  75. addField("loop", TypeBool, Offset(mLoop, guiAnimBitmapCtrl), "loop?");
  76. addField("play", TypeBool, Offset(mPlay, guiAnimBitmapCtrl), "play?");
  77. addField("reverse", TypeBool, Offset(mReverse, guiAnimBitmapCtrl), "play reversed?");
  78. addField("fps", TypeS32, Offset(mFramesPerSec, guiAnimBitmapCtrl), "Frame Rate");
  79. addProtectedField("curFrame", TypeS32, Offset(mCurFrameIndex, guiAnimBitmapCtrl), &ptSetFrame, &defaultProtectedGetFn, "Index of currently Displaying Frame ");
  80. Parent::initPersistFields();
  81. removeField("wrap");
  82. }
  83. bool guiAnimBitmapCtrl::onAdd()
  84. {
  85. if (Parent::onAdd() == false)
  86. return false;
  87. if (!mAnimTexFramesString || !mAnimTexFramesString[0])
  88. {
  89. S32 n_tiles = mAnimTexTiling.x * mAnimTexTiling.y - 1;
  90. for (S32 i = 0; i <= n_tiles; i++)
  91. mAnimTexFrames.push_back(i);
  92. mNumFrames = mAnimTexFrames.size() - 1;
  93. if (mCurFrameIndex > mNumFrames)
  94. mCurFrameIndex = mNumFrames;
  95. return true;
  96. }
  97. return true;
  98. }
  99. bool guiAnimBitmapCtrl::ptSetFrame(void *object, const char *index, const char *data)
  100. {
  101. guiAnimBitmapCtrl *pData = static_cast<guiAnimBitmapCtrl*>(object);
  102. if (!pData->mNumFrames)
  103. {
  104. pData->mCurFrameIndex = 0;
  105. return false;
  106. }
  107. S32 val = dAtoi(data);
  108. if (val < 0)
  109. {
  110. pData->mCurFrameIndex = pData->mNumFrames;
  111. return false;
  112. }
  113. else if (val > pData->mNumFrames)
  114. {
  115. pData->mCurFrameIndex = 0;
  116. return false;
  117. };
  118. pData->mCurFrameIndex = val;
  119. return true;
  120. }
  121. bool guiAnimBitmapCtrl::ptSetFrameRanges(void *object, const char *index, const char *data)
  122. {
  123. guiAnimBitmapCtrl *pData = static_cast<guiAnimBitmapCtrl*>(object);
  124. // Here we parse mAnimTexFramesString into byte-size frame numbers in mAnimTexFrames.
  125. // Each frame token must be separated by whitespace.
  126. // A frame token must be a positive integer frame number or a range of frame numbers
  127. // separated with a '-'.
  128. // The range separator, '-', cannot have any whitspace around it.
  129. // Ranges can be specified to move through the frames in reverse as well as forward.
  130. // Frame numbers exceeding the number of tiles will wrap.
  131. // example:
  132. // "0-16 20 19 18 17 31-21"
  133. S32 n_tiles = pData->mAnimTexTiling.x * pData->mAnimTexTiling.y - 1;
  134. pData->mAnimTexFrames.clear();
  135. if (!data || !data[0])
  136. {
  137. for (S32 i = 0; i <= n_tiles; i++)
  138. pData->mAnimTexFrames.push_back(i);
  139. pData->mNumFrames = pData->mAnimTexFrames.size() - 1;
  140. if (pData->mCurFrameIndex > pData->mNumFrames)
  141. pData->mCurFrameIndex = pData->mNumFrames;
  142. return true;
  143. }
  144. char* tokCopy = new char[dStrlen(data) + 1];
  145. dStrcpy(tokCopy, data, dStrlen(data) + 1);
  146. char* currTok = dStrtok(tokCopy, " \t");
  147. while (currTok != NULL)
  148. {
  149. char* minus = dStrchr(currTok, '-');
  150. if (minus)
  151. {
  152. // add a range of frames
  153. *minus = '\0';
  154. S32 range_a = dAtoi(currTok);
  155. S32 range_b = dAtoi(minus + 1);
  156. if (range_b < range_a)
  157. {
  158. // reverse frame range
  159. for (S32 i = range_a; i >= range_b; i--)
  160. pData->mAnimTexFrames.push_back(i);
  161. }
  162. else
  163. {
  164. // forward frame range
  165. for (S32 i = range_a; i <= range_b; i++)
  166. pData->mAnimTexFrames.push_back(i);
  167. }
  168. }
  169. else
  170. {
  171. // add one frame
  172. pData->mAnimTexFrames.push_back(dAtoi(currTok));
  173. }
  174. currTok = dStrtok(NULL, " \t");
  175. }
  176. // cleanup
  177. delete[] tokCopy;
  178. pData->mNumFrames = pData->mAnimTexFrames.size() - 1;
  179. if (pData->mCurFrameIndex > pData->mNumFrames)
  180. pData->mCurFrameIndex = pData->mNumFrames;
  181. return true;
  182. }
  183. void guiAnimBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  184. {
  185. if (mTextureObject)
  186. {
  187. if (mFrameTime->getElapsedMs() > 1000 / mFramesPerSec) //fps to msfp conversion
  188. {
  189. mFrameTime->reset();
  190. if (mPlay)
  191. {
  192. if (mReverse) //play backward
  193. {
  194. mCurFrameIndex--;
  195. if (mCurFrameIndex < 0)
  196. {
  197. if (mLoop)
  198. {
  199. mCurFrameIndex = mNumFrames;
  200. onLoop_callback();
  201. mFinished = false;
  202. }
  203. else
  204. {
  205. mCurFrameIndex = 0;
  206. if (!mFinished)
  207. onCompleted_callback();
  208. mFinished = true;
  209. }
  210. }
  211. else
  212. onFrame_callback(mCurFrameIndex, mAnimTexFrames[mCurFrameIndex]);
  213. }
  214. else // play forward
  215. {
  216. mCurFrameIndex++;
  217. if (mCurFrameIndex > mNumFrames)
  218. {
  219. if (mLoop)
  220. {
  221. mCurFrameIndex = 0;
  222. onLoop_callback();
  223. mFinished = false;
  224. }
  225. else
  226. {
  227. mCurFrameIndex = mNumFrames;
  228. if (!mFinished)
  229. onCompleted_callback();
  230. mFinished = true;
  231. }
  232. }
  233. else
  234. onFrame_callback(mCurFrameIndex, mAnimTexFrames[mCurFrameIndex]);
  235. }
  236. }
  237. }
  238. GFX->getDrawUtil()->clearBitmapModulation();
  239. GFX->getDrawUtil()->setBitmapModulation(mColor);
  240. GFXTextureObject* texture = mTextureObject;
  241. Point2I modifiedSRC = Point2I(texture->mBitmapSize.x / mAnimTexTiling.x, texture->mBitmapSize.y / mAnimTexTiling.y);
  242. RectI srcRegion;
  243. Point2I offsetSRC = Point2I::Zero;
  244. offsetSRC.x = (texture->mBitmapSize.x / mAnimTexTiling.x) * (mAnimTexFrames[mCurFrameIndex] % mAnimTexTiling.x);
  245. offsetSRC.y = (texture->mBitmapSize.y / mAnimTexTiling.y) * (mAnimTexFrames[mCurFrameIndex] / mAnimTexTiling.x);
  246. srcRegion.set(offsetSRC, modifiedSRC);
  247. GFX->getDrawUtil()->drawBitmapStretchSR(texture, updateRect, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  248. }
  249. if (mProfile->mBorder || !mTextureObject)
  250. {
  251. RectI rect(offset, getExtent());
  252. GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
  253. }
  254. renderChildControls(offset, updateRect);
  255. }