handscontainer.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. class HandsContainer: Container
  2. {
  3. protected bool m_Hidden;
  4. protected ref HandsHeader m_CollapsibleHeader;
  5. protected ref HandsPreview m_HandsPreview;
  6. protected ref Attachments m_Atts;
  7. protected ref CargoContainer m_CargoGrid;
  8. protected ref map<EntityAI, ref CargoContainer> m_AttachmentCargos;
  9. protected ref map<EntityAI, AttachmentsWrapper> m_AttachmentAttachmentsContainers;
  10. protected ref map<EntityAI, ref Attachments> m_AttachmentAttachments;
  11. protected ref array<int> m_AttachmentSlotsSorted;
  12. protected int m_StaticAttCount = 0;
  13. protected int m_StaticCargoCount = 0;
  14. protected ScrollWidget m_ScrollWidget;
  15. void HandsContainer( LayoutHolder parent )
  16. {
  17. m_AttachmentCargos = new map<EntityAI, ref CargoContainer>;
  18. m_AttachmentAttachmentsContainers = new map<EntityAI, AttachmentsWrapper>;
  19. m_AttachmentAttachments = new map<EntityAI, ref Attachments>;
  20. m_CollapsibleHeader = new HandsHeader( this, "CollapseButtonOnMouseButtonDown" );
  21. GetMainWidget().SetFlags( WidgetFlags.IGNOREPOINTER );
  22. m_MainWidget = m_MainWidget.FindWidget( "body" );
  23. GetMainWidget().SetFlags( WidgetFlags.IGNOREPOINTER );
  24. m_Body = new array<ref LayoutHolder>;
  25. m_HandsPreview = new HandsPreview( this );
  26. ItemManager.GetInstance().SetHandsPreview( m_HandsPreview );
  27. m_Body.Insert( m_HandsPreview );
  28. ItemBase hands_item = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  29. if ( hands_item )
  30. m_HandsPreview.CreateNewIcon( hands_item );
  31. m_ScrollWidget = ScrollWidget.Cast( m_RootWidget.GetParent().GetParent() );
  32. RecomputeOpenedContainers();
  33. }
  34. void ~HandsContainer()
  35. {
  36. if ( m_Entity )
  37. {
  38. m_Entity.GetOnItemAttached().Remove( AttachmentAdded );
  39. m_Entity.GetOnItemDetached().Remove( AttachmentRemoved );
  40. }
  41. m_AttachmentCargos.Clear();
  42. m_AttachmentAttachmentsContainers.Clear();
  43. m_AttachmentAttachments.Clear();
  44. }
  45. void ResetContainer()
  46. {
  47. m_Body.Clear();
  48. m_Body.Insert( m_HandsPreview );
  49. }
  50. override Header GetHeader()
  51. {
  52. return m_CollapsibleHeader;
  53. }
  54. override bool IsLastIndex()
  55. {
  56. return m_ActiveIndex == m_OpenedContainers.Count() - 1;
  57. }
  58. void SetFocusToIndex()
  59. {
  60. int index = m_ActiveIndex - 1;
  61. int attachment_start_index = -1;
  62. int cargo_start_index = -1;
  63. int attachment_end_index = -1;
  64. int cargo_end_index = -1;
  65. if ( m_Atts || m_AttachmentAttachments.Count() > 0 )
  66. {
  67. attachment_start_index = 0;
  68. if ( m_Atts )
  69. attachment_end_index++;
  70. attachment_end_index += m_AttachmentAttachments.Count();
  71. }
  72. if ( m_CargoGrid || m_AttachmentCargos.Count() > 0 )
  73. {
  74. cargo_start_index = attachment_end_index + 1;
  75. if ( m_CargoGrid )
  76. cargo_end_index++;
  77. cargo_end_index += cargo_start_index + m_AttachmentCargos.Count();
  78. }
  79. if ( m_ActiveIndex > m_OpenedContainers.Count() )
  80. {
  81. m_ActiveIndex = m_OpenedContainers.Count();
  82. }
  83. if ( index == -1 )
  84. {
  85. #ifdef PLATFORM_CONSOLE
  86. if ( m_MainWidget.FindAnyWidget("Cursor") )
  87. m_MainWidget.FindAnyWidget("Cursor").Show( true );
  88. #endif
  89. m_ScrollWidget.VScrollToPos01( 0 );
  90. }
  91. else
  92. {
  93. if( index.InRange( 0, attachment_end_index ) )
  94. {
  95. if( m_Atts )
  96. {
  97. if( index == 0 )
  98. {
  99. m_Atts.SetDefaultFocus();
  100. SetFocusedContainer( m_Atts.GetWrapper() );
  101. }
  102. else
  103. {
  104. m_AttachmentAttachments.GetElement( index - 1 ).SetDefaultFocus();
  105. SetFocusedContainer( m_AttachmentAttachments.GetElement( index - 1 ).GetWrapper() );
  106. }
  107. }
  108. else
  109. {
  110. m_AttachmentAttachments.GetElement( index ).SetDefaultFocus();
  111. SetFocusedContainer( m_AttachmentAttachments.GetElement( index ).GetWrapper() );
  112. }
  113. }
  114. else if( index.InRange( cargo_start_index, cargo_end_index ) )
  115. {
  116. if( m_CargoGrid )
  117. {
  118. if( index == cargo_start_index )
  119. {
  120. m_CargoGrid.SetDefaultFocus();
  121. SetFocusedContainer( m_CargoGrid );
  122. }
  123. else
  124. {
  125. m_AttachmentCargos.GetElement( index - 1 - cargo_start_index ).SetDefaultFocus();
  126. SetFocusedContainer( m_AttachmentCargos.GetElement( index - 1 - cargo_start_index ) );
  127. }
  128. }
  129. else
  130. {
  131. m_AttachmentCargos.GetElement( index - cargo_start_index ).SetDefaultFocus();
  132. SetFocusedContainer( m_AttachmentCargos.GetElement( index - cargo_start_index ) );
  133. }
  134. }
  135. if( m_MainWidget.FindAnyWidget("Cursor") )
  136. m_MainWidget.FindAnyWidget("Cursor").Show( false );
  137. ScrollToActiveContainer( GetFocusedContainer() );
  138. }
  139. }
  140. void ScrollToActiveContainer( Container active_container )
  141. {
  142. if( !active_container )
  143. return;
  144. float x, y, y_s;
  145. m_ScrollWidget.GetScreenPos( x, y );
  146. m_ScrollWidget.GetScreenSize( x, y_s );
  147. float amount;
  148. float next_pos = active_container.GetFocusedContainerYScreenPos( true ) + active_container.GetFocusedContainerHeight( true );
  149. if( next_pos > ( y + y_s ) )
  150. {
  151. amount = y + active_container.GetFocusedContainerYScreenPos( true );
  152. m_ScrollWidget.VScrollToPos( m_ScrollWidget.GetVScrollPos() + active_container.GetFocusedContainerHeight( true ) + 2 );
  153. }
  154. else if( active_container.GetFocusedContainerYScreenPos( true ) < y )
  155. {
  156. amount = active_container.GetFocusedContainerYScreenPos( true ) - y;
  157. m_ScrollWidget.VScrollToPos( m_ScrollWidget.GetVScrollPos() + amount - 2 );
  158. }
  159. }
  160. override void Insert( LayoutHolder container, int pos = -1, bool immedUpdate = true )
  161. {
  162. super.Insert( container, pos, immedUpdate );
  163. }
  164. override bool TransferItem()
  165. {
  166. if (m_ActiveIndex == 0)
  167. {
  168. if (CanTakeToInventory())
  169. {
  170. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  171. if (item_in_hands && GetGame().GetPlayer().GetHumanInventory().CanRemoveEntityInHands() && !GetGame().GetPlayer().GetInventory().HasInventoryReservation(item_in_hands, null))
  172. {
  173. if (GetGame().GetPlayer().PredictiveTakeEntityToInventory( FindInventoryLocationType.CARGO, item_in_hands ))
  174. {
  175. m_MainWidget.FindAnyWidget("Cursor").Show( false );
  176. m_MainWidget.FindAnyWidget("hands_preview_root").SetAlpha( 0.7 );
  177. return true;
  178. }
  179. }
  180. }
  181. }
  182. else if (GetFocusedContainer())
  183. {
  184. return GetFocusedContainer().TransferItem();
  185. }
  186. return false;
  187. }
  188. override bool SplitItem()
  189. {
  190. if (CanSplit())
  191. {
  192. if (m_ActiveIndex == 0)
  193. {
  194. ItemBase item_in_hands = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  195. if ( item_in_hands )
  196. {
  197. if ( item_in_hands.CanBeSplit() )
  198. {
  199. item_in_hands.OnRightClick();
  200. }
  201. }
  202. }
  203. else if( GetFocusedContainer() )
  204. {
  205. return GetFocusedContainer().SplitItem();
  206. }
  207. }
  208. return false;
  209. }
  210. override bool EquipItem()
  211. {
  212. if (CanEquip())
  213. {
  214. if (m_ActiveIndex == 0)
  215. {
  216. ItemBase item_in_hands = ItemBase.Cast( GetGame().GetPlayer().GetHumanInventory().GetEntityInHands() );
  217. if ( item_in_hands )
  218. {
  219. if ( GetGame().GetPlayer().GetHumanInventory().CanRemoveEntityInHands() && !GetGame().GetPlayer().GetInventory().HasInventoryReservation(item_in_hands, null) )
  220. {
  221. bool res = GetGame().GetPlayer().PredictiveTakeOrSwapAttachment( item_in_hands );
  222. if(!res)
  223. {
  224. res = GetGame().GetPlayer().PredictiveTakeEntityToInventory(FindInventoryLocationType.ATTACHMENT, item_in_hands);
  225. }
  226. if(res)
  227. {
  228. m_MainWidget.FindAnyWidget("Cursor").Show( false );
  229. m_MainWidget.FindAnyWidget("hands_preview_root").SetAlpha( 0.7 );
  230. return true;
  231. }
  232. }
  233. }
  234. }
  235. else if (GetFocusedContainer())
  236. {
  237. return GetFocusedContainer().EquipItem();
  238. }
  239. }
  240. return false;
  241. }
  242. override void SetActive( bool active )
  243. {
  244. super.SetActive(active);
  245. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  246. EntityAI item_in_hands = player.GetHumanInventory().GetEntityInHands();
  247. if( item_in_hands )
  248. {
  249. if( m_MainWidget.FindAnyWidget( "Cursor" ) )
  250. {
  251. m_MainWidget.FindAnyWidget( "Cursor" ).Show( active );
  252. }
  253. if( active )
  254. {
  255. float x, y;
  256. GetMainWidget().GetScreenPos( x, y );
  257. PrepareOwnedTooltip( item_in_hands, -1, y );
  258. }
  259. }
  260. else
  261. {
  262. if (active)
  263. m_MainWidget.FindAnyWidget( "hands_preview_root" ).SetAlpha( 0.85 );
  264. else
  265. m_MainWidget.FindAnyWidget( "hands_preview_root" ).SetAlpha( 0.7 );
  266. }
  267. }
  268. override void SetLastActive()
  269. {
  270. super.SetLastActive();
  271. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  272. EntityAI item_in_hands = player.GetHumanInventory().GetEntityInHands();
  273. if( item_in_hands )
  274. {
  275. if ( m_ActiveIndex == 0)
  276. {
  277. if( m_MainWidget.FindAnyWidget( "Cursor" ) )
  278. {
  279. m_MainWidget.FindAnyWidget( "Cursor" ).Show( true );
  280. }
  281. float x, y;
  282. GetMainWidget().GetScreenPos( x, y );
  283. PrepareOwnedTooltip( item_in_hands, x, y );
  284. }
  285. }
  286. else
  287. {
  288. m_MainWidget.FindAnyWidget( "hands_preview_root" ).SetAlpha( 0.85 );
  289. }
  290. }
  291. override void Refresh()
  292. {
  293. super.Refresh();
  294. SetActive( m_IsActive );
  295. }
  296. override bool IsItemActive()
  297. {
  298. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  299. ItemBase item = ItemBase.Cast( player.GetHumanInventory().GetEntityInHands() );
  300. if( item == null )
  301. {
  302. return false;
  303. }
  304. return !IsEmpty() && ( !QuantityConversions.HasItemQuantity( item ) || ( QuantityConversions.HasItemQuantity( item ) && !item.CanBeSplit() ) );
  305. }
  306. override bool IsItemWithQuantityActive()
  307. {
  308. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  309. ItemBase item = ItemBase.Cast( player.GetHumanInventory().GetEntityInHands() );
  310. if( item == null )
  311. {
  312. return false;
  313. }
  314. return !IsEmpty() && QuantityConversions.HasItemQuantity( item ) && item.CanBeSplit();
  315. }
  316. override bool IsEmpty()
  317. {
  318. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  319. return player.GetHumanInventory().GetEntityInHands() == null;
  320. }
  321. override bool TransferItemToVicinity()
  322. {
  323. if (CanDrop())
  324. {
  325. if (m_ActiveIndex == 0)
  326. {
  327. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  328. ItemBase item_in_hands = ItemBase.Cast(player.GetHumanInventory().GetEntityInHands());
  329. if ( item_in_hands && player.CanDropEntity( item_in_hands ) && GetGame().GetPlayer().GetHumanInventory().CanRemoveEntityInHands() && !player.GetInventory().HasInventoryReservation(item_in_hands, null) )
  330. {
  331. if ( item_in_hands.GetTargetQuantityMax() < item_in_hands.GetQuantity() )
  332. item_in_hands.SplitIntoStackMaxClient( null, -1 );
  333. else
  334. player.PhysicalPredictiveDropItem( item_in_hands );
  335. m_MainWidget.FindAnyWidget("Cursor").Show( false );
  336. m_MainWidget.FindAnyWidget("hands_preview_root").SetAlpha( 0.7 );
  337. return true;
  338. }
  339. }
  340. else if ( GetFocusedContainer() )
  341. {
  342. return GetFocusedContainer().TransferItemToVicinity();
  343. }
  344. }
  345. return false;
  346. }
  347. override bool InspectItem()
  348. {
  349. if(m_ActiveIndex == 0)
  350. {
  351. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  352. EntityAI item_in_hands = player.GetHumanInventory().GetEntityInHands();
  353. if( item_in_hands )
  354. {
  355. InspectItem( item_in_hands );
  356. return true;
  357. }
  358. }
  359. else if( GetFocusedContainer() )
  360. {
  361. return GetFocusedContainer().InspectItem();
  362. }
  363. return false;
  364. }
  365. override bool OnSelectButton()
  366. {
  367. if(m_ActiveIndex == 0)
  368. {
  369. if (ItemManager.GetInstance().IsMicromanagmentMode())
  370. {
  371. EntityAI selectedItem = ItemManager.GetInstance().GetSelectedItem();
  372. if( selectedItem == GetFocusedItem() )
  373. {
  374. Inventory.GetInstance().DisableMicromanagement();
  375. return true;
  376. }
  377. else
  378. {
  379. return Select();
  380. }
  381. }
  382. else
  383. {
  384. Inventory.GetInstance().EnableMicromanagement();
  385. return true;
  386. }
  387. }
  388. else
  389. {
  390. if( GetFocusedContainer() )
  391. {
  392. return GetFocusedContainer().OnSelectButton();
  393. }
  394. }
  395. return false;
  396. }
  397. EntityAI GetItemPreviewItem( Widget w )
  398. {
  399. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  400. if( !ipw )
  401. {
  402. string name = w.GetName();
  403. name.Replace( "PanelWidget", "Render" );
  404. ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  405. }
  406. if( !ipw )
  407. {
  408. ipw = ItemPreviewWidget.Cast( w );
  409. }
  410. if( !ipw )
  411. {
  412. return null;
  413. }
  414. return ipw.GetItem();
  415. }
  416. override void DraggingOverHeader( Widget w, int x, int y, Widget receiver )
  417. {
  418. if( w == null )
  419. {
  420. return;
  421. }
  422. EntityAI receiver_item;
  423. EntityAI slot_owner;
  424. int slot_id = -1;
  425. string name = w.GetName();
  426. name.Replace( "PanelWidget", "Render" );
  427. ItemPreviewWidget w_ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  428. if( w_ipw == null )
  429. {
  430. w_ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  431. }
  432. if( w_ipw == null )
  433. {
  434. return;
  435. }
  436. SlotsIcon slots_icon;
  437. receiver.GetUserData(slots_icon);
  438. if(slots_icon)
  439. {
  440. receiver_item = slots_icon.GetEntity();
  441. slot_id = slots_icon.GetSlotID();
  442. slot_owner = slots_icon.GetSlotParent();
  443. }
  444. PlayerBase p = PlayerBase.Cast( GetGame().GetPlayer() );
  445. InventoryItem receiver_entity = InventoryItem.Cast( p.GetHumanInventory().GetEntityInHands() );
  446. InventoryItem w_entity = InventoryItem.Cast( w_ipw.GetItem() );
  447. if( !w_entity )
  448. {
  449. return;
  450. }
  451. if( receiver_entity )
  452. {
  453. if( receiver_item )
  454. {
  455. ItemBase receiver_itemIB = ItemBase.Cast( receiver_item );
  456. ItemBase itemIB = ItemBase.Cast( receiver_item );
  457. if( receiver_itemIB && itemIB && receiver_itemIB.CanBeCombined( itemIB ) )
  458. {
  459. ItemManager.GetInstance().HideDropzones();
  460. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  461. {
  462. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  463. }
  464. else
  465. {
  466. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  467. }
  468. ColorManager.GetInstance().SetColor( w, ColorManager.COMBINE_COLOR );
  469. }
  470. else
  471. {
  472. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  473. Magazine mag = Magazine.Cast(receiver_item);
  474. Weapon_Base wpn = Weapon_Base.Cast(receiver_item.GetHierarchyParent());
  475. if( wpn && mag )
  476. {
  477. if( player.GetWeaponManager().CanSwapMagazine( wpn, Magazine.Cast(w_entity) ) )
  478. {
  479. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  480. if( w_entity.GetHierarchyRootPlayer() == player )
  481. {
  482. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  483. }
  484. else
  485. {
  486. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  487. }
  488. }
  489. else
  490. {
  491. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  492. ItemManager.GetInstance().ShowSourceDropzone( w_entity );
  493. }
  494. }
  495. else
  496. {
  497. if( GameInventory.CanSwapEntitiesEx( receiver_item, w_entity ) )
  498. {
  499. ItemManager.GetInstance().HideDropzones();
  500. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  501. {
  502. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  503. }
  504. else
  505. {
  506. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  507. }
  508. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  509. }
  510. else if( receiver_itemIB.GetInventory().CanAddAttachment( w_entity ) )
  511. {
  512. ItemManager.GetInstance().HideDropzones();
  513. if( receiver_itemIB.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  514. {
  515. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  516. }
  517. else
  518. {
  519. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  520. }
  521. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  522. }
  523. }
  524. }
  525. }
  526. else if( slot_owner && slot_owner.GetInventory().CanAddAttachment( w_entity ) )
  527. {
  528. ItemManager.GetInstance().HideDropzones();
  529. ItemManager.GetInstance().GetCenterDropzone().SetAlpha( 1 );
  530. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  531. }
  532. else if( receiver_entity.GetInventory().CanAddAttachment( w_entity ) )
  533. {
  534. ItemManager.GetInstance().HideDropzones();
  535. if( receiver_entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  536. {
  537. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  538. }
  539. else
  540. {
  541. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  542. }
  543. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  544. }
  545. else
  546. {
  547. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  548. ItemManager.GetInstance().ShowSourceDropzone( w_entity );
  549. }
  550. }
  551. else
  552. {
  553. /*if( m_HandsIcon )
  554. {
  555. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  556. ItemManager.GetInstance().HideDropzones();
  557. ItemManager.GetInstance().GetCenterDropzone().SetAlpha( 1 );
  558. }
  559. else
  560. {*/
  561. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  562. ItemManager.GetInstance().ShowSourceDropzone( w_entity );
  563. //}
  564. }
  565. }
  566. void OnDropReceivedFromHeader2( Widget w, int x, int y, Widget receiver )
  567. {
  568. TakeAsAttachment( w, receiver );
  569. }
  570. void OnPerformCombination(int combinationFlags)
  571. {
  572. PlayerBase m_player = PlayerBase.Cast( GetGame().GetPlayer() );
  573. if ( m_am_entity1 == null || m_am_entity2 == null ) return;
  574. if ( combinationFlags == InventoryCombinationFlags.NONE ) return;
  575. if ( combinationFlags & InventoryCombinationFlags.LOAD_CHAMBER )
  576. {
  577. Magazine mag;
  578. if ( Class.CastTo(mag, m_am_entity2 ) )
  579. {
  580. m_player.GetWeaponManager().LoadBullet( mag );
  581. return;
  582. }
  583. }
  584. if ( combinationFlags & InventoryCombinationFlags.ADD_AS_ATTACHMENT )
  585. {
  586. if( m_Atts )
  587. {
  588. m_Body.RemoveItem( m_Atts.GetWrapper() );
  589. delete m_Atts;
  590. }
  591. if( m_am_entity2.GetSlotsCountCorrect() > 0 )
  592. {
  593. m_Atts = new Attachments( this, m_am_entity2 );
  594. m_Atts.InitAttachmentGrid( 1 );
  595. }
  596. Refresh();
  597. m_Parent.Refresh();
  598. Weapon_Base wpn1;
  599. Magazine mag1;
  600. if ( Class.CastTo(wpn1, m_am_entity1 ) && Class.CastTo(mag1, m_am_entity2 ) )
  601. {
  602. if( m_player.GetWeaponManager().CanAttachMagazine(wpn1, mag1) )
  603. {
  604. m_player.GetWeaponManager().AttachMagazine(mag1);
  605. return;
  606. }
  607. }
  608. SplitItemUtils.TakeOrSplitToInventory(m_player, m_am_entity1, m_am_entity2);
  609. //m_player.PredictiveTakeEntityToTargetAttachment( m_am_entity1, m_am_entity2 );
  610. }
  611. if ( combinationFlags & InventoryCombinationFlags.ADD_AS_CARGO )
  612. {
  613. m_player.PredictiveTakeEntityToTargetCargo( m_am_entity1, m_am_entity2 );
  614. }
  615. if ( combinationFlags & InventoryCombinationFlags.SWAP )
  616. {
  617. Magazine swapmag1;
  618. Magazine swapmag2;
  619. if (Class.CastTo(swapmag1, m_am_entity1) && Class.CastTo(swapmag2, m_am_entity2 ) )
  620. {
  621. if( m_player.GetWeaponManager().CanSwapMagazine( Weapon_Base.Cast( swapmag1.GetParent() ), Magazine.Cast( swapmag2 ) ) )
  622. {
  623. m_player.GetWeaponManager().SwapMagazine(swapmag2);
  624. return;
  625. }
  626. if( m_player.GetWeaponManager().CanSwapMagazine(Weapon_Base.Cast( swapmag2.GetParent() ), Magazine.Cast( swapmag1 )) )
  627. {
  628. m_player.GetWeaponManager().SwapMagazine(swapmag1);
  629. return;
  630. }
  631. }
  632. if ( !m_player.PredictiveSwapEntities( m_am_entity1, m_am_entity2 ) && m_player.GetHumanInventory().CanAddEntityInHands( m_am_entity1 ) )
  633. {
  634. m_player.PredictiveTakeEntityToHands( m_am_entity1 );
  635. }
  636. }
  637. if ( combinationFlags & InventoryCombinationFlags.TAKE_TO_HANDS )
  638. {
  639. if ( m_Atts )
  640. {
  641. m_Body.RemoveItem( m_Atts.GetWrapper() );
  642. delete m_Atts;
  643. }
  644. if ( m_am_entity2.GetSlotsCountCorrect() > 0 )
  645. {
  646. m_Atts = new Attachments( this, m_am_entity2 );
  647. m_Atts.InitAttachmentGrid( 1 );
  648. }
  649. Refresh();
  650. m_Parent.Refresh();
  651. if ( m_player.GetHumanInventory().CanAddEntityInHands( m_am_entity2 ) )
  652. {
  653. m_player.PredictiveTakeEntityToHands( m_am_entity2 );
  654. }
  655. }
  656. }
  657. void ShowActionMenuCombine( EntityAI entity1, EntityAI entity2, int combinationFlags )
  658. {
  659. if ( entity1 && entity2 ) PrintString( "Showing action menu for " + entity1.GetDisplayName() + " and " + entity2.GetDisplayName() );
  660. ContextMenu cmenu = ContextMenu.Cast(GetGame().GetUIManager().GetMenu().GetContextMenu());
  661. m_am_entity1 = entity1;
  662. m_am_entity2 = entity2;
  663. cmenu.Hide();
  664. cmenu.Clear();
  665. if( combinationFlags & InventoryCombinationFlags.COMBINE_QUANTITY2 )
  666. {
  667. ItemBase entity = ItemBase.Cast( entity1 );
  668. entity.CombineItemsClient( ItemBase.Cast( entity2 ) );
  669. return;
  670. }
  671. if ( entity1 == null || entity2 == null ) return;
  672. if ( combinationFlags == InventoryCombinationFlags.NONE ) return;
  673. if ( combinationFlags & InventoryCombinationFlags.ADD_AS_ATTACHMENT )
  674. {
  675. cmenu.Add( "#inv_context_add_as_attachment", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.ADD_AS_ATTACHMENT ) );
  676. }
  677. if ( combinationFlags & InventoryCombinationFlags.LOAD_CHAMBER )
  678. {
  679. cmenu.Add( "#inv_context_load_chamber", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.LOAD_CHAMBER ) );
  680. }
  681. if ( combinationFlags & InventoryCombinationFlags.ADD_AS_CARGO )
  682. {
  683. cmenu.Add( "#inv_context_add_as_cargo", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.ADD_AS_CARGO ) );
  684. }
  685. if ( combinationFlags & InventoryCombinationFlags.SWAP )
  686. {
  687. cmenu.Add( "#inv_context_swap", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.SWAP ) );
  688. }
  689. if ( combinationFlags & InventoryCombinationFlags.TAKE_TO_HANDS )
  690. {
  691. cmenu.Add( "#inv_context_take_to_hands", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.TAKE_TO_HANDS ) );
  692. }
  693. if ( combinationFlags & InventoryCombinationFlags.COMBINE_QUANTITY2 )
  694. {
  695. cmenu.Add( "#inv_context_combine", this, "OnPerformCombination", new Param1<int>( InventoryCombinationFlags.COMBINE_QUANTITY2 ) );
  696. }
  697. int m_am_pos_x, m_am_pos_y;
  698. GetMousePos( m_am_pos_x, m_am_pos_y );
  699. m_am_pos_x -= 5;
  700. m_am_pos_y -= 5;
  701. cmenu.Show( m_am_pos_x, m_am_pos_y );
  702. }
  703. override void OnDropReceivedFromHeader( Widget w, int x, int y, Widget receiver )
  704. {
  705. TakeAsAttachment( w, receiver );
  706. }
  707. int GetCombinationFlags( EntityAI entity1, EntityAI entity2 )
  708. {
  709. int flags = 0;
  710. PlayerBase m_player = PlayerBase.Cast( GetGame().GetPlayer() );
  711. EntityAI entity_in_hands = m_player.GetHumanInventory().GetEntityInHands();
  712. if (!entity1 || !entity2) return flags;
  713. Magazine swapmag1;
  714. Magazine swapmag2;
  715. bool skipSwap = false;
  716. if (Class.CastTo(swapmag1, entity1) && Class.CastTo(swapmag2, entity2))
  717. {
  718. Weapon_Base parentWpn;
  719. if (Class.CastTo(parentWpn, swapmag1.GetHierarchyParent()))
  720. {
  721. skipSwap = true;
  722. if (m_player.GetWeaponManager().CanSwapMagazine(parentWpn,swapmag2))
  723. flags = flags | InventoryCombinationFlags.SWAP;
  724. }
  725. if (Class.CastTo(parentWpn, swapmag2.GetHierarchyParent()))
  726. {
  727. skipSwap = true;
  728. if (m_player.GetWeaponManager().CanSwapMagazine(parentWpn,swapmag1))
  729. flags = flags | InventoryCombinationFlags.SWAP;
  730. }
  731. }
  732. if ( !skipSwap )
  733. {
  734. if (entity1 == m_player.GetHumanInventory().GetEntityInHands() ) flags = flags | InventoryCombinationFlags.TAKE_TO_HANDS;
  735. else if (GameInventory.CanSwapEntitiesEx( entity1, entity2 ))
  736. {
  737. if (!entity1.IsInherited( ZombieBase ) && !entity1.IsInherited( Car ))
  738. {
  739. flags = flags | InventoryCombinationFlags.SWAP;
  740. }
  741. }
  742. }
  743. if (entity1.IsInherited( ItemBase) && entity2.IsInherited( ItemBase ))
  744. {
  745. ItemBase ent1 = ItemBase.Cast( entity1 );
  746. if (ent1.CanBeCombined( ItemBase.Cast( entity2 ) )) flags = flags | InventoryCombinationFlags.COMBINE_QUANTITY2;
  747. }
  748. else if (entity1.GetInventory().CanAddAttachment( entity2 ))
  749. {
  750. if (!entity1.IsInherited( ZombieBase ) && !entity1.IsInherited( Car ) && !entity2.IsInherited( ZombieBase ) && !entity2.IsInherited( Car ))
  751. {
  752. flags = flags | InventoryCombinationFlags.ADD_AS_ATTACHMENT;
  753. }
  754. }
  755. if (!entity1.GetInventory().HasEntityInInventory(entity2) && entity1.GetInventory().CanAddEntityInCargo( entity2, entity2.GetInventory().GetFlipCargo() )) flags = flags | InventoryCombinationFlags.ADD_AS_CARGO;
  756. if (entity1 == m_player.GetHumanInventory().GetEntityInHands() || entity2 == m_player.GetHumanInventory().GetEntityInHands())
  757. {
  758. ActionManagerClient amc;
  759. Class.CastTo(amc, m_player.GetActionManager());
  760. if (entity1 == m_player.GetHumanInventory().GetEntityInHands())
  761. {
  762. if (amc.CanSetActionFromInventory( ItemBase.Cast( entity1 ), ItemBase.Cast( entity2 ) ))
  763. {
  764. flags = flags | InventoryCombinationFlags.SET_ACTION;
  765. }
  766. }
  767. else
  768. {
  769. if (amc.CanSetActionFromInventory( ItemBase.Cast( entity2 ), ItemBase.Cast( entity1 ) ))
  770. {
  771. flags = flags | InventoryCombinationFlags.SET_ACTION;
  772. }
  773. }
  774. }
  775. if (GetRecipeCount( true, entity1, entity2 ) > 0)
  776. {
  777. flags = flags | InventoryCombinationFlags.RECIPE_ANYWHERE;
  778. }
  779. return flags;
  780. }
  781. int GetRecipeCount( bool recipe_anywhere, EntityAI entity1, EntityAI entity2 )
  782. {
  783. PluginRecipesManager plugin_recipes_manager = PluginRecipesManager.Cast( GetPlugin( PluginRecipesManager ) );
  784. return plugin_recipes_manager.GetValidRecipes( ItemBase.Cast( entity1 ), ItemBase.Cast( entity2 ), null, PlayerBase.Cast( GetGame().GetPlayer() ) );
  785. }
  786. void RemoveItem()
  787. {
  788. m_Entity = null;
  789. m_CollapsibleHeader.ShowCollapseButtons( false, true );
  790. }
  791. void DestroyAtt()
  792. {
  793. if( m_Atts )
  794. {
  795. m_Body.RemoveItem( m_Atts.GetWrapper() );
  796. delete m_Atts;
  797. if( m_Entity )
  798. {
  799. m_Entity.GetOnItemAttached().Remove( AttachmentAdded );
  800. m_Entity.GetOnItemDetached().Remove( AttachmentRemoved );
  801. }
  802. }
  803. if( m_AttachmentAttachmentsContainers )
  804. {
  805. foreach( EntityAI ent, AttachmentsWrapper att : m_AttachmentAttachmentsContainers )
  806. {
  807. m_Body.RemoveItem( att );
  808. m_AttachmentAttachments.Remove( ent );
  809. delete att;
  810. }
  811. }
  812. m_AttachmentAttachmentsContainers.Clear();
  813. Refresh();
  814. m_Parent.Refresh();
  815. RecomputeOpenedContainers();
  816. }
  817. override void UnfocusGrid()
  818. {
  819. if( m_Atts )
  820. {
  821. m_Atts.UnfocusAll();
  822. }
  823. if( m_CargoGrid )
  824. {
  825. m_CargoGrid.UnfocusAll();
  826. }
  827. if( m_AttachmentCargos )
  828. {
  829. foreach( EntityAI e1, CargoContainer cargo : m_AttachmentCargos )
  830. {
  831. cargo.UnfocusAll();
  832. }
  833. }
  834. if( m_AttachmentCargos )
  835. {
  836. foreach( EntityAI e2, Attachments att : m_AttachmentAttachments )
  837. {
  838. att.UnfocusAll();
  839. }
  840. }
  841. }
  842. override void UnfocusAll()
  843. {
  844. UnfocusGrid();
  845. }
  846. void DestroyCargo()
  847. {
  848. if( m_CargoGrid )
  849. {
  850. m_Body.RemoveItem( m_CargoGrid );
  851. delete m_CargoGrid;
  852. }
  853. if( m_AttachmentCargos )
  854. {
  855. foreach( CargoContainer cargo : m_AttachmentCargos )
  856. {
  857. m_Body.RemoveItem( cargo );
  858. delete cargo;
  859. }
  860. }
  861. m_AttachmentCargos.Clear();
  862. Refresh();
  863. m_Parent.Refresh();
  864. RecomputeOpenedContainers();
  865. }
  866. void DropReceived( Widget w, int x, int y, CargoContainer cargo )
  867. {
  868. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  869. EntityAI item_in_hands = player.GetHumanInventory().GetEntityInHands();
  870. EntityAI item = GetItemPreviewItem( w );
  871. if( !item )
  872. {
  873. return;
  874. }
  875. int idx = 0;
  876. int c_x, c_y;
  877. EntityAI target_entity;
  878. CargoBase target_cargo;
  879. if( cargo == m_CargoGrid )
  880. {
  881. target_entity = m_Entity;
  882. target_cargo = m_Entity.GetInventory().GetCargo();
  883. #ifdef PLATFORM_CONSOLE
  884. if( m_CargoGrid.HasItem( item ) )
  885. {
  886. return;
  887. }
  888. #endif
  889. }
  890. else
  891. {
  892. target_entity = m_AttachmentCargos.GetKeyByValue( cargo );
  893. if( target_entity )
  894. {
  895. target_cargo = target_entity.GetInventory().GetCargo();
  896. #ifdef PLATFORM_CONSOLE
  897. if( cargo.HasItem( item ) )
  898. {
  899. return;
  900. }
  901. #endif
  902. }
  903. else
  904. return;
  905. }
  906. if( target_cargo && target_entity )
  907. {
  908. c_x = target_cargo.GetHeight();
  909. c_y = target_cargo.GetWidth();
  910. }
  911. else
  912. return;
  913. InventoryLocation dst = new InventoryLocation;
  914. #ifdef PLATFORM_CONSOLE
  915. x = 0;
  916. y = target_cargo.GetItemCount();
  917. target_entity.GetInventory().FindFreeLocationFor( item, FindInventoryLocationType.CARGO, dst );
  918. #else
  919. dst.SetCargoAuto(target_cargo, item, x, y, item.GetInventory().GetFlipCargo());
  920. #endif
  921. InventoryLocation src = new InventoryLocation;
  922. item.GetInventory().GetCurrentInventoryLocation(src);
  923. if(src.CompareLocationOnly(dst) && src.GetFlip() == dst.GetFlip())
  924. return;
  925. #ifdef PLATFORM_CONSOLE
  926. if(dst.IsValid() && target_entity.GetInventory().LocationCanAddEntity(dst))
  927. #else
  928. if( c_x > x && c_y > y && target_entity.GetInventory().LocationCanAddEntity(dst))
  929. #endif
  930. {
  931. SplitItemUtils.TakeOrSplitToInventoryLocation( player, dst );
  932. Icon icon = cargo.GetIcon( item );
  933. if( icon )
  934. {
  935. if( w && w.FindAnyWidget("Cursor") )
  936. w.FindAnyWidget("Cursor").SetColor( ColorManager.BASE_COLOR );
  937. icon.Refresh();
  938. Refresh();
  939. }
  940. }
  941. ItemManager.GetInstance().HideDropzones();
  942. ItemManager.GetInstance().SetIsDragging( false );
  943. }
  944. bool DraggingOverGrid( Widget w, int x, int y, Widget reciever, CargoContainer cargo )
  945. {
  946. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  947. if( w == null )
  948. {
  949. return false;
  950. }
  951. EntityAI item = GetItemPreviewItem( w );
  952. if( !item || !item_in_hands )
  953. {
  954. return false;
  955. }
  956. int color;
  957. int idx = 0;
  958. int c_x, c_y;
  959. if( item_in_hands.GetInventory().GetCargo() )
  960. {
  961. c_x = item_in_hands.GetInventory().GetCargo().GetHeight();
  962. c_y = item_in_hands.GetInventory().GetCargo().GetWidth();
  963. }
  964. if( c_x > x && c_y > y && item_in_hands.GetInventory().CanAddEntityInCargoEx( item, 0, x, y, item.GetInventory().GetFlipCargo() ) )
  965. {
  966. ItemManager.GetInstance().HideDropzones();
  967. ItemManager.GetInstance().GetCenterDropzone().SetAlpha( 1 );
  968. color = ColorManager.GREEN_COLOR;
  969. }
  970. else
  971. {
  972. color = ColorManager.RED_COLOR;
  973. ItemManager.GetInstance().ShowSourceDropzone( item );
  974. }
  975. if( w.FindAnyWidget("Cursor") )
  976. {
  977. w.FindAnyWidget("Cursor").SetColor( color );
  978. }
  979. else
  980. {
  981. string name = w.GetName();
  982. name.Replace( "PanelWidget", "Cursor" );
  983. w.FindAnyWidget( name ).SetColor( color );
  984. }
  985. return true;
  986. }
  987. void TakeAsAttachment( Widget w, Widget receiver )
  988. {
  989. EntityAI receiver_item;
  990. EntityAI slot_owner;
  991. int slot_id = -1;
  992. SlotsIcon slots_icon;
  993. receiver.GetUserData(slots_icon);
  994. if( slots_icon )
  995. {
  996. receiver_item = slots_icon.GetEntity();
  997. slot_id = slots_icon.GetSlotID();
  998. slot_owner = slots_icon.GetSlotParent();
  999. }
  1000. EntityAI item = GetItemPreviewItem( w );
  1001. if( !item )
  1002. {
  1003. return;
  1004. }
  1005. ItemBase item_base = ItemBase.Cast( item );
  1006. float stack_max = item_base.GetTargetQuantityMax(slot_id);
  1007. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  1008. if( !item.GetInventory().CanRemoveEntity() || !player.CanManipulateInventory() )
  1009. return;
  1010. EntityAI target_att_entity = m_Entity;
  1011. Weapon_Base wpn;
  1012. Magazine mag;
  1013. if( Class.CastTo(wpn, target_att_entity ) && Class.CastTo(mag, item ) )
  1014. {
  1015. if( player.GetWeaponManager().CanAttachMagazine( wpn, mag ) )
  1016. {
  1017. player.GetWeaponManager().AttachMagazine( mag );
  1018. }
  1019. else if(player.GetWeaponManager().CanSwapMagazine( wpn, mag ))
  1020. {
  1021. player.GetWeaponManager().SwapMagazine( mag );
  1022. }
  1023. }
  1024. else if( receiver_item )
  1025. {
  1026. if( receiver_item != item )
  1027. {
  1028. if( ( ItemBase.Cast( receiver_item ) ).CanBeCombined( ItemBase.Cast( item ) ) )
  1029. {
  1030. ( ItemBase.Cast( receiver_item ) ).CombineItemsClient( ItemBase.Cast( item ) );
  1031. }
  1032. else if( GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  1033. {
  1034. if( !receiver_item.GetInventory().CanRemoveEntity() )
  1035. return;
  1036. GetGame().GetPlayer().PredictiveSwapEntities( item, receiver_item );
  1037. }
  1038. }
  1039. }
  1040. else if( slot_owner && slot_owner.GetInventory().CanAddAttachmentEx( item, slot_id ) )
  1041. {
  1042. if( stack_max == 0 || stack_max >= item_base.GetQuantity() )
  1043. {
  1044. player.PredictiveTakeEntityToTargetAttachmentEx(slot_owner, item, slot_id);
  1045. }
  1046. else if( stack_max != 0 && stack_max < item_base.GetQuantity() )
  1047. {
  1048. item_base.SplitIntoStackMaxClient( slot_owner, slot_id );
  1049. }
  1050. }
  1051. else if( slot_owner && slot_owner.GetInventory().CanAddAttachment( item ) )
  1052. {
  1053. if( stack_max == 0 || stack_max >= item_base.GetQuantity() )
  1054. {
  1055. player.PredictiveTakeEntityToTargetAttachment(slot_owner, item);
  1056. }
  1057. else if( stack_max != 0 && stack_max < item_base.GetQuantity() )
  1058. {
  1059. item_base.SplitIntoStackMaxClient( slot_owner, -1 );
  1060. }
  1061. }
  1062. else if( target_att_entity.GetInventory().CanAddAttachment( item ) )
  1063. {
  1064. if( stack_max == 0 || stack_max >= item_base.GetQuantity() )
  1065. {
  1066. player.PredictiveTakeEntityToTargetAttachment(target_att_entity, item);
  1067. }
  1068. else if( stack_max != 0 && stack_max < item_base.GetQuantity() )
  1069. {
  1070. item_base.SplitIntoStackMaxClient( target_att_entity, -1 );
  1071. }
  1072. }
  1073. else if( ( target_att_entity.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && ( !player.GetInventory().HasEntityInInventory( item ) || !m_Entity.GetInventory().HasEntityInCargo( item )) ) || player.GetHumanInventory().HasEntityInHands( item ) )
  1074. {
  1075. SplitItemUtils.TakeOrSplitToInventory( PlayerBase.Cast( GetGame().GetPlayer() ), target_att_entity, item );
  1076. }
  1077. /*else if( !player.GetInventory().HasEntityInInventory( item ) || !target_att_entity.GetInventory().HasEntityInCargo( item ) )
  1078. {
  1079. SplitItemUtils.TakeOrSplitToInventory( PlayerBase.Cast( GetGame().GetPlayer() ), target_att_entity, item );
  1080. }*/
  1081. ItemManager.GetInstance().HideDropzones();
  1082. ItemManager.GetInstance().SetIsDragging( false );
  1083. }
  1084. void ShowAtt( EntityAI entity )
  1085. {
  1086. ResetContainer();
  1087. m_StaticCargoCount = 0;
  1088. m_Entity = entity;
  1089. m_ActiveIndex = 0;
  1090. if( entity.GetSlotsCountCorrect() > 0 )
  1091. {
  1092. m_Atts = new Attachments( this, entity );
  1093. m_Atts.InitAttachmentGrid( 1 );
  1094. m_AttachmentSlotsSorted = m_Atts.GetSlotsSorted();
  1095. m_StaticAttCount = m_Atts.GetAttachmentHeight();
  1096. m_Entity.GetOnItemAttached().Insert( AttachmentAdded );
  1097. m_Entity.GetOnItemDetached().Insert( AttachmentRemoved );
  1098. }
  1099. else
  1100. {
  1101. m_StaticAttCount = 0;
  1102. }
  1103. if( entity.GetInventory().GetCargo() )
  1104. {
  1105. m_CargoGrid = new CargoContainer( this, false );
  1106. m_CargoGrid.SetEntity( entity );
  1107. m_CargoGrid.GetRootWidget().SetSort( 1 );
  1108. Insert( m_CargoGrid );
  1109. m_StaticCargoCount++;
  1110. }
  1111. if (m_AttachmentSlotsSorted)
  1112. {
  1113. foreach( int slot_id : m_AttachmentSlotsSorted )
  1114. {
  1115. EntityAI item = m_Entity.GetInventory().FindAttachment( slot_id );
  1116. if( item )
  1117. AttachmentAdded( item, InventorySlots.GetSlotName( slot_id ), m_Entity );
  1118. }
  1119. }
  1120. if (m_CargoGrid)
  1121. {
  1122. bool hideCargo = m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() || m_ForcedHide;
  1123. if (m_CargoGrid.IsVisible() && hideCargo)
  1124. {
  1125. HideCargo();
  1126. }
  1127. else if (!m_CargoGrid.IsVisible() && !hideCargo)
  1128. {
  1129. ShowCargo();
  1130. }
  1131. }
  1132. m_CollapsibleHeader.ShowCollapseButtons(false);
  1133. //m_CollapsibleHeader.ShowCollapseButtons( m_Atts || m_CargoGrid, m_Atts || m_CargoGrid );
  1134. RecomputeOpenedContainers();
  1135. Refresh();
  1136. m_Parent.Refresh();
  1137. }
  1138. void RefreshHands()
  1139. {
  1140. for (int i = 0; i < m_Body.Count(); i++)
  1141. {
  1142. m_Body.Get( i ).OnShow();
  1143. }
  1144. Refresh();
  1145. m_Parent.Refresh();
  1146. }
  1147. void AttachmentAdded(EntityAI item, string slot, EntityAI parent)
  1148. {
  1149. int slot_id = InventorySlots.GetSlotIdFromString( slot );
  1150. int sort = -1;
  1151. Attachments att_cont = null;
  1152. CargoContainer cont = null;
  1153. if( item.GetInventory().GetAttachmentSlotsCount() > 0 )
  1154. {
  1155. att_cont = new Attachments( this, item );
  1156. sort = (m_AttachmentSlotsSorted.Find( slot_id ) * 2) + SORT_ATTACHMENTS_NEXT_OFFSET;
  1157. att_cont.InitAttachmentGrid( sort );
  1158. m_AttachmentAttachments.Insert( item, att_cont );
  1159. m_AttachmentAttachmentsContainers.Insert( item, att_cont.GetWrapper() );
  1160. att_cont.UpdateInterval();
  1161. if( m_Parent )
  1162. m_Parent.Refresh();
  1163. Inventory.GetInstance().UpdateConsoleToolbar();
  1164. }
  1165. if( item.GetInventory().GetCargo() )
  1166. {
  1167. cont = new CargoContainer( this, true );
  1168. sort = (m_AttachmentSlotsSorted.Find( slot_id ) * 2) + SORT_CARGO_NEXT_OFFSET; //m_AttachmentSlotsSorted.Find( slot_id ) + 50
  1169. cont.GetRootWidget().SetSort( sort );
  1170. cont.SetEntity( item );
  1171. Insert(cont, -1);
  1172. m_AttachmentCargos.Insert( item, cont );
  1173. if( m_Parent )
  1174. m_Parent.Refresh();
  1175. Inventory.GetInstance().UpdateConsoleToolbar();
  1176. }
  1177. if (att_cont)
  1178. {
  1179. att_cont.ShowFalseAttachmentsHeader(true);
  1180. if (cont)
  1181. {
  1182. cont.ShowFalseCargoHeader(false);
  1183. cont.UpdateSize();
  1184. cont.SetAlternateFalseTextHeaderWidget(att_cont.GetFalseHeaderTextWidget());
  1185. }
  1186. }
  1187. else if (cont)
  1188. {
  1189. cont.SetAlternateFalseTextHeaderWidget(null); //just to be safe..
  1190. }
  1191. RecomputeOpenedContainers();
  1192. }
  1193. void AttachmentRemoved(EntityAI item, string slot, EntityAI parent)
  1194. {
  1195. int slot_id = InventorySlots.GetSlotIdFromString( slot );
  1196. CargoContainer old_cont = m_AttachmentCargos.Get( item );
  1197. if( old_cont )
  1198. {
  1199. m_Body.RemoveItem( old_cont );
  1200. m_AttachmentCargos.Remove( item );
  1201. delete old_cont;
  1202. if( m_Parent )
  1203. m_Parent.Refresh();
  1204. Inventory.GetInstance().UpdateConsoleToolbar();
  1205. }
  1206. AttachmentsWrapper old_att_cont = m_AttachmentAttachmentsContainers.Get( item );
  1207. if( old_att_cont )
  1208. {
  1209. m_AttachmentAttachmentsContainers.Remove( item );
  1210. m_AttachmentAttachments.Remove( item );
  1211. m_Body.RemoveItem( old_att_cont );
  1212. delete old_att_cont;
  1213. if( m_Parent )
  1214. m_Parent.Refresh();
  1215. Inventory.GetInstance().UpdateConsoleToolbar();
  1216. }
  1217. RecomputeOpenedContainers();
  1218. }
  1219. override void UpdateInterval()
  1220. {
  1221. super.UpdateInterval();
  1222. if ( m_Entity )
  1223. {
  1224. if (m_CargoGrid)
  1225. {
  1226. bool hideCargo = m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() || m_ForcedHide;
  1227. if (m_CargoGrid.IsVisible() && hideCargo)
  1228. {
  1229. HideCargo();
  1230. }
  1231. else if (!m_CargoGrid.IsVisible() && !hideCargo)
  1232. {
  1233. ShowCargo();
  1234. }
  1235. m_CargoGrid.UpdateInterval();
  1236. }
  1237. if ( m_Atts )
  1238. {
  1239. m_Atts.UpdateInterval();
  1240. }
  1241. for ( int i = 0; i < m_AttachmentCargos.Count(); i++ )
  1242. {
  1243. m_AttachmentCargos.GetElement( i ).UpdateInterval();
  1244. }
  1245. }
  1246. ElectricityIcon();
  1247. m_CollapsibleHeader.UpdateInterval();
  1248. }
  1249. override void SetLayoutName()
  1250. {
  1251. m_LayoutName = WidgetLayoutName.ClosableContainer;
  1252. }
  1253. override void OnShow()
  1254. {
  1255. super.OnShow();
  1256. for ( int i = 0; i < m_Body.Count(); i++ )
  1257. {
  1258. m_Body.Get( i ).OnShow();
  1259. }
  1260. }
  1261. void MouseClick2(Widget w, int x, int y, int button)
  1262. {
  1263. string name = w.GetName();
  1264. name.Replace("PanelWidget", "Render");
  1265. ItemPreviewWidget itemPreview = ItemPreviewWidget.Cast(w.FindAnyWidget(name));
  1266. if (itemPreview)
  1267. {
  1268. ItemBase selectedItem = ItemBase.Cast(itemPreview.GetItem());
  1269. if (selectedItem)
  1270. {
  1271. switch (button)
  1272. {
  1273. case MouseState.RIGHT:
  1274. #ifdef DIAG_DEVELOPER
  1275. if (g_Game.IsLeftCtrlDown())
  1276. ShowActionMenu(selectedItem);
  1277. #endif
  1278. if (CanSplitEx(selectedItem))
  1279. {
  1280. selectedItem.OnRightClick();
  1281. }
  1282. break;
  1283. case MouseState.MIDDLE:
  1284. InspectItem(selectedItem);
  1285. break;
  1286. case MouseState.LEFT:
  1287. PlayerBase controlledPlayer = PlayerBase.Cast(GetGame().GetPlayer());
  1288. if (g_Game.IsLeftCtrlDown())
  1289. {
  1290. if (controlledPlayer.CanDropEntity(selectedItem))
  1291. {
  1292. if (selectedItem.GetTargetQuantityMax() < selectedItem.GetQuantity())
  1293. selectedItem.SplitIntoStackMaxClient(null, -1);
  1294. else
  1295. controlledPlayer.PhysicalPredictiveDropItem(selectedItem);
  1296. }
  1297. else
  1298. {
  1299. bool draggable = !controlledPlayer.GetInventory().HasInventoryReservation(selectedItem, null ) && !controlledPlayer.GetInventory().IsInventoryLocked() && selectedItem.GetInventory().CanRemoveEntity() && !controlledPlayer.IsItemsToDelete();
  1300. ItemManager.GetInstance().SetWidgetDraggable(w, draggable);
  1301. }
  1302. }
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. void DoubleClick(Widget w, int x, int y, int button)
  1309. {
  1310. if (button == MouseState.LEFT && !g_Game.IsLeftCtrlDown())
  1311. {
  1312. ItemPreviewWidget iw = ItemPreviewWidget.Cast(w.FindAnyWidget("Render"));
  1313. if (!iw)
  1314. {
  1315. string name = w.GetName();
  1316. name.Replace("PanelWidget", "Render");
  1317. iw = ItemPreviewWidget.Cast(w.FindAnyWidget(name));
  1318. }
  1319. if (!iw)
  1320. iw = ItemPreviewWidget.Cast(w);
  1321. ItemBase item = ItemBase.Cast(iw.GetItem());
  1322. if (item)
  1323. {
  1324. PlayerBase controlledPlayer = PlayerBase.Cast(GetGame().GetPlayer());
  1325. EntityAI itemInHands = controlledPlayer.GetHumanInventory().GetEntityInHands();
  1326. if (!item.GetInventory().CanRemoveEntity())
  1327. return;
  1328. if (controlledPlayer.GetInventory().HasEntityInInventory(item) && controlledPlayer.GetHumanInventory().CanAddEntityInHands(item))
  1329. {
  1330. controlledPlayer.PredictiveTakeEntityToHands(item);
  1331. }
  1332. else
  1333. {
  1334. if (controlledPlayer.GetInventory().CanAddEntityToInventory(item) && controlledPlayer.GetHumanInventory().CanRemoveEntityInHands())
  1335. {
  1336. controlledPlayer.PredictiveTakeEntityToInventory(FindInventoryLocationType.ANY, item);
  1337. }
  1338. else
  1339. {
  1340. if (controlledPlayer.GetHumanInventory().CanAddEntityInHands(item))
  1341. controlledPlayer.PredictiveTakeEntityToHands(item);
  1342. }
  1343. }
  1344. HideOwnedTooltip();
  1345. name = w.GetName();
  1346. name.Replace("PanelWidget", "Temperature");
  1347. w.FindAnyWidget(name).Show(false);
  1348. }
  1349. }
  1350. }
  1351. void DraggingOverHeader2(Widget w, int x, int y, Widget receiver )
  1352. {
  1353. DraggingOverHeader(w, x, y, receiver );
  1354. }
  1355. void ElectricityIcon()
  1356. {
  1357. EntityAI item_in_hands = GetGame().GetPlayer().GetHumanInventory().GetEntityInHands();
  1358. if( item_in_hands && item_in_hands.GetCompEM() )
  1359. {
  1360. if( GetRootWidget().FindAnyWidget( "electricity" ) )
  1361. {
  1362. bool show_electricity_icon = item_in_hands.GetCompEM().HasElectricityIcon();
  1363. GetRootWidget().FindAnyWidget( "electricity" ).Show( show_electricity_icon );
  1364. }
  1365. if( GetRootWidget().FindAnyWidget( "electricity_can_work" ) )
  1366. {
  1367. bool show_electricity_can_work_icon = item_in_hands.GetCompEM().CanWork() && !item_in_hands.IsRuined();
  1368. GetRootWidget().FindAnyWidget( "electricity_can_work" ).Show( show_electricity_can_work_icon );
  1369. }
  1370. }
  1371. else
  1372. {
  1373. GetRootWidget().FindAnyWidget( "electricity" ).Show( false );
  1374. }
  1375. }
  1376. void CollapseButtonOnMouseButtonDown( Widget w )
  1377. {
  1378. int i;
  1379. if( !m_Hidden )
  1380. {
  1381. for (i = 0; i < m_Body.Count(); i++)
  1382. {
  1383. m_Body.Get( i ).OnHide();
  1384. }
  1385. m_Hidden = true;
  1386. }
  1387. else
  1388. {
  1389. for (i = 0; i < m_Body.Count(); i++)
  1390. {
  1391. m_Body.Get( i ).OnShow();
  1392. }
  1393. m_Hidden = false;
  1394. }
  1395. w.FindAnyWidget("opened").Show(m_Hidden);
  1396. w.FindAnyWidget("closed").Show(!m_Hidden);
  1397. m_Parent.Refresh();
  1398. }
  1399. void HideCargo()
  1400. {
  1401. if( m_CargoGrid )
  1402. {
  1403. m_CargoGrid.OnHide();
  1404. RecomputeOpenedContainers();
  1405. }
  1406. }
  1407. void ShowCargo()
  1408. {
  1409. if( m_CargoGrid )
  1410. {
  1411. m_CargoGrid.OnShow();
  1412. RecomputeOpenedContainers();
  1413. }
  1414. }
  1415. }