Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
inventoryquickbar.c
Go to the documentation of this file.
1 class InventoryQuickbar extends InventoryGridController
2 {
3 
4  protected ref TItemsMap m_Items;
5  protected InventoryGrid m_Grid;
6  protected int m_DraggingIndex;
7 
8  void InventoryQuickbar(Widget quickbarGridWidget)
9  {
10  m_DraggingIndex = INDEX_NOT_FOUND;
11  m_Items = new TItemsMap;
12  UpdateItems( quickbarGridWidget );
13  }
14 
15  void ~InventoryQuickbar()
16  {
17  }
18 
19  void UpdateItems( Widget quickbarGridWidget )
20  {
21  if( !quickbarGridWidget )
22  {
23  return;
24  }
25 
26  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
27  if ( !player )
28  return;
29 
30  int i;
31 
32  // create grid and align it to center
33  if( !m_Grid )
34  {
35  quickbarGridWidget.GetScript( m_Grid );
36  m_Grid.SetController( this );
37  m_Grid.GenerateQuickBarBackgroundTiles( 10 );
38  }
39 
40  m_Items.Clear();
41  InventoryItem item;
42 
43  if( m_Grid )
44  {
45  if( m_Grid.GetGridSize() != player.GetQuickBarSize() )
46  {
47  m_Grid.SetGridSize( player.GetQuickBarSize() );
48  }
49  }
50 
51  for( i = 0; i < m_Grid.GetGridSize(); i++)
52  {
53  item = InventoryItem.Cast( player.GetQuickBarEntity(i) );
54  if( item )
55  {
56  m_Items.Set( item, Vector(i, 1, 1) );
57  }
58  }
59 
60  if( m_Grid )
61  {
62  m_Grid.UpdateQuickbarItems( m_Items );
63  }
64  }
65 
66  void Remove(InventoryItem itemToRemove)
67  {
68  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
69 
70  for ( int i = 0; i < m_Grid.GetGridSize(); i++ )
71  {
72  InventoryItem item = InventoryItem.Cast( player.GetQuickBarEntity(i) );
73  if(item == itemToRemove)
74  {
75  player.RemoveQuickBarEntityShortcut( item );
76  }
77  }
78  }
79 
80  // InventoryGridController override
81  override void OnItemEnter(InventoryGrid grid, Widget w, int row, int col)
82  {
83  Widget quickbar = grid.GetRoot();
84  if( quickbar )
85  {
86  // quickbar.SetColor( 0xFFFFFFFF );
87  }
88  }
89 
90  override void OnItemLeave(InventoryGrid grid, Widget w)
91  {
92  Widget quickbar = grid.GetRoot();
93  if( quickbar )
94  {
95  // quickbar.SetColor( 0x7FFFFFFF );
96  }
97  }
98 
99  override void OnItemDraggingOver(InventoryGrid grid, Widget w, int row, int col)
100  {
102  InventoryItem dragged_item;
103  iw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
104  if( !iw )
105  {
106  string name = w.GetName();
107  name.Replace( "PanelWidget", "Render" );
108  iw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
109  }
110 
111  if( !iw || !iw.GetItem() )
112  {
113  return;
114  }
115 
116  if(!dragged_item && iw)
117  dragged_item = InventoryItem.Cast( iw.GetItem() );
118 
119  if ( dragged_item == NULL)
120  return;
121 
122  if (dragged_item && dragged_item.GetInventory().CanRemoveEntity() == false)
123  {
124  int color;
125 
126  color = ARGB( 150, 255, 1, 1 );
127 
128  if( w.FindAnyWidget("Cursor") )
129  {
130  w.FindAnyWidget("Cursor").SetColor( color );
131  }
132  else
133  {
134  name = w.GetName();
135  name.Replace( "PanelWidget", "Cursor" );
136  if( w.FindAnyWidget( name ) )
137  {
138  w.FindAnyWidget( name ).SetColor( color );
139  }
140  }
141  }
142  else
143  {
144  color = ARGB( 150, 1, 255, 1 );
145 
146  if( w.FindAnyWidget("Cursor") )
147  {
148  w.FindAnyWidget("Cursor").SetColor( color );
149  }
150  else
151  {
152  name = w.GetName();
153  name.Replace( "PanelWidget", "Cursor" );
154  if( w.FindAnyWidget( name ) )
155  {
156  w.FindAnyWidget( name ).SetColor( color );
157  }
158  }
159  }
160  }
161 
162  override void OnItemDropReceived(InventoryGrid grid, Widget w, int row, int col)
163  {
164  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
165 
166  ItemBase dragged_item = ItemBase.Cast( ItemManager.GetInstance().GetDraggedItem() );
167 
168  ItemPreviewWidget iw = ItemPreviewWidget.Cast( w.FindAnyWidget( "Render" ) );
169  if( !iw )
170  {
171  string name = w.GetName();
172  name.Replace( "PanelWidget", "Render" );
173  iw = ItemPreviewWidget.Cast( w.FindAnyWidget( name ) );
174  }
175 
176  if(!dragged_item && iw)
177  dragged_item = ItemBase.Cast( iw.GetItem() );
178 
179  if(dragged_item && dragged_item.GetHierarchyRootPlayer()!= GetGame().GetPlayer())
180  return;
181 
182  ItemBase quickbar_item = ItemBase.Cast( player.GetQuickBarEntity(col) );
183 
184  if (m_DraggingIndex != INDEX_NOT_FOUND)
185  {
186  dragged_item = ItemBase.Cast( player.GetQuickBarEntity(m_DraggingIndex) );
187  }
188 
189  if (dragged_item && dragged_item.GetInventory().CanRemoveEntity())
190  {
191  player.SetQuickBarEntityShortcut(dragged_item, col);
192 
193  if (quickbar_item && quickbar_item != dragged_item && m_DraggingIndex != INDEX_NOT_FOUND)
194  {
195  player.SetQuickBarEntityShortcut(quickbar_item, m_DraggingIndex);
196  }
197  }
198  m_DraggingIndex = INDEX_NOT_FOUND;
199  InventoryMenu menu = InventoryMenu.Cast( GetGame().GetUIManager().FindMenu(MENU_INVENTORY) );
200  if ( menu )
201  {
202  menu.RefreshQuickbar();
203  }
204  }
205 
206  override void OnItemDrop(InventoryGrid grid, Widget w, int row, int col)
207  {
208  if (m_DraggingIndex != INDEX_NOT_FOUND)
209  {
210  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
211  InventoryItem dragged_item = InventoryItem.Cast( player.GetQuickBarEntity(m_DraggingIndex) );
212  Remove(dragged_item);
213  m_DraggingIndex = INDEX_NOT_FOUND;
214  }
215 
216  InventoryMenu menu = InventoryMenu.Cast( GetGame().GetUIManager().FindMenu(MENU_INVENTORY) );
217  if ( menu )
218  {
219  menu.RefreshQuickbar();
220  }
221  ItemManager.GetInstance().HideDropzones();
222  }
223 
224  override void OnItemDrag(InventoryGrid grid, Widget w, int row, int col)
225  {
226  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
227  InventoryItem item = InventoryItem.Cast( player.GetQuickBarEntity(col) );
228  if (item)
229  {
230  m_DraggingIndex = col;
231  m_Grid.SetItemColor(item, InventoryGrid.ITEM_COLOR_DRAG);
232  }
233  }
234 
235  override int GetQuickbarItemColor( InventoryGrid grid, InventoryItem item )
236  {
237  int color = 0x0AFFFFFF;
238  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
239  EntityAI itemInHand = player.GetHumanInventory().GetEntityInHands();
240  if ( player && itemInHand == item )
241  {
242  if ( player.GetHumanInventory().FindUserReservedLocationIndex(item) != -1 || grid.CanAddItemInHandToInventory() )
243  color = InventoryGrid.ITEM_COLOR_QUICKBAR_H_GOOD;
244  else
245  color = InventoryGrid.ITEM_COLOR_QUICKBAR_H_BAD;
246  }
247  else if ( itemInHand )
248  {
249  if ( !grid.CanAddItemInHandToInventory() && !GameInventory.CanSwapEntitiesEx(item, itemInHand) )
250  color = InventoryGrid.ITEM_COLOR_QUICKBAR_I_BAD;
251  }
252  return color;
253  }
254 
255 
256  override int HasItemQuantity( InventoryItem item )
257  {
258  return QuantityConversions.HasItemQuantity( item );
259  }
260 
261  override string GetItemQuantityText( InventoryItem item )
262  {
263  return QuantityConversions.GetItemQuantityText( item );
264  }
265 
266  override float GetItemQuantity( InventoryItem item )
267  {
268  return QuantityConversions.GetItemQuantity( item );
269  }
270 
271  override int GetItemQuantityMax( InventoryItem item )
272  {
273  ItemBase ib = ItemBase.Cast(item);
274  return ib.m_VarQuantityMax;
275  }
276 
277  override int GetItemCount( InventoryItem item )
278  {
279  ItemBase ib = ItemBase.Cast(item);
280  return ib.m_Count;
281  }
282 
283  override bool CanAddItemInHandToInventory()
284  {
285  PlayerBase player = PlayerBase.Cast( GetGame().GetPlayer() );
286  EntityAI itemInHand = player.GetHumanInventory().GetEntityInHands();
287  return itemInHand && player.GetInventory().CanAddEntityToInventory(itemInHand);
288  }
289 }
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
InventoryGridController
Definition: inventoryquickbar.c:1
QuantityConversions
Definition: quantityconversions.c:1
INDEX_NOT_FOUND
const int INDEX_NOT_FOUND
Definition: gameplay.c:13
Remove
void Remove(Object object)
Definition: actiontargets.c:95
InventoryItem
Definition: itembase.c:13
MENU_INVENTORY
const int MENU_INVENTORY
Definition: constants.c:170
PlayerBase
Definition: playerbaseclient.c:1
ItemManager
Definition: itemmanager.c:1
name
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
GetPlayer
protected void GetPlayer()
Definition: crosshairselector.c:127
ItemPreviewWidget
Definition: gameplay.c:275
InventoryMenu
void InventoryMenu()
Definition: inventorymenu.c:20
Widget
Definition: enwidgets.c:189
Vector
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
EntityAI
Definition: building.c:5
ARGB
int ARGB(int a, int r, int g, int b)
Definition: proto.c:322
GameInventory
script counterpart to engine's class Inventory
Definition: inventory.c:78