handscontainer.c 42 KB

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