containerwithcargoandattachments.c 25 KB

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