cargocontainer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. //cargo grid wrapper
  2. class CargoContainer extends Container
  3. {
  4. protected const int ROWS_NUMBER_XBOX = 5;
  5. protected CargoBase m_Cargo;
  6. protected int m_CargoIndex = -1;
  7. protected int m_FocusedItemPosition = -1;
  8. protected ref array<ref CargoContainerRow> m_Rows;
  9. protected ref array<ref Icon> m_Icons;
  10. protected ref map<EntityAI, ref Param3<ref Icon, int, int>> m_ShowedItemPositions;
  11. protected ref map<EntityAI, ref Param3<ref Icon, int, int>> m_ShowedLockPositions;
  12. protected float m_IconSize;
  13. protected float m_SpaceSize;
  14. protected bool m_IsAttachment;
  15. protected TextWidget m_FalseHeaderTextWidget;
  16. protected TextWidget m_AlternateFalseHeaderTextWidget; //to be set and updated along with the main one
  17. protected Widget m_CargoHeader;
  18. protected Widget m_CargoContainer;
  19. protected Widget m_ItemsContainer;
  20. #ifndef PLATFORM_CONSOLE
  21. protected ref SizeToChild m_Resizer1;
  22. #endif
  23. protected ref SizeToChild m_Resizer2;
  24. protected ref Timer m_ResizeTimer;
  25. protected int m_AttachmentSlotID = -1;
  26. void CargoContainer( LayoutHolder parent, bool is_attachment = false )
  27. {
  28. m_IsAttachment = is_attachment;
  29. m_Rows = new array<ref CargoContainerRow>;
  30. m_Icons = new array<ref Icon>;
  31. m_ShowedItemPositions = new map<EntityAI, ref Param3<ref Icon, int, int>>;
  32. m_ShowedLockPositions = new map<EntityAI, ref Param3<ref Icon, int, int>>;
  33. m_CargoContainer = m_RootWidget.FindAnyWidget( "grid_background" );
  34. m_ItemsContainer = m_RootWidget.FindAnyWidget( "grid_overlay" );
  35. m_CargoHeader = m_RootWidget.FindAnyWidget( "grid_container_header_spacer" );
  36. #ifndef PLATFORM_CONSOLE
  37. m_RootWidget.GetScript( m_Resizer1 );
  38. #endif
  39. m_RootWidget.FindAnyWidget( "grid_container" ).GetScript( m_Resizer2 );
  40. m_CargoHeader.Show( is_attachment );
  41. m_FalseHeaderTextWidget = TextWidget.Cast(m_CargoHeader.FindAnyWidget( "TextWidget0" ));
  42. m_MainWidget = m_CargoContainer;
  43. m_FocusedItemPosition = -1;
  44. }
  45. void ~CargoContainer()
  46. {
  47. if ( m_Entity )
  48. {
  49. m_Entity.GetOnItemAddedIntoCargo().Remove( AddedToCargo );
  50. m_Entity.GetOnItemRemovedFromCargo().Remove( RemovedFromCargo );
  51. m_Entity.GetOnItemMovedInCargo().Remove( MovedInCargo );
  52. m_Entity.GetOnSetLock().Remove( SetLock );
  53. m_Entity.GetOnReleaseLock().Remove( ReleaseLock );
  54. }
  55. }
  56. int GetCargoIndex() { return m_CargoIndex; }
  57. void AddedToCargoEx( EntityAI item, bool refresh = true )
  58. {
  59. InventoryLocation il = new InventoryLocation;
  60. item.GetInventory().GetCurrentInventoryLocation( il );
  61. int x = il.GetCol();
  62. int y = il.GetRow();
  63. if ( m_ShowedItemPositions.Contains( item ) )
  64. {
  65. Param3<ref Icon, int, int> item_pos = m_ShowedItemPositions.Get( item );
  66. InitIconEx( item_pos.param1, item, x, y, refresh );
  67. item_pos.param2 = x;
  68. item_pos.param3 = y;
  69. }
  70. else
  71. {
  72. ref Icon icon = new Icon( this, false );
  73. m_Icons.Insert( icon );
  74. InitIconEx( icon, item, x, y, refresh );
  75. m_ShowedItemPositions.Insert( item, new Param3<ref Icon, int, int>( icon, x, y ) );
  76. }
  77. if (refresh)
  78. UpdateHeaderText();
  79. #ifdef PLATFORM_CONSOLE
  80. for ( int i = 0; i < m_Cargo.GetItemCount(); i++ )
  81. {
  82. EntityAI item2 = m_Cargo.GetItem( i );
  83. Param3<ref Icon, int, int> data = m_ShowedItemPositions.Get( item2 );
  84. if ( data )
  85. {
  86. data.param1.SetCargoPos( i );
  87. data.param1.SetPos();
  88. }
  89. }
  90. m_FocusedItemPosition = Math.Min( m_ShowedItemPositions.Count() - 1, m_FocusedItemPosition );
  91. if (refresh)
  92. Refresh();
  93. #endif
  94. }
  95. void AddedToCargo( EntityAI item )
  96. {
  97. AddedToCargoEx( item );
  98. }
  99. void RemovedFromCargo( EntityAI item )
  100. {
  101. Param3<ref Icon, int, int> data = m_ShowedItemPositions.Get( item );
  102. if( data )
  103. {
  104. m_Icons.RemoveItem( data.param1 );
  105. m_ShowedItemPositions.Remove( item );
  106. }
  107. UpdateHeaderText();
  108. #ifdef PLATFORM_CONSOLE
  109. for( int i = 0; i < m_Cargo.GetItemCount(); i++ )
  110. {
  111. EntityAI item2 = m_Cargo.GetItem( i );
  112. data = m_ShowedItemPositions.Get( item2 );
  113. if( data && data.param1 )
  114. {
  115. data.param1.SetCargoPos( i );
  116. data.param1.SetPos();
  117. }
  118. }
  119. m_FocusedItemPosition = Math.Min( m_ShowedItemPositions.Count() - 1, m_FocusedItemPosition );
  120. Refresh();
  121. #endif
  122. }
  123. void MovedInCargo( EntityAI item )
  124. {
  125. InventoryLocation il = new InventoryLocation;
  126. item.GetInventory().GetCurrentInventoryLocation( il );
  127. int x = il.GetCol();
  128. #ifdef PLATFORM_CONSOLE
  129. int y = il.GetRow() - 1;
  130. #else
  131. int y = il.GetRow();
  132. #endif
  133. if( m_ShowedItemPositions.Contains( item ) )
  134. {
  135. ref Param3<ref Icon, int, int> data = m_ShowedItemPositions.Get( item );
  136. if( data.param1 )
  137. {
  138. if( data.param2 != x || data.param3 != y )
  139. {
  140. data.param2 = x;
  141. data.param3 = y;
  142. #ifdef PLATFORM_CONSOLE
  143. data.param1.SetCargoPos( data.param3 );
  144. #endif
  145. #ifdef PLATFORM_WINDOWS
  146. data.param1.SetPosX( data.param2 );
  147. data.param1.SetPosY( data.param3 );
  148. #endif
  149. }
  150. data.param1.UpdateInterval();
  151. }
  152. }
  153. UpdateSelection();
  154. }
  155. void SetLock( EntityAI item )
  156. {
  157. #ifndef PLATFORM_CONSOLE
  158. if( GetGame().GetPlayer() )
  159. {
  160. InventoryLocation il = new InventoryLocation;
  161. int index = GetGame().GetPlayer().GetHumanInventory().FindUserReservedLocationIndex( item );
  162. if( index >= 0 )
  163. {
  164. GetGame().GetPlayer().GetHumanInventory().GetUserReservedLocation( index, il );
  165. ref Icon icon = new Icon( this, false );
  166. m_Icons.Insert( icon );
  167. icon.InitLock( m_Entity, item, il.GetCol(), il.GetRow(), il.GetFlip() );
  168. m_ShowedLockPositions.Insert( item, new Param3<ref Icon, int, int>( icon, 1, 1 ) );
  169. item.GetOnReleaseLock().Insert( ReleaseLock );
  170. }
  171. }
  172. #endif
  173. }
  174. void ReleaseLock( EntityAI item )
  175. {
  176. #ifndef PLATFORM_CONSOLE
  177. if( m_ShowedLockPositions.Contains( item ) )
  178. {
  179. Icon ic = m_ShowedLockPositions.Get( item ).param1;
  180. m_Icons.RemoveItem( ic );
  181. m_ShowedLockPositions.Remove( item );
  182. item.GetOnReleaseLock().Remove( ReleaseLock );
  183. }
  184. #endif
  185. }
  186. override void SetLayoutName()
  187. {
  188. #ifdef PLATFORM_CONSOLE
  189. m_LayoutName = WidgetLayoutName.CargoContainerXbox;
  190. #else
  191. switch( InventoryMenu.GetWidthType() )
  192. {
  193. case ScreenWidthType.NARROW:
  194. {
  195. m_LayoutName = WidgetLayoutName.CargoContainerNarrow;
  196. break;
  197. }
  198. case ScreenWidthType.MEDIUM:
  199. {
  200. m_LayoutName = WidgetLayoutName.CargoContainerMedium;
  201. break;
  202. }
  203. case ScreenWidthType.WIDE:
  204. {
  205. m_LayoutName = WidgetLayoutName.CargoContainerWide;
  206. break;
  207. }
  208. }
  209. #endif
  210. }
  211. void SetEntity( EntityAI item, int cargo_index = 0, bool immedUpdate = true )
  212. {
  213. if ( item )
  214. {
  215. m_Entity = item;
  216. m_Cargo = item.GetInventory().GetCargoFromIndex(cargo_index);
  217. m_CargoIndex = cargo_index;
  218. m_Entity.GetOnItemAddedIntoCargo().Insert( AddedToCargo );
  219. m_Entity.GetOnItemRemovedFromCargo().Insert( RemovedFromCargo );
  220. m_Entity.GetOnItemMovedInCargo().Insert( MovedInCargo );
  221. m_Entity.GetOnSetLock().Insert( SetLock );
  222. m_Entity.GetOnReleaseLock().Insert( ReleaseLock );
  223. if (immedUpdate)
  224. UpdateHeaderText();
  225. InitGridHeight();
  226. m_MainWidget = m_ItemsContainer;
  227. if ( m_Cargo )
  228. {
  229. int i;
  230. int prev_count = m_ShowedItemPositions.Count();
  231. //START - Add new item Icons
  232. for ( i = 0; i < m_Cargo.GetItemCount(); i++ )
  233. {
  234. EntityAI cargo_item = m_Cargo.GetItem( i );
  235. if ( cargo_item )
  236. {
  237. AddedToCargoEx( cargo_item, immedUpdate );
  238. }
  239. }
  240. #ifdef PLATFORM_CONSOLE
  241. if (immedUpdate)
  242. Refresh();
  243. #endif
  244. }
  245. }
  246. }
  247. EntityAI GetEntity()
  248. {
  249. return m_Entity;
  250. }
  251. void UpdateHeaderText()
  252. {
  253. Widget header;
  254. string name = m_Entity.GetDisplayName();
  255. name.ToUpper();
  256. if ( m_Entity.CanDisplayCargo() && m_Entity.GetInventory().GetCargoFromIndex(m_CargoIndex) )
  257. {
  258. name = name + " (" + GetCargoCapacity().ToString() + "/" + GetMaxCargoCapacity() + ")";
  259. if ( m_IsAttachment && m_CargoHeader )
  260. {
  261. m_FalseHeaderTextWidget.SetText(name);
  262. float x, y;
  263. m_FalseHeaderTextWidget.Update();
  264. m_FalseHeaderTextWidget.GetScreenSize( x, y );
  265. m_CargoHeader.FindAnyWidget( "grid_container_header" ).SetSize( 1, y + InventoryMenu.GetHeightMultiplied( 10 ) );
  266. m_CargoHeader.Update();
  267. if (m_AlternateFalseHeaderTextWidget)
  268. {
  269. m_AlternateFalseHeaderTextWidget.SetText(name);
  270. }
  271. return;
  272. }
  273. }
  274. if ( Container.Cast( GetParent() ) && Container.Cast( GetParent() ).GetHeader() )
  275. Container.Cast( GetParent() ).GetHeader().SetName(name);
  276. }
  277. void InitGridHeight()
  278. {
  279. m_Rows.Clear();
  280. m_ShowedItemPositions.Clear();
  281. ref CargoContainerRow row;
  282. #ifdef PLATFORM_CONSOLE
  283. int cargo_height = 1;
  284. #else
  285. int cargo_height = m_Entity.GetInventory().GetCargoFromIndex(m_CargoIndex).GetHeight();
  286. #endif
  287. for ( int j = 0; j < cargo_height; j++ )
  288. {
  289. row = new CargoContainerRow( this );
  290. row.SetNumber( j );
  291. row.SetEntity( m_Entity );
  292. #ifdef PLATFORM_WINDOWS
  293. #ifndef PLATFORM_CONSOLE
  294. row.SetWidth( m_Entity.GetInventory().GetCargoFromIndex(m_CargoIndex).GetWidth(), false );
  295. #endif
  296. #endif
  297. row.GetRootWidget().SetSort( j, false );
  298. m_Rows.Insert( row );
  299. }
  300. float y;
  301. row.GetRootWidget().FindAnyWidget( "Icon0" ).GetScreenSize( y, m_IconSize );
  302. #ifdef PLATFORM_WINDOWS
  303. #ifndef PLATFORM_CONSOLE
  304. row.GetRootWidget().FindAnyWidget( "Spacer0" ).GetScreenSize( m_SpaceSize, y );
  305. #endif
  306. #endif
  307. m_Resizer2.ResizeParentToChild();
  308. #ifndef PLATFORM_CONSOLE
  309. m_Resizer1.ResizeParentToChild();
  310. #endif
  311. }
  312. void UpdateSize()
  313. {
  314. #ifndef PLATFORM_CONSOLE
  315. m_Resizer1.ResizeParentToChild();
  316. #else
  317. m_Resizer2.ResizeParentToChild();
  318. #endif
  319. }
  320. float GetIconSize()
  321. {
  322. return m_IconSize;
  323. }
  324. float GetSpaceSize()
  325. {
  326. return m_SpaceSize;
  327. }
  328. int GetCargoCapacity()
  329. {
  330. #ifdef PLATFORM_CONSOLE
  331. #ifndef PLATFORM_WINDOWS
  332. return CargoList.Cast( m_Cargo ).GetTotalWeight( null );
  333. #endif
  334. #endif
  335. int total_size = 0;
  336. for ( int i = 0; i < m_Cargo.GetItemCount(); ++i )
  337. {
  338. int x, y;
  339. m_Cargo.GetItemSize( i, x, y );
  340. total_size += x * y;
  341. }
  342. return total_size;
  343. }
  344. int GetMaxCargoCapacity()
  345. {
  346. #ifdef PLATFORM_CONSOLE
  347. #ifndef PLATFORM_WINDOWS
  348. return CargoList.Cast( m_Cargo ).GetMaxWeight();
  349. #endif
  350. #endif
  351. return m_Cargo.GetWidth() * m_Cargo.GetHeight();
  352. }
  353. Icon GetIcon( EntityAI item )
  354. {
  355. if ( item && m_ShowedItemPositions.Contains( item ) )
  356. {
  357. Param3<ref Icon, int, int> data = m_ShowedItemPositions.Get( item );
  358. return data.param1;
  359. }
  360. return null;
  361. }
  362. Icon GetIcon( int index )
  363. {
  364. if ( m_Cargo == null )
  365. {
  366. return null;
  367. }
  368. if ( index >= 0 && m_Cargo.GetItemCount() > index )
  369. return GetIcon( m_Cargo.GetItem( index ) );
  370. return null;
  371. }
  372. Icon GetFocusedIcon()
  373. {
  374. return GetIcon( m_FocusedItemPosition );
  375. }
  376. override float GetFocusedContainerHeight( bool contents = false )
  377. {
  378. float x, y;
  379. if( contents && GetFocusedIcon() )
  380. GetFocusedIcon().GetRootWidget().GetScreenSize( x, y );
  381. else
  382. GetRootWidget().GetScreenSize( x, y );
  383. return y;
  384. }
  385. override float GetFocusedContainerYPos( bool contents = false )
  386. {
  387. float x, y;
  388. if( contents && GetFocusedIcon() )
  389. GetFocusedIcon().GetRootWidget().GetPos( x, y );
  390. else
  391. GetRootWidget().GetPos( x, y );
  392. return y;
  393. }
  394. override float GetFocusedContainerYScreenPos( bool contents = false )
  395. {
  396. float x, y;
  397. if( contents && GetFocusedIcon() )
  398. GetFocusedIcon().GetRootWidget().GetScreenPos( x, y );
  399. else
  400. GetRootWidget().GetScreenPos( x, y );
  401. return y;
  402. }
  403. void UpdateSelection()
  404. {
  405. if( m_IsActive )
  406. {
  407. if( m_FocusedItemPosition >= m_Icons.Count() )
  408. m_FocusedItemPosition = m_Icons.Count() - 1;
  409. Icon icon = GetIcon( m_FocusedItemPosition );
  410. if( icon && !icon.IsActive() )
  411. {
  412. icon.SetActive( true );
  413. Inventory.GetInstance().UpdateConsoleToolbar();
  414. }
  415. }
  416. }
  417. void UpdateRowVisibility(int count)
  418. {
  419. int i;
  420. int rows = Math.Max(1, Math.Ceil((count + 1) / ROWS_NUMBER_XBOX));
  421. if (m_Cargo)
  422. {
  423. int maxRows = Math.Ceil(GetMaxCargoCapacity() / ROWS_NUMBER_XBOX);
  424. if (rows > maxRows) // limit amout of cargo rows depending on max cargo capacity
  425. rows = maxRows;
  426. }
  427. else
  428. {
  429. if (m_Entity)
  430. Error(string.Format("%1::UpdateRowVisibility - CargoBase is NULL for entity %2 at position %3", ToString(), m_Entity.GetType(), m_Entity.GetPosition()));
  431. }
  432. int diff = rows - m_Rows.Count();
  433. if(diff < 0)
  434. {
  435. for(i = m_Rows.Count() - 1; i >= rows; i--)
  436. {
  437. m_Rows.Remove(i);
  438. }
  439. }
  440. else if(diff > 0)
  441. {
  442. m_MainWidget = m_CargoContainer;
  443. for(i = m_Rows.Count(); i < rows; i++)
  444. {
  445. ref CargoContainerRow row = new CargoContainerRow(this);
  446. row.SetNumber(i);
  447. row.SetEntity(m_Entity);
  448. row.GetRootWidget().SetSort(i);
  449. m_Rows.Insert(row);
  450. }
  451. m_MainWidget = m_ItemsContainer;
  452. }
  453. m_Resizer2.ResizeParentToChild();
  454. #ifndef PLATFORM_CONSOLE
  455. m_Resizer1.ResizeParentToChild();
  456. #endif
  457. }
  458. override void Refresh()
  459. {
  460. #ifdef PLATFORM_CONSOLE
  461. if ( !m_ResizeTimer )
  462. m_ResizeTimer = new Timer();
  463. if ( m_ResizeTimer.IsRunning() )
  464. m_ResizeTimer.Stop();
  465. m_ResizeTimer.Run( 0.05, this, "RefreshImpl" );
  466. #endif
  467. }
  468. void RefreshImpl()
  469. {
  470. UpdateRowVisibility( m_ShowedItemPositions.Count() );
  471. UpdateSelection();
  472. }
  473. override void UpdateInterval()
  474. {
  475. foreach( Param3<ref Icon, int, int> data : m_ShowedItemPositions )
  476. {
  477. if( data.param1 )
  478. {
  479. data.param1.UpdateInterval();
  480. }
  481. }
  482. }
  483. Icon InitIconEx( Icon icon, EntityAI item, int pos_x, int pos_y, bool refresh = true )
  484. {
  485. #ifdef PLATFORM_CONSOLE
  486. icon.SetSize( 1, 1 );
  487. #ifdef PLATFORM_WINDOWS
  488. pos_y = pos_y * 5 + pos_x;
  489. #endif
  490. icon.SetCargoPos( pos_y );
  491. icon.SetPosY( pos_y );
  492. icon.SetPosEx( refresh );
  493. #else
  494. int size_x, size_y;
  495. GetGame().GetInventoryItemSize( InventoryItem.Cast( item ), size_x, size_y );
  496. if ( item.GetInventory().GetFlipCargo() )
  497. icon.SetSize( size_y, size_x );
  498. else
  499. icon.SetSize( size_x, size_y );
  500. icon.SetPosX( pos_x );
  501. icon.SetPosY( pos_y );
  502. icon.SetPosEx( refresh );
  503. #endif
  504. icon.InitEx( item, refresh );
  505. return icon;
  506. }
  507. Icon InitIcon( Icon icon, EntityAI item, int pos_x, int pos_y )
  508. {
  509. return InitIconEx( icon, item, pos_x, pos_y );
  510. }
  511. bool HasItem( EntityAI item )
  512. {
  513. return m_ShowedItemPositions.Contains( item );
  514. }
  515. override bool TransferItemToVicinity()
  516. {
  517. if (CanDrop())
  518. {
  519. Man player = GetGame().GetPlayer();
  520. if( GetFocusedIcon() )
  521. {
  522. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  523. if( item && player.CanDropEntity( item ) )
  524. {
  525. if( item.GetTargetQuantityMax() < item.GetQuantity() )
  526. item.SplitIntoStackMaxClient( null, -1 );
  527. else
  528. player.PhysicalPredictiveDropItem( item );
  529. return true;
  530. }
  531. }
  532. }
  533. return false;
  534. }
  535. override void SetDefaultFocus( bool while_micromanagment_mode = false )
  536. {
  537. super.SetDefaultFocus(while_micromanagment_mode);
  538. Unfocus();
  539. m_FocusedItemPosition = 0;
  540. UpdateSelection();
  541. }
  542. override void SetLastFocus()
  543. {
  544. SetDefaultFocus();
  545. }
  546. override void Unfocus()
  547. {
  548. Icon icon = GetFocusedIcon();
  549. if ( icon )
  550. {
  551. icon.SetActive( false );
  552. }
  553. }
  554. override void UnfocusAll()
  555. {
  556. if( m_Icons )
  557. {
  558. foreach( Icon icon : m_Icons )
  559. {
  560. icon.SetActive( false );
  561. }
  562. }
  563. m_FocusedItemPosition = 0;
  564. }
  565. override void SetNextActive()
  566. {
  567. Unfocus();
  568. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  569. int max_row = ( m_Icons.Count() - 1) / ROWS_NUMBER_XBOX;
  570. if ( max_row > focused_row )
  571. {
  572. m_FocusedItemPosition += ROWS_NUMBER_XBOX;
  573. if ( m_FocusedItemPosition >= m_Icons.Count() )
  574. {
  575. m_FocusedItemPosition = m_Icons.Count() - 1;
  576. }
  577. UpdateSelection();
  578. }
  579. else
  580. {
  581. SetActive(false);
  582. }
  583. }
  584. override void SetPreviousActive( bool force = false )
  585. {
  586. Unfocus();
  587. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  588. if ( focused_row > 0 )
  589. {
  590. m_FocusedItemPosition = m_FocusedItemPosition - ROWS_NUMBER_XBOX;
  591. UpdateSelection();
  592. }
  593. else
  594. {
  595. SetActive(false);
  596. }
  597. }
  598. override void SetNextRightActive()
  599. {
  600. if ( m_Icons.Count() > 0)
  601. {
  602. Unfocus();
  603. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  604. int row_min = focused_row * ROWS_NUMBER_XBOX;
  605. int row_max = row_min + ROWS_NUMBER_XBOX - 1;
  606. if ( row_max >= m_Icons.Count() )
  607. {
  608. row_max = m_Icons.Count() - 1;
  609. }
  610. m_FocusedItemPosition++;
  611. if( m_FocusedItemPosition > row_max )
  612. {
  613. m_FocusedItemPosition = row_min;
  614. }
  615. UpdateSelection();
  616. }
  617. }
  618. override void SetNextLeftActive()
  619. {
  620. if ( m_Icons.Count() > 0)
  621. {
  622. Unfocus();
  623. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  624. int row_min = focused_row * ROWS_NUMBER_XBOX;
  625. int row_max = row_min + ROWS_NUMBER_XBOX - 1;
  626. if ( row_max >= m_Icons.Count() )
  627. {
  628. row_max = m_Icons.Count() - 1;
  629. }
  630. m_FocusedItemPosition--;
  631. if( m_FocusedItemPosition < row_min )
  632. {
  633. m_FocusedItemPosition = row_max;
  634. }
  635. UpdateSelection();
  636. }
  637. }
  638. override EntityAI GetFocusedItem()
  639. {
  640. Icon icon = GetFocusedIcon();
  641. if( icon )
  642. {
  643. return EntityAI.Cast( icon.GetObject() );
  644. }
  645. return null;
  646. }
  647. override void SetLastActive()
  648. {
  649. super.SetLastActive();
  650. if( GetFocusedIcon() )
  651. {
  652. GetFocusedIcon().SetActive( false );
  653. }
  654. int focusedIconCount = m_Icons.Count();
  655. int columCount = m_Icons.Count() / ROWS_NUMBER_XBOX;
  656. if (focusedIconCount > ROWS_NUMBER_XBOX)
  657. {
  658. int iconMax = columCount * ROWS_NUMBER_XBOX;
  659. int diff = focusedIconCount - iconMax;
  660. if (diff == 0)
  661. diff = ROWS_NUMBER_XBOX;
  662. m_FocusedItemPosition = focusedIconCount - diff;
  663. }
  664. else
  665. {
  666. m_FocusedItemPosition = 0;
  667. }
  668. UpdateSelection();
  669. }
  670. override void SetActive( bool active )
  671. {
  672. super.SetActive( active );
  673. UpdateSelection();
  674. }
  675. override bool IsItemActive()
  676. {
  677. if( GetFocusedIcon() )
  678. {
  679. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  680. return ( item != null );
  681. }
  682. return false;
  683. }
  684. override bool IsItemWithQuantityActive()
  685. {
  686. if( GetFocusedIcon() )
  687. {
  688. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  689. return ( !IsEmpty() && QuantityConversions.HasItemQuantity( item ) && item.CanBeSplit() );
  690. }
  691. return false;
  692. }
  693. override bool IsEmpty()
  694. {
  695. return m_Icons.Count() == 0;
  696. }
  697. int GetRecipeCount( bool recipe_anywhere, ItemBase entity1, ItemBase entity2, PlayerBase player )
  698. {
  699. PluginRecipesManager recipes_manager = PluginRecipesManager.Cast( GetPlugin( PluginRecipesManager ) );
  700. return recipes_manager.GetValidRecipes( entity1, entity2, null, player );
  701. }
  702. override bool CanCombineAmmo()
  703. {
  704. if( GetFocusedIcon() )
  705. {
  706. ActionManagerClient amc = ActionManagerClient.Cast( PlayerBase.Cast( GetGame().GetPlayer() ).GetActionManager() );
  707. ItemBase entity = ItemBase.Cast( GetFocusedIcon().GetObject() );
  708. ItemBase item_in_hands = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  709. return ( amc.CanPerformActionFromInventory( item_in_hands, entity ) || amc.CanSetActionFromInventory( item_in_hands, entity ) );
  710. }
  711. return false;
  712. }
  713. override bool TransferItem()
  714. {
  715. if (CanTakeToInventory())
  716. {
  717. if (GetFocusedIcon())
  718. {
  719. EntityAI entity = EntityAI.Cast( GetFocusedIcon().GetObject() );
  720. if (entity)
  721. {
  722. GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO, entity );
  723. return true;
  724. }
  725. }
  726. }
  727. return false;
  728. }
  729. override bool SelectItem()
  730. {
  731. Icon focused_item = GetFocusedIcon();
  732. if (focused_item)
  733. {
  734. ItemBase item = ItemBase.Cast(focused_item.GetObject());
  735. if (item && item.IsTakeable() && item.CanPutIntoHands(null))
  736. {
  737. ItemManager.GetInstance().SetSelectedItemEx(item, this, focused_item);
  738. return true;
  739. }
  740. }
  741. return false;
  742. }
  743. override bool Select()
  744. {
  745. EntityAI focused_item = GetFocusedItem();
  746. EntityAI selected_item = ItemManager.GetInstance().GetSelectedItem();
  747. DayZPlayer player = GetGame().GetPlayer();
  748. if( focused_item != selected_item )
  749. {
  750. if( selected_item )
  751. {
  752. if( selected_item.GetInventory().CanRemoveEntity() && m_Entity )
  753. {
  754. bool can_add = m_Entity.GetInventory().CanAddEntityInCargo( selected_item, selected_item.GetInventory().GetFlipCargo());
  755. bool in_cargo = !player.GetInventory().HasEntityInInventory( selected_item ) || !m_Entity.GetInventory().HasEntityInCargo( selected_item );
  756. if( can_add && in_cargo )
  757. {
  758. player.PredictiveTakeEntityToTargetCargo( m_Entity, selected_item );
  759. Container selected_cont2 = ItemManager.GetInstance().GetSelectedContainer();
  760. if( selected_cont2 )
  761. {
  762. selected_cont2.SetActive( false );
  763. }
  764. SetActive( true );
  765. m_FocusedItemPosition = 0;
  766. return true;
  767. }
  768. else
  769. {
  770. Container selected_cont = ItemManager.GetInstance().GetSelectedContainer();
  771. if( selected_cont )
  772. {
  773. selected_cont.SetActive( false );
  774. }
  775. SetActive( true );
  776. SetDefaultFocus( true );
  777. }
  778. }
  779. }
  780. else if ( focused_item && focused_item.GetInventory().CanRemoveEntity() )
  781. {
  782. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  783. if( item_in_hands )
  784. {
  785. if( GameInventory.CanSwapEntitiesEx( item_in_hands, focused_item ) )
  786. {
  787. player.PredictiveSwapEntities( item_in_hands, focused_item );
  788. return true;
  789. }
  790. }
  791. else
  792. {
  793. if( player.GetHumanInventory().CanAddEntityInHands( focused_item ) )
  794. {
  795. player.PredictiveTakeEntityToHands( focused_item );
  796. return true;
  797. }
  798. }
  799. }
  800. }
  801. return false;
  802. }
  803. override bool Combine()
  804. {
  805. if (CanCombine())
  806. {
  807. if (GetFocusedIcon())
  808. {
  809. Icon icon = GetFocusedIcon();
  810. if( icon )
  811. {
  812. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  813. EntityAI prev_item = EntityAI.Cast( icon.GetObject() );
  814. if( item_in_hands && prev_item )
  815. {
  816. return icon.CombineItems( item_in_hands, prev_item );
  817. }
  818. }
  819. }
  820. }
  821. return false;
  822. }
  823. void ShowFalseCargoHeader(bool show)
  824. {
  825. m_CargoHeader.Show(show);
  826. }
  827. void SetAlternateFalseTextHeaderWidget(TextWidget w)
  828. {
  829. bool update = !m_AlternateFalseHeaderTextWidget;
  830. m_AlternateFalseHeaderTextWidget = w;
  831. if (update)
  832. {
  833. UpdateHeaderText();
  834. }
  835. }
  836. void SetAttachmentSlotID(int slotID)
  837. {
  838. m_AttachmentSlotID = slotID;
  839. }
  840. int GetAttachmentSlotID()
  841. {
  842. return m_AttachmentSlotID;
  843. }
  844. int GetIconsCount()
  845. {
  846. return m_Icons.Count();
  847. }
  848. }