guiAnimBitmapCtrl.cpp 9.8 KB

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