2
0

guiAnimBitmapCtrl.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. dsize_t tokLen = dStrlen(data) + 1;
  145. char* tokCopy = new char[tokLen];
  146. dStrcpy(tokCopy, data, tokLen);
  147. char* currTok = dStrtok(tokCopy, " \t");
  148. while (currTok != NULL)
  149. {
  150. char* minus = dStrchr(currTok, '-');
  151. if (minus)
  152. {
  153. // add a range of frames
  154. *minus = '\0';
  155. S32 range_a = dAtoi(currTok);
  156. S32 range_b = dAtoi(minus + 1);
  157. if (range_b < range_a)
  158. {
  159. // reverse frame range
  160. for (S32 i = range_a; i >= range_b; i--)
  161. pData->mAnimTexFrames.push_back(i);
  162. }
  163. else
  164. {
  165. // forward frame range
  166. for (S32 i = range_a; i <= range_b; i++)
  167. pData->mAnimTexFrames.push_back(i);
  168. }
  169. }
  170. else
  171. {
  172. // add one frame
  173. pData->mAnimTexFrames.push_back(dAtoi(currTok));
  174. }
  175. currTok = dStrtok(NULL, " \t");
  176. }
  177. // cleanup
  178. delete[] tokCopy;
  179. pData->mNumFrames = pData->mAnimTexFrames.size() - 1;
  180. if (pData->mCurFrameIndex > pData->mNumFrames)
  181. pData->mCurFrameIndex = pData->mNumFrames;
  182. return true;
  183. }
  184. void guiAnimBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  185. {
  186. if (mBitmap)
  187. {
  188. if (mFrameTime->getElapsedMs() > 1000 / mFramesPerSec) //fps to msfp conversion
  189. {
  190. mFrameTime->reset();
  191. if (mPlay)
  192. {
  193. if (mReverse) //play backward
  194. {
  195. mCurFrameIndex--;
  196. if (mCurFrameIndex < 0)
  197. {
  198. if (mLoop)
  199. {
  200. mCurFrameIndex = mNumFrames;
  201. onLoop_callback();
  202. mFinished = false;
  203. }
  204. else
  205. {
  206. mCurFrameIndex = 0;
  207. if (!mFinished)
  208. onCompleted_callback();
  209. mFinished = true;
  210. }
  211. }
  212. else
  213. onFrame_callback(mCurFrameIndex, mAnimTexFrames[mCurFrameIndex]);
  214. }
  215. else // play forward
  216. {
  217. mCurFrameIndex++;
  218. if (mCurFrameIndex > mNumFrames)
  219. {
  220. if (mLoop)
  221. {
  222. mCurFrameIndex = 0;
  223. onLoop_callback();
  224. mFinished = false;
  225. }
  226. else
  227. {
  228. mCurFrameIndex = mNumFrames;
  229. if (!mFinished)
  230. onCompleted_callback();
  231. mFinished = true;
  232. }
  233. }
  234. else
  235. onFrame_callback(mCurFrameIndex, mAnimTexFrames[mCurFrameIndex]);
  236. }
  237. }
  238. }
  239. GFX->getDrawUtil()->clearBitmapModulation();
  240. GFX->getDrawUtil()->setBitmapModulation(mColor);
  241. GFXTextureObject* texture = mBitmap;
  242. Point2I modifiedSRC = Point2I(texture->mBitmapSize.x / mAnimTexTiling.x, texture->mBitmapSize.y / mAnimTexTiling.y);
  243. RectI srcRegion;
  244. Point2I offsetSRC = Point2I::Zero;
  245. offsetSRC.x = (texture->mBitmapSize.x / mAnimTexTiling.x) * (mAnimTexFrames[mCurFrameIndex] % mAnimTexTiling.x);
  246. offsetSRC.y = (texture->mBitmapSize.y / mAnimTexTiling.y) * (mAnimTexFrames[mCurFrameIndex] / mAnimTexTiling.x);
  247. srcRegion.set(offsetSRC, modifiedSRC);
  248. GFX->getDrawUtil()->drawBitmapStretchSR(texture, updateRect, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  249. }
  250. if (mProfile->mBorder || !mBitmap)
  251. {
  252. RectI rect(offset, getExtent());
  253. GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
  254. }
  255. renderChildControls(offset, updateRect);
  256. }