ComponentListBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #define COMPONENTLISTBOX_CPP
  2. /*************************************************************************************************\
  3. ComponentListBox.cpp : Implementation of the ComponentListBox component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "ComponentListBox.h"
  9. #include "LogisticsVariant.h"
  10. #include "LogisticsComponent.h"
  11. #include "..\resource.h"
  12. #include "malloc.h"
  13. #include "mcLib.h"
  14. #include "LogisticsData.h"
  15. #include "MechLabScreen.h"
  16. #include "gamesound.h"
  17. #include "logisticserrors.h"
  18. ComponentListItem* ComponentListItem::s_templateItem = NULL;
  19. ComponentIconListBox* ComponentIconListBox::s_instance = NULL;
  20. ///////////////////////////////////////////////////////
  21. ComponentListItem::ComponentListItem( LogisticsComponent* pComp )
  22. {
  23. if ( s_templateItem )
  24. *this = *s_templateItem;
  25. pComponent = pComp;
  26. if ( pComp )
  27. {
  28. name.setText( pComponent->getName() );
  29. char numericText[32];
  30. sprintf( numericText, "%ld", pComponent->getCost() );
  31. costText.setText( numericText );
  32. sprintf( numericText, "%.0lf", pComponent->getHeat() );
  33. heatText.setText( numericText );
  34. int sizeX = pComponent->getComponentWidth();
  35. int sizeY = pComponent->getComponentHeight();
  36. const char* pFile = pComponent->getIconFileName();
  37. FullPathFileName path;
  38. path.init( artPath, pFile, "tga" );
  39. icon.setTexture( path );
  40. icon.resize( sizeX * LogisticsComponent::XICON_FACTOR, sizeY * LogisticsComponent::YICON_FACTOR);
  41. icon.setUVs( 0.f, 0.f, sizeX * 48.f, sizeY * 32.f );
  42. // figure out the difference between this thing's size and
  43. // the template objects
  44. int deltaY = icon.height() - s_templateItem->icon.height();
  45. int deltaX = icon.width() - s_templateItem->icon.width();
  46. // resize outlines and icon outlines
  47. outline.resize(outline.width(), outline.height()+deltaY);
  48. iconOutline.resize(iconOutline.width()+deltaX, iconOutline.height()+deltaY);
  49. disabledText.resize( outline.width(), outline.height() );
  50. disabledText.moveTo( 0, 0 );
  51. disabledText.alignment = 3;
  52. aObject::init( outline.left(), outline.top(), outline.width(), outline.height() );
  53. addChild( &icon );
  54. addChild( &name );
  55. addChild( &costText );
  56. addChild( &heatText );
  57. addChild( &costIcon );
  58. addChild( &heatIcon );
  59. addChild( &disabledText );
  60. if ( s_templateItem )
  61. {
  62. for ( int i = 0; i < 6; i++ )
  63. {
  64. for ( int j = 0; j < COMP_ANIMATION_COUNT; j++ )
  65. {
  66. if ( s_templateItem->pChildAnims[i] == &s_templateItem->animations[j] )
  67. {
  68. pChildAnims[i] = &animations[j];
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ComponentListItem::~ComponentListItem()
  76. {
  77. }
  78. int ComponentListItem::init( FitIniFile& file )
  79. {
  80. if ( !s_templateItem )
  81. {
  82. s_templateItem = new ComponentListItem( NULL );
  83. char animName[COMP_ANIMATION_COUNT][32];
  84. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  85. {
  86. sprintf( animName[i], "Animation%ld", i );
  87. s_templateItem->animations[i].init( &file, animName[i] );
  88. }
  89. s_templateItem->icon.init( &file, "ComponentIcon" );
  90. assignAnimation( file, 0, animName, &s_templateItem->icon );
  91. s_templateItem->iconOutline.init( &file, "ComponentIconOutlineRect" );
  92. s_templateItem->outline.init( &file, "ComponentEntryBox" );
  93. s_templateItem->name.init( &file, "ComponentNameText" );
  94. assignAnimation( file, 1, animName, &s_templateItem->name );
  95. s_templateItem->costText.init( &file, "ComponentCostText" );
  96. assignAnimation( file, 2, animName, &s_templateItem->costText );
  97. s_templateItem->heatText.init( &file, "ComponentHeatText" );
  98. assignAnimation( file, 3, animName, &s_templateItem->heatText );
  99. s_templateItem->costIcon.init( &file, "ComponentCostIcon" );
  100. assignAnimation( file, 4, animName, &s_templateItem->costIcon );
  101. s_templateItem->heatIcon.init( &file, "ComponentHeatIcon" );
  102. assignAnimation( file, 5, animName, &s_templateItem->heatIcon );
  103. s_templateItem->disabledText.init( &file, "ComponentDisabledText" );
  104. }
  105. return 0;
  106. }
  107. void ComponentListItem::assignAnimation( FitIniFile& file, int whichChild, char animNames[COMP_ANIMATION_COUNT][32],
  108. aObject* object )
  109. {
  110. s_templateItem->pChildAnims[whichChild] = 0;
  111. char tmpAnimName[32];
  112. file.readIdString("Animation", tmpAnimName, 31);
  113. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  114. {
  115. if ( stricmp( animNames[i], tmpAnimName ) == 0 )
  116. {
  117. s_templateItem->pChildAnims[whichChild] = &s_templateItem->animations[i];
  118. break;
  119. }
  120. }
  121. s_templateItem->addChild( object );
  122. }
  123. void ComponentListItem::render( )
  124. {
  125. aListItem::render();
  126. // draw rects, these aren't children
  127. outline.moveTo(globalX(), globalY());
  128. outline.setColor( animations[0].getCurrentColor( animations[0].getState() ) );
  129. outline.render();
  130. iconOutline.moveTo( globalX() + s_templateItem->iconOutline.left()
  131. , globalY() + s_templateItem->iconOutline.top());
  132. iconOutline.setColor( animations[0].getCurrentColor( animations[0].getState() ) );
  133. iconOutline.render();
  134. for ( int i = 0; i < 6; i++ )
  135. {
  136. if ( pChildAnims[i] )
  137. {
  138. long color = pChildAnims[i]->getCurrentColor( pChildAnims[i]->getState() );
  139. child(i)->setColor( color );
  140. }
  141. }
  142. if ( MechLabScreen::instance()->canAddComponent( pComponent ) )
  143. {
  144. icon.setColor( 0xff404040 );
  145. }
  146. else
  147. icon.setColor( 0xffffffff );
  148. }
  149. void ComponentListItem::update()
  150. {
  151. int bCanAdd = MechLabScreen::instance()->canAddComponent( pComponent );
  152. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  153. {
  154. animations[i].update();
  155. }
  156. bool isInside = pointInside( userInput->getMouseX(), userInput->getMouseY() );
  157. if ( state == aListItem::SELECTED )
  158. {
  159. if ( (userInput->isLeftClick() && isInside)
  160. || ( animations[0].getState() != aAnimGroup::PRESSED )
  161. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
  162. {
  163. setComponent();
  164. if ( bCanAdd )
  165. {
  166. soundSystem->playDigitalSample( LOG_WRONGBUTTON );
  167. }
  168. ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
  169. }
  170. if ( !bCanAdd )
  171. {
  172. if ( animations[0].getState() != aAnimGroup::PRESSED )
  173. {
  174. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  175. {
  176. animations[i].setState( aAnimGroup::PRESSED );
  177. }
  178. }
  179. if ( isInside && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
  180. {
  181. ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
  182. }
  183. disabledText.setText( "" );
  184. }
  185. else
  186. {
  187. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  188. animations[i].setState( aAnimGroup::DISABLED );
  189. if ( COMPONENT_TOO_HOT == bCanAdd )
  190. {
  191. disabledText.setText( IDS_MC_COMPONENT_TOO_HOT );
  192. }
  193. else if ( NO_MORE_ARMOR == bCanAdd )
  194. {
  195. disabledText.setText( IDS_MC_COMPONENT_TOO_MUCH_ARMOR );
  196. }
  197. else
  198. disabledText.setText( "" );
  199. }
  200. if ( !bCanAdd )
  201. {
  202. if ( userInput->isLeftDrag() && isInside &&
  203. pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() )
  204. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
  205. startDrag();
  206. }
  207. }
  208. else if ( isInside && !bCanAdd
  209. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
  210. {
  211. if ( animations[0].getState() != aAnimGroup::HIGHLIGHT )
  212. {
  213. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  214. {
  215. animations[i].setState( aAnimGroup::HIGHLIGHT );
  216. }
  217. soundSystem->playDigitalSample( LOG_HIGHLIGHTBUTTONS );
  218. }
  219. state = aListItem::HIGHLITE;
  220. ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
  221. }
  222. else if ( !bCanAdd )
  223. {
  224. state = aListItem::ENABLED;
  225. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  226. animations[i].setState( aAnimGroup::NORMAL );
  227. disabledText.setText( "" );
  228. }
  229. else
  230. {
  231. state = DISABLED;
  232. if ( isInside
  233. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ))
  234. ::helpTextID = IDS_HELP_COMP0 + pComponent->getID();
  235. if ( (userInput->isLeftClick() && isInside
  236. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) ) )
  237. {
  238. setComponent();
  239. soundSystem->playDigitalSample( LOG_WRONGBUTTON );
  240. }
  241. for ( int i = 0; i < COMP_ANIMATION_COUNT; i++ )
  242. animations[i].setState( aAnimGroup::DISABLED );
  243. if ( COMPONENT_TOO_HOT == bCanAdd )
  244. {
  245. disabledText.setText( IDS_MC_COMPONENT_TOO_HOT );
  246. }
  247. else if ( NO_MORE_ARMOR == bCanAdd )
  248. {
  249. disabledText.setText( IDS_MC_COMPONENT_TOO_MUCH_ARMOR );
  250. }
  251. else
  252. disabledText.setText( "" );
  253. }
  254. if ( userInput->isLeftDoubleClick() && isInside
  255. && ComponentIconListBox::s_instance->pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
  256. doAdd();
  257. aObject::update();
  258. }
  259. void ComponentListItem::doAdd()
  260. {
  261. long x = -1;
  262. long y = -1;
  263. MechLabScreen::instance()->addComponent( pComponent, x, y);
  264. }
  265. void ComponentListItem::setComponent()
  266. {
  267. MechLabScreen::instance()->setComponent( pComponent, 1 );
  268. }
  269. void ComponentListItem::startDrag()
  270. {
  271. MechLabScreen::instance()->beginDrag( pComponent );
  272. }
  273. //*************************************************************************************************
  274. void ComponentIconListBox::setType( int newType, int otherNewType, int orThis )
  275. {
  276. if ( newType == type && itemCount )
  277. return;
  278. type = newType;
  279. scrollBar->setGreen();
  280. removeAllItems( 0 );
  281. itemSelected = -1;
  282. if ( !masterComponentList.Count() )
  283. {
  284. int count = 256;
  285. LogisticsComponent* pComp[256];
  286. LogisticsData::instance->getAllComponents( pComp, count );
  287. //for ( int j = 0; j < 2048; j++ )
  288. //{
  289. for ( int i = 0; i < count; i++ )
  290. {
  291. ComponentListItem* pItem = new ComponentListItem( pComp[i] );
  292. masterComponentList.Append( pItem );
  293. }
  294. // for ( EList< ComponentListItem*, ComponentListItem* >::EIterator iter = masterComponentList.Begin();
  295. // !iter.IsDone(); iter++ )
  296. // {
  297. /// delete *iter;
  298. // turn++;
  299. // }
  300. // masterComponentList.Clear();
  301. // }
  302. }
  303. for ( EList< ComponentListItem*, ComponentListItem* >::EIterator iter = masterComponentList.Begin();
  304. !iter.IsDone(); iter++ )
  305. {
  306. if ( (*iter)->getComponent()->isAvailable() )
  307. {
  308. if ( (*iter)->getComponent()->getType() == type ||
  309. (*iter)->getComponent()->getType() == otherNewType ||
  310. (*iter)->getComponent()->getType() == orThis )
  311. {
  312. if ( (*iter)->globalX() > globalX() )
  313. {
  314. (*iter)->move( -globalX(), 0 );
  315. }
  316. addSortedItem( (*iter) );
  317. }
  318. }
  319. }
  320. selectFirstAvailableComponent();
  321. }
  322. void ComponentIconListBox::update()
  323. {
  324. aListBox::update();
  325. if ( itemSelected != -1 )
  326. {
  327. if ( items[itemSelected]->getState() == aListItem::DISABLED
  328. || MechLabScreen::instance()->canAddComponent(
  329. ((ComponentListItem*)items[itemSelected])->getComponent() ) )
  330. {
  331. selectFirstAvailableComponent();
  332. }
  333. }
  334. }
  335. int ComponentIconListBox::selectFirstAvailableComponent()
  336. {
  337. bool bFound = 0;
  338. for ( int i = 0; i < itemCount; i++ )
  339. {
  340. if ( items[i]->getState() != aListItem::DISABLED
  341. && !MechLabScreen::instance()->canAddComponent(
  342. ((ComponentListItem*)items[i])->getComponent() ))
  343. {
  344. SelectItem( i );
  345. ((ComponentListItem*)items[i])->setComponent();
  346. bFound = true;
  347. return i;
  348. }
  349. }
  350. if ( !bFound )
  351. SelectItem( -1 );
  352. return -1;
  353. }
  354. ComponentIconListBox::ComponentIconListBox()
  355. {
  356. skipAmount = 5;
  357. type = -1;
  358. s_instance = this;
  359. }
  360. ComponentIconListBox::~ComponentIconListBox()
  361. {
  362. masterComponentList.Clear();
  363. s_instance = NULL;
  364. delete ComponentListItem::s_templateItem;
  365. ComponentListItem::s_templateItem = NULL;
  366. }
  367. LogisticsComponent* ComponentIconListBox::getComponent()
  368. {
  369. if ( itemSelected != -1 )
  370. {
  371. return ((ComponentListItem*)items[itemSelected])->pComponent;
  372. }
  373. return NULL;
  374. }
  375. void ComponentIconListBox::addSortedItem( ComponentListItem* pItem )
  376. {
  377. int size = pItem->getComponent()->getComponentHeight() *
  378. pItem->getComponent()->getComponentWidth();
  379. for ( int i = 0; i < itemCount; i++ )
  380. {
  381. LogisticsComponent* pTmp = ((ComponentListItem*)items[i])->getComponent();
  382. long tmpSize = pTmp->getComponentHeight() * pTmp->getComponentWidth();
  383. if ( size > tmpSize )
  384. {
  385. InsertItem( pItem, i );
  386. return;
  387. }
  388. else if ( size == tmpSize &&
  389. stricmp( pItem->getComponent()->getName(), pTmp->getName() ) < 0 )
  390. {
  391. InsertItem( pItem, i );
  392. return;
  393. }
  394. }
  395. aListBox::AddItem( pItem );
  396. }
  397. //*************************************************************************************************
  398. // end of file ( ComponentListBox.cpp )