handscontainer.c 42 KB

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