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