containerwithcargo.c 11 KB

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