ABUTTON.CPP 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "aButton.h"
  5. #include "aFont.h"
  6. #include "mclib.h"
  7. #include <windows.h>
  8. #include "soundSys.h"
  9. extern SoundSystem *sndSystem;
  10. aButton::aButton()
  11. {
  12. toggleButton = 0;
  13. singlePress = 0;
  14. messageOnRelease = 0;
  15. state = ENABLED;
  16. memset( &data, 0, sizeof( data ) );
  17. clickSFX = LOG_CLICKONBUTTON;
  18. highlightSFX = LOG_HIGHLIGHTBUTTONS;
  19. disabledSFX = LOG_WRONGBUTTON;
  20. data.textAlign = 2;
  21. holdTime = .5f;
  22. }
  23. long aButton::init(long xPos, long yPos, long w, long h)
  24. {
  25. long err;
  26. err = aObject::init(xPos,yPos,w,h);
  27. if (err)
  28. return err;
  29. return (NO_ERR);
  30. }
  31. void aButton::destroy()
  32. {
  33. aObject::destroy();
  34. }
  35. aButton& aButton::operator=( const aButton& src)
  36. {
  37. copyData( src );
  38. aObject::operator=( src );
  39. return *this;
  40. }
  41. aButton::aButton( const aButton& src ) : aObject( src )
  42. {
  43. copyData( src );
  44. }
  45. void aButton::copyData( const aButton& src )
  46. {
  47. if ( &src != this )
  48. {
  49. data = src.data;
  50. state = src.state;
  51. }
  52. }
  53. void aButton::update()
  54. {
  55. if ( !isShowing() )
  56. return;
  57. long mouseX = userInput->getMouseX();
  58. long mouseY = userInput->getMouseY();
  59. if ( pointInside( mouseX, mouseY ) )
  60. {
  61. long mouseDragX = userInput->getMouseDragX();
  62. long mouseDragY = userInput->getMouseDragY();
  63. if ( messageOnRelease && userInput->leftMouseReleased()
  64. && pointInside( mouseDragX, mouseDragY ) )
  65. {
  66. press( false );
  67. if ( getParent() )
  68. getParent()->handleMessage( aMSG_LEFTMOUSEDOWN, data.ID );
  69. if ( state != DISABLED && state != HIDDEN )
  70. sndSystem->playDigitalSample( clickSFX );
  71. else
  72. sndSystem->playDigitalSample( disabledSFX );
  73. }
  74. if (userInput->isLeftClick())
  75. {
  76. press( true );
  77. if ( getParent() && !messageOnRelease && pointInside(userInput->getMouseDragX(), userInput->getMouseDragY() ) )
  78. getParent()->handleMessage( aMSG_LEFTMOUSEDOWN, data.ID );
  79. if ( state != DISABLED )
  80. sndSystem->playDigitalSample( clickSFX );
  81. else
  82. sndSystem->playDigitalSample( disabledSFX );
  83. }
  84. else if ( userInput->getMouseLeftHeld() > holdTime && !messageOnRelease &&
  85. pointInside(userInput->getMouseDragX(), userInput->getMouseDragY()) )
  86. handleMessage( aMSG_LEFTMOUSEHELD, data.ID );
  87. else
  88. {
  89. if ( state != HIGHLIGHT && state != DISABLED && state != HIDDEN )
  90. sndSystem->playDigitalSample( highlightSFX );
  91. state = HIGHLIGHT;
  92. makeUVs( location, state, data );
  93. }
  94. }
  95. else if ( state == PRESSED && messageOnRelease )
  96. state = ENABLED;
  97. aObject::update();
  98. }
  99. bool aButton::pointInside(long xPos, long yPos) const
  100. {
  101. if ( aObject::pointInside( xPos, yPos ) )
  102. return true;
  103. if ( data.textRect.left <= xPos &&
  104. data.textRect.right >= xPos &&
  105. data.textRect.top <= yPos &&
  106. data.textRect.bottom >= yPos )
  107. {
  108. return true;
  109. }
  110. return 0;
  111. }
  112. /////////////////////////////////////////////////
  113. void aButton::render()
  114. {
  115. if ( state != HIDDEN )
  116. {
  117. if ( textureHandle )
  118. {
  119. unsigned long gosID = mcTextureManager->get_gosTextureHandle( textureHandle );
  120. gos_SetRenderState( gos_State_Texture, gosID );
  121. }
  122. else
  123. gos_SetRenderState( gos_State_Texture, 0 );
  124. gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha);
  125. gos_SetRenderState( gos_State_Filter, gos_FilterNone);
  126. gos_SetRenderState( gos_State_AlphaTest, true);
  127. gos_SetRenderState( gos_State_TextureAddress, gos_TextureClamp );
  128. gos_SetRenderState( gos_State_TextureMapBlend,gos_BlendModulateAlpha );
  129. gos_DrawQuads( location, 4 );
  130. if ( data.textID && data.textFont )
  131. {
  132. char buffer[256];
  133. cLoadString( data.textID, buffer, 256 );
  134. DWORD width, height;
  135. gos_TextSetAttributes(data.textFont, data.textColors[state], data.textSize, true, true, false, false, data.textAlign);
  136. gos_TextSetRegion( data.textRect.left, data.textRect.top, data.textRect.right, data.textRect.bottom );
  137. gos_TextStringLength( &width, &height, buffer );
  138. gos_TextSetPosition( data.textRect.left, (data.textRect.top + data.textRect.bottom)/2 - height/2 + 1 );
  139. gos_TextDraw( buffer );
  140. if ( data.outlineText )
  141. {
  142. drawEmptyRect( data.textRect, data.textColors[state], data.textColors[state] );
  143. }
  144. }
  145. if ( data.outline )
  146. {
  147. GUI_RECT tmp;
  148. tmp.left = location[0].x;
  149. tmp.top = location[0].y;
  150. tmp.right = location[2].x;
  151. tmp.bottom = location[2].y;
  152. drawEmptyRect( tmp, location[0].argb, location[0].argb );
  153. }
  154. for ( int i = 0; i < numberOfChildren(); i++ )
  155. {
  156. pChildren[i]->render();
  157. }
  158. }
  159. }
  160. void aButton::press(bool bPress)
  161. {
  162. if ( !isEnabled() )
  163. return;
  164. if ( !bPress && state == HIGHLIGHT )
  165. return;
  166. state = bPress ? PRESSED : ENABLED;
  167. makeUVs( location, state, data );
  168. }
  169. void aButton::makeAmbiguous( bool bAmbiguous )
  170. {
  171. state = bAmbiguous ? AMBIGUOUS : ENABLED;
  172. makeUVs( location, state, data );
  173. }
  174. void aButton::disable( bool bDisable )
  175. {
  176. if ( !bDisable )
  177. {
  178. if ( state == DISABLED )
  179. state = ENABLED;
  180. }
  181. else
  182. state = DISABLED;
  183. makeUVs( location, state, data );
  184. }
  185. void aButton::hide( bool bHide )
  186. {
  187. state = bHide ? HIDDEN : ENABLED;
  188. if ( state != HIDDEN )
  189. aButton::makeUVs( location, state, data );
  190. }
  191. bool aButton::isEnabled()
  192. {
  193. return state == ENABLED || state == PRESSED || state == AMBIGUOUS || state == HIGHLIGHT;
  194. }
  195. int aButton::getID()
  196. {
  197. return data.ID;
  198. }
  199. void aButton::setID( int newID )
  200. {
  201. data.ID = newID;
  202. }
  203. void aButton::makeUVs( gos_VERTEX* vertices, int State, aButton::aButtonData& data )
  204. {
  205. float left = data.stateCoords[State][0];
  206. float top = data.stateCoords[State][1];
  207. if ( left == -1 || top == -1 )
  208. {
  209. SPEW(( 0, "makeUVs given an Invalid state\n" ));
  210. }
  211. float width = data.textureWidth;
  212. float height = data.textureHeight;
  213. float right = left + width;
  214. float bottom = top + height;
  215. if ( data.fileWidth && data.fileHeight ) // will crash if 0
  216. {
  217. if ( data.textureRotated )
  218. {
  219. vertices[0].u = right/(float)data.fileWidth + (.1f / (float)data.fileWidth);;
  220. vertices[1].u = left/(float)data.fileWidth + (.1f / (float)data.fileWidth);;
  221. vertices[2].u = left/(float)data.fileWidth + (.1f / (float)data.fileWidth);
  222. vertices[3].u = right/(float)data.fileWidth + (.1f / (float)data.fileWidth);
  223. vertices[0].v = top/(float)data.fileHeight + (.1f / (float)data.fileWidth);;
  224. vertices[1].v = top/(float)data.fileHeight + (.1f / (float)data.fileWidth);;
  225. vertices[2].v = bottom/(float)data.fileHeight + (.1f / (float)data.fileHeight);;
  226. vertices[3].v = bottom/(float)data.fileHeight + (.1f / (float)data.fileHeight);;
  227. }
  228. else
  229. {
  230. {
  231. vertices[0].u = vertices[1].u = left/(float)data.fileWidth + (.1f / (float)data.fileWidth);;
  232. vertices[2].u = vertices[3].u = right/(float)data.fileWidth + (.1f / (float)data.fileWidth);
  233. vertices[0].v = vertices[3].v = top/(float)data.fileHeight + (.1f / (float)data.fileWidth);;
  234. vertices[1].v = vertices[2].v = bottom/(float)data.fileHeight + (.1f / (float)data.fileHeight);
  235. }
  236. }
  237. }
  238. }
  239. void aButton::init( FitIniFile& buttonFile, const char* str, HGOSFONT3D font )
  240. {
  241. textureHandle = 0;
  242. long result = buttonFile.seekBlock( str );
  243. if ( result != NO_ERR )
  244. {
  245. char errorStr[256];
  246. sprintf( errorStr, "couldn't find button %s", str );
  247. Assert( 0, 0, errorStr );
  248. return;
  249. }
  250. buttonFile.readIdLong( "ID", data.ID );
  251. buttonFile.readIdString("FileName", data.fileName, 32 );
  252. buttonFile.readIdLong( "HelpCaption", helpHeader );
  253. buttonFile.readIdLong( "HelpDesc", helpID );
  254. buttonFile.readIdLong( "TextID", data.textID );
  255. buttonFile.readIdLong( "TextNormal", data.textColors[0] );
  256. buttonFile.readIdLong( "TextPressed", data.textColors[1] );
  257. buttonFile.readIdLong( "TextDisabled", data.textColors[2] );
  258. buttonFile.readIdBoolean( "Toggle", toggleButton );
  259. buttonFile.readIdBoolean( "outline", data.outline );
  260. long fontID;
  261. buttonFile.readIdLong( "Font", fontID );
  262. if ( fontID )
  263. data.textFont = aFont::loadFont( fontID, data.textSize );
  264. else
  265. data.textFont = 0;
  266. long x, y, width, height;
  267. buttonFile.readIdLong( "XLocation", x );
  268. buttonFile.readIdLong( "YLocation", y );
  269. buttonFile.readIdLong( "Width", width );
  270. buttonFile.readIdLong( "Height", height );
  271. buttonFile.readIdLong( "HelpCaption", helpHeader );
  272. buttonFile.readIdLong( "HelpDesc", helpID );
  273. buttonFile.readIdBoolean( "texturesRotated", data.textureRotated );
  274. if ( NO_ERR != buttonFile.readIdLong( "Alignment", data.textAlign ) )
  275. data.textAlign = 2;
  276. location[0].x = location[1].x = x;
  277. location[0].y = location[3].y = y;
  278. location[2].x = location[3].x = x + width;
  279. location[1].y = location[2].y = y + height;
  280. for ( int j = 0; j < 4; j++ )
  281. {
  282. location[j].argb = 0xffffffff;
  283. location[j].frgb = 0;
  284. location[j].rhw = .5;
  285. location[j].u = 0.f;
  286. location[j].v = 0.f;
  287. location[j].z = 0.f;
  288. }
  289. if ( 0 == textureHandle && data.fileName && strlen( data.fileName ) )
  290. {
  291. char file[256];
  292. strcpy( file, artPath );
  293. strcat( file, data.fileName );
  294. _strlwr( file );
  295. if ( !strstr( data.fileName, ".tga" ) )
  296. strcat( file, ".tga" );
  297. int ID = mcTextureManager->loadTexture( file, gos_Texture_Alpha, 0, 0, 0x2 );
  298. int gosID = mcTextureManager->get_gosTextureHandle( ID );
  299. TEXTUREPTR textureData;
  300. gos_LockTexture( gosID, 0, 0, &textureData );
  301. gos_UnLockTexture( gosID );
  302. textureHandle = ID;
  303. data.fileWidth = textureData.Width;
  304. data.fileHeight = data.fileWidth;
  305. }
  306. if ( NO_ERR != buttonFile.readIdLong( "UNormal", data.stateCoords[0][0] ) )
  307. data.stateCoords[0][0] = -1.f;
  308. if ( NO_ERR != buttonFile.readIdLong( "VNormal", data.stateCoords[0][1] ) )
  309. data.stateCoords[0][1] = -1.f;
  310. if ( NO_ERR != buttonFile.readIdLong( "UPressed", data.stateCoords[1][0] ) )
  311. data.stateCoords[1][0] = -1.f;
  312. if ( NO_ERR != buttonFile.readIdLong( "VPressed", data.stateCoords[1][1] ) )
  313. data.stateCoords[1][1] = -1.f;
  314. if ( NO_ERR != buttonFile.readIdLong( "UDisabled", data.stateCoords[2][0] ) )
  315. data.stateCoords[2][0] = -1.f;
  316. if ( NO_ERR != buttonFile.readIdLong( "VDisabled", data.stateCoords[2][1] ) )
  317. data.stateCoords[2][1] = -1.f;
  318. if ( NO_ERR != buttonFile.readIdLong( "UAmbiguous", data.stateCoords[3][0] ) )
  319. data.stateCoords[3][0] = -1.f;
  320. if ( NO_ERR != buttonFile.readIdLong( "VAmbiguous", data.stateCoords[3][1] ) )
  321. data.stateCoords[3][1] = -1.f;
  322. if ( NO_ERR != buttonFile.readIdLong( "UHighlight", data.stateCoords[4][0] ) )
  323. {
  324. data.stateCoords[4][0] = data.stateCoords[0][0];
  325. }
  326. if ( NO_ERR != buttonFile.readIdLong( "VHighlight", data.stateCoords[4][1] ) )
  327. {
  328. data.stateCoords[4][1] = data.stateCoords[0][1];
  329. }
  330. buttonFile.readIdLong( "UWidth", data.textureWidth );
  331. buttonFile.readIdLong( "VHeight", data.textureHeight );
  332. if ( data.textID )
  333. buttonFile.readIdBoolean( "TextOutline", data.outlineText );
  334. if ( NO_ERR == buttonFile.readIdLong( "XTextLocation", data.textRect.left ) )
  335. {
  336. buttonFile.readIdLong( "YTextLocation", data.textRect.top );
  337. buttonFile.readIdLong( "TextWidth", width );
  338. buttonFile.readIdLong( "TextHeight", height );
  339. data.textRect.right = data.textRect.left + width;
  340. data.textRect.bottom = data.textRect.top + height;
  341. buttonFile.readIdBoolean( "TextOutline", data.outlineText );
  342. }
  343. else
  344. {
  345. data.textRect.left = x;
  346. data.textRect.right = x + width;
  347. data.textRect.top = y;
  348. data.textRect.bottom = y + height;
  349. }
  350. char bmpName[256];
  351. strcpy( bmpName, str );
  352. strcat( bmpName, "Bmp" );
  353. char finalName[256];
  354. int counter = 0;
  355. while(true)
  356. {
  357. sprintf( finalName, "%s%ld", bmpName, counter );
  358. if ( NO_ERR != buttonFile.seekBlock( finalName) )
  359. break;
  360. aObject* pObject = new aObject;
  361. pObject->init( &buttonFile, finalName );
  362. // Dorje is doing this in global coords
  363. pObject->move( -globalX(), -globalY() );
  364. addChild( pObject );
  365. counter++;
  366. }
  367. buttonFile.seekBlock( str );
  368. disable( 0 );
  369. press( 0 );
  370. }
  371. aAnimButton::aAnimButton()
  372. {
  373. animateText = 1;
  374. animateBmp = 1;
  375. bAnimateChildren = 1;
  376. }
  377. aAnimButton& aAnimButton::operator=( const aAnimButton& src)
  378. {
  379. copyData( src );
  380. aButton::operator=( src );
  381. return *this;
  382. }
  383. aAnimButton::aAnimButton( const aAnimButton& src ) : aButton( src )
  384. {
  385. copyData( src );
  386. }
  387. void aAnimButton::copyData( const aAnimButton& src )
  388. {
  389. if ( &src != this )
  390. {
  391. animateBmp = src.animateBmp;
  392. animateText = src.animateText;
  393. highlightData = src.highlightData;
  394. disabledData = src.disabledData;
  395. pressedData = src.pressedData;
  396. normalData = src.normalData;
  397. toggleButton = src.toggleButton;
  398. bAnimateChildren = src.bAnimateChildren;
  399. }
  400. }
  401. void aAnimButton::destroy()
  402. {
  403. normalData.destroy();
  404. highlightData.destroy();
  405. pressedData.destroy();
  406. disabledData.destroy();
  407. aButton::destroy();
  408. }
  409. void aAnimButton::init( FitIniFile& file, const char* headerName, HGOSFONT3D font )
  410. {
  411. if ( NO_ERR != file.seekBlock( headerName ) )
  412. {
  413. char errorStr[256];
  414. sprintf( errorStr, "couldn't find block %s in file %s", headerName, file.getFilename() );
  415. Assert( 0, 0, errorStr );
  416. animateBmp = 0;
  417. animateText = 0;
  418. return;
  419. }
  420. aButton::init( file, headerName, font );
  421. normalData.init( &file, "Normal" );
  422. pressedData.init( &file, "Pressed" );
  423. highlightData.init( &file, "Highlight" );
  424. disabledData.init( &file, "Disabled" );
  425. normalData.begin();
  426. if ( NO_ERR != file.readIdBoolean( "AnimateBmp", animateBmp ) )
  427. animateBmp = 1;
  428. if ( NO_ERR != file.readIdBoolean( "AnimateText", animateText ) )
  429. animateText = 1;
  430. bool bTmp = 0;
  431. if ( NO_ERR == file.readIdBoolean( "AnimateChildren", bTmp ) )
  432. {
  433. bAnimateChildren = bTmp;
  434. }
  435. }
  436. void aAnimButton::update()
  437. {
  438. if ( !isShowing() )
  439. return;
  440. long mouseX = userInput->getMouseX();
  441. long mouseY = userInput->getMouseY();
  442. bool bInside = pointInside( mouseX, mouseY );
  443. if ( bInside && state == DISABLED )
  444. {
  445. ::helpTextID = this->helpID;
  446. ::helpTextHeaderID = this->helpHeader;
  447. }
  448. if ( bInside && state != DISABLED && state != HIDDEN )
  449. {
  450. ::helpTextID = this->helpID;
  451. ::helpTextHeaderID = this->helpHeader;
  452. if (userInput->isLeftClick())
  453. {
  454. if ( toggleButton )
  455. {
  456. if ( state == PRESSED )
  457. {
  458. press( 0 );
  459. pressedData.end();
  460. }
  461. else
  462. {
  463. pressedData.begin();
  464. highlightData.end();
  465. press( 1 );
  466. }
  467. sndSystem->playDigitalSample( clickSFX );
  468. }
  469. else
  470. {
  471. press( true );
  472. pressedData.begin();
  473. highlightData.end();
  474. sndSystem->playDigitalSample( clickSFX );
  475. }
  476. if ( getParent() && !messageOnRelease && pointInside(userInput->getMouseDragX(), userInput->getMouseDragY() ) )
  477. getParent()->handleMessage( aMSG_LEFTMOUSEDOWN, data.ID );
  478. }
  479. else if (userInput->getMouseLeftHeld() > holdTime && getParent() && !messageOnRelease
  480. && pointInside(userInput->getMouseDragX(), userInput->getMouseDragY()) )
  481. getParent()->handleMessage( aMSG_LEFTMOUSEHELD, data.ID );
  482. else if ( userInput->leftMouseReleased() && getParent() && messageOnRelease )
  483. {
  484. long mouseDragX = userInput->getMouseDragX();
  485. long mouseDragY = userInput->getMouseDragY();
  486. if ( pointInside( mouseDragX, mouseDragY ) )
  487. {
  488. getParent()->handleMessage( aMSG_LEFTMOUSEDOWN, data.ID );
  489. if ( !toggleButton )
  490. state = ENABLED;
  491. }
  492. // sndSystem->playDigitalSample( clickSFX );
  493. }
  494. else if ( state != PRESSED )
  495. {
  496. if ( state != HIGHLIGHT && state != DISABLED && state != HIDDEN && isShowing() )
  497. sndSystem->playDigitalSample( highlightSFX );
  498. if ( !highlightData.isAnimating() )
  499. highlightData.begin();
  500. state = HIGHLIGHT;
  501. makeUVs( location, state, data );
  502. }
  503. }
  504. else if ( state == PRESSED )
  505. {
  506. // if clicked inside and release outside
  507. if ( userInput->leftMouseReleased() && ( messageOnRelease || singlePress )
  508. && pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() ) )
  509. {
  510. state = ENABLED;
  511. }
  512. else
  513. {
  514. if ( !pressedData.isAnimating() )
  515. pressedData.begin();
  516. if ( singlePress && pressedData.isDone() )
  517. press( 0 );
  518. }
  519. }
  520. else if ( state == DISABLED )
  521. {
  522. if ( pointInside( mouseX, mouseY ) && userInput->isLeftClick() )
  523. {
  524. sndSystem->playDigitalSample( disabledSFX );
  525. }
  526. if ( !disabledData.isAnimating() )
  527. disabledData.begin();
  528. disabledData.update();
  529. }
  530. else
  531. {
  532. highlightData.end();
  533. if ( state == HIGHLIGHT )
  534. state = ENABLED;
  535. press( 0 );
  536. }
  537. if ( pressedData.isAnimating() && state != PRESSED )
  538. {
  539. pressedData.end();
  540. press( 0 );
  541. }
  542. if ( state != DISABLED )
  543. disabledData.end();
  544. highlightData.update();
  545. pressedData.update();
  546. normalData.update();
  547. }
  548. void aAnimButton::render()
  549. {
  550. if ( !isShowing() )
  551. return;
  552. if ( disabledData.isAnimating() )
  553. {
  554. update( disabledData );
  555. }
  556. else if ( pressedData.isAnimating() )
  557. {
  558. update( pressedData );
  559. }
  560. else if ( highlightData.isAnimating() )
  561. {
  562. update( highlightData );
  563. }
  564. else
  565. {
  566. update( normalData );
  567. }
  568. }
  569. void aAnimButton::update( const aAnimation& animData )
  570. {
  571. if ( !isShowing() )
  572. return;
  573. long color = animData.getColor();
  574. if ( animateBmp )
  575. setColor( color, 0 );
  576. if ( animateText )
  577. data.textColors[state] = color;
  578. long xPos = animData.getXDelta();
  579. long yPos = animData.getYDelta();
  580. move( xPos, yPos );
  581. if ( bAnimateChildren )
  582. {
  583. for ( int i = 0; i < numberOfChildren(); i++ )
  584. pChildren[i]->setColor( color );
  585. }
  586. float fXcaleX = animData.getScaleX();
  587. float fScaleY = animData.getScaleX();
  588. if ( fXcaleX != 1.0 && fScaleY != 1.0 )
  589. {
  590. float oldWidth = width();
  591. float oldHeight = height();
  592. float oldLeft = globalX();
  593. float oldTop = globalY();
  594. float scaleX = .5 * fXcaleX * width();
  595. float scaleY = .5 * fScaleY * height();
  596. float midX = globalX() + .5 * width();
  597. float midY = globalY() + .5 * height();
  598. float newLeft = midX - scaleX;
  599. float newTop = midY - scaleY;
  600. moveToNoRecurse( newLeft, newTop );
  601. resize( width() * scaleX, height() * scaleY );
  602. aButton::render();
  603. resize( oldWidth, oldHeight );
  604. moveToNoRecurse( oldLeft, oldTop );
  605. }
  606. else
  607. aButton::render();
  608. }
  609. void aButton::move( float offsetX, float offsetY )
  610. {
  611. aObject::move( offsetX, offsetY );
  612. data.textRect.left += offsetX;
  613. data.textRect.right += offsetX;
  614. data.textRect.top += offsetY;
  615. data.textRect.bottom += offsetY;
  616. }
  617. void aAnimButton::setAnimationInfo( aAnimation* normal, aAnimation* highlight,
  618. aAnimation* pressed, aAnimation* disabled )
  619. {
  620. if ( normal )
  621. {
  622. normalData.destroy();
  623. normalData = *normal;
  624. }
  625. else
  626. normalData.destroy();
  627. if ( highlight )
  628. {
  629. highlightData.destroy();
  630. highlightData = *highlight;
  631. }
  632. else
  633. highlightData.destroy();
  634. if ( pressed )
  635. {
  636. pressedData.destroy();
  637. pressedData = *pressed;
  638. }
  639. else
  640. pressedData.destroy();
  641. if ( disabled )
  642. {
  643. disabledData.destroy();
  644. disabledData = *disabled;
  645. }
  646. else
  647. disabledData.destroy();
  648. }