2
0

guiAnimBitmapCtrl.cpp 10 KB

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