DayZ 1.24
Loading...
Searching...
No Matches
InventoryGrid.c
Go to the documentation of this file.
1// -----------------------------------------------------------
3{
4 void OnItemEnter(InventoryGrid grid, Widget w, int row, int col) {}
5 void OnItemLeave(InventoryGrid grid, Widget w) {}
6 void OnItemDrag(InventoryGrid grid, Widget w, int row, int col) {}
7 void OnItemDraggingOver(InventoryGrid grid, Widget w, int row, int col) {}
8 void OnItemDrop(InventoryGrid grid, Widget w, int row, int col) {}
9 void OnItemDropReceived(InventoryGrid grid, Widget w, int row, int col) {}
10 void OnItemClick(InventoryGrid grid, Widget w, int row, int col) {}
11 void OnItemLeftClick(InventoryGrid grid, Widget w, int row, int col) {}
12 void OnItemRightClick(InventoryGrid grid, Widget w, int row, int col) {}
13 void OnItemDoubleClick(InventoryGrid grid, Widget w, int row, int col) {}
14 // float GetItemQuantity(InventoryGrid grid, InventoryItem item) {}
21 {
22 return Vector(0, 1, 1);
23 }
30};
31
32// -----------------------------------------------------------
35
36class InventoryGrid extends ScriptedWidgetEventHandler
37{
38 // AARRGGBB
39 static int ITEM_COLOR_QUICKBAR_NORMAL = 0x0AFFFFFF;
40 static int ITEM_COLOR_QUICKBAR_H_GOOD = 0x2A6e980d;
41 static int ITEM_COLOR_QUICKBAR_H_BAD = 0x2A980d0d;
42 static int ITEM_COLOR_QUICKBAR_I_BAD = 0x2A986e0d;
43 static int ITEM_COLOR_NORMAL = 0x0AFFFFFF;
44 static int ITEM_COLOR_DRAG = 0x0AFFFFFF;
45
47
52
53
54 protected Widget m_Root;
55 protected int m_GridSize;
56 protected bool m_IsMouseLeftDown;
58
60 {
61 m_Items = new TItemsMap;
62 m_BackgroundWidgets = new map<int, Widget>;
63 m_ItemWidgets = new map<int, Widget>;
64 }
65
66 protected void OnWidgetScriptInit(Widget w)
67 {
68 m_Root = w;
69 m_Root.SetHandler(this);
70 }
71
72 // NOTE: This is a cached value, depending on when it is called it might not be 100% accurate
77
78 // ScriptedWidgetEventHandler override
79 override bool OnUpdate(Widget w)
80 {
81 return false;
82 }
83
84 // -----------------------------------------------------------
85 override bool OnMouseEnter(Widget w, int x, int y)
86 {
87 int col = GetCol(w);
88
89 if (!IsValidPos(col))
90 return false;
91
92 if (m_IsDebugOutput)
93 PrintString(m_Root.GetName() + "::OnMouseEnter(" + col.ToString() + ")");
94
95 if (m_Controller) m_Controller.OnItemEnter(this, w, 0, col);
96
97 return true;
98 }
99
100 // -----------------------------------------------------------
101 override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
102 {
103 if (m_Controller) m_Controller.OnItemLeave(this, w);
104
105 return true;
106 }
107
108 // -----------------------------------------------------------
109 override bool OnMouseButtonDown(Widget w, int x, int y, int button)
110 {
111 if (button == MouseState.RIGHT || button == MouseState.LEFT)
112 {
113 int col = GetCol(w);
114
115 if (!IsValidPos(col))
116 return false;
117
118 if (m_IsDebugOutput)
119 PrintString(m_Root.GetName() + "::OnMouseButtonDown(" + col.ToString() + ")");
120
121 if (m_Controller)
122 {
123 if (button == MouseState.RIGHT)
124 m_Controller.OnItemRightClick(this, w, 0, col);
125
126 if (button == MouseState.LEFT)
127 m_IsMouseLeftDown = true;
128 }
129 return true;
130 }
131 else
132 return false;
133 }
134
135 // -----------------------------------------------------------
136 override bool OnMouseButtonUp(Widget w, int x, int y, int button)
137 {
138 if (button == MouseState.LEFT)
139 {
140 if (m_IsMouseLeftDown)
141 {
142 m_Controller.OnItemLeftClick(this, w, 0, m_col);
143 m_IsMouseLeftDown = false;
144 }
145 }
146
147 if (button == MouseState.RIGHT || button == MouseState.LEFT)
148 {
149 int col = GetCol(w);
150
151 if (!IsValidPos(col))
152 return false;
153
154 if (m_IsDebugOutput)
155 PrintString(m_Root.GetName() + "::OnMouseButtonUp(" + col.ToString() + ")");
156
157 return true;
158 }
159 else
160 return false;
161 }
162
163 // -----------------------------------------------------------
164 override bool OnDoubleClick(Widget w, int x, int y, int button)
165 {
166 if (button != MouseState.LEFT) return false;
167
168 int col = GetCol(w);
169
170 if (!IsValidPos(col))
171 return false;
172
173 if (m_IsDebugOutput)
174 PrintString(m_Root.GetName() + "::OnDoubleClick(" + col.ToString() + ")");
175
176 if (m_Controller) m_Controller.OnItemDoubleClick(this, w, 0, col);
177
178 return true;
179 }
180
181 //--------------------------------------------------------------------------
182 override bool OnDrop(Widget w, int x, int y, Widget reciever)
183 {
184 if (m_IsDebugOutput)
185 PrintString(m_Root.GetName() + "::OnDrop()");
186
187 if (m_Controller)
188 m_Controller.OnItemDrop(this, w, 0, m_col);
189
190 if (w)
191 {
192 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast(w.FindAnyWidget("Preview"));
193 if (item_preview)
194 item_preview.SetView(item_preview.GetItem().GetViewIndex());
195 }
196
197 return true;
198 }
199
201 {
202 int index;
203
204 if (m_ItemWidgets.GetKeyByValueChecked(w, index))
205 return index;
206 else if (m_BackgroundWidgets.GetKeyByValueChecked(w, index))
207 return index;
208 else
209 return -1;
210 }
211
213 {
214 return m_BackgroundWidgets.GetKeyByValue(w);
215 }
216
218 {
219 return m_GridSize;
220 }
221
223 {
224 m_GridSize = size;
225 for (int i = 0; i < m_BackgroundWidgets.Count(); i++)
226 {
227 if (i < size)
228 m_BackgroundWidgets.Get(i).Show(true);
229 else
230 m_BackgroundWidgets.Get(i).Show(false);
231 }
232 }
233
235 {
236 return (m_GridSize > index && index > -1);
237 }
238
240 private int m_col;
241
242 //--------------------------------------------------------------------------
243 override bool OnDrag(Widget w, int x, int y)
244 {
245 w.SetPos(x, y);
246
247 m_col = GetCol(w);
248
249 if (!IsValidPos(m_col))
250 return false;
251
252 if (m_IsDebugOutput)
253 PrintString(m_Root.GetName() + "::OnDrag(" + m_col.ToString() + ")");
254
255 if (m_Controller) m_Controller.OnItemDrag(this, w, 0, m_col);
256
257 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast(w.FindAnyWidget("Preview"));
258 if (item_preview)
259 item_preview.SetView(item_preview.GetItem().GetViewIndex());
260 return true;
261 }
262
263 //--------------------------------------------------------------------------
264 override bool OnDraggingOver(Widget w, int x, int y, Widget reciever)
265 {
266 int col = GetCol(reciever);
267
268 if (!IsValidPos(col))
269 return false;
270
271 if (m_IsDebugOutput)
272 PrintString(m_Root.GetName() + "::OnDraggingOver(" + col.ToString() + ")");
273
274 if (m_Controller) m_Controller.OnItemDraggingOver(this, w, 0, col);
275
276
277 return true;
278 }
279
280 //--------------------------------------------------------------------------
281 override bool OnDropReceived(Widget w, int x, int y, Widget reciever)
282 {
283 int col = GetCol(reciever);
284
285 if (!IsValidPos(col))
286 return false;
287
288 if (m_IsDebugOutput)
289 PrintString(m_Root.GetName() + "::OnDropReceived(" + col.ToString() + ")");
290
291 if (m_Controller) m_Controller.OnItemDropReceived(this, w, 0, col);
292
293
294 return true;
295 }
296
298 {
299 return m_Items;
300 }
301
302 //--------------------------------------------------------------------------
304 {
305 if (m_ItemWidgets.Contains(index))
306 return m_ItemWidgets.Get(index);
307 else
308 return null;
309 }
310
311 //--------------------------------------------------------------------------
313 {
314 if (m_BackgroundWidgets.Contains(index))
315 return m_BackgroundWidgets.Get(index);
316 else
317 return null;
318 }
319
321 {
322 return m_Root;
323 }
324
326 {
327 if (m_Items.Contains(item))
328 {
329 vector data = m_Items.Get(item);
330 int index = Math.Round(data[0]);
331
333 if (w)
334 w.SetColor(color);
335 }
336 }
337
339 {
340 m_Controller = controller;
341 }
342
347
348 //--------------------------------------------------------------------------
350 {
351 for (int i = 0; i < count; i++)
352 {
353 Widget root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/inventory/inventoryGridBackground.layout", m_Root);
354 TextWidget label_widget = TextWidget.Cast(root_widget.FindAnyWidget("Label1"));
355 TextWidget label_widget2 = TextWidget.Cast(root_widget.FindAnyWidget("Label2"));
356 label_widget.SetText((i + 1).ToString());
357 label_widget2.SetText((i + 1).ToString());
358 m_BackgroundWidgets.Insert(i, root_widget);
359 }
360 }
361
362 //--------------------------------------------------------------------------
364 {
365 int i;
366 int c;
367 int index;
368 int width;
369 int height;
370 int row;
371 int col;
373 vector data;
374 Widget bck;
376
377 // remove not actual items
378 c = m_Items.Count();
379 for (i = 0; i < c; i++)
380 {
381 item = m_Items.GetKey(i);
382 if (item != NULL)
383 {
384 bool remove_item = false;
385
386 if (items.Contains(item) == false)
387 remove_item = true;
388 else
389 {
390 // check items position actual
391 if ((m_Items.Get(item) - items.Get(item)).LengthSq() > 0.01)
392 {
393 // item has different position or size
394 remove_item = true;
395 }
396 }
397
398 if (remove_item)
399 {
401 c--;
402 i--;
403 }
404 }
405 }
406
407 // add new items
408 for (i = 0; i < items.Count(); i++)
409 {
410 item = items.GetKey(i);
411 data = items.Get(item);
412
413 if (m_Items.Contains(item) == false)
414 AddItem(item, data, Vector(0, 0, 0));
415 }
416
417 // cache if entity in hands can be added to inventory
418 m_CanAddItemInHandToInventory = m_Controller.CanAddItemInHandToInventory();
419
420 // refresh quickbar
421 for (i = 0; i < items.Count(); i++)
422 {
423 item = items.GetKey(i);
424 data = items.Get(item);
426 }
427 }
428
429
430 //--------------------------------------------------------------------------
432 {
433 int i;
434 int c;
435 int index;
436 int width;
437 int height;
438 int row;
439 int col;
441 vector data;
442 Widget bck;
444
445 // remove not actual items
446 c = m_Items.Count();
447 for (i = 0; i < c; i++)
448 {
449 item = m_Items.GetKey(i);
450
451 bool remove_item = false;
452
453 if (items.Contains(item) == false)
454 remove_item = true;
455 else
456 {
457 // check items position actual
458 if ((m_Items.Get(item) - items.Get(item)).LengthSq() > 0.01)
459 {
460 // item has different position or size
461 remove_item = true;
462 }
463 }
464
465 if (remove_item)
466 {
468 c--;
469 i--;
470 }
471 }
472
473 // add new items
474 for (i = 0; i < items.Count(); i++)
475 {
476 item = items.GetKey(i);
477 data = items.Get(item);
478
479 if (m_Items.Contains(item) == false)
480 AddItem(item, data, Vector(0, 0, 0));
481 }
482
483 // add new items
484 for (i = 0; i < items.Count(); i++)
485 {
486 item = items.GetKey(i);
487 data = items.Get(item);
488 // refresh quantity
490 }
491 }
492
493 //--------------------------------------------------------------------------
495 {
496 int i;
497 int index;
499 vector data;
500 for (i = 0; i < m_Items.Count(); i++)
501 {
502 item = m_Items.GetKey(i);
503 data = m_Items.Get(item);
504 index = Math.Round(data[0]);
505 RefreshItemVariables(item, data, true, false);
506 }
507 }
508
509 //--------------------------------------------------------------------------
511 {
512 int i;
513 int index;
515 vector data;
516 for (i = 0; i < m_Items.Count(); i++)
517 {
518 item = m_Items.GetKey(i);
519 data = m_Items.Get(item);
520 index = Math.Round(data[0]);
521 RefreshItemVariables(item, data, false, true);
522 }
523 }
524
525 //--------------------------------------------------------------------------
527 {
528 int index = Math.Round(data[0]);
530 if (bck)
531 {
532 Widget item_w = bck.FindAnyWidget("GridItem");
533 if (item_w)
534 {
535 int color = m_Controller.GetQuickbarItemColor(this, item); // !!!!!
536 item_w.SetColor(color);
537 }
538 }
539 RefreshItemVariables(item, data, true, false);
540 }
541
542 //--------------------------------------------------------------------------
544 {
545 int index = Math.Round(data[0]);
548
549 if (bck)
550 {
551 item_w = bck.FindAnyWidget("GridItem");
552 if (item_w)
553 {
554 int has_quantity = m_Controller.HasItemQuantity(item);
555 Widget quantity_panel = bck.FindAnyWidget("QuantityPanel");
556 TextWidget item_quantity = TextWidget.Cast(bck.FindAnyWidget("Quantity"));
557 ProgressBarWidget quantity_progress = ProgressBarWidget.Cast(bck.FindAnyWidget("QuantityBar"));
558 Widget quantity_stack = bck.FindAnyWidget("QuantityStackPanel");
560 quantity_panel.Show(false);
561 else
562 {
563 quantity_panel.Show(true);
565 {
566 item_quantity.SetText(m_Controller.GetItemQuantityText(item));
567 quantity_stack.Show(true);
568 quantity_progress.Show(false);
569 }
571 {
572 float progress_max = quantity_progress.GetMax();
573 int max = m_Controller.GetItemQuantityMax(item);
574 int count = m_Controller.GetItemCount(item);
575 float quantity = m_Controller.GetItemQuantity(item);
576 if (count > 0)
577 max = count;
578 if (max > 0)
579 {
580
581 float value = Math.Round((quantity / max) * 100);
582 quantity_progress.SetCurrent(value);
583 }
584 quantity_stack.Show(false);
585 quantity_progress.Show(true);
586 }
587 }
588
590 {
591 if (item && item.IsInherited(InventoryItem))
592 {
593 int color = m_Controller.GetItemColor(this, item); // !!!!!
594 if (color)
595 item_w.SetColor(color);
596 }
597 }
598 }
599 }
600 }
601
602 //--------------------------------------------------------------------------
604 {
605 m_Items.Set(item, data);
606
607 int index = Math.Round(data[0]);
608 int width = Math.Round(data[1]);
609 int height = Math.Round(data[2]);
610
612 Widget item_w_bck = GetGame().GetWorkspace().CreateWidgets("gui/layouts/inventory/inventoryGridItem.layout", bck);
613 Widget item_w = item_w_bck.FindAnyWidget("GridItem");
614
615 bck.FindAnyWidget("LabelTR").Show(true);
616 bck.FindAnyWidget("LabelCC").Show(false);
617
618 m_ItemWidgets.Insert(index, item_w);
619
621
622 if (item)
623 {
624 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast(item_w.FindAnyWidget("Preview"));
625 item_preview.SetItem(item);
626 item_preview.SetModelOrientation(rotation);
627 item_preview.SetView(item_preview.GetItem().GetViewIndex());
628
629 RefreshItemVariables(item, data, true, true);
630 }
631 }
632
633 //--------------------------------------------------------------------------
635 {
636 m_Items.Set(item, data);
637
638 int index = Math.Round(data[0]);
639 int width = Math.Round(data[1]);
640 int height = Math.Round(data[2]);
641
643 Widget item_w_bck = bck.FindAnyWidget("GridItemBck");
644
645 if (item_w_bck)
646 {
647 bck.FindAnyWidget("LabelTR").Show(true);
648 bck.FindAnyWidget("LabelCC").Show(false);
649
650 Widget item_w = item_w_bck.FindAnyWidget("GridItem");
652
653 if (item)
654 {
655 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast(item_w.FindAnyWidget("Preview"));
656 item_preview.SetItem(item);
657 item_preview.Show(true);
658 item_preview.SetModelOrientation(rotation);
659 item_preview.SetView(item_preview.GetItem().GetViewIndex());
660
661 RefreshItemVariables(item, data, true, true);
662 }
663 else
664 {
665 item_preview = ItemPreviewWidget.Cast(item_w.FindAnyWidget("Preview"));
666 item_preview.Show(false);
667 }
668 }
669 }
670
671 //--------------------------------------------------------------------------
672 protected void ResetItemWidget(Widget item_w, int width, int height)
673 {
674 if (item_w)
675 item_w.SetColor(ITEM_COLOR_NORMAL);
676 }
677
678 //--------------------------------------------------------------------------
680 {
681 if (m_Items.Contains(item))
682 {
683 vector data = m_Items.Get(item);
684 int index = Math.Round(data[0]);
685 int width = Math.Round(data[1]);
686 int height = Math.Round(data[2]);
688 Widget item_w = bck.FindAnyWidget("GridItem");
690 RefreshItemVariables(item, data, true, true);
691 }
692 }
693
694 //--------------------------------------------------------------------------
696 {
697 return m_Items.Contains(item);
698 }
699
700 //--------------------------------------------------------------------------
702 {
703 Widget bck;
705 if (item && m_Items.Contains(item))
706 {
707 vector data = m_Items.Get(item);
708 int index = Math.Round(data[0]);
709 int width = Math.Round(data[1]);
710 int height = Math.Round(data[2]);
712 if (bck)
713 {
714 itemW = bck.FindAnyWidget("GridCell");
715 if (itemW)
716 delete itemW;
717 bck.FindAnyWidget("LabelTR").Show(false);
718 bck.FindAnyWidget("LabelCC").Show(true);
719 }
720
721 m_Items.Remove(item);
722 m_ItemWidgets.Remove(index);
723 }
724 }
725};
proto string ToString()
Icon x
Icon y
EntityAI GetItem()
Widget m_Root
Definition SizeToChild.c:85
override void OnItemDrag(InventoryGrid grid, Widget w, int row, int col)
override bool CanAddItemInHandToInventory()
override int GetItemQuantityMax(InventoryItem item)
override int GetItemCount(InventoryItem item)
override void OnItemDropReceived(InventoryGrid grid, Widget w, int row, int col)
override void OnItemLeave(InventoryGrid grid, Widget w)
override void OnItemEnter(InventoryGrid grid, Widget w, int row, int col)
override int HasItemQuantity(InventoryItem item)
override int GetQuickbarItemColor(InventoryGrid grid, InventoryItem item)
override void OnItemDraggingOver(InventoryGrid grid, Widget w, int row, int col)
override void OnItemDrop(InventoryGrid grid, Widget w, int row, int col)
override float GetItemQuantity(InventoryItem item)
override string GetItemQuantityText(InventoryItem item)
Definition EnMath.c:7
map: item x vector(index, width, height)
Definition EnWidgets.c:651
void OnItemDropReceived(InventoryGrid grid, Widget w, int row, int col)
void SetItem(InventoryItem item, vector data, vector rotation)
reference bool m_IsDebugOutput
void OnItemLeftClick(InventoryGrid grid, Widget w, int row, int col)
void AddItem(InventoryItem item, vector data, vector rotation)
void OnWidgetScriptInit(Widget w)
override bool OnMouseButtonUp(Widget w, int x, int y, int button)
override bool OnDoubleClick(Widget w, int x, int y, int button)
override bool OnDraggingOver(Widget w, int x, int y, Widget reciever)
override bool OnDropReceived(Widget w, int x, int y, Widget reciever)
ref map< int, Widget > m_BackgroundWidgets
void SetController(InventoryGridController controller)
bool HasItem(InventoryItem item)
void OnItemDrag(InventoryGrid grid, Widget w, int row, int col)
int GetQuickbarItemColor(InventoryGrid grid, InventoryItem item)
void OnItemLeave(InventoryGrid grid, Widget w)
override bool OnDrag(Widget w, int x, int y)
void OnItemDraggingOver(InventoryGrid grid, Widget w, int row, int col)
override bool OnUpdate(Widget w)
void OnItemClick(InventoryGrid grid, Widget w, int row, int col)
int HasItemQuantity(InventoryItem item)
void OnItemRightClick(InventoryGrid grid, Widget w, int row, int col)
float GetItemQuantity(InventoryItem item)
void RefreshQuickbarItemVariables(InventoryItem item, vector data)
InventoryGridController m_Controller
void OnItemDrop(InventoryGrid grid, Widget w, int row, int col)
override bool OnDrop(Widget w, int x, int y, Widget reciever)
void RemoveItem(InventoryItem item)
void GenerateQuickBarBackgroundTiles(int count)
InventoryGridController GetController()
void UpdateItems(TItemsMap items, bool show_quantity, bool show_temperature)
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
int GetItemColor(ScriptedWidgetEventHandler grid, InventoryItem item)
ref map< int, Widget > m_ItemWidgets
Widget GetItem(int index)
void OnItemEnter(InventoryGrid grid, Widget w, int row, int col)
void RefreshItemVariables(InventoryItem item, vector data, bool show_quantity, bool show_temperature)
void OnItemDoubleClick(InventoryGrid grid, Widget w, int row, int col)
int GetItemCount(InventoryItem item)
void ResetItemWidget(Widget item_w, int width, int height)
void UpdateQuickbarItems(TItemsMap items)
void SetItemColor(InventoryItem item, int color)
override bool OnMouseEnter(Widget w, int x, int y)
void ResetItem(InventoryItem item)
vector GetItemSize(InventoryGrid grid, InventoryItem item)
Widget GetItemBackground(int index)
string GetItemQuantityText(InventoryItem item)
override bool OnMouseButtonDown(Widget w, int x, int y, int button)
int GetItemQuantityMax(InventoryItem item)
proto native CGame GetGame()
const int QUANTITY_PROGRESS
Definition constants.c:484
const int QUANTITY_COUNT
Definition constants.c:483
const int QUANTITY_HIDDEN
Definition constants.c:482
void PrintString(string s)
Helper for printing out string expression. Example: PrintString("Hello " + var);.
Definition EnScript.c:344
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
static proto float Round(float f)
Returns mathematical round of value.
MouseState
Definition EnSystem.c:311