containerwithcargoandattachments.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. class ContainerWithCargoAndAttachments extends ClosableContainer
  2. {
  3. protected ref Attachments m_Atts;
  4. protected ref CargoContainer m_CargoGrid;
  5. protected ref map<EntityAI, ref CargoContainer> m_AttachmentCargos;
  6. protected ref map<EntityAI, ref AttachmentsWrapper> m_AttachmentAttachmentsContainers;
  7. protected ref map<EntityAI, ref Attachments> m_AttachmentAttachments;
  8. protected ref array<int> m_AttachmentSlotsSorted;
  9. void ContainerWithCargoAndAttachments( LayoutHolder parent, int sort = -1 )
  10. {
  11. WidgetEventHandler.GetInstance().RegisterOnDraggingOver( m_MainWidget, this, "DraggingOverHeader2" );
  12. }
  13. void ~ContainerWithCargoAndAttachments()
  14. {
  15. if ( m_Entity )
  16. {
  17. m_Entity.GetOnItemAttached().Remove( AttachmentAdded );
  18. m_Entity.GetOnItemDetached().Remove( AttachmentRemoved );
  19. }
  20. if ( m_Atts )
  21. delete m_Atts;
  22. foreach ( EntityAI e, Attachments att : m_AttachmentAttachments )
  23. {
  24. delete att;
  25. }
  26. if ( m_AttachmentAttachments )
  27. m_AttachmentAttachments.Clear();
  28. if ( m_AttachmentAttachmentsContainers )
  29. m_AttachmentAttachmentsContainers.Clear();
  30. if ( m_AttachmentCargos )
  31. m_AttachmentCargos.Clear();
  32. }
  33. void RecomputeContainers()
  34. {
  35. int currentIndex = -1;
  36. map<int, int> bodyAttachmentSortIndex = new map<int, int>;
  37. m_Body.Clear();
  38. if(m_Atts)
  39. {
  40. currentIndex = m_Body.Insert(m_Atts.GetWrapper());
  41. }
  42. if(m_CargoGrid)
  43. {
  44. currentIndex = m_Body.Insert(m_CargoGrid);
  45. }
  46. GameInventory inv = m_Entity.GetInventory();
  47. for (int i = 0; i < inv.AttachmentCount(); i++)
  48. {
  49. int slotIndex = -1;
  50. int sortIndex = -1;
  51. EntityAI ent = inv.GetAttachmentFromIndex(i);
  52. if(ent)
  53. {
  54. AttachmentsWrapper att = m_AttachmentAttachmentsContainers.Get(ent);
  55. if(att)
  56. {
  57. if(m_Atts && m_Atts.GetSlotsSorted().Count())
  58. {
  59. sortIndex = m_Atts.GetSlotsSorted().Find(att.m_Attachments.GetAttachmentSlotID());
  60. }
  61. if(bodyAttachmentSortIndex.Find(currentIndex, slotIndex))
  62. {
  63. if(slotIndex > sortIndex)
  64. {
  65. m_Body.InsertAt(att, currentIndex);
  66. bodyAttachmentSortIndex.Set(currentIndex, sortIndex);
  67. }
  68. else
  69. {
  70. currentIndex = m_Body.Insert(att);
  71. bodyAttachmentSortIndex.Insert(currentIndex, sortIndex);
  72. }
  73. }
  74. else
  75. {
  76. currentIndex = m_Body.Insert(att);
  77. bodyAttachmentSortIndex.Insert(currentIndex, sortIndex);
  78. }
  79. }
  80. CargoContainer cargo = m_AttachmentCargos.Get(ent);
  81. if(cargo)
  82. {
  83. if(m_Atts && m_Atts.GetSlotsSorted().Count())
  84. {
  85. sortIndex = m_Atts.GetSlotsSorted().Find(cargo.GetAttachmentSlotID());
  86. }
  87. if (bodyAttachmentSortIndex.Find(currentIndex, slotIndex))
  88. {
  89. if(slotIndex > sortIndex)
  90. {
  91. m_Body.InsertAt(cargo, currentIndex);
  92. bodyAttachmentSortIndex.Set(currentIndex, sortIndex);
  93. }
  94. else
  95. {
  96. currentIndex = m_Body.Insert(cargo);
  97. bodyAttachmentSortIndex.Insert(currentIndex, sortIndex);
  98. }
  99. }
  100. else
  101. {
  102. currentIndex = m_Body.Insert(cargo);
  103. bodyAttachmentSortIndex.Insert(currentIndex, sortIndex);
  104. }
  105. }
  106. }
  107. }
  108. bodyAttachmentSortIndex.Clear();
  109. }
  110. void AttachmentAddedEx(EntityAI item, string slot, EntityAI parent, bool immedUpdate = true)
  111. {
  112. int slot_id = InventorySlots.GetSlotIdFromString( slot );
  113. int sort = -1;
  114. bool updateNeeded = false;
  115. ref Attachments att_cont = null;
  116. ref CargoContainer cont = null;
  117. if ( item.GetInventory().GetAttachmentSlotsCount() > 0 && item.CanDisplayAnyAttachmentSlot() )
  118. {
  119. updateNeeded = true;
  120. att_cont = new Attachments( this, item );
  121. sort = (m_AttachmentSlotsSorted.Find( slot_id ) * 2) + SORT_ATTACHMENTS_NEXT_OFFSET;
  122. att_cont.InitAttachmentGrid( sort );
  123. att_cont.SetAttachmentSlotID(slot_id);
  124. m_AttachmentAttachments.Insert( item, att_cont );
  125. m_AttachmentAttachmentsContainers.Insert( item, att_cont.GetWrapper() );
  126. att_cont.UpdateInterval();
  127. }
  128. if ( item.GetInventory().GetCargo() )
  129. {
  130. updateNeeded = true;
  131. cont = new CargoContainer( this, true );
  132. sort = (m_AttachmentSlotsSorted.Find( slot_id ) * 2) + SORT_CARGO_NEXT_OFFSET;
  133. cont.GetRootWidget().SetSort( sort );
  134. cont.SetEntity( item, false );
  135. cont.SetAttachmentSlotID(slot_id);
  136. Insert( cont, m_Atts.GetAttachmentHeight() + m_AttachmentCargos.Count() + 1 );
  137. m_AttachmentCargos.Insert( item, cont );
  138. }
  139. if (updateNeeded)
  140. {
  141. if (att_cont)
  142. {
  143. att_cont.ShowFalseAttachmentsHeader(true);
  144. if (cont)
  145. {
  146. cont.ShowFalseCargoHeader(false);
  147. cont.UpdateSize();
  148. cont.SetAlternateFalseTextHeaderWidget(att_cont.GetFalseHeaderTextWidget());
  149. }
  150. }
  151. else if (cont)
  152. {
  153. cont.SetAlternateFalseTextHeaderWidget(null); //just to be safe..
  154. }
  155. RecomputeContainers();
  156. RecomputeOpenedContainers();
  157. Inventory.GetInstance().UpdateConsoleToolbar();
  158. if (m_Parent && immedUpdate)
  159. m_Parent.Refresh();
  160. }
  161. }
  162. void AttachmentAdded(EntityAI item, string slot, EntityAI parent)
  163. {
  164. AttachmentAddedEx(item, slot, parent);
  165. }
  166. void AttachmentRemoved(EntityAI item, string slot, EntityAI parent)
  167. {
  168. int slot_id = InventorySlots.GetSlotIdFromString( slot );
  169. CargoContainer old_cont = m_AttachmentCargos.Get( item );
  170. if( old_cont )
  171. {
  172. m_AttachmentCargos.Remove( item );
  173. delete old_cont;
  174. if( m_Parent )
  175. m_Parent.Refresh();
  176. Inventory.GetInstance().UpdateConsoleToolbar();
  177. }
  178. AttachmentsWrapper old_att_cont = m_AttachmentAttachmentsContainers.Get( item );
  179. if( old_att_cont )
  180. {
  181. m_AttachmentAttachmentsContainers.Remove( item );
  182. m_AttachmentAttachments.Remove( item );
  183. delete old_att_cont;
  184. if( m_Parent )
  185. m_Parent.Refresh();
  186. Inventory.GetInstance().UpdateConsoleToolbar();
  187. }
  188. RecomputeContainers();
  189. RecomputeOpenedContainers();
  190. }
  191. override void UpdateInterval()
  192. {
  193. if ( m_Entity )
  194. {
  195. if (m_CargoGrid)
  196. {
  197. bool hideCargo = m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() || m_ForcedHide;
  198. if (m_CargoGrid.IsVisible() && hideCargo)
  199. {
  200. HideCargo();
  201. }
  202. else if (!m_CargoGrid.IsVisible() && !hideCargo)
  203. {
  204. ShowCargo();
  205. }
  206. m_CargoGrid.UpdateInterval();
  207. }
  208. if ( m_SlotIcon )
  209. {
  210. bool hide = m_LockCargo || ItemManager.GetInstance().GetDraggedItem() == m_Entity;
  211. if (!hide)
  212. {
  213. SetOpenForSlotIcon(IsOpened());
  214. }
  215. m_SlotIcon.GetRadialIconPanel().Show( !hide );
  216. }
  217. super.UpdateInterval();
  218. }
  219. }
  220. override EntityAI GetFocusedContainerEntity()
  221. {
  222. return m_Entity;
  223. }
  224. override void UnfocusAll()
  225. {
  226. if( m_Atts )
  227. {
  228. m_Atts.UnfocusAll();
  229. }
  230. if( m_CargoGrid )
  231. {
  232. m_CargoGrid.UnfocusAll();
  233. }
  234. foreach( EntityAI e1, CargoContainer cargo : m_AttachmentCargos )
  235. {
  236. cargo.UnfocusAll();
  237. }
  238. foreach( EntityAI e2, Attachments att : m_AttachmentAttachments )
  239. {
  240. att.UnfocusAll();
  241. }
  242. }
  243. override bool IsLastIndex()
  244. {
  245. return m_ActiveIndex == ( m_OpenedContainers.Count() - 1 );
  246. }
  247. override bool IsFirstContainerFocused()
  248. {
  249. return IsFirstIndex();
  250. }
  251. override bool IsLastContainerFocused()
  252. {
  253. return IsLastIndex();
  254. }
  255. override void MoveGridCursor( int direction )
  256. {
  257. Container c = GetFocusedContainer();
  258. if ( c )
  259. {
  260. c.MoveGridCursor( direction );
  261. Inventory.GetInstance().UpdateConsoleToolbar();
  262. }
  263. }
  264. void SetEntity( EntityAI entity, bool immedUpdate = true )
  265. {
  266. m_Entity = entity;
  267. m_Atts = new Attachments( this, m_Entity );
  268. m_Atts.InitAttachmentGrid( SORT_ATTACHMENTS_OWN );
  269. m_AttachmentSlotsSorted = m_Atts.GetSlotsSorted();
  270. m_Entity.GetOnItemAttached().Insert( AttachmentAdded );
  271. m_Entity.GetOnItemDetached().Insert( AttachmentRemoved );
  272. m_ClosableHeader.SetItemPreview( m_Entity );
  273. CheckHeaderDragability();
  274. if ( m_Entity.GetInventory().GetCargo() )
  275. {
  276. m_CargoGrid = new CargoContainer( this, false );
  277. m_CargoGrid.GetRootWidget().SetSort( SORT_CARGO_OWN );
  278. m_CargoGrid.SetEntity( m_Entity, 0, immedUpdate );
  279. m_CargoGrid.UpdateHeaderText(); // TODO: refresh?
  280. Insert( m_CargoGrid );
  281. }
  282. else
  283. {
  284. string name = m_Entity.GetDisplayName();
  285. name.ToUpper();
  286. m_ClosableHeader.SetName( name );
  287. }
  288. m_AttachmentCargos = new map<EntityAI, ref CargoContainer>;
  289. m_AttachmentAttachmentsContainers = new map<EntityAI, ref AttachmentsWrapper>;
  290. m_AttachmentAttachments = new map<EntityAI, ref Attachments>;
  291. (Container.Cast( m_Parent )).Insert( this );
  292. foreach ( int slot_id : m_AttachmentSlotsSorted )
  293. {
  294. EntityAI item = m_Entity.GetInventory().FindAttachment( slot_id );
  295. if ( item )
  296. AttachmentAddedEx( item, InventorySlots.GetSlotName( slot_id ), m_Entity, false );
  297. }
  298. RecomputeContainers();
  299. if (m_CargoGrid)
  300. {
  301. bool hideCargo = m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() || m_ForcedHide;
  302. if (m_CargoGrid.IsVisible() && hideCargo)
  303. {
  304. HideCargo();
  305. }
  306. else if (!m_CargoGrid.IsVisible() && !hideCargo)
  307. {
  308. ShowCargo();
  309. }
  310. }
  311. if( IsDisplayable() )
  312. SetOpenState( true );
  313. else
  314. SetOpenState( false );
  315. if (immedUpdate)
  316. m_Parent.m_Parent.Refresh();
  317. RecomputeOpenedContainers();
  318. }
  319. void HideCargo()
  320. {
  321. if( m_CargoGrid )
  322. {
  323. if(m_CargoGrid.IsVisible())
  324. {
  325. m_CargoGrid.OnHide();
  326. RecomputeOpenedContainers();
  327. }
  328. }
  329. }
  330. void ShowCargo()
  331. {
  332. if( m_CargoGrid )
  333. {
  334. if(!m_CargoGrid.IsVisible())
  335. {
  336. m_CargoGrid.OnShow();
  337. RecomputeOpenedContainers();
  338. }
  339. }
  340. }
  341. EntityAI GetEntity()
  342. {
  343. return m_Entity;
  344. }
  345. ItemPreviewWidget GetItemPreviewWidgetDragOrDrop( Widget w )
  346. {
  347. string name = w.GetName();
  348. name.Replace( "PanelWidget", "Render" );
  349. return ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  350. }
  351. EntityAI GetItemPreviewItem( Widget w )
  352. {
  353. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  354. if( !ipw )
  355. {
  356. string name = w.GetName();
  357. name.Replace( "PanelWidget", "Render" );
  358. ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  359. }
  360. if( !ipw )
  361. {
  362. ipw = ItemPreviewWidget.Cast( w );
  363. }
  364. if( !ipw || !ipw.IsInherited( ItemPreviewWidget ) )
  365. {
  366. return NULL;
  367. }
  368. return ipw.GetItem();
  369. }
  370. Widget GetItemPreviewWidget( Widget w )
  371. {
  372. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  373. if( !ipw )
  374. {
  375. string name = w.GetName();
  376. name.Replace( "PanelWidget", "Render" );
  377. ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  378. }
  379. if( !ipw )
  380. {
  381. ipw = ItemPreviewWidget.Cast( w );
  382. }
  383. return ipw;
  384. }
  385. void MouseClick2(Widget w, int x, int y, int button)
  386. {
  387. SlotsIcon icon;
  388. w.GetUserData(icon);
  389. ItemBase selectedItem;
  390. if (icon)
  391. selectedItem = ItemBase.Cast(icon.GetEntity());
  392. if (selectedItem)
  393. {
  394. bool isReserved = icon.IsReserved();
  395. switch (button)
  396. {
  397. case MouseState.RIGHT:
  398. #ifdef DIAG_DEVELOPER
  399. if (GetDayZGame().IsLeftCtrlDown())
  400. ShowActionMenu(selectedItem);
  401. #endif
  402. if (isReserved)
  403. {
  404. EntityAI attachmentParent = icon.GetSlotParent();
  405. GetGame().GetPlayer().GetHumanInventory().ClearUserReservedLocationSynced(selectedItem);
  406. attachmentParent.GetOnAttachmentReleaseLock().Invoke(selectedItem, icon.GetSlotID());
  407. }
  408. break;
  409. case MouseState.MIDDLE:
  410. if (!isReserved)
  411. InspectItem(selectedItem);
  412. break;
  413. case MouseState.LEFT:
  414. if (!isReserved)
  415. {
  416. PlayerBase controlledPlayer = PlayerBase.Cast(GetGame().GetPlayer());
  417. if (g_Game.IsLeftCtrlDown())
  418. {
  419. if (controlledPlayer.CanDropEntity(selectedItem))
  420. {
  421. if (selectedItem.CanBeSplit() && selectedItem.GetTargetQuantityMax() < selectedItem.GetQuantity())
  422. selectedItem.SplitIntoStackMaxClient(null, -1);
  423. else
  424. controlledPlayer.PhysicalPredictiveDropItem(selectedItem);
  425. }
  426. }
  427. else
  428. {
  429. bool draggable = !controlledPlayer.GetInventory().HasInventoryReservation(selectedItem, null ) && !controlledPlayer.GetInventory().IsInventoryLocked() && selectedItem.GetInventory().CanRemoveEntity() && !controlledPlayer.IsItemsToDelete();
  430. ItemManager.GetInstance().SetWidgetDraggable(w, draggable);
  431. }
  432. }
  433. break;
  434. }
  435. }
  436. }
  437. //! NOTE Used for mouse only
  438. void DropReceived(Widget w, int x, int y, CargoContainer cargo)
  439. {
  440. EntityAI item = GetItemPreviewItem(w);
  441. if (!item)
  442. return;
  443. #ifndef PLATFORM_CONSOLE
  444. int c_x, c_y;
  445. #endif
  446. EntityAI targetEntity = m_Entity;
  447. CargoBase targetCargo;
  448. if (cargo != m_CargoGrid)
  449. {
  450. targetEntity = m_AttachmentCargos.GetKeyByValue(cargo);
  451. }
  452. if (targetEntity)
  453. {
  454. targetCargo = targetEntity.GetInventory().GetCargo();
  455. #ifdef PLATFORM_CONSOLE
  456. if (m_CargoGrid && m_CargoGrid.HasItem(item))
  457. return;
  458. #endif
  459. }
  460. if (!targetCargo || !targetEntity)
  461. return;
  462. InventoryLocation dst = new InventoryLocation();
  463. #ifdef PLATFORM_CONSOLE
  464. x = 0;
  465. y = targetCargo.GetItemCount();
  466. targetEntity.GetInventory().FindFreeLocationFor(item, FindInventoryLocationType.CARGO, dst);
  467. #else
  468. c_x = targetCargo.GetHeight();
  469. c_y = targetCargo.GetWidth();
  470. dst.SetCargoAuto(targetCargo, item, x, y, item.GetInventory().GetFlipCargo());
  471. #endif
  472. InventoryLocation src = new InventoryLocation();
  473. item.GetInventory().GetCurrentInventoryLocation(src);
  474. if (src.CompareLocationOnly(dst) && src.GetFlip() == dst.GetFlip())
  475. return;
  476. #ifdef PLATFORM_CONSOLE
  477. if (dst.IsValid() && targetEntity.GetInventory().LocationCanAddEntity(dst))
  478. #else
  479. if (c_x > x && c_y > y && targetEntity.GetInventory().LocationCanAddEntity(dst))
  480. #endif
  481. {
  482. PlayerBase player = PlayerBase.Cast(GetGame().GetPlayer());
  483. SplitItemUtils.TakeOrSplitToInventoryLocation(player, dst);
  484. Icon icon = cargo.GetIcon(item);
  485. if (icon)
  486. {
  487. if (w && w.FindAnyWidget("Cursor"))
  488. w.FindAnyWidget("Cursor").SetColor(ColorManager.BASE_COLOR);
  489. icon.Refresh();
  490. Refresh();
  491. }
  492. }
  493. ItemManager.GetInstance().HideDropzones();
  494. ItemManager.GetInstance().SetIsDragging(false);
  495. }
  496. void TakeAsAttachment( Widget w, Widget receiver )
  497. {
  498. ItemManager.GetInstance().HideDropzones();
  499. ItemManager.GetInstance().SetIsDragging( false );
  500. SlotsIcon slots_icon;
  501. EntityAI receiver_item;
  502. int slot_id = -1;
  503. bool is_reserved = false;
  504. EntityAI attached_entity;
  505. receiver.GetUserData(slots_icon);
  506. //string name = receiver.GetName();
  507. //name.Replace("PanelWidget", "Render");
  508. //ItemPreviewWidget receiver_iw = ItemPreviewWidget.Cast( receiver.FindAnyWidget(name) );
  509. if( slots_icon )
  510. {
  511. receiver_item = slots_icon.GetEntity();
  512. slot_id = slots_icon.GetSlotID();
  513. attached_entity = slots_icon.GetSlotParent();
  514. is_reserved = slots_icon.IsReserved();
  515. }
  516. EntityAI item = GetItemPreviewItem( w );
  517. if( !item )
  518. {
  519. return;
  520. }
  521. ItemBase item_base = ItemBase.Cast( item );
  522. InventoryLocation il = new InventoryLocation;
  523. float stackable;
  524. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  525. if( !item.GetInventory().CanRemoveEntity() || !player.CanManipulateInventory() )
  526. return;
  527. EntityAI target_att_entity = m_Entity;
  528. Weapon_Base wpn;
  529. Magazine mag;
  530. if( Class.CastTo(wpn, m_Entity ) && Class.CastTo(mag, item ) )
  531. {
  532. if( player.GetWeaponManager().CanAttachMagazine( wpn, mag ) )
  533. {
  534. player.GetWeaponManager().AttachMagazine( mag );
  535. }
  536. }
  537. else if( receiver_item && !is_reserved )
  538. {
  539. if( ( ItemBase.Cast( receiver_item ) ).CanBeCombined( ItemBase.Cast( item ) ) )
  540. {
  541. ( ItemBase.Cast( receiver_item ) ).CombineItemsClient( ItemBase.Cast( item ) );
  542. }
  543. else if( GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  544. {
  545. if( !receiver_item.GetInventory().CanRemoveEntity() )
  546. return;
  547. GetGame().GetPlayer().PredictiveSwapEntities( receiver_item, item );
  548. }
  549. else if( receiver_item.GetInventory().CanAddAttachment( item ) )
  550. {
  551. player.PredictiveTakeEntityToTargetAttachment(receiver_item, item);
  552. }
  553. }
  554. else if( attached_entity && attached_entity.GetInventory().CanAddAttachmentEx( item, slot_id ) )
  555. {
  556. player.PredictiveTakeEntityToTargetAttachmentEx(attached_entity, item, slot_id);
  557. }
  558. else if(attached_entity && attached_entity.GetInventory().CanAddAttachment(item))
  559. {
  560. attached_entity.GetInventory().FindFreeLocationFor(item,FindInventoryLocationType.ATTACHMENT,il);
  561. player.PredictiveTakeEntityToTargetAttachmentEx(attached_entity, item, il.GetSlot());
  562. }
  563. else if( m_Entity.GetInventory().CanAddAttachment(item) )
  564. {
  565. m_Entity.GetInventory().FindFreeLocationFor(item,FindInventoryLocationType.ATTACHMENT,il);
  566. player.PredictiveTakeEntityToTargetAttachmentEx(m_Entity, item, il.GetSlot());
  567. }
  568. else if( m_Entity.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && !m_Entity.GetInventory().HasEntityInCargo( item ) )
  569. {
  570. SplitItemUtils.TakeOrSplitToInventory( PlayerBase.Cast( GetGame().GetPlayer() ), m_Entity, item );
  571. }
  572. /*else if( !player.GetInventory().HasEntityInInventory( item ) || !m_Entity.GetInventory().HasEntityInCargo( item ) )
  573. {
  574. SplitItemUtils.TakeOrSplitToInventory( PlayerBase.Cast( GetGame().GetPlayer() ), m_Entity, item );
  575. }*/
  576. }
  577. override void OnDropReceivedFromHeader( Widget w, int x, int y, Widget receiver )
  578. {
  579. TakeAsAttachment( w, receiver );
  580. }
  581. void OnDropReceivedFromHeader2( Widget w, int x, int y, Widget receiver )
  582. {
  583. TakeAsAttachment( w, receiver );
  584. }
  585. void DoubleClick(Widget w, int x, int y, int button)
  586. {
  587. if( button == MouseState.LEFT && !g_Game.IsLeftCtrlDown())
  588. {
  589. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  590. if( w == NULL || player.GetInventory().IsInventoryLocked() )
  591. {
  592. return;
  593. }
  594. ItemPreviewWidget iw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  595. if( !iw )
  596. {
  597. string name = w.GetName();
  598. name.Replace( "PanelWidget", "Render" );
  599. iw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  600. }
  601. if( !iw )
  602. {
  603. iw = ItemPreviewWidget.Cast( w );
  604. }
  605. EntityAI item = iw.GetItem();
  606. if( !item )
  607. {
  608. return;
  609. }
  610. SlotsIcon icon;
  611. iw.GetUserData(icon);
  612. if(icon && icon.IsReserved())
  613. {
  614. return;
  615. }
  616. if( !item.GetInventory().CanRemoveEntity() || !player.CanManipulateInventory() )
  617. return;
  618. if( player.GetInventory().HasEntityInInventory( item ) && player.GetHumanInventory().CanAddEntityInHands( item ) )
  619. {
  620. player.PredictiveTakeEntityToHands( item );
  621. }
  622. else
  623. {
  624. if(player.GetInventory().CanAddEntityToInventory( item ) && item.GetInventory().CanRemoveEntity())
  625. {
  626. player.PredictiveTakeEntityToInventory( FindInventoryLocationType.ANY, InventoryItem.Cast( item ) );
  627. }
  628. else
  629. {
  630. if( player.GetHumanInventory().CanAddEntityInHands( item ) )
  631. {
  632. player.PredictiveTakeEntityToHands( item );
  633. }
  634. }
  635. }
  636. HideOwnedTooltip();
  637. name = w.GetName();
  638. name.Replace( "PanelWidget", "Temperature" );
  639. w.FindAnyWidget( name ).Show( false );
  640. }
  641. }
  642. bool DraggingOverGrid( Widget w, int x, int y, Widget reciever, CargoContainer cargo )
  643. {
  644. if( w == NULL )
  645. {
  646. return false;
  647. }
  648. EntityAI item = GetItemPreviewItem( w );
  649. if( !item )
  650. {
  651. return false;
  652. }
  653. int color;
  654. int idx = 0;
  655. int c_x, c_y;
  656. EntityAI target_entity;
  657. CargoBase target_cargo;
  658. if( cargo == m_CargoGrid )
  659. {
  660. target_entity = m_Entity;
  661. target_cargo = m_Entity.GetInventory().GetCargo();
  662. }
  663. else
  664. {
  665. target_entity = m_AttachmentCargos.GetKeyByValue( cargo );
  666. if( target_entity )
  667. {
  668. target_cargo = target_entity.GetInventory().GetCargo();
  669. }
  670. else
  671. return false;
  672. }
  673. if( target_cargo && target_entity )
  674. {
  675. c_x = target_cargo.GetHeight();
  676. c_y = target_cargo.GetWidth();
  677. }
  678. else
  679. return false;
  680. if( c_x > x && c_y > y && target_entity.GetInventory().CanAddEntityInCargoEx( item, idx, x, y, item.GetInventory().GetFlipCargo() ) )
  681. {
  682. color = ColorManager.GREEN_COLOR;
  683. if( target_entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  684. {
  685. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  686. }
  687. else
  688. {
  689. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  690. }
  691. }
  692. else
  693. {
  694. color = ColorManager.RED_COLOR;
  695. }
  696. if( w.FindAnyWidget("Cursor") )
  697. {
  698. w.FindAnyWidget("Cursor").SetColor( color );
  699. }
  700. else
  701. {
  702. string name = w.GetName();
  703. name.Replace( "PanelWidget", "Cursor" );
  704. if( w.FindAnyWidget( name ) )
  705. {
  706. w.FindAnyWidget( name ).SetColor( color );
  707. }
  708. }
  709. return true;
  710. }
  711. override void DraggingOver( Widget w, int x, int y, Widget receiver )
  712. {
  713. DraggingOverHeader( w, x, y, receiver );
  714. }
  715. override void DraggingOverHeader( Widget w, int x, int y, Widget receiver )
  716. {
  717. if( w == NULL )
  718. {
  719. return;
  720. }
  721. EntityAI item = GetItemPreviewItem( w );
  722. if( !item )
  723. {
  724. return;
  725. }
  726. SlotsIcon slots_icon;
  727. receiver.GetUserData(slots_icon);
  728. EntityAI attached_entity;
  729. EntityAI receiver_item;
  730. bool is_reserved = false;
  731. int slot_id = -1;
  732. if(slots_icon)
  733. {
  734. attached_entity = slots_icon.GetSlotParent();
  735. slot_id = slots_icon.GetSlotID();
  736. receiver_item = slots_icon.GetEntity();
  737. is_reserved = slots_icon.IsReserved();
  738. }
  739. Weapon_Base wpn;
  740. Magazine mag;
  741. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  742. if( m_Entity )
  743. {
  744. if( Class.CastTo(wpn, m_Entity ) && Class.CastTo(mag, item ) )
  745. {
  746. if( player.GetWeaponManager().CanAttachMagazine( wpn, mag ) )
  747. {
  748. ItemManager.GetInstance().HideDropzones();
  749. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  750. {
  751. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  752. }
  753. else
  754. {
  755. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  756. }
  757. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  758. }
  759. }
  760. else if( receiver_item && !is_reserved )
  761. {
  762. ItemBase receiver_itemIB = ItemBase.Cast( receiver_item );
  763. ItemBase itemIB = ItemBase.Cast( item );
  764. if( receiver_itemIB && itemIB && receiver_itemIB.CanBeCombined( itemIB ) )
  765. {
  766. ItemManager.GetInstance().HideDropzones();
  767. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  768. {
  769. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  770. }
  771. else
  772. {
  773. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  774. }
  775. ColorManager.GetInstance().SetColor( w, ColorManager.COMBINE_COLOR );
  776. }
  777. else if( GameInventory.CanSwapEntitiesEx( receiver_item, item ) )
  778. {
  779. ItemManager.GetInstance().HideDropzones();
  780. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  781. {
  782. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  783. }
  784. else
  785. {
  786. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  787. }
  788. ColorManager.GetInstance().SetColor( w, ColorManager.SWAP_COLOR );
  789. }
  790. else if( receiver_itemIB.GetInventory().CanAddAttachment( item ) )
  791. {
  792. ItemManager.GetInstance().HideDropzones();
  793. if( receiver_itemIB.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  794. {
  795. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  796. }
  797. else
  798. {
  799. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  800. }
  801. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  802. }
  803. }
  804. else if( attached_entity && attached_entity.GetInventory().CanAddAttachmentEx( item, slot_id ) )
  805. {
  806. ItemManager.GetInstance().HideDropzones();
  807. if( attached_entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  808. {
  809. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  810. }
  811. else
  812. {
  813. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  814. }
  815. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  816. }
  817. else if( m_Entity.GetInventory().CanAddAttachment( item ) )
  818. {
  819. ItemManager.GetInstance().HideDropzones();
  820. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  821. {
  822. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  823. }
  824. else
  825. {
  826. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  827. }
  828. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  829. }
  830. else if( ( m_Entity.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && !m_Entity.GetInventory().HasEntityInCargo( item ) ) /*|| player.GetHumanInventory().HasEntityInHands( item )*/ )
  831. {
  832. ItemManager.GetInstance().HideDropzones();
  833. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  834. {
  835. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  836. }
  837. else
  838. {
  839. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  840. }
  841. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  842. }
  843. else
  844. {
  845. ItemManager.GetInstance().ShowSourceDropzone( item );
  846. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  847. }
  848. }
  849. }
  850. CargoContainer GetCargo()
  851. {
  852. return m_CargoGrid;
  853. }
  854. map<EntityAI, ref CargoContainer> GetAttachmentCargos()
  855. {
  856. return m_AttachmentCargos;
  857. }
  858. map<EntityAI, ref AttachmentsWrapper> GetAttachmentAttachmentsContainers()
  859. {
  860. return m_AttachmentAttachmentsContainers;
  861. }
  862. }