cargocontainer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. int diff = rows - m_Rows.Count();
  422. if( diff < 0 )
  423. {
  424. for( i = m_Rows.Count() - 1; i >= rows; i-- )
  425. {
  426. m_Rows.Remove( i );
  427. }
  428. }
  429. else if( diff > 0 )
  430. {
  431. m_MainWidget = m_CargoContainer;
  432. for( i = m_Rows.Count(); i < rows; i++ )
  433. {
  434. ref CargoContainerRow row = new CargoContainerRow( this );
  435. row.SetNumber( i );
  436. row.SetEntity( m_Entity );
  437. row.GetRootWidget().SetSort( i );
  438. m_Rows.Insert( row );
  439. }
  440. m_MainWidget = m_ItemsContainer;
  441. }
  442. m_Resizer2.ResizeParentToChild();
  443. #ifndef PLATFORM_CONSOLE
  444. m_Resizer1.ResizeParentToChild();
  445. #endif
  446. }
  447. override void Refresh()
  448. {
  449. #ifdef PLATFORM_CONSOLE
  450. if ( !m_ResizeTimer )
  451. m_ResizeTimer = new Timer();
  452. if ( m_ResizeTimer.IsRunning() )
  453. m_ResizeTimer.Stop();
  454. m_ResizeTimer.Run( 0.05, this, "RefreshImpl" );
  455. #endif
  456. }
  457. void RefreshImpl()
  458. {
  459. UpdateRowVisibility( m_ShowedItemPositions.Count() );
  460. UpdateSelection();
  461. }
  462. override void UpdateInterval()
  463. {
  464. foreach( Param3<ref Icon, int, int> data : m_ShowedItemPositions )
  465. {
  466. if( data.param1 )
  467. {
  468. data.param1.UpdateInterval();
  469. }
  470. }
  471. }
  472. Icon InitIconEx( Icon icon, EntityAI item, int pos_x, int pos_y, bool refresh = true )
  473. {
  474. #ifdef PLATFORM_CONSOLE
  475. icon.SetSize( 1, 1 );
  476. #ifdef PLATFORM_WINDOWS
  477. pos_y = pos_y * 5 + pos_x;
  478. #endif
  479. icon.SetCargoPos( pos_y );
  480. icon.SetPosY( pos_y );
  481. icon.SetPosEx( refresh );
  482. #else
  483. int size_x, size_y;
  484. GetGame().GetInventoryItemSize( InventoryItem.Cast( item ), size_x, size_y );
  485. if ( item.GetInventory().GetFlipCargo() )
  486. icon.SetSize( size_y, size_x );
  487. else
  488. icon.SetSize( size_x, size_y );
  489. icon.SetPosX( pos_x );
  490. icon.SetPosY( pos_y );
  491. icon.SetPosEx( refresh );
  492. #endif
  493. icon.InitEx( item, refresh );
  494. return icon;
  495. }
  496. Icon InitIcon( Icon icon, EntityAI item, int pos_x, int pos_y )
  497. {
  498. return InitIconEx( icon, item, pos_x, pos_y );
  499. }
  500. bool HasItem( EntityAI item )
  501. {
  502. return m_ShowedItemPositions.Contains( item );
  503. }
  504. override bool TransferItemToVicinity()
  505. {
  506. if (CanDrop())
  507. {
  508. Man player = GetGame().GetPlayer();
  509. if( GetFocusedIcon() )
  510. {
  511. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  512. if( item && player.CanDropEntity( item ) )
  513. {
  514. if( item.GetTargetQuantityMax() < item.GetQuantity() )
  515. item.SplitIntoStackMaxClient( null, -1 );
  516. else
  517. player.PhysicalPredictiveDropItem( item );
  518. return true;
  519. }
  520. }
  521. }
  522. return false;
  523. }
  524. override void SetDefaultFocus( bool while_micromanagment_mode = false )
  525. {
  526. super.SetDefaultFocus(while_micromanagment_mode);
  527. Unfocus();
  528. m_FocusedItemPosition = 0;
  529. UpdateSelection();
  530. }
  531. override void SetLastFocus()
  532. {
  533. SetDefaultFocus();
  534. }
  535. override void Unfocus()
  536. {
  537. Icon icon = GetFocusedIcon();
  538. if ( icon )
  539. {
  540. icon.SetActive( false );
  541. }
  542. }
  543. override void UnfocusAll()
  544. {
  545. if( m_Icons )
  546. {
  547. foreach( Icon icon : m_Icons )
  548. {
  549. icon.SetActive( false );
  550. }
  551. }
  552. m_FocusedItemPosition = 0;
  553. }
  554. override void SetNextActive()
  555. {
  556. Unfocus();
  557. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  558. int max_row = ( m_Icons.Count() - 1) / ROWS_NUMBER_XBOX;
  559. if ( max_row > focused_row )
  560. {
  561. m_FocusedItemPosition += ROWS_NUMBER_XBOX;
  562. if ( m_FocusedItemPosition >= m_Icons.Count() )
  563. {
  564. m_FocusedItemPosition = m_Icons.Count() - 1;
  565. }
  566. UpdateSelection();
  567. }
  568. else
  569. {
  570. SetActive(false);
  571. }
  572. }
  573. override void SetPreviousActive( bool force = false )
  574. {
  575. Unfocus();
  576. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  577. if ( focused_row > 0 )
  578. {
  579. m_FocusedItemPosition = m_FocusedItemPosition - ROWS_NUMBER_XBOX;
  580. UpdateSelection();
  581. }
  582. else
  583. {
  584. SetActive(false);
  585. }
  586. }
  587. override void SetNextRightActive()
  588. {
  589. if ( m_Icons.Count() > 0)
  590. {
  591. Unfocus();
  592. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  593. int row_min = focused_row * ROWS_NUMBER_XBOX;
  594. int row_max = row_min + ROWS_NUMBER_XBOX - 1;
  595. if ( row_max >= m_Icons.Count() )
  596. {
  597. row_max = m_Icons.Count() - 1;
  598. }
  599. m_FocusedItemPosition++;
  600. if( m_FocusedItemPosition > row_max )
  601. {
  602. m_FocusedItemPosition = row_min;
  603. }
  604. UpdateSelection();
  605. }
  606. }
  607. override void SetNextLeftActive()
  608. {
  609. if ( m_Icons.Count() > 0)
  610. {
  611. Unfocus();
  612. int focused_row = m_FocusedItemPosition / ROWS_NUMBER_XBOX;
  613. int row_min = focused_row * ROWS_NUMBER_XBOX;
  614. int row_max = row_min + ROWS_NUMBER_XBOX - 1;
  615. if ( row_max >= m_Icons.Count() )
  616. {
  617. row_max = m_Icons.Count() - 1;
  618. }
  619. m_FocusedItemPosition--;
  620. if( m_FocusedItemPosition < row_min )
  621. {
  622. m_FocusedItemPosition = row_max;
  623. }
  624. UpdateSelection();
  625. }
  626. }
  627. override EntityAI GetFocusedItem()
  628. {
  629. Icon icon = GetFocusedIcon();
  630. if( icon )
  631. {
  632. return EntityAI.Cast( icon.GetObject() );
  633. }
  634. return null;
  635. }
  636. override void SetLastActive()
  637. {
  638. super.SetLastActive();
  639. if( GetFocusedIcon() )
  640. {
  641. GetFocusedIcon().SetActive( false );
  642. }
  643. int focusedIconCount = m_Icons.Count();
  644. int columCount = m_Icons.Count() / ROWS_NUMBER_XBOX;
  645. if (focusedIconCount > ROWS_NUMBER_XBOX)
  646. {
  647. int iconMax = columCount * ROWS_NUMBER_XBOX;
  648. int diff = focusedIconCount - iconMax;
  649. if (diff == 0)
  650. diff = ROWS_NUMBER_XBOX;
  651. m_FocusedItemPosition = focusedIconCount - diff;
  652. }
  653. else
  654. {
  655. m_FocusedItemPosition = 0;
  656. }
  657. UpdateSelection();
  658. }
  659. override void SetActive( bool active )
  660. {
  661. super.SetActive( active );
  662. UpdateSelection();
  663. }
  664. override bool IsItemActive()
  665. {
  666. if( GetFocusedIcon() )
  667. {
  668. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  669. return ( item != null );
  670. }
  671. return false;
  672. }
  673. override bool IsItemWithQuantityActive()
  674. {
  675. if( GetFocusedIcon() )
  676. {
  677. ItemBase item = ItemBase.Cast( GetFocusedIcon().GetObject() );
  678. return ( !IsEmpty() && QuantityConversions.HasItemQuantity( item ) && item.CanBeSplit() );
  679. }
  680. return false;
  681. }
  682. override bool IsEmpty()
  683. {
  684. return m_Icons.Count() == 0;
  685. }
  686. int GetRecipeCount( bool recipe_anywhere, ItemBase entity1, ItemBase entity2, PlayerBase player )
  687. {
  688. PluginRecipesManager recipes_manager = PluginRecipesManager.Cast( GetPlugin( PluginRecipesManager ) );
  689. return recipes_manager.GetValidRecipes( entity1, entity2, null, player );
  690. }
  691. override bool CanCombineAmmo()
  692. {
  693. if( GetFocusedIcon() )
  694. {
  695. ActionManagerClient amc = ActionManagerClient.Cast( PlayerBase.Cast( GetGame().GetPlayer() ).GetActionManager() );
  696. ItemBase entity = ItemBase.Cast( GetFocusedIcon().GetObject() );
  697. ItemBase item_in_hands = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  698. return ( amc.CanPerformActionFromInventory( item_in_hands, entity ) || amc.CanSetActionFromInventory( item_in_hands, entity ) );
  699. }
  700. return false;
  701. }
  702. override bool TransferItem()
  703. {
  704. if (CanTakeToInventory())
  705. {
  706. if (GetFocusedIcon())
  707. {
  708. EntityAI entity = EntityAI.Cast( GetFocusedIcon().GetObject() );
  709. if (entity)
  710. {
  711. GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO, entity );
  712. return true;
  713. }
  714. }
  715. }
  716. return false;
  717. }
  718. override bool SplitItem()
  719. {
  720. if (CanSplit())
  721. {
  722. if ( GetFocusedIcon() )
  723. {
  724. ItemBase entity = ItemBase.Cast( GetFocusedIcon().GetObject() );
  725. if ( entity )
  726. {
  727. if ( entity.HasQuantity() && entity.CanBeSplit() )
  728. {
  729. entity.OnRightClick();
  730. Icon icon = m_ShowedItemPositions.Get( entity ).param1;
  731. if ( icon )
  732. {
  733. icon.SetQuantity();
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return false;
  740. }
  741. override bool EquipItem()
  742. {
  743. if (CanEquip())
  744. {
  745. if (GetFocusedIcon())
  746. {
  747. ItemBase entity = ItemBase.Cast( GetFocusedIcon().GetObject() );
  748. if( entity )
  749. {
  750. GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.ATTACHMENT, entity );
  751. return true;
  752. }
  753. }
  754. }
  755. return false;
  756. }
  757. override bool SelectItem()
  758. {
  759. Icon focused_item = GetFocusedIcon();
  760. if (focused_item)
  761. {
  762. ItemBase item = ItemBase.Cast(focused_item.GetObject());
  763. if (item && item.IsTakeable() && item.CanPutIntoHands(null))
  764. {
  765. ItemManager.GetInstance().SetSelectedItemEx(item, this, focused_item);
  766. return true;
  767. }
  768. }
  769. return false;
  770. }
  771. override bool Select()
  772. {
  773. EntityAI focused_item = GetFocusedItem();
  774. EntityAI selected_item = ItemManager.GetInstance().GetSelectedItem();
  775. DayZPlayer player = GetGame().GetPlayer();
  776. if( focused_item != selected_item )
  777. {
  778. if( selected_item )
  779. {
  780. if( selected_item.GetInventory().CanRemoveEntity() && m_Entity )
  781. {
  782. bool can_add = m_Entity.GetInventory().CanAddEntityInCargo( selected_item, selected_item.GetInventory().GetFlipCargo());
  783. bool in_cargo = !player.GetInventory().HasEntityInInventory( selected_item ) || !m_Entity.GetInventory().HasEntityInCargo( selected_item );
  784. if( can_add && in_cargo )
  785. {
  786. player.PredictiveTakeEntityToTargetCargo( m_Entity, selected_item );
  787. Container selected_cont2 = ItemManager.GetInstance().GetSelectedContainer();
  788. if( selected_cont2 )
  789. {
  790. selected_cont2.SetActive( false );
  791. }
  792. SetActive( true );
  793. m_FocusedItemPosition = 0;
  794. return true;
  795. }
  796. else
  797. {
  798. Container selected_cont = ItemManager.GetInstance().GetSelectedContainer();
  799. if( selected_cont )
  800. {
  801. selected_cont.SetActive( false );
  802. }
  803. SetActive( true );
  804. SetDefaultFocus( true );
  805. }
  806. }
  807. }
  808. else if ( focused_item && focused_item.GetInventory().CanRemoveEntity() )
  809. {
  810. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  811. if( item_in_hands )
  812. {
  813. if( GameInventory.CanSwapEntitiesEx( item_in_hands, focused_item ) )
  814. {
  815. player.PredictiveSwapEntities( item_in_hands, focused_item );
  816. return true;
  817. }
  818. }
  819. else
  820. {
  821. if( player.GetHumanInventory().CanAddEntityInHands( focused_item ) )
  822. {
  823. player.PredictiveTakeEntityToHands( focused_item );
  824. return true;
  825. }
  826. }
  827. }
  828. }
  829. return false;
  830. }
  831. override bool Combine()
  832. {
  833. if (CanCombine())
  834. {
  835. if (GetFocusedIcon())
  836. {
  837. Icon icon = GetFocusedIcon();
  838. if( icon )
  839. {
  840. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  841. EntityAI prev_item = EntityAI.Cast( icon.GetObject() );
  842. if( item_in_hands && prev_item )
  843. {
  844. return icon.CombineItems( item_in_hands, prev_item );
  845. }
  846. }
  847. }
  848. }
  849. return false;
  850. }
  851. void ShowFalseCargoHeader(bool show)
  852. {
  853. m_CargoHeader.Show(show);
  854. }
  855. void SetAlternateFalseTextHeaderWidget(TextWidget w)
  856. {
  857. bool update = !m_AlternateFalseHeaderTextWidget;
  858. m_AlternateFalseHeaderTextWidget = w;
  859. if (update)
  860. {
  861. UpdateHeaderText();
  862. }
  863. }
  864. void SetAttachmentSlotID(int slotID)
  865. {
  866. m_AttachmentSlotID = slotID;
  867. }
  868. int GetAttachmentSlotID()
  869. {
  870. return m_AttachmentSlotID;
  871. }
  872. int GetIconsCount()
  873. {
  874. return m_Icons.Count();
  875. }
  876. }