AANIM.CPP 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "aanim.h"
  5. #include "mclib.h"
  6. #include "Estring.h"
  7. extern float frameRate;
  8. aAnimation::aAnimation()
  9. {
  10. currentTime = -1.f;
  11. infoCount = 0;
  12. infos = NULL;
  13. bLoops = 0;
  14. refX = 0;
  15. refY = 0;
  16. direction = 1.0;
  17. }
  18. aAnimation::~aAnimation()
  19. {
  20. destroy();
  21. }
  22. aAnimation& aAnimation::operator=( const aAnimation& src )
  23. {
  24. copyData( src );
  25. return *this;
  26. }
  27. aAnimation::aAnimation( const aAnimation& src )
  28. {
  29. copyData( src );
  30. }
  31. void aAnimation::copyData( const aAnimation& src )
  32. {
  33. if ( &src != this )
  34. {
  35. currentTime = src.currentTime;
  36. infoCount = src.infoCount;
  37. infos = NULL;
  38. bLoops = src.bLoops;
  39. refX = src.refX;
  40. refY = src.refY;
  41. //Maybe someone already inited this?
  42. if (infos)
  43. {
  44. delete [] infos;
  45. infos = NULL;
  46. }
  47. if ( src.infoCount )
  48. {
  49. infos = new MoveInfo[infoCount];
  50. for ( int i = 0; i < infoCount; i++ )
  51. {
  52. infos[i] = src.infos[i];
  53. }
  54. }
  55. }
  56. }
  57. long aAnimation::init(FitIniFile* file, const char* headerName)
  58. {
  59. EString strToCheck = headerName;
  60. strToCheck += "AnimationTimeStamps";
  61. if ( NO_ERR != file->readIdLong( strToCheck, infoCount ) )
  62. {
  63. EString strToCheck = headerName;
  64. strToCheck += "NumTimeStamps";
  65. file->readIdLong( strToCheck, infoCount );
  66. }
  67. strToCheck = headerName;
  68. strToCheck += "AnimationLoops";
  69. file->readIdBoolean( strToCheck, bLoops );
  70. //Maybe someone already inited this?
  71. if (infos)
  72. {
  73. delete [] infos;
  74. infos = NULL;
  75. }
  76. if ( infoCount )
  77. {
  78. infos = new MoveInfo[infoCount];
  79. for ( int i = 0; i < infoCount; i++ )
  80. {
  81. strToCheck.Format( "%s%s%ld", headerName, "Time", i );
  82. file->readIdFloat( strToCheck, infos[i].time );
  83. strToCheck.Format( "%s%s%ld", headerName, "Color", i );
  84. if ( NO_ERR != file->readIdLong( strToCheck, infos[i].color ) )
  85. infos[i].color = 0xffffffff;
  86. strToCheck.Format( "%s%s%ld%s", headerName, "Pos", i, "X" );
  87. long tmp;
  88. file->readIdLong( strToCheck, tmp );
  89. infos[i].positionX = tmp;
  90. strToCheck.Format( "%s%s%ld%s", headerName, "Pos", i, "Y" );
  91. file->readIdLong( strToCheck, tmp );
  92. infos[i].positionY = tmp;
  93. strToCheck.Format( "%s%s%ld", headerName, "Scale", i );
  94. if ( NO_ERR != file->readIdFloat( strToCheck, infos[i].scaleX ) )
  95. {
  96. // look for scaleX and scale Y
  97. strToCheck.Format( "%s%s%ld", headerName, "ScaleX", i );
  98. if ( NO_ERR != file->readIdFloat( strToCheck, infos[i].scaleX ) )
  99. {
  100. infos[i].scaleX = infos[i].scaleY = 1.0f;
  101. }
  102. else
  103. {
  104. strToCheck.Format( "%s%s%ld", headerName, "ScaleY", i );
  105. if ( NO_ERR != file->readIdFloat( strToCheck, infos[i].scaleY ) )
  106. {
  107. infos[i].scaleY = 1.0f;
  108. }
  109. }
  110. }
  111. else
  112. infos[i].scaleY = infos[i].scaleX;
  113. }
  114. }
  115. return 0;
  116. }
  117. void aAnimation::destroy()
  118. {
  119. if ( infos )
  120. delete [] infos;
  121. infos = NULL;
  122. infoCount = 0;
  123. currentTime = -1.f;
  124. }
  125. void aAnimation::begin()
  126. {
  127. currentTime = 0;
  128. direction = 1.0;
  129. }
  130. void aAnimation::reverseBegin()
  131. {
  132. if ( infos && infoCount )
  133. currentTime = infos[infoCount-1].time;
  134. else
  135. currentTime = 0.0;
  136. direction = -1.0;
  137. }
  138. void aAnimation::end()
  139. {
  140. currentTime = -1.f;
  141. }
  142. void aAnimation::update()
  143. {
  144. if ( currentTime != -1.f )
  145. currentTime += direction * frameLength;
  146. if ( direction < 0 && bLoops && infoCount && infos )
  147. {
  148. if ( currentTime < 0 )
  149. currentTime += infos[infoCount-1].time;
  150. }
  151. else if ( infos && currentTime > infos[infoCount-1].time )
  152. {
  153. if ( bLoops )
  154. currentTime -= infos[infoCount-1].time;
  155. }
  156. }
  157. float aAnimation::getXDelta() const
  158. {
  159. float t0, t1, p0, p1;
  160. t1 = p1 = t0 = p0 = 0.f;
  161. float delta = 0.f;
  162. // figure out where we are in animation
  163. for ( int j = 0; j < infoCount - 1; j++ )
  164. {
  165. if ( infos[j].time <= currentTime && infos[j+1].time > currentTime )
  166. {
  167. t0 = infos[j].time;
  168. t1 = infos[j + 1].time;
  169. p0 = (infos[j].positionX);
  170. p1 = (infos[j + 1].positionX);
  171. break;
  172. }
  173. }
  174. // if not done yet
  175. if ( t1 )
  176. {
  177. float dT = currentTime - t0;
  178. float currentPosition = p0 + dT * ( (p1 - p0)/(t1 -t0) );
  179. delta = currentPosition - refX;
  180. }
  181. else if ( infos )
  182. {
  183. delta = infos[infoCount - 1].positionX - refX;
  184. }
  185. return delta;
  186. }
  187. float aAnimation::getYDelta() const
  188. {
  189. float t0, t1, p0, p1;
  190. t1 = p1 = t0 = p0 = 0.f;
  191. float delta = 0.f;
  192. // figure out where we are in animation
  193. for ( int j = 0; j < infoCount - 1; j++ )
  194. {
  195. if ( infos[j].time <= currentTime && infos[j+1].time > currentTime )
  196. {
  197. t0 = infos[j].time;
  198. t1 = infos[j + 1].time;
  199. p0 = (infos[j].positionY);
  200. p1 = (infos[j + 1].positionY);
  201. break;
  202. }
  203. }
  204. // if not done yet
  205. if ( t1 )
  206. {
  207. float dT = currentTime - t0;
  208. float currentPosition = p0 + dT * ( (p1 - p0)/(t1 -t0) );
  209. delta = currentPosition - refY;
  210. }
  211. else if ( infos )
  212. {
  213. delta = infos[infoCount - 1].positionY - refY ;
  214. }
  215. return delta;
  216. }
  217. float aAnimation::getScaleX() const
  218. {
  219. float t0, t1, p0, p1;
  220. t1 = p1 = t0 = p0 = 0.f;
  221. float curScale = 1.0;
  222. // figure out where we are in animation
  223. for ( int j = 0; j < infoCount - 1; j++ )
  224. {
  225. if ( infos[j].time <= currentTime && infos[j+1].time > currentTime )
  226. {
  227. t0 = infos[j].time;
  228. t1 = infos[j + 1].time;
  229. p0 = (infos[j].scaleX);
  230. p1 = (infos[j + 1].scaleX);
  231. break;
  232. }
  233. }
  234. // if not done yet
  235. if ( t1 )
  236. {
  237. float dT = currentTime - t0;
  238. curScale = p0 + dT * ( (p1 - p0)/(t1 -t0) );
  239. }
  240. else if ( infos )
  241. {
  242. curScale = infos[infoCount - 1].scaleX ;
  243. }
  244. return curScale;
  245. }
  246. float aAnimation::getScaleY() const
  247. {
  248. float t0, t1, p0, p1;
  249. t1 = p1 = t0 = p0 = 0.f;
  250. float curScale = 1.0;
  251. // figure out where we are in animation
  252. for ( int j = 0; j < infoCount - 1; j++ )
  253. {
  254. if ( infos[j].time <= currentTime && infos[j+1].time > currentTime )
  255. {
  256. t0 = infos[j].time;
  257. t1 = infos[j + 1].time;
  258. p0 = (infos[j].scaleY);
  259. p1 = (infos[j + 1].scaleY);
  260. break;
  261. }
  262. }
  263. // if not done yet
  264. if ( t1 )
  265. {
  266. float dT = currentTime - t0;
  267. curScale = p0 + dT * ( (p1 - p0)/(t1 -t0) );
  268. }
  269. else if ( infos )
  270. {
  271. curScale = infos[infoCount - 1].scaleY ;
  272. }
  273. return curScale;
  274. }
  275. unsigned long aAnimation::getColor( ) const
  276. {
  277. return getColor( currentTime );
  278. }
  279. unsigned long aAnimation::getColor( float time ) const
  280. {
  281. float t0, t1;
  282. if ( infoCount && (time > infos[infoCount-1].time) && bLoops )
  283. {
  284. float numCycles = infos[infoCount-1].time ? time/infos[infoCount-1].time : 0;
  285. int numCyc = (long)numCycles;
  286. time -= ((float)numCyc * infos[infoCount-1].time);
  287. }
  288. t1 = t0 = 0.f;
  289. unsigned long color = 0xffffffff;
  290. unsigned long color1, color2;
  291. color1 = color2 = 0;
  292. // figure out where we are in animation
  293. for ( int j = 0; j < infoCount - 1; j++ )
  294. {
  295. if ( infos[j].time <= time && infos[j+1].time > time )
  296. {
  297. t0 = infos[j].time;
  298. t1 = infos[j + 1].time;
  299. color1 = (infos[j].color);
  300. color2 = (infos[j + 1].color);
  301. break;
  302. }
  303. }
  304. // if not done yet
  305. if ( t1 )
  306. {
  307. float totalTime = t1 - t0;
  308. float tmpTime= time - t0;
  309. float percent = tmpTime/totalTime;
  310. return interpolateColor( color1, color2, percent );
  311. }
  312. else if ( infos )
  313. {
  314. return infos[infoCount-1].color;
  315. }
  316. return color;
  317. }
  318. void aAnimation::setReferencePoints( float X, float Y )
  319. {
  320. refX = X;
  321. refY = Y;
  322. }
  323. bool aAnimation::isDone() const
  324. {
  325. if ( bLoops )
  326. return 0;
  327. if ( direction < 0 )
  328. return currentTime < 0 ? 1 : 0;
  329. else if ( infos && currentTime > infos[infoCount-1].time )
  330. {
  331. return 1;
  332. }
  333. return 0;
  334. }
  335. long aAnimation::initWithBlockName( FitIniFile* file, const char* blockName )
  336. {
  337. if ( NO_ERR != file->seekBlock( blockName ) )
  338. {
  339. char errorStr[255];
  340. sprintf( errorStr, "couldn't find block %s in file %s", blockName, file->getFilename() );
  341. Assert( 0, 0, errorStr );
  342. return -1;
  343. }
  344. return init( file, "" );
  345. }
  346. float aAnimation::getMaxTime()
  347. {
  348. if ( infos && infoCount)
  349. {
  350. return infos[infoCount-1].time;
  351. }
  352. return -1;
  353. }
  354. long aAnimGroup::init( FitIniFile* file, const char* blockName )
  355. {
  356. if ( NO_ERR != file->seekBlock( blockName ) )
  357. {
  358. char errorStr[255];
  359. sprintf( errorStr, "couldn't find block %s in file %s", blockName, file->getFilename() );
  360. Assert( 0, 0, errorStr );
  361. return -1;
  362. }
  363. animations[NORMAL].init( file, "Normal" );
  364. animations[HIGHLIGHT].init( file, "Highlight" );
  365. animations[PRESSED].init( file, "Pressed" );
  366. animations[PRESSEDHIGHLIGHT].init( file, "HighlightPressed" );
  367. animations[DISABLED].init( file, "Disabled" );
  368. return 0;
  369. }
  370. void aAnimGroup::update()
  371. {
  372. animations[NORMAL].update();
  373. animations[getState()].update();
  374. }
  375. long aAnimGroup::getCurrentColor( aAnimGroup::STATE state) const
  376. {
  377. return animations[state].getColor();
  378. }
  379. void aAnimGroup::setState( aAnimGroup::STATE newState )
  380. {
  381. if ( newState != curState )
  382. {
  383. curState = newState;
  384. if ( newState != NORMAL )
  385. animations[curState].begin();
  386. }
  387. }
  388. aAnimGroup::STATE aAnimGroup::getState() const
  389. {
  390. return curState;
  391. }