playercontainer.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. class PlayerContainer: CollapsibleContainer
  2. {
  3. protected ref AttachmentsGroupContainer m_PlayerAttachmentsContainer;
  4. protected ref map<int, SlotsIcon> m_InventorySlots;
  5. protected ref map<EntityAI, ref Container> m_ShowedItems = new map<EntityAI, ref Container>;
  6. protected ref map<int, ref Container> m_ShowedItemsIDs = new map<int, ref Container>;
  7. protected PlayerBase m_Player;
  8. protected const int HEADER_INDEX_OFFSET = 2;
  9. override void UpdateRadialIcon()
  10. {
  11. if ( m_SlotIcon )
  12. {
  13. bool show_radial_icon;
  14. show_radial_icon = IsHidden();
  15. Widget rip = m_SlotIcon.GetRadialIconPanel();
  16. rip.Show( !m_Player.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) && m_Player.CanDisplayCargo());
  17. SetOpenForSlotIcon(show_radial_icon);
  18. }
  19. }
  20. bool HasEntityContainerVisible( EntityAI entity )
  21. {
  22. ClosableContainer cont = ClosableContainer.Cast( m_ShowedItems.Get( entity ) );
  23. return ( cont && cont.IsOpened() );
  24. }
  25. SlotsIcon GetSlotsIcon( int row, int column )
  26. {
  27. return m_PlayerAttachmentsContainer.GetSlotsIcon(row, column);
  28. }
  29. void PlayerContainer( LayoutHolder parent, int sort = -1 )
  30. {
  31. m_InventorySlots = new map<int, SlotsIcon>;
  32. m_PlayerAttachmentsContainer = new AttachmentsGroupContainer(this);
  33. #ifndef PLATFORM_CONSOLE
  34. RightArea rightArea = RightArea.Cast(GetParent());
  35. if (rightArea)
  36. {
  37. rightArea.GetSlotsHeader().AddChild(GetHeader().GetRootWidget());
  38. rightArea.GetSlotsArea().AddChild(m_PlayerAttachmentsContainer.GetRootWidget());
  39. }
  40. #endif
  41. m_PlayerAttachmentsContainer.SetHeader(GetHeader());
  42. m_CollapsibleHeader.SetName( "#container_inventory" );
  43. SetHeader(null);
  44. m_Body.Insert( m_PlayerAttachmentsContainer );
  45. m_MainWidget = m_RootWidget.FindAnyWidget( "body" );
  46. m_PlayerAttachmentsContainer.GetRootWidget().SetColor(166 << 24 | 80 << 16 | 80 << 8 | 80);
  47. WidgetEventHandler.GetInstance().RegisterOnChildAdd( m_MainWidget, this, "OnChildAdd" );
  48. WidgetEventHandler.GetInstance().RegisterOnChildRemove( m_MainWidget, this, "OnChildRemove" );
  49. //START - InitGhostSlots
  50. string config_path_ghosts_slots = "CfgVehicles SurvivorBase InventoryEquipment playerSlots";
  51. ref array<string> player_ghosts_slots = new array<string>;
  52. GetGame().ConfigGetTextArray( config_path_ghosts_slots, player_ghosts_slots );
  53. for ( int i = 0; i < player_ghosts_slots.Count(); i++ )
  54. {
  55. string slot_name = player_ghosts_slots.Get ( i );
  56. string path = "CfgSlots" + " " + slot_name;
  57. if ( GetGame().ConfigIsExisting( path ) )
  58. {
  59. string icon_name; //icon_name must be in format "set:<setname> image:<imagename>"
  60. GetGame().ConfigGetText( path + " ghostIcon", icon_name );
  61. int slot_number = i;
  62. int column = slot_number % ITEMS_IN_ROW;
  63. //START - GetWidgetSlot
  64. int row = slot_number / ITEMS_IN_ROW;
  65. if( row >= m_PlayerAttachmentsContainer.Count() )
  66. {
  67. if( row < ( player_ghosts_slots.Count() / ITEMS_IN_ROW ) )
  68. AddSlotsContainer( ITEMS_IN_ROW );
  69. else
  70. AddSlotsContainer( player_ghosts_slots.Count() % ITEMS_IN_ROW );
  71. }
  72. SlotsIcon icon = GetSlotsIcon( row, column );
  73. icon.GetMainWidget().Show( true );
  74. icon.Clear();
  75. WidgetEventHandler.GetInstance().RegisterOnDoubleClick( icon.GetPanelWidget(), this, "DoubleClick" );
  76. //END - GetWidgetSlot
  77. WidgetEventHandler.GetInstance().RegisterOnDropReceived( icon.GetPanelWidget(), this, "OnDropReceivedFromGhostArea" );
  78. WidgetEventHandler.GetInstance().RegisterOnDropReceived( icon.GetGhostSlot(), this, "OnDropReceivedFromGhostArea" );
  79. WidgetEventHandler.GetInstance().RegisterOnDraggingOver( icon.GetGhostSlot(), this, "DraggingOver" );
  80. WidgetEventHandler.GetInstance().RegisterOnDraggingOver( icon.GetPanelWidget(), this, "DraggingOver" );
  81. WidgetEventHandler.GetInstance().RegisterOnMouseButtonDown( icon.GetPanelWidget(), this, "MouseClick" );
  82. icon.GetGhostSlot().LoadImageFile( 0, StaticGUIUtils.VerifyIconImageString(StaticGUIUtils.IMAGESETGROUP_INVENTORY,icon_name) );
  83. //END - LoadIconIntoWidgetSlot
  84. GetGame().ConfigGetText( path + " name", slot_name );
  85. int slot_id = InventorySlots.GetSlotIdFromString( slot_name );
  86. icon.SetSlotID(slot_id);
  87. icon.SetSlotDisplayName(InventorySlots.GetSlotDisplayName(slot_id));
  88. m_InventorySlots.Set( slot_id, icon );
  89. }
  90. }
  91. m_PlayerAttachmentsContainer.GetMainWidget().Update();
  92. //END - InitGhostSlots
  93. RecomputeOpenedContainers();
  94. }
  95. void ~PlayerContainer()
  96. {
  97. if( m_Player )
  98. {
  99. m_Player.GetOnItemAttached().Remove(ItemAttached);
  100. m_Player.GetOnItemDetached().Remove(ItemDetached);
  101. m_Player.GetOnAttachmentSetLock().Remove(OnAttachmentReservationSet);
  102. m_Player.GetOnAttachmentReleaseLock().Remove(OnAttachmentReservationRelease);
  103. }
  104. }
  105. void ItemAttached(EntityAI item, string slot_name)
  106. {
  107. int slot = InventorySlots.GetSlotIdFromString( slot_name );
  108. SlotsIcon icon = m_InventorySlots.Get( slot );
  109. if( icon )
  110. {
  111. if( icon.IsReserved())
  112. m_Player.GetOnAttachmentReleaseLock().Invoke(item, slot);
  113. icon.Init( item );
  114. if( m_ShowedItems.Contains( item ) == false )
  115. {
  116. string name;
  117. int sort_index = Inventory.GetPlayerAttachmentIndex( slot );
  118. int current_sort = 9999;
  119. if( GetFocusedContainer() )
  120. current_sort = GetFocusedContainer().GetRootWidget().GetSort();
  121. if( item.GetSlotsCountCorrect() > 0 )
  122. {
  123. ContainerWithCargoAndAttachments iwca = new ContainerWithCargoAndAttachments( this, sort_index );
  124. iwca.SetEntity( item );
  125. iwca.SetSlotIcon( icon );
  126. WidgetEventHandler.GetInstance().RegisterOnMouseButtonUp( icon.GetPanelWidget(), this, "ToggleWidget" );
  127. if( iwca.IsDisplayable() )
  128. {
  129. icon.GetRadialIconPanel().Show( true );
  130. }
  131. m_ShowedItems.Insert( item, iwca );
  132. RecomputeOpenedContainers();
  133. Refresh();
  134. }
  135. else if( item.GetInventory().GetCargo() )
  136. {
  137. ContainerWithCargo iwc = new ContainerWithCargo( this, sort_index );
  138. iwc.SetEntity( item );
  139. iwc.SetSlotIcon( icon );
  140. iwc.UpdateInterval();
  141. WidgetEventHandler.GetInstance().RegisterOnMouseButtonUp( icon.GetPanelWidget(), this, "ToggleWidget" );
  142. m_ShowedItems.Insert( item, iwc );
  143. RecomputeOpenedContainers();
  144. if( iwc.IsDisplayable() )
  145. icon.GetRadialIconPanel().Show(true);
  146. Refresh();
  147. }
  148. }
  149. ClosableContainer conta = ClosableContainer.Cast( m_ShowedItems.Get( item ) );
  150. string config = "CfgVehicles " + item.GetType() + " GUIInventoryAttachmentsProps";
  151. if( conta && conta.IsInherited( ClosableContainer ) )
  152. {
  153. bool show_radial_icon = ( item.GetInventory().GetCargo() || item.GetSlotsCountCorrect() > 0 ) && !GetGame().ConfigIsExisting( config );
  154. Widget rip = icon.GetRadialIconPanel();
  155. rip.Show( show_radial_icon );
  156. SetOpenForSlotIcon(conta.IsOpened(),icon);
  157. }
  158. }
  159. UpdateSelectionIcons();
  160. }
  161. void ItemDetached(EntityAI item, string slot_name)
  162. {
  163. int slot = InventorySlots.GetSlotIdFromString( slot_name );
  164. SlotsIcon icon = m_InventorySlots.Get( slot );
  165. if( icon )
  166. {
  167. Container con = m_ShowedItems.Get( item );
  168. if( con )
  169. {
  170. ( Container.Cast( con.m_Parent ) ).Remove( con );
  171. RecomputeOpenedContainers();
  172. GetMainWidget().Update();
  173. Refresh();
  174. m_ShowedItems.Remove( item );
  175. }
  176. icon.Clear();
  177. Inventory in = Inventory.Cast( GetRoot() );
  178. if( in )
  179. in.UpdateConsoleToolbar();
  180. }
  181. UpdateSelectionIcons();
  182. }
  183. void OnAttachmentReservationSet( EntityAI item, int slot_id )
  184. {
  185. SlotsIcon icon = m_InventorySlots.Get( slot_id );
  186. if( item )
  187. {
  188. icon.Init( item, true );
  189. }
  190. }
  191. void OnAttachmentReservationRelease( EntityAI item, int slot_id )
  192. {
  193. SlotsIcon icon = m_InventorySlots.Get( slot_id );
  194. icon.Clear();
  195. }
  196. void AddSlotsContainer( int row_count )
  197. {
  198. SlotsContainer container = new SlotsContainer(m_PlayerAttachmentsContainer, m_Player);
  199. container.SetColumnCount(row_count);
  200. m_PlayerAttachmentsContainer.Insert(container);
  201. }
  202. void MouseClick( Widget w, int x, int y, int button )
  203. {
  204. SlotsIcon icon;
  205. w.GetUserData(icon);
  206. ItemBase selectedItem;
  207. bool isReserved;
  208. if (icon)
  209. {
  210. selectedItem = ItemBase.Cast(icon.GetEntity());
  211. isReserved = icon.IsReserved();
  212. }
  213. if (selectedItem)
  214. {
  215. switch (button)
  216. {
  217. case MouseState.RIGHT:
  218. #ifdef DIAG_DEVELOPER
  219. if (GetDayZGame().IsLeftCtrlDown())
  220. ShowActionMenu(selectedItem);
  221. #endif
  222. if (isReserved)
  223. {
  224. GetGame().GetPlayer().GetHumanInventory().ClearUserReservedLocationSynced(selectedItem);
  225. m_Player.GetOnAttachmentReleaseLock().Invoke(selectedItem, icon.GetSlotID());
  226. }
  227. break;
  228. case MouseState.MIDDLE:
  229. if (!isReserved)
  230. InspectItem(selectedItem);
  231. break;
  232. case MouseState.LEFT:
  233. if (!isReserved)
  234. {
  235. PlayerBase controlledPlayer = PlayerBase.Cast(GetGame().GetPlayer());
  236. if (g_Game.IsLeftCtrlDown())
  237. {
  238. if (controlledPlayer.CanDropEntity(selectedItem))
  239. {
  240. if (selectedItem.GetTargetQuantityMax() < selectedItem.GetQuantity())
  241. selectedItem.SplitIntoStackMaxClient(null, -1);
  242. else
  243. controlledPlayer.PhysicalPredictiveDropItem(selectedItem);
  244. }
  245. }
  246. else
  247. {
  248. bool draggable = !m_Player.GetInventory().HasInventoryReservation(selectedItem, null ) && !m_Player.GetInventory().IsInventoryLocked() && selectedItem.GetInventory().CanRemoveEntity() && !m_Player.IsItemsToDelete();
  249. ItemManager.GetInstance().SetWidgetDraggable(w, draggable);
  250. }
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. float GetMainContainerTopPos()
  257. {
  258. float x, y;
  259. GetRootWidget().GetScreenPos( x, y );
  260. return y;
  261. }
  262. float GetMainContainerBottomPos()
  263. {
  264. float x, y;
  265. float x2, y2;
  266. GetRootWidget().GetScreenPos( x, y );
  267. GetRootWidget().GetScreenSize( x2, y2 );
  268. y += y2;
  269. return y;
  270. }
  271. override void Insert( LayoutHolder container, int pos = -1, bool immedUpdate = true )
  272. {
  273. ClosableContainer c_cont = ClosableContainer.Cast( container );
  274. if (c_cont && m_Body.Count() > 1)
  275. {
  276. ClosableContainer prev = ClosableContainer.Cast(m_Body.Get( 1 ));
  277. int index = 1;
  278. int curr_sort = c_cont.GetRootWidget().GetSort();
  279. while ( prev )
  280. {
  281. int prev_sort = prev.GetRootWidget().GetSort();
  282. if ( prev_sort > curr_sort )
  283. break;
  284. if ( ++index < m_Body.Count() )
  285. prev = ClosableContainer.Cast( m_Body.Get( index ) );
  286. else
  287. break;
  288. }
  289. m_Body.InsertAt( container, index );
  290. }
  291. else
  292. {
  293. m_Body.Insert( container );
  294. }
  295. if ( immedUpdate )
  296. RecomputeOpenedContainers();
  297. }
  298. bool IsItemWithContainerActive()
  299. {
  300. EntityAI ent = GetFocusedItem();
  301. return ent && ( ent.GetInventory().GetCargo() || ent.GetSlotsCountCorrect() > 0 );
  302. }
  303. override bool IsItemWithQuantityActive()
  304. {
  305. EntityAI ent = GetFocusedItem();
  306. return ent && QuantityConversions.HasItemQuantity( ent ) && ent.CanBeSplit();
  307. }
  308. override bool IsItemActive()
  309. {
  310. EntityAI ent = GetFocusedItem();
  311. return ent && !IsItemWithQuantityActive() && !IsItemWithContainerActive();
  312. }
  313. bool IsEmptyItemActive()
  314. {
  315. EntityAI ent = GetFocusedItem();
  316. return ent == null;
  317. }
  318. void UnfocusPlayerAttachmentsContainer()
  319. {
  320. m_PlayerAttachmentsContainer.UnfocusAll();
  321. }
  322. void DoubleClick(Widget w, int x, int y, int button)
  323. {
  324. if( button == MouseState.LEFT && !g_Game.IsLeftCtrlDown())
  325. {
  326. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  327. if( w == null || m_Player.GetInventory().IsInventoryLocked() )
  328. {
  329. return;
  330. }
  331. ItemPreviewWidget iw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  332. if( !iw )
  333. {
  334. string name = w.GetName();
  335. name.Replace( "PanelWidget", "Render" );
  336. iw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  337. }
  338. if( !iw )
  339. {
  340. iw = ItemPreviewWidget.Cast( w );
  341. }
  342. ItemBase item = ItemBase.Cast( iw.GetItem() );
  343. if( !item )
  344. {
  345. return;
  346. }
  347. SlotsIcon icon;
  348. iw.GetUserData(icon);
  349. if(icon && icon.IsReserved())
  350. {
  351. return;
  352. }
  353. if( !item.GetInventory().CanRemoveEntity() )
  354. return;
  355. if ( player.GetInventory().HasEntityInInventory( item ) && player.GetHumanInventory().CanAddEntityInHands( item ) )
  356. {
  357. player.PredictiveTakeEntityToHands( item );
  358. }
  359. else
  360. {
  361. if (player.GetInventory().CanAddEntityToInventory( item ) && item.GetInventory().CanRemoveEntity())
  362. {
  363. if( item.GetTargetQuantityMax() < item.GetQuantity() )
  364. item.SplitIntoStackMaxClient( player, -1 );
  365. else
  366. player.PredictiveTakeEntityToInventory( FindInventoryLocationType.ANY, item );
  367. }
  368. else
  369. {
  370. if( item.GetTargetQuantityMax() < item.GetQuantity() )
  371. item.SplitIntoStackMaxHandsClient( player );
  372. else
  373. player.PredictiveTakeEntityToHands( item );
  374. }
  375. }
  376. ToggleWidget( w );
  377. HideOwnedTooltip();
  378. InventoryMenu menu = InventoryMenu.Cast( GetGame().GetUIManager().FindMenu( MENU_INVENTORY ) );
  379. if ( menu )
  380. {
  381. menu.RefreshQuickbar();
  382. }
  383. }
  384. }
  385. override void ExpandCollapseContainer()
  386. {
  387. if( m_PlayerAttachmentsContainer.IsActive() )
  388. {
  389. ToggleWidget(m_PlayerAttachmentsContainer.GetFocusedSlotsIcon().GetPanelWidget());
  390. }
  391. }
  392. bool IsContainerWithCargoActive()
  393. {
  394. return GetFocusedContainer().IsInherited( ContainerWithCargo );
  395. }
  396. bool IsItemWithAttachmentsActive()
  397. {
  398. return GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments );
  399. }
  400. override bool TransferItemToVicinity()
  401. {
  402. if (CanDrop())
  403. {
  404. if (GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ))
  405. {
  406. return GetFocusedContainer().TransferItemToVicinity();
  407. }
  408. else
  409. {
  410. Man player = GetGame().GetPlayer();
  411. ItemBase item = ItemBase.Cast(GetFocusedItem());
  412. if ( item && player.CanDropEntity( item ) )
  413. {
  414. if ( item.GetTargetQuantityMax() < item.GetQuantity() )
  415. item.SplitIntoStackMaxClient( null, -1 );
  416. else
  417. player.PhysicalPredictiveDropItem( item );
  418. return true;
  419. }
  420. }
  421. }
  422. return false;
  423. }
  424. override bool InspectItem()
  425. {
  426. if( GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ) )
  427. {
  428. return GetFocusedContainer().InspectItem();
  429. }
  430. else
  431. {
  432. Man player = GetGame().GetPlayer();
  433. EntityAI item = GetFocusedItem();
  434. if( item )
  435. {
  436. InspectItem( item );
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. int GetRecipeCount( bool recipe_anywhere, EntityAI entity1, EntityAI entity2 )
  443. {
  444. PluginRecipesManager plugin_recipes_manager = PluginRecipesManager.Cast( GetPlugin( PluginRecipesManager ) );
  445. return plugin_recipes_manager.GetValidRecipes( ItemBase.Cast( entity1 ), ItemBase.Cast( entity2 ), null, PlayerBase.Cast( GetGame().GetPlayer() ) );
  446. }
  447. override bool TransferItem()
  448. {
  449. if (CanTakeToInventory())
  450. {
  451. LeftArea left_area = LeftArea.Cast( GetParent() );
  452. EntityAI item;
  453. if (left_area)
  454. {
  455. if (GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ))
  456. {
  457. return GetFocusedContainer().TransferItem();
  458. }
  459. else
  460. {
  461. item = GetFocusedItem();
  462. if( item )
  463. {
  464. GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO, item );
  465. return true;
  466. }
  467. }
  468. }
  469. else
  470. {
  471. if (!GetFocusedContainer().IsInherited( ContainerWithCargo ) && !GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ))
  472. {
  473. item = GetFocusedItem();
  474. if( item )
  475. {
  476. GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO, item );
  477. return true;
  478. }
  479. }
  480. }
  481. }
  482. return false;
  483. }
  484. override EntityAI GetFocusedItem()
  485. {
  486. return GetFocusedContainer().GetFocusedItem();
  487. }
  488. override bool Combine()
  489. {
  490. if (CanCombine())
  491. {
  492. if ( GetFocusedContainer() && ( GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ) ) )
  493. {
  494. return GetFocusedContainer().Combine();
  495. }
  496. else
  497. {
  498. EntityAI item = GetFocusedItem();
  499. ItemBase item_in_hands = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  500. Icon hands_icon = ItemManager.GetInstance().GetHandsPreview().GetIcon();
  501. if( item_in_hands && item )
  502. {
  503. return hands_icon.CombineItems( item_in_hands, item );
  504. }
  505. }
  506. }
  507. return true;
  508. }
  509. override bool Select()
  510. {
  511. if( GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ) )
  512. {
  513. return GetFocusedContainer().Select();
  514. }
  515. else
  516. {
  517. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  518. EntityAI selected_item = ItemManager.GetInstance().GetSelectedItem();
  519. SlotsIcon focused_slot = GetFocusedSlotsIcon();
  520. EntityAI focused_item = focused_slot.GetEntity();
  521. if( focused_slot.IsReserved() || focused_item != selected_item )
  522. {
  523. if( selected_item )
  524. {
  525. if( selected_item.GetInventory().CanRemoveEntity() )
  526. {
  527. if (m_Player.GetInventory().CanAddAttachmentEx( selected_item, focused_slot.GetSlotID() ))
  528. {
  529. player.PredictiveTakeEntityToTargetAttachmentEx( m_Player, selected_item, focused_slot.GetSlotID());
  530. //m_Player.PredictiveTakeEntityAsAttachmentEx( selected_item, focused_slot.GetSlotID() );
  531. ItemManager.GetInstance().SetSelectedItemEx(null, null, null);
  532. return true;
  533. }
  534. else if (m_Player.GetInventory().CanAddAttachment( selected_item ))
  535. {
  536. player.PredictiveTakeEntityToTargetAttachment( m_Player, selected_item );
  537. //m_Player.PredictiveTakeEntityToInventory( FindInventoryLocationType.ATTACHMENT, selected_item );
  538. ItemManager.GetInstance().SetSelectedItemEx(null, null, null);
  539. return true;
  540. }
  541. }
  542. }
  543. else
  544. {
  545. if( focused_item && focused_item.GetInventory().CanRemoveEntity() )
  546. {
  547. EntityAI item_in_hands = player.GetHumanInventory().GetEntityInHands();
  548. if( item_in_hands && item_in_hands.GetInventory().CanRemoveEntity() )
  549. {
  550. if( GameInventory.CanSwapEntitiesEx( item_in_hands, focused_item ) )
  551. {
  552. player.PredictiveSwapEntities( item_in_hands, focused_item );
  553. return true;
  554. }
  555. else
  556. {
  557. InventoryLocation il_hands_dst = new InventoryLocation;
  558. if( player.GetHumanInventory().FindFreeLocationFor( item_in_hands, FindInventoryLocationType.ANY, il_hands_dst ) )
  559. {
  560. InventoryMode invMode = InventoryMode.PREDICTIVE;
  561. if ( player.NeedInventoryJunctureFromServer(item_in_hands, item_in_hands.GetHierarchyParent(), il_hands_dst.GetParent()) || GetGame().GetPlayer().NeedInventoryJunctureFromServer(focused_item, focused_item.GetHierarchyParent(), GetGame().GetPlayer()) )
  562. invMode = InventoryMode.JUNCTURE;
  563. player.GetHumanInventory().ForceSwapEntities( invMode, focused_item, item_in_hands, il_hands_dst );
  564. return true;
  565. }
  566. }
  567. }
  568. else
  569. {
  570. if( player.GetHumanInventory().CanAddEntityInHands( focused_item ) )
  571. {
  572. player.PredictiveTakeEntityToHands( focused_item );
  573. return true;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. return false;
  581. }
  582. void SetPlayer(PlayerBase player)
  583. {
  584. if (m_Player)
  585. {
  586. m_Player.GetOnItemAttached().Remove(ItemAttached);
  587. m_Player.GetOnItemDetached().Remove(ItemDetached);
  588. m_Player.GetOnAttachmentSetLock().Remove(OnAttachmentReservationSet);
  589. m_Player.GetOnAttachmentReleaseLock().Remove(OnAttachmentReservationRelease);
  590. }
  591. m_Player = player;
  592. if (m_Player)
  593. {
  594. m_Player.GetOnItemAttached().Insert(ItemAttached);
  595. m_Player.GetOnItemDetached().Insert(ItemDetached);
  596. m_Player.GetOnAttachmentSetLock().Insert(OnAttachmentReservationSet);
  597. m_Player.GetOnAttachmentReleaseLock().Insert(OnAttachmentReservationRelease);
  598. }
  599. //START - InitGhostSlots
  600. string config_path_ghosts_slots = "CfgVehicles SurvivorBase InventoryEquipment playerSlots";
  601. ref array<string> player_ghosts_slots = new array<string>;
  602. GetGame().ConfigGetTextArray( config_path_ghosts_slots, player_ghosts_slots );
  603. for ( int i = 0; i < player_ghosts_slots.Count(); i++ )
  604. {
  605. string slot_name = player_ghosts_slots.Get ( i );
  606. int column = i % ITEMS_IN_ROW;
  607. int row = i / ITEMS_IN_ROW;
  608. SlotsIcon icon = GetSlotsIcon( row, column );
  609. icon.SetSlotParent(m_Player);
  610. icon.GetMainWidget().Show( true );
  611. icon.Clear();
  612. GetGame().ConfigGetText( "CfgSlots" + " " + slot_name + " name", slot_name );
  613. EntityAI item = m_Player.GetInventory().FindAttachment( InventorySlots.GetSlotIdFromString( slot_name ) );
  614. if( item )
  615. {
  616. ItemAttached( item, slot_name );
  617. }
  618. }
  619. }
  620. override void OnDropReceivedFromHeader( Widget w, int x, int y, Widget receiver )
  621. {
  622. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( GetItemPreviewWidget( w ) );
  623. if( !ipw.IsInherited( ItemPreviewWidget ) )
  624. {
  625. return;
  626. }
  627. EntityAI item = ipw.GetItem();
  628. if( !item )
  629. {
  630. return;
  631. }
  632. if( !item.GetInventory().CanRemoveEntity() )
  633. return;
  634. if( m_Player.GetInventory().CanAddEntityToInventory( item, FindInventoryLocationType.CARGO | FindInventoryLocationType.ATTACHMENT ) && ( !m_Player.GetInventory().HasEntityInInventory( item ) ) )
  635. {
  636. m_Player.PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO | FindInventoryLocationType.ATTACHMENT, item );
  637. }
  638. }
  639. ItemPreviewWidget GetItemPreviewWidget( Widget w )
  640. {
  641. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget("Render") );
  642. if( !ipw )
  643. {
  644. string name = w.GetName();
  645. name.Replace( "PanelWidget", "Render" );
  646. ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  647. }
  648. if( !ipw )
  649. {
  650. ipw = ItemPreviewWidget.Cast( w );
  651. }
  652. return ipw;
  653. }
  654. override void DraggingOverHeader( Widget w, int x, int y, Widget receiver )
  655. {
  656. if( w == null )
  657. {
  658. return;
  659. }
  660. ItemPreviewWidget ipw = GetItemPreviewWidget( w );
  661. if( !ipw || !ipw.IsInherited( ItemPreviewWidget ) )
  662. {
  663. return;
  664. }
  665. EntityAI item = ipw.GetItem();
  666. if( !item )
  667. {
  668. return;
  669. }
  670. if( m_Player.GetInventory().CanAddEntityToInventory( item, FindInventoryLocationType.CARGO | FindInventoryLocationType.ATTACHMENT ) && !m_Player.GetInventory().HasEntityInInventory( item ) )
  671. {
  672. ItemManager.GetInstance().HideDropzones();
  673. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  674. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  675. }
  676. else
  677. {
  678. ItemManager.GetInstance().HideDropzones();
  679. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  680. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  681. //ItemManager.GetInstance().ShowSourceDropzone( item );
  682. }
  683. }
  684. override void DraggingOver( Widget w, int x, int y, Widget receiver )
  685. {
  686. if( w == null )
  687. {
  688. return;
  689. }
  690. EntityAI receiver_item;
  691. bool is_reserved = false;
  692. SlotsIcon slots_icon;
  693. receiver.GetUserData(slots_icon);
  694. if(slots_icon)
  695. {
  696. receiver_item = slots_icon.GetEntity();
  697. is_reserved = slots_icon.IsReserved();
  698. }
  699. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( GetItemPreviewWidget( w ) );
  700. if( !ipw || !ipw.IsInherited( ItemPreviewWidget ) )
  701. {
  702. return;
  703. }
  704. EntityAI item = ipw.GetItem();
  705. if( !item )
  706. {
  707. return;
  708. }
  709. if( receiver_item && !is_reserved )
  710. {
  711. if( GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  712. {
  713. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  714. ItemManager.GetInstance().HideDropzones();
  715. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  716. return;
  717. }
  718. else if( receiver_item.GetInventory().CanAddAttachment(item) || receiver_item.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && !receiver_item.GetInventory().HasEntityInCargo( item ))
  719. {
  720. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  721. ItemManager.GetInstance().HideDropzones();
  722. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  723. return;
  724. }
  725. else if( ( ItemBase.Cast( receiver_item ) ).CanBeCombined( ItemBase.Cast( item ) ) )
  726. {
  727. ColorManager.GetInstance().SetColor( w, ColorManager.COMBINE_COLOR );
  728. ItemManager.GetInstance().HideDropzones();
  729. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  730. return;
  731. }
  732. }
  733. if( item.GetInventory().CanRemoveEntity() )
  734. {
  735. if( m_Player.GetInventory().CanAddAttachment( item) )
  736. {
  737. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  738. ItemManager.GetInstance().HideDropzones();
  739. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  740. }
  741. else if( m_Player.GetInventory().CanAddEntityToInventory( item, FindInventoryLocationType.CARGO | FindInventoryLocationType.ATTACHMENT ) && ( !m_Player.GetInventory().HasEntityInInventory( item ) ) || m_Player.GetHumanInventory().HasEntityInHands( item ) )
  742. {
  743. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  744. ItemManager.GetInstance().HideDropzones();
  745. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  746. }
  747. else if ( receiver_item && GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  748. {
  749. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  750. ItemManager.GetInstance().HideDropzones();
  751. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  752. }
  753. else
  754. {
  755. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  756. ItemManager.GetInstance().HideDropzones();
  757. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  758. }
  759. }
  760. else
  761. {
  762. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  763. ItemManager.GetInstance().HideDropzones();
  764. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  765. //ItemManager.GetInstance().ShowSourceDropzone( item );
  766. }
  767. }
  768. void OnDropReceivedFromGhostArea( Widget w, int x, int y, Widget receiver )
  769. {
  770. ItemManager.GetInstance().HideDropzones();
  771. ItemManager.GetInstance().SetIsDragging( false );
  772. EntityAI receiver_item;
  773. bool is_reserved = false;
  774. InventoryMenu menu = InventoryMenu.Cast( GetGame().GetUIManager().FindMenu( MENU_INVENTORY ) );
  775. float stack_max;
  776. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( GetItemPreviewWidget( w ) );
  777. SlotsIcon slots_icon;
  778. receiver.GetUserData(slots_icon);
  779. int slot_id = slots_icon.GetSlotID();
  780. receiver_item = slots_icon.GetEntity();
  781. is_reserved = slots_icon.IsReserved();
  782. InventoryLocation il;
  783. if ( !ipw )
  784. {
  785. return;
  786. }
  787. ItemBase item = ItemBase.Cast(ipw.GetItem());
  788. PlayerBase real_player = PlayerBase.Cast( GetGame().GetPlayer() );
  789. if ( !item )
  790. {
  791. return;
  792. }
  793. if ( receiver_item && !is_reserved )
  794. {
  795. if ( GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  796. {
  797. GetGame().GetPlayer().PredictiveSwapEntities( item, receiver_item );
  798. if ( menu )
  799. {
  800. menu.RefreshQuickbar();
  801. }
  802. return;
  803. }
  804. else if ( receiver_item.GetInventory().CanAddAttachment( item ) )
  805. {
  806. GetGame().GetPlayer().PredictiveTakeEntityToTargetAttachment( receiver_item, item );
  807. if ( menu )
  808. {
  809. menu.RefreshQuickbar();
  810. }
  811. return;
  812. }
  813. else if ( receiver_item.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && !receiver_item.GetInventory().HasEntityInCargo( item ) )
  814. {
  815. SplitItemUtils.TakeOrSplitToInventory(real_player, receiver_item,item);
  816. if ( menu )
  817. {
  818. menu.RefreshQuickbar();
  819. }
  820. return;
  821. }
  822. else if ( ( ItemBase.Cast( receiver_item ) ).CanBeCombined( ItemBase.Cast( item ) ) )
  823. {
  824. ( ItemBase.Cast( receiver_item ) ).CombineItemsClient( ItemBase.Cast( item ) );
  825. return;
  826. }
  827. }
  828. if ( m_Player.GetInventory().CanAddAttachmentEx( item, slot_id ) )
  829. {
  830. if (item.GetQuantity() > item.GetTargetQuantityMax(slot_id))
  831. {
  832. item.SplitIntoStackMaxClient( real_player, slot_id );
  833. }
  834. else
  835. {
  836. real_player.PredictiveTakeEntityToTargetAttachmentEx( m_Player, item, slots_icon.GetSlotID() );
  837. }
  838. }
  839. else if ( m_Player.GetInventory().CanAddAttachment( item ) )
  840. {
  841. il = new InventoryLocation;
  842. m_Player.GetInventory().FindFreeLocationFor( item, FindInventoryLocationType.ATTACHMENT, il );
  843. float stackable = item.GetTargetQuantityMax(-1);
  844. if( il.IsValid() )
  845. {
  846. if ( stackable == 0 || stackable >= item.GetQuantity() )
  847. {
  848. real_player.PredictiveTakeEntityToTargetAttachment( il.GetParent(), item );
  849. }
  850. else
  851. {
  852. ItemBase.Cast(item).SplitIntoStackMaxToInventoryLocationClient( il );
  853. }
  854. }
  855. }
  856. else if( m_Player.GetInventory().CanAddEntityToInventory( item, FindInventoryLocationType.CARGO | FindInventoryLocationType.ATTACHMENT ) && ( !m_Player.GetInventory().HasEntityInInventory( item ) ) || m_Player.GetHumanInventory().HasEntityInHands( item ) )
  857. {
  858. SplitItemUtils.TakeOrSplitToInventory(m_Player, m_Player, item);
  859. }
  860. if ( menu )
  861. {
  862. menu.RefreshQuickbar();
  863. }
  864. }
  865. void SwapItemsInOrder( int slot1, int slot2 )
  866. {
  867. EntityAI item1 = m_Player.GetInventory().FindAttachment( slot1 );
  868. EntityAI item2 = m_Player.GetInventory().FindAttachment( slot2 );
  869. if( item1 && item2 )
  870. {
  871. Container cont1 = m_ShowedItems.Get( item1 );
  872. Container cont2 = m_ShowedItems.Get( item2 );
  873. if( cont1 && cont2 )
  874. {
  875. int sort1 = cont1.GetRootWidget().GetSort();
  876. int sort2 = cont2.GetRootWidget().GetSort();
  877. cont1.GetRootWidget().SetSort( sort2 );
  878. cont2.GetRootWidget().SetSort( sort1 );
  879. m_Body.SwapItems( m_Body.Find( cont1 ), m_Body.Find( cont2 ) );
  880. }
  881. }
  882. RecomputeOpenedContainers();
  883. }
  884. override void UpdateInterval()
  885. {
  886. int i;
  887. for ( i = 0; i < m_InventorySlots.Count(); i++ )
  888. {
  889. SlotsIcon icon = m_InventorySlots.GetElement( i );
  890. if ( icon )
  891. {
  892. icon.UpdateInterval();
  893. }
  894. }
  895. for ( i = 0; i < m_ShowedItems.Count(); i++ )
  896. {
  897. Container cont = m_ShowedItems.GetElement( i );
  898. if ( cont )
  899. {
  900. cont.UpdateInterval();
  901. }
  902. }
  903. }
  904. // Call from ToggleWidget
  905. void ToggleContainer( Widget w, EntityAI item )
  906. {
  907. InventoryLocation il = new InventoryLocation;
  908. item.GetInventory().GetCurrentInventoryLocation( il );
  909. SlotsIcon icon = m_InventorySlots.Get( il.GetSlot() );
  910. ClosableContainer c = ClosableContainer.Cast( m_ShowedItems.Get( item ) );
  911. if (c)
  912. {
  913. c.Toggle();
  914. Refresh();
  915. }
  916. }
  917. override bool CanOpenCloseContainerEx(EntityAI focusedEntity)
  918. {
  919. if (focusedEntity)
  920. {
  921. ClosableContainer c = ClosableContainer.Cast( m_ShowedItems.Get( focusedEntity ) );
  922. if (c && c.IsDisplayable())
  923. {
  924. return true;
  925. }
  926. }
  927. return false;
  928. }
  929. // Mouse button UP or Call other fn
  930. void ToggleWidget( Widget w )
  931. {
  932. if( ItemManager.GetInstance().IsDragging() )
  933. {
  934. return;
  935. }
  936. string name = w.GetName();
  937. name.Replace( "PanelWidget", "Render" );
  938. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  939. if( ipw.GetItem() )
  940. {
  941. ToggleContainer( w, ipw.GetItem() );
  942. RecomputeOpenedContainers();
  943. }
  944. Refresh();
  945. }
  946. override void CollapseButtonOnMouseButtonDown(Widget w)
  947. {
  948. super.CollapseButtonOnMouseButtonDown(w);
  949. }
  950. bool IsPlayerEquipmentActive()
  951. {
  952. if( GetFocusedContainer() && ( GetFocusedContainer().IsInherited( ContainerWithCargo ) || GetFocusedContainer().IsInherited( ContainerWithCargoAndAttachments ) ) )
  953. {
  954. return false;
  955. }
  956. return true;
  957. }
  958. override void Refresh()
  959. {
  960. super.Refresh();
  961. m_MainWidget.Update();
  962. m_RootWidget.Update();
  963. UpdateSelectionIcons();
  964. }
  965. override bool OnChildRemove( Widget w, Widget child )
  966. {
  967. Refresh();
  968. return true;
  969. }
  970. override bool OnChildAdd( Widget w, Widget child )
  971. {
  972. Refresh();
  973. return true;
  974. }
  975. }