LogisticsPilotListBox.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #define LogisticsPilotListBox_CPP
  2. /*************************************************************************************************\
  3. LogisticsPilotListBox.cpp : Implementation of the LogisticsPilotListBox component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "LogisticsPilotListBox.h"
  9. #include "LogisticsPilot.h"
  10. #include "..\resource.h"
  11. #include "PilotReadyScreen.h"
  12. LogisticsPilotListBoxItem* LogisticsPilotListBoxItem::s_templateItem = NULL;
  13. LogisticsPilotListBox::LogisticsPilotListBox( )
  14. {
  15. skipAmount = 5;
  16. }
  17. LogisticsPilotListBox::~LogisticsPilotListBox()
  18. {
  19. delete LogisticsPilotListBoxItem::s_templateItem;
  20. LogisticsPilotListBoxItem::s_templateItem = NULL;
  21. aListBox::destroy();
  22. }
  23. void LogisticsPilotListBox::update()
  24. {
  25. aListBox::update();
  26. for ( int i = 0; i < itemCount; i++ )
  27. {
  28. if ( ((LogisticsPilotListBoxItem*)items[i])->getPilot() &&
  29. ((LogisticsPilotListBoxItem*)items[i])->getPilot()->isUsed() )
  30. {
  31. RemoveItem( items[i], true );
  32. i--;
  33. }
  34. }
  35. }
  36. //-------------------------------------------------------------------------------------------------
  37. LogisticsPilotListBoxItem::~LogisticsPilotListBoxItem()
  38. {
  39. removeAllChildren( 0 );
  40. }
  41. LogisticsPilotListBoxItem::LogisticsPilotListBoxItem( LogisticsPilot* pNewPilot )
  42. {
  43. pPilot = pNewPilot;
  44. state = ENABLED;
  45. if ( !s_templateItem )
  46. return;
  47. aObject::init( 0,0, s_templateItem->outline.width(), s_templateItem->outline.height() );
  48. outline = s_templateItem->outline;
  49. line = s_templateItem->line;
  50. nameText = s_templateItem->nameText;
  51. rankText = s_templateItem->rankText;
  52. rankIcon = s_templateItem->rankIcon;
  53. icon = s_templateItem->icon;
  54. for ( int i= 0; i < PILOT_LIST_BOX_CHILD_COUNT; i++ )
  55. {
  56. pChildAnimations[i] = s_templateItem->pChildAnimations[i];
  57. }
  58. for ( i = 0; i < 3; i++ )
  59. animations[i] = s_templateItem->animations[i];
  60. addChild( &nameText );
  61. addChild( &rankText );
  62. addChild( &icon );
  63. addChild( &rankIcon );
  64. addChild( &outline );
  65. addChild( &line );
  66. addChild( &pilotOutline );
  67. EString name = pPilot->getName();
  68. name.MakeUpper();
  69. nameText.setText(name);
  70. //ACE not continguous with other ranks. Added too late!
  71. if (pPilot->getRank() != 4)
  72. rankText.setText( pPilot->getRank() + IDS_GREEN );
  73. else
  74. rankText.setText( IDS_ACE );
  75. int rank = pPilot->getRank();
  76. rankIcon.setUVs( rank * 15, 96, rank * 15 + 15, 96 + 15 );
  77. LogisticsPilotListBox::makeUVs( pPilot, icon );
  78. }
  79. int LogisticsPilotListBoxItem::init( FitIniFile* file )
  80. {
  81. if ( !s_templateItem )
  82. s_templateItem = new LogisticsPilotListBoxItem( NULL );
  83. memset( s_templateItem->pChildAnimations, 0, sizeof( aAnimGroup* ) * PILOT_LIST_BOX_CHILD_COUNT );
  84. s_templateItem->nameText.init( file, "Text0" );
  85. setAnimation( *file, 0 );
  86. s_templateItem->rankText.init( file, "Text1" );
  87. setAnimation( *file, 1 );
  88. s_templateItem->icon.init( file, "Static0" );
  89. setAnimation( *file, 2 );
  90. s_templateItem->rankIcon.init( file, "Static1" );
  91. setAnimation( *file, 3 );
  92. s_templateItem->animations[0].init( file, "Animation0" );
  93. s_templateItem->animations[1].init( file, "Animation1" );
  94. s_templateItem->animations[2].init( file, "Animation2" );
  95. s_templateItem->outline.init( file, "Rect0" );
  96. setAnimation( *file, 4 );
  97. s_templateItem->line.init( file, "Rect2" );
  98. setAnimation( *file, 6 );
  99. s_templateItem->pilotOutline.init( file, "Rect1" );
  100. setAnimation( *file, 5 );
  101. return 0;
  102. }
  103. void LogisticsPilotListBoxItem::setAnimation( FitIniFile& file, int whichOne )
  104. {
  105. char animationText[64];
  106. if ( NO_ERR == file.readIdString( "Animation", animationText, 63 ) )
  107. {
  108. for ( int i = 0; i < strlen( animationText ); i++ )
  109. {
  110. if ( isdigit( animationText[i] ) )
  111. {
  112. animationText[i+1] = NULL;
  113. s_templateItem->pChildAnimations[whichOne] = atoi( &animationText[i] );
  114. return;
  115. }
  116. }
  117. }
  118. s_templateItem->pChildAnimations[whichOne] = -1;
  119. }
  120. void LogisticsPilotListBoxItem::render()
  121. {
  122. for ( int i = 0; i < this->pNumberOfChildren; i++ )
  123. {
  124. long newColor = 0xffffffff;
  125. if ( pChildAnimations[i] != -1 )
  126. newColor = animations[pChildAnimations[i]].getCurrentColor((aAnimGroup::STATE)state );
  127. pChildren[i]->setColor( newColor, 1 );
  128. pChildren[i]->render();
  129. }
  130. /* long newColor = animations[pChildAnimations[4]].getCurrentColor( (aAnimGroup::STATE)state );
  131. outline.setColor(newColor);
  132. outline.render( location[0].x, location[0].y );
  133. newColor = animations[pChildAnimations[5]].getCurrentColor( (aAnimGroup::STATE)state );
  134. line.setColor( newColor );
  135. line.render( location[0].x, location[0].y );*/
  136. }
  137. void LogisticsPilotListBoxItem::update()
  138. {
  139. bool isInside = pointInside( userInput->getMouseX(), userInput->getMouseY() );
  140. for ( int i = 0; i < 3; i++ )
  141. animations[i].update();
  142. if ( state == aListItem::SELECTED )
  143. {
  144. if ( animations[0].getState() != aAnimGroup::PRESSED )
  145. {
  146. for ( int i = 0; i < 3; i++ )
  147. animations[i].setState( aAnimGroup::PRESSED );
  148. }
  149. if ( userInput->isLeftDoubleClick() && isInside )
  150. {
  151. PilotReadyScreen::instance()->setPilot( pPilot );
  152. PilotReadyScreen::instance()->addSelectedPilot( );
  153. }
  154. if ( userInput->isLeftClick() && isInside )
  155. PilotReadyScreen::instance()->setPilot( pPilot );
  156. if ( userInput->isLeftDrag() && isInside &&
  157. pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() ) )
  158. {
  159. PilotReadyScreen::instance()->beginDrag( pPilot );
  160. icon.setColor( 0x7f000000 );
  161. }
  162. else if ( !userInput->isLeftDrag() )
  163. {
  164. icon.setColor( 0xffffffff );
  165. }
  166. }
  167. else if ( state == aListItem::HIGHLITE )
  168. {
  169. if ( animations[0].getState() != aAnimGroup::HIGHLIGHT )
  170. {
  171. for ( int i = 0; i < 3; i++ )
  172. animations[i].setState( aAnimGroup::HIGHLIGHT );
  173. }
  174. }
  175. else
  176. {
  177. if ( animations[0].getState() != aAnimGroup::NORMAL )
  178. {
  179. for ( int i = 0; i < 3; i++ )
  180. animations[i].setState( aAnimGroup::NORMAL );
  181. }
  182. }
  183. aObject::update();
  184. }
  185. void LogisticsPilotListBox::makeUVs( LogisticsPilot* pPilot, aObject& icon )
  186. {
  187. icon = LogisticsPilotListBoxItem::s_templateItem->icon;
  188. // need to set the UV's
  189. int index = pPilot->getPhotoIndex();
  190. long xIndex = index % 12;
  191. long yIndex = index / 12;
  192. float fX = xIndex;
  193. float fY = yIndex;
  194. float width = icon.width();
  195. float height = icon.height();
  196. float u = (fX * width);
  197. float v = (fY * height);
  198. fX += 1.f;
  199. fY += 1.f;
  200. float u2 = (fX * width);
  201. float v2 = (fY * height);
  202. icon.setFileWidth(256.f);
  203. icon.setUVs( u, v, u2, v2 );
  204. }
  205. long LogisticsPilotListBox::AddItem( aListItem* pNewItem )
  206. {
  207. scrollBar->setOrange();
  208. LogisticsPilotListBoxItem* pItem = dynamic_cast<LogisticsPilotListBoxItem*>(pNewItem);
  209. if ( pItem )
  210. {
  211. LogisticsPilot* pPilot = pItem->getPilot();
  212. for ( int i = 0; i < itemCount; i++ )
  213. {
  214. LogisticsPilotListBoxItem* pTmpItem = dynamic_cast<LogisticsPilotListBoxItem*>(items[i]);
  215. if ( pTmpItem )
  216. {
  217. // do not put in twice.
  218. if ( pPilot->getName().Compare( pTmpItem->getPilot()->getName() ) == 0 )
  219. {
  220. delete pItem;
  221. return -1;
  222. }
  223. if ( pPilot->getRank() > pTmpItem->getPilot()->getRank() )
  224. {
  225. return InsertItem( pItem, i );
  226. }
  227. else if ( pPilot->getRank() == pTmpItem->getPilot()->getRank() )
  228. {
  229. if ( pPilot->getGunnery() > pTmpItem->getPilot()->getGunnery() )
  230. {
  231. return InsertItem( pItem, i );
  232. }
  233. else if ( pPilot->getGunnery() == pTmpItem->getPilot()->getGunnery() &&
  234. pPilot->getName().Compare( pTmpItem->getPilot()->getName() ) < 0 )
  235. {
  236. return InsertItem( pItem, i );
  237. }
  238. }
  239. }
  240. }
  241. }
  242. // if we got here
  243. return aListBox::AddItem( pNewItem );
  244. }
  245. void LogisticsPilotListBox::removePilot( LogisticsPilot* pPilot )
  246. {
  247. for ( int i = 0; i < itemCount;i++ )
  248. {
  249. LogisticsPilotListBoxItem* pItem = dynamic_cast<LogisticsPilotListBoxItem*>(items[i]);
  250. if ( pItem && pItem->getPilot() == pPilot )
  251. {
  252. RemoveItem( pItem, true );
  253. return;
  254. }
  255. }
  256. }
  257. //*************************************************************************************************
  258. // end of file ( LogisticsPilotListBox.cpp )