containerwithcargo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. class ContainerWithCargo extends ClosableContainer
  2. {
  3. protected ref CargoContainer m_CargoGrid;
  4. protected int m_CargoIndex = -1;
  5. void ContainerWithCargo( LayoutHolder parent, int sort = -1 )
  6. {
  7. m_LockCargo = false;
  8. m_CargoGrid = new CargoContainer( this );
  9. Insert( m_CargoGrid );
  10. m_CargoGrid.GetRootWidget().SetSort( 1 );
  11. WidgetEventHandler.GetInstance().RegisterOnDraggingOver( m_MainWidget, this, "DraggingOverGrid" );
  12. RecomputeOpenedContainers();
  13. }
  14. override bool IsDisplayable()
  15. {
  16. if (m_Entity)
  17. return m_Entity.CanDisplayCargo();
  18. return false;
  19. }
  20. override bool IsEmpty()
  21. {
  22. return m_CargoGrid.IsEmpty();
  23. }
  24. override bool IsItemActive()
  25. {
  26. return m_CargoGrid.IsItemActive();
  27. }
  28. override bool CanCombine()
  29. {
  30. return m_CargoGrid.CanCombine();
  31. }
  32. override bool CanCombineAmmo()
  33. {
  34. return m_CargoGrid.CanCombineAmmo();
  35. }
  36. override bool IsItemWithQuantityActive()
  37. {
  38. return m_CargoGrid.IsItemWithQuantityActive();
  39. }
  40. void LockCargo(bool value)
  41. {
  42. if( value != m_LockCargo )
  43. {
  44. if( value )
  45. {
  46. m_LockCargo = true;
  47. OnHide();
  48. }
  49. else
  50. {
  51. m_LockCargo = false;
  52. SetOpenState(!m_Closed);
  53. }
  54. }
  55. }
  56. override void Open()
  57. {
  58. if( !m_LockCargo )
  59. {
  60. ItemManager.GetInstance().SetDefaultOpenState( m_Entity.GetType(), true );
  61. m_Closed = false;
  62. SetOpenForSlotIcon(true);
  63. OnShow();
  64. m_Parent.m_Parent.Refresh();
  65. }
  66. if ( m_SlotIcon )
  67. {
  68. m_SlotIcon.GetRadialIconPanel().Show( !m_LockCargo );
  69. }
  70. }
  71. override void Close()
  72. {
  73. if( !m_LockCargo )
  74. {
  75. ItemManager.GetInstance().SetDefaultOpenState( m_Entity.GetType(), false );
  76. m_Closed = true;
  77. SetOpenForSlotIcon(false);
  78. OnHide();
  79. m_Parent.m_Parent.Refresh();
  80. }
  81. if ( m_SlotIcon )
  82. {
  83. m_SlotIcon.GetRadialIconPanel().Show( !m_LockCargo );
  84. }
  85. }
  86. override bool IsOpened()
  87. {
  88. return !m_Closed && !m_LockCargo;
  89. }
  90. override void UpdateInterval()
  91. {
  92. if ( m_Entity )
  93. {
  94. if ( m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() )
  95. {
  96. LockCargo(true);
  97. if ( m_CargoGrid.IsVisible() )
  98. {
  99. RecomputeOpenedContainers();
  100. }
  101. }
  102. else
  103. {
  104. LockCargo(false);
  105. if ( !m_CargoGrid.IsVisible() )
  106. {
  107. RecomputeOpenedContainers();
  108. }
  109. }
  110. super.UpdateInterval();
  111. m_CargoGrid.UpdateInterval();
  112. bool hide = m_LockCargo || ItemManager.GetInstance().GetDraggedItem() == m_Entity;
  113. if (!hide)
  114. {
  115. SetOpenForSlotIcon(IsOpened());
  116. }
  117. if ( m_SlotIcon )
  118. {
  119. m_SlotIcon.GetRadialIconPanel().Show( !hide );
  120. }
  121. }
  122. }
  123. override bool IsFirstContainerFocused()
  124. {
  125. return m_CargoGrid.IsFirstContainerFocused();
  126. }
  127. override bool IsLastContainerFocused()
  128. {
  129. return m_CargoGrid.IsLastContainerFocused();
  130. }
  131. override void SetDefaultFocus( bool while_micromanagment_mode = false )
  132. {
  133. m_CargoGrid.SetDefaultFocus( while_micromanagment_mode );
  134. }
  135. override void UnfocusAll()
  136. {
  137. m_CargoGrid.Unfocus();
  138. }
  139. override bool SplitItem()
  140. {
  141. return m_CargoGrid.SplitItem();
  142. }
  143. override bool EquipItem()
  144. {
  145. return m_CargoGrid.EquipItem();
  146. }
  147. override bool TransferItem()
  148. {
  149. return m_CargoGrid.TransferItem();
  150. }
  151. override bool TransferItemToVicinity()
  152. {
  153. return m_CargoGrid.TransferItemToVicinity();
  154. }
  155. override bool InspectItem()
  156. {
  157. return m_CargoGrid.InspectItem();
  158. }
  159. void SetEntity( EntityAI entity, int cargo_index = 0, bool immedUpdate = true )
  160. {
  161. m_Entity = entity;
  162. m_CargoIndex = cargo_index;
  163. SetOpenState( true );
  164. m_CargoGrid.SetEntity( entity, immedUpdate );
  165. m_CargoGrid.UpdateHeaderText();
  166. m_ClosableHeader.SetItemPreview( entity );
  167. CheckHeaderDragability();
  168. ( Container.Cast( m_Parent ) ).Insert( this, -1, false );
  169. if ( m_Entity.GetInventory().IsInventoryLockedForLockType( HIDE_INV_FROM_SCRIPT ) || !m_Entity.CanDisplayCargo() )
  170. LockCargo(true);
  171. else
  172. LockCargo(false);
  173. if ( immedUpdate )
  174. {
  175. Refresh();
  176. GetMainWidget().Update();
  177. }
  178. }
  179. EntityAI GetEntity()
  180. {
  181. return m_Entity;
  182. }
  183. override EntityAI GetFocusedContainerEntity()
  184. {
  185. return m_Entity;
  186. }
  187. EntityAI GetItemPreviewItem( Widget w )
  188. {
  189. ItemPreviewWidget ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
  190. if( !ipw )
  191. {
  192. string name = w.GetName();
  193. name.Replace( "PanelWidget", "Render" );
  194. ipw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
  195. }
  196. if( !ipw )
  197. {
  198. ipw = ItemPreviewWidget.Cast( w );
  199. }
  200. if( !ipw || !ipw.IsInherited( ItemPreviewWidget ) )
  201. {
  202. return null;
  203. }
  204. return ipw.GetItem();
  205. }
  206. bool DraggingOverGrid( Widget w, int x, int y, Widget reciever )
  207. {
  208. if( w == null )
  209. {
  210. return false;
  211. }
  212. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  213. if( !player.CanManipulateInventory() )
  214. {
  215. return false;
  216. }
  217. EntityAI item = GetItemPreviewItem( w );
  218. if( !item )
  219. {
  220. return false;
  221. }
  222. if( !item.GetInventory().CanRemoveEntity() )
  223. {
  224. return false;
  225. }
  226. int color, c_x, c_y;
  227. #ifdef PLATFORM_CONSOLE
  228. int idx = -1;
  229. #else
  230. int idx = 0;
  231. #endif
  232. CargoBase cargo = m_Entity.GetInventory().GetCargoFromIndex(m_CargoIndex);
  233. if( cargo )
  234. {
  235. c_x = cargo.GetHeight();
  236. c_y = cargo.GetWidth();
  237. }
  238. InventoryLocation dst = new InventoryLocation;
  239. #ifdef PLATFORM_CONSOLE
  240. x = 0;
  241. y = cargo.GetItemCount();
  242. m_Entity.GetInventory().FindFreeLocationFor( item, FindInventoryLocationType.CARGO, dst );
  243. #else
  244. dst.SetCargoAuto(cargo, item, x, y, item.GetInventory().GetFlipCargo());
  245. #endif
  246. #ifdef PLATFORM_CONSOLE
  247. if( dst.IsValid() && m_Entity.GetInventory().LocationCanAddEntityEx(dst))
  248. #else
  249. if( m_Entity && c_x > x && c_y > y && m_Entity.GetInventory().LocationCanAddEntityEx(dst))
  250. #endif
  251. {
  252. ItemManager.GetInstance().HideDropzones();
  253. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  254. {
  255. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  256. }
  257. else
  258. {
  259. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  260. }
  261. color = ColorManager.GREEN_COLOR;
  262. }
  263. else
  264. {
  265. color = ColorManager.RED_COLOR;
  266. ItemManager.GetInstance().ShowSourceDropzone( item );
  267. }
  268. if( w.FindAnyWidget("Cursor") )
  269. {
  270. w.FindAnyWidget("Cursor").SetColor( color );
  271. }
  272. else
  273. {
  274. string name = w.GetName();
  275. name.Replace( "PanelWidget", "Cursor" );
  276. if( w.FindAnyWidget( name ) )
  277. {
  278. w.FindAnyWidget( name ).SetColor( color );
  279. }
  280. }
  281. return true;
  282. }
  283. void DropReceived( Widget w, int x, int y )
  284. {
  285. float xx, yy;
  286. GetMainWidget().Update();
  287. GetMainWidget().GetScreenSize( xx, yy );
  288. if( GetMainWidget().FindAnyWidget("Background") )
  289. {
  290. GetMainWidget().FindAnyWidget("Background").Show( true );
  291. GetMainWidget().FindAnyWidget("Background").SetSize( xx, yy );
  292. }
  293. EntityAI item = GetItemPreviewItem( w );
  294. if( !item )
  295. {
  296. return;
  297. }
  298. #ifdef PLATFORM_CONSOLE
  299. if( m_CargoGrid.HasItem( item ) )
  300. {
  301. return;
  302. }
  303. #endif
  304. #ifdef PLATFORM_CONSOLE
  305. int idx = -1;
  306. #else
  307. int idx = 0;
  308. #endif
  309. int c_x, c_y;
  310. CargoBase cargo = m_Entity.GetInventory().GetCargoFromIndex(m_CargoIndex);
  311. if( cargo )
  312. {
  313. c_x = cargo.GetHeight();
  314. c_y = cargo.GetWidth();
  315. }
  316. InventoryLocation dst = new InventoryLocation;
  317. #ifdef PLATFORM_CONSOLE
  318. x = 0;
  319. y = cargo.GetItemCount();
  320. m_Entity.GetInventory().FindFreeLocationFor( item, FindInventoryLocationType.CARGO, dst );
  321. #else
  322. dst.SetCargoAuto(cargo, item, x, y, item.GetInventory().GetFlipCargo());
  323. #endif
  324. InventoryLocation src = new InventoryLocation;
  325. item.GetInventory().GetCurrentInventoryLocation(src);
  326. if(src.CompareLocationOnly(dst) && src.GetFlip() == dst.GetFlip())
  327. return;
  328. #ifdef PLATFORM_CONSOLE
  329. if(dst.IsValid() && m_Entity.GetInventory().LocationCanAddEntityEx(dst))
  330. #else
  331. if( c_x > x && c_y > y && m_Entity.GetInventory().LocationCanAddEntityEx(dst))
  332. #endif
  333. {
  334. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  335. SplitItemUtils.TakeOrSplitToInventoryLocation( player, dst );
  336. Icon icon = m_CargoGrid.GetIcon( item );
  337. if( icon && w && w.FindAnyWidget("Cursor") )
  338. {
  339. w.FindAnyWidget("Cursor").SetColor( ColorManager.BASE_COLOR );
  340. icon.Refresh();
  341. Refresh();
  342. }
  343. }
  344. ItemManager.GetInstance().HideDropzones();
  345. ItemManager.GetInstance().SetIsDragging( false );
  346. }
  347. void TakeIntoHands( notnull PlayerBase player, notnull EntityAI item )
  348. {
  349. ItemBase item_base = ItemBase.Cast( item );
  350. if( !item.GetInventory().CanRemoveEntity() || !player.CanManipulateInventory() )
  351. return;
  352. float stackable = item_base.GetTargetQuantityMax();
  353. if( stackable == 0 || stackable >= item_base.GetQuantity() )
  354. {
  355. player.PredictiveTakeEntityToHands( item );
  356. }
  357. else if( stackable != 0 && stackable < item_base.GetQuantity() )
  358. {
  359. item_base.SplitIntoStackMaxHandsClient( player );
  360. }
  361. }
  362. override void DraggingOverHeader( Widget w, int x, int y, Widget receiver )
  363. {
  364. DraggingOver( w, x, y, receiver );
  365. }
  366. override void DraggingOver( Widget w, int x, int y, Widget receiver )
  367. {
  368. if( !w )
  369. return;
  370. EntityAI item = GetItemPreviewItem( w );
  371. if( !item )
  372. return;
  373. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  374. if( item.GetInventory().CanRemoveEntity() && player.CanManipulateInventory() && m_Entity.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() ) && !m_Entity.GetInventory().HasEntityInCargo( item ) )
  375. {
  376. ColorManager.GetInstance().SetColor( w, ColorManager.GREEN_COLOR );
  377. ItemManager.GetInstance().HideDropzones();
  378. if( m_Entity.GetHierarchyRootPlayer() == GetGame().GetPlayer() )
  379. {
  380. ItemManager.GetInstance().GetRightDropzone().SetAlpha( 1 );
  381. }
  382. else
  383. {
  384. ItemManager.GetInstance().GetLeftDropzone().SetAlpha( 1 );
  385. }
  386. }
  387. else
  388. {
  389. ColorManager.GetInstance().SetColor( w, ColorManager.RED_COLOR );
  390. ItemManager.GetInstance().ShowSourceDropzone( item );
  391. }
  392. }
  393. override void OnDropReceivedFromHeader( Widget w, int x, int y, Widget receiver )
  394. {
  395. EntityAI item = GetItemPreviewItem( w );
  396. if( !item )
  397. {
  398. return;
  399. }
  400. PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
  401. InventoryLocation src = new InventoryLocation();
  402. if( GetEntity() && item.GetInventory().GetCurrentInventoryLocation(src))
  403. {
  404. InventoryLocation dst = new InventoryLocation();
  405. if (m_Entity.GetInventory().FindFreeLocationFor(item, FindInventoryLocationType.ATTACHMENT, dst))
  406. {
  407. player.PredictiveTakeToDst(src, dst);
  408. }
  409. bool can_add = m_Entity.GetInventory().CanAddEntityInCargo( item, item.GetInventory().GetFlipCargo() );
  410. bool in_cargo = !player.GetInventory().HasEntityInInventory( item ) || !m_Entity.GetInventory().HasEntityInCargo( item );
  411. if( can_add && in_cargo )
  412. {
  413. SplitItemUtils.TakeOrSplitToInventory( player, m_Entity, item );
  414. }
  415. }
  416. }
  417. CargoContainer GetCargo()
  418. {
  419. return m_CargoGrid;
  420. }
  421. }