MechPurchaseScreen.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #define MECHPURCHASESCREEN_CPP
  2. /*************************************************************************************************\
  3. MechPurchaseScreen.cpp : Implementation of the MechPurchaseScreen component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "MechPurchaseScreen.h"
  9. #include "mcLib.h"
  10. #include "LogisticsData.h"
  11. #include "MechBayScreen.h"
  12. #include "gamesound.h"
  13. #include "..\resource.h"
  14. #include "SimpleCamera.h"
  15. #include "AttributeMeter.h"
  16. #include "Chatwindow.h"
  17. #include "Multplyr.h"
  18. MechPurchaseScreen* MechPurchaseScreen::s_instance = NULL;
  19. #define INVENTORY_ID 88
  20. #define VARIANT_ID 89
  21. //-------------------------------------------------------------------------------------------------
  22. MechPurchaseScreen::MechPurchaseScreen( )
  23. : inventoryListBox( 1, 0 ), variantListBox( 0, 0 )
  24. {
  25. status = RUNNING;
  26. gosASSERT( !s_instance );
  27. s_instance = this;
  28. acceptPressed = 0;
  29. pDragMech = NULL;
  30. dragStartLeft = 0;
  31. helpTextArrayID = 4;
  32. inventoryListBox.setID( INVENTORY_ID );
  33. variantListBox.setID( VARIANT_ID );
  34. countDownTime = .5;
  35. curCount = 0.0;
  36. previousAmount = 0;
  37. oldCBillsAmount =0;
  38. flashTime = 0.f;
  39. }
  40. //-------------------------------------------------------------------------------------------------
  41. MechPurchaseScreen::~MechPurchaseScreen()
  42. {
  43. s_instance = NULL;
  44. variantListBox.destroy();
  45. inventoryListBox.destroy();
  46. }
  47. //-------------------------------------------------------------------------------------------------
  48. int MechPurchaseScreen::init( FitIniFile& file )
  49. {
  50. MechListBox::init();
  51. mechDisplay.init();
  52. LogisticsScreen::init( file, "Static", "Text", "Rect", "Button" );
  53. ((aObject*)&inventoryListBox)->init( rects[0].left(), rects[0].top(),
  54. rects[0].width(), rects[0].height() );
  55. inventoryListBox.setScrollBarOrange();
  56. ((aObject*)&variantListBox)->init( rects[1].left(), rects[1].top(),
  57. rects[1].width(), rects[1].height() );
  58. variantListBox.setScrollBarOrange();
  59. for ( int i= 0; i < buttonCount; i++ )
  60. buttons[i].setMessageOnRelease();
  61. return NO_ERR;
  62. }
  63. //-------------------------------------------------------------------------------------------------
  64. void MechPurchaseScreen::update()
  65. {
  66. if ( !MPlayer || !ChatWindow::instance()->pointInside(userInput->getMouseX(), userInput->getMouseY()) )
  67. LogisticsScreen::update();
  68. // update CBills
  69. int amount = LogisticsData::instance->getCBills();
  70. long color = 0xff005392;
  71. if ( amount != oldCBillsAmount )
  72. {
  73. previousAmount = oldCBillsAmount - amount;
  74. curCount = .00001f;
  75. oldCBillsAmount = amount;
  76. if ( previousAmount < 0 )
  77. soundSystem->playDigitalSample( WINDOW_OPEN );
  78. else
  79. soundSystem->playDigitalSample( WINDOW_CLOSE );
  80. }
  81. if ( curCount && curCount + frameLength < countDownTime )
  82. {
  83. curCount += frameLength;
  84. float curAmount = previousAmount - (curCount/countDownTime * previousAmount);
  85. amount += curAmount;
  86. color = 0xffc8e100;
  87. if ( curAmount > 0 )
  88. color = 0xffa21600;
  89. }
  90. else if ( flashTime )
  91. {
  92. flashTime += frameLength;
  93. if ( flashTime < .125
  94. || ( flashTime > .25 && flashTime < .3875 )
  95. || ( flashTime > .5 && flashTime < .6625 ) )
  96. {
  97. color = 0xff000000;
  98. }
  99. else if ( flashTime > .75 )
  100. {
  101. flashTime = 0;
  102. }
  103. else
  104. {
  105. color = 0xffff0000;
  106. }
  107. }
  108. char tmp[64];
  109. sprintf( tmp, "%ld ", amount);
  110. textObjects[1].setText( tmp );
  111. textObjects[1].setColor( color );
  112. int oldSell = inventoryListBox.GetSelectedItem();
  113. inventoryListBox.update();
  114. int newSell = inventoryListBox.GetSelectedItem();
  115. if ( oldSell != newSell && newSell != -1 )
  116. {
  117. variantListBox.SelectItem( -1 );
  118. }
  119. oldSell = variantListBox.GetSelectedItem();
  120. variantListBox.update();
  121. newSell = variantListBox.GetSelectedItem();
  122. if ( oldSell != newSell && newSell != -1 )
  123. {
  124. inventoryListBox.SelectItem( -1 );
  125. }
  126. variantListBox.disableItemsThatCostMoreThanRP();
  127. LogisticsMech* pCurSell = inventoryListBox.getCurrentMech();
  128. LogisticsMech* pCurBuy = variantListBox.getCurrentMech();
  129. // disable the sell button
  130. buttons[2].disable( pCurSell ? 0 : 1);
  131. if ( pCurBuy )
  132. {
  133. if ( pCurBuy->getCost() > LogisticsData::instance->getCBills() )
  134. {
  135. pCurBuy = NULL;
  136. }
  137. setMech( pCurBuy );
  138. }
  139. else if ( pCurSell )
  140. setMech( pCurSell );
  141. buttons[1].disable( pCurBuy ? 0 : 1 );
  142. if ( !pCurBuy &&
  143. userInput->isLeftClick() && buttons[1].pointInside( userInput->getMouseX(), userInput->getMouseY() ) )
  144. {
  145. if ( !flashTime )
  146. flashTime = .0001f;
  147. }
  148. // drag and drop
  149. // update drag and drop
  150. if ( pDragMech )
  151. {
  152. dragIcon.moveTo( userInput->getMouseX() - dragIcon.width() / 2,
  153. userInput->getMouseY() - dragIcon.height() / 2 );
  154. if ( userInput->leftMouseReleased( ) )
  155. {
  156. endDrag();
  157. }
  158. }
  159. mechDisplay.update();
  160. inventoryListBox.enableAllItems();
  161. if ( MPlayer && ChatWindow::instance() )
  162. ChatWindow::instance()->update();
  163. }
  164. //-------------------------------------------------------------------------------------------------
  165. void MechPurchaseScreen::render( int xOffset, int yOffset )
  166. {
  167. if ( !xOffset && !yOffset )
  168. {
  169. if ( !MPlayer && !LogisticsData::instance->isSingleMission() && LogisticsData::instance->newMechsAvailable() )
  170. {
  171. soundSystem->playBettySample( BETTY_NEW_MECHS );
  172. LogisticsData::instance->setNewMechsAcknowledged();
  173. }
  174. }
  175. inventoryListBox.move( xOffset, yOffset);
  176. inventoryListBox.render();
  177. inventoryListBox.move( -xOffset, -yOffset );
  178. variantListBox.move(xOffset, yOffset);
  179. variantListBox.render();
  180. variantListBox.move(-xOffset, -yOffset);
  181. mechDisplay.render( xOffset, yOffset );
  182. LogisticsScreen::render( xOffset, yOffset );
  183. if ( pDragMech )
  184. dragIcon.render();
  185. if ( MPlayer && ChatWindow::instance() )
  186. ChatWindow::instance()->render(xOffset, yOffset);
  187. }
  188. //-------------------------------------------------------------------------------------------------
  189. void MechPurchaseScreen::begin()
  190. {
  191. variantListBox.removeAllItems(true);
  192. inventoryListBox.removeAllItems(true);
  193. // initialize both the inventory and icon lists
  194. EList< LogisticsMech*, LogisticsMech* > mechList;
  195. LogisticsData::instance->getInventory( mechList );
  196. prevInventory.Clear();
  197. LogisticsMech* pSelMech = 0;
  198. oldCBillsAmount = LogisticsData::instance->getCBills();
  199. for ( EList< LogisticsMech*, LogisticsMech* >::EIterator iter = mechList.Begin();
  200. !iter.IsDone(); iter++ )
  201. {
  202. if ( (*iter)->getForceGroup() )
  203. continue;
  204. prevInventory.Append( *(*iter) );
  205. bool bFound = 0;
  206. for ( int i = 0; i < inventoryListBox.GetItemCount(); i++ )
  207. {
  208. if ( ((MechListBoxItem*)inventoryListBox.GetItem(i))->getMech()->getVariant()
  209. == (*iter)->getVariant() )
  210. bFound = true;
  211. }
  212. if ( !bFound )
  213. {
  214. MechListBoxItem* item = new MechListBoxItem( (*iter), 1 );
  215. inventoryListBox.AddItem( item );
  216. }
  217. }
  218. MechListBoxItem* item = (MechListBoxItem*)inventoryListBox.GetItem( 0 );
  219. if ( item )
  220. {
  221. inventoryListBox.SelectItem( 0 );
  222. pSelMech = item->getMech();
  223. }
  224. LogisticsVariant* pVariants[256];
  225. int count = 256;
  226. LogisticsData::instance->getPurchasableMechs( pVariants, count );
  227. for ( int i =0; i < count; i++ )
  228. {
  229. if ( !MPlayer || MPlayer->missionSettings.variants || pVariants[i]->isDesignerMech() )
  230. {
  231. LogisticsMech* pMech = new LogisticsMech( pVariants[i], -1 );
  232. MechListBoxItem* item = new MechListBoxItem( pMech, 1 );
  233. variantListBox.AddItem( item );
  234. }
  235. }
  236. if ( !pSelMech )
  237. {
  238. MechListBoxItem* item = (MechListBoxItem*)variantListBox.GetItem( 0 );
  239. if ( item )
  240. {
  241. variantListBox.SelectItem( 0 );
  242. pSelMech = item->getMech();
  243. }
  244. }
  245. status = RUNNING;
  246. acceptPressed = 0;
  247. inventoryListBox.drawCBills(1);
  248. variantListBox.drawCBills(1);
  249. variantListBox.setOrange( 0 );
  250. inventoryListBox.setOrange( 1 );
  251. setMech( pSelMech );
  252. pDragMech = NULL;
  253. }
  254. //-------------------------------------------------------------------------------------------------
  255. void MechPurchaseScreen::end()
  256. {
  257. if ( !acceptPressed && status != MAINMENU )
  258. {
  259. // sell all current stuff
  260. EList< LogisticsMech*, LogisticsMech* > list;
  261. LogisticsData::instance->getInventory( list );
  262. for ( EList< LogisticsMech*, LogisticsMech* >::EIterator iter = list.End(); !iter.IsDone(); iter-- )
  263. {
  264. LogisticsData::instance->sellMech( (*iter) );
  265. }
  266. unsigned long base, color1, color2;
  267. for ( MECH_LIST::EIterator pIter = prevInventory.Begin(); !pIter.IsDone(); pIter++ )
  268. {
  269. (*pIter).getColors(base, color1, color2);
  270. LogisticsData::instance->addMechToInventory( (*pIter).getVariant(), (*pIter).getPilot(),
  271. (*pIter).getForceGroup(), true );
  272. }
  273. }
  274. variantListBox.removeAllItems(true);
  275. inventoryListBox.removeAllItems(true);
  276. mechDisplay.setMech( NULL );
  277. }
  278. //-------------------------------------------------------------------------------------------------
  279. void MechPurchaseScreen::setMech( LogisticsMech* pMech, bool FromLB )
  280. {
  281. if ( status == RUNNING )
  282. {
  283. if ( FromLB ) // clicked on a disabled item
  284. {
  285. if ( !flashTime )
  286. flashTime = .000001f;
  287. }
  288. if ( FromLB ) // clicked on a disabled item
  289. {
  290. inventoryListBox.SelectItem( -1 );
  291. variantListBox.SelectItem( -1 );
  292. }
  293. mechDisplay.setMech( pMech );
  294. }
  295. }
  296. //-------------------------------------------------------------------------------------------------
  297. void MechPurchaseScreen::beginDrag( LogisticsMech* pMech )
  298. {
  299. if ( status != RUNNING )
  300. return;
  301. if ( !pDragMech )
  302. {
  303. pDragMech = pMech;
  304. }
  305. MechListBox::initIcon( pMech, dragIcon );
  306. if ( inventoryListBox.pointInside( userInput->getMouseDragX(),
  307. userInput->getMouseDragY() ) )
  308. {
  309. dragStartLeft = true;
  310. inventoryListBox.dimItem( pMech, true );
  311. }
  312. else
  313. {
  314. dragStartLeft = false;
  315. variantListBox.dimItem( pMech, true );
  316. }
  317. }
  318. void MechPurchaseScreen::endDrag( )
  319. {
  320. if ( pDragMech )
  321. {
  322. if ( inventoryListBox.pointInside( userInput->getMouseX(),
  323. userInput->getMouseY() ) )
  324. {
  325. if ( !dragStartLeft )
  326. {
  327. addMech( pDragMech );
  328. }
  329. }
  330. else
  331. {
  332. if ( dragStartLeft )
  333. removeMech( pDragMech );
  334. }
  335. }
  336. inventoryListBox.undimAll();
  337. variantListBox.undimAll();
  338. pDragMech = NULL;
  339. }
  340. //-------------------------------------------------------------------------------------------------
  341. int MechPurchaseScreen::handleMessage( unsigned long what, unsigned long who )
  342. {
  343. if ( status == RUNNING )
  344. {
  345. switch (who)
  346. {
  347. case MB_MSG_ADD:
  348. if ( INVENTORY_ID == what )
  349. {
  350. int index = inventoryListBox.GetSelectedItem();
  351. if ( index != -1 )
  352. {
  353. aListItem* pItem = inventoryListBox.GetItem( index );
  354. if ( pItem )
  355. {
  356. LogisticsMech* pMech = ((MechListBoxItem*)pItem)->getMech();
  357. int oldCount = LogisticsData::instance->getVariantsInInventory( pMech->getVariant(), true );
  358. if ( NO_ERR == LogisticsData::instance->sellMech( pMech ) && ( oldCount < 2 ) )
  359. ((MechListBoxItem*)pItem)->resetMech( );
  360. soundSystem->playDigitalSample( LOG_SELECT );
  361. }
  362. }
  363. }
  364. else
  365. addSelectedMech();
  366. soundSystem->playDigitalSample( LOG_SELECT );
  367. break;
  368. case MB_MSG_REMOVE:
  369. removeSelectedMech();
  370. break;
  371. case MB_MSG_NEXT:
  372. acceptPressed = true;
  373. status = DOWN;
  374. break;
  375. case MB_MSG_PREV:
  376. status = DOWN;
  377. break;
  378. case MB_MSG_MAINMENU:
  379. status = MAINMENU;
  380. break;
  381. }
  382. }
  383. return 0;
  384. }
  385. //-------------------------------------------------------------------------------------------------
  386. void MechPurchaseScreen::addSelectedMech()
  387. {
  388. LogisticsMech* pMech = variantListBox.getCurrentMech();
  389. if ( pMech )
  390. {
  391. addMech( pMech );
  392. }
  393. variantListBox.disableItemsThatCostMoreThanRP();
  394. int curSel = variantListBox.GetSelectedItem();
  395. if ( curSel == -1 )
  396. {
  397. variantListBox.SelectItem( -1 );
  398. inventoryListBox.SelectItem( 0 );
  399. }
  400. }
  401. void MechPurchaseScreen::addMech( LogisticsMech* pMech )
  402. {
  403. LogisticsData::instance->purchaseMech( pMech->getVariant() );
  404. for ( int i = 0; i < inventoryListBox.GetItemCount(); i++ )
  405. {
  406. if ( ((MechListBoxItem*)inventoryListBox.GetItem(i))->getMech()->getVariant()
  407. == pMech->getVariant() )
  408. {
  409. return;
  410. }
  411. }
  412. MechListBoxItem* pItem = new MechListBoxItem( pMech, 1 );
  413. inventoryListBox.AddItem( pItem );
  414. }
  415. //-------------------------------------------------------------------------------------------------
  416. void MechPurchaseScreen::removeSelectedMech()
  417. {
  418. LogisticsMech* pMech = inventoryListBox.getCurrentMech();
  419. if ( pMech )
  420. {
  421. removeMech( pMech );
  422. }
  423. }
  424. void MechPurchaseScreen::removeMech( LogisticsMech* pMech )
  425. {
  426. int index = inventoryListBox.GetSelectedItem();
  427. aListItem* pItem = inventoryListBox.GetItem( index );
  428. int oldCount = LogisticsData::instance->getVariantsInInventory( pMech->getVariant(), true );
  429. if ( NO_ERR == LogisticsData::instance->sellMech( pMech ) && ( oldCount < 2 ) )
  430. {
  431. inventoryListBox.RemoveItem( pItem, true );
  432. }
  433. if ( !inventoryListBox.GetItemCount() )
  434. {
  435. selectFirstBuyableMech();
  436. }
  437. else if ( -1 == inventoryListBox.GetSelectedItem() )
  438. inventoryListBox.SelectItem( 0 );
  439. }
  440. bool MechPurchaseScreen::selectFirstBuyableMech()
  441. {
  442. for ( int i = 0; i < variantListBox.GetItemCount(); i++ )
  443. {
  444. if ( NO_ERR == LogisticsData::instance->canPurchaseMech(
  445. ((MechListBoxItem*)variantListBox.GetItem(i))->getMech()->getVariant() ) )
  446. {
  447. variantListBox.SelectItem( i );
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. //*************************************************************************************************
  454. // end of file ( MechPurchaseScreen.cpp )