playercontainer.c 32 KB

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