Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
inventorygrid.c
Go to the documentation of this file.
1// -----------------------------------------------------------
2class InventoryGridController extends ScriptedWidgetEventHandler
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) {}
15 int GetItemColor(ScriptedWidgetEventHandler grid, InventoryItem item)
16 {
17 return 0;
18 }
19 int GetQuickbarItemColor(InventoryGrid grid, InventoryItem item) {}
20 vector GetItemSize(InventoryGrid grid, InventoryItem item)
21 {
22 return Vector(0, 1, 1);
23 }
24 string GetItemQuantityText( InventoryItem item ) {}
25 int HasItemQuantity( InventoryItem item ) {}
26 float GetItemQuantity( InventoryItem item ) {}
28 int GetItemCount( InventoryItem item ) {}
30};
31
32// -----------------------------------------------------------
34typedef map<InventoryItem, vector> TItemsMap
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
46 reference bool m_IsDebugOutput;
47
48 protected ref map<int, Widget> m_BackgroundWidgets;
49 protected ref map<int, Widget> m_ItemWidgets;
50 protected ref TItemsMap m_Items;
51 protected InventoryGridController m_Controller;
52
53
54 protected Widget m_Root;
55 protected int m_GridSize;
56 protected bool m_IsMouseLeftDown;
57 protected bool m_CanAddItemInHandToInventory;
58
59 void InventoryGrid()
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
73 bool CanAddItemInHandToInventory()
74 {
75 return m_CanAddItemInHandToInventory;
76 }
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 {
94 PrintString (m_Root.GetName() + "::OnMouseEnter(" + col.ToString() + ")");
95 }
96
97 if (m_Controller) m_Controller.OnItemEnter(this, w, 0, col);
98
99 return true;
100 }
101
102 // -----------------------------------------------------------
103 override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
104 {
105 if (m_Controller) m_Controller.OnItemLeave(this, w);
106
107 return true;
108 }
109
110 // -----------------------------------------------------------
111 override bool OnMouseButtonDown(Widget w, int x, int y, int button)
112 {
113 if (button == MouseState.RIGHT || button == MouseState.LEFT)
114 {
115 int col = GetCol( w );
116
117 if ( !IsValidPos( col ) )
118 return false;
119
120 if (m_IsDebugOutput)
121 {
122 PrintString (m_Root.GetName() + "::OnMouseButtonDown(" + col.ToString() + ")");
123 }
124
125 if (m_Controller)
126 {
127 if (button == MouseState.RIGHT )
128 {
129 m_Controller.OnItemRightClick(this, w, 0, col);
130 }
131
132 if (button == MouseState.LEFT )
133 {
134 m_IsMouseLeftDown = true;
135 }
136 }
137 return true;
138 }
139 else
140 {
141 return false;
142 }
143 }
144
145 // -----------------------------------------------------------
146 override bool OnMouseButtonUp(Widget w, int x, int y, int button)
147 {
148 if ( button == MouseState.LEFT )
149 {
150 if( m_IsMouseLeftDown )
151 {
152 m_Controller.OnItemLeftClick(this, w, 0, m_col);
153 m_IsMouseLeftDown = false;
154 }
155 }
156
157 if (button == MouseState.RIGHT || button == MouseState.LEFT)
158 {
159 int col = GetCol( w );
160
161 if ( !IsValidPos( col ) )
162 return false;
163
164 if (m_IsDebugOutput)
165 {
166 PrintString (m_Root.GetName() + "::OnMouseButtonUp(" + col.ToString() + ")");
167 }
168
169 return true;
170 }
171 else
172 {
173 return false;
174 }
175 }
176
177 // -----------------------------------------------------------
178 override bool OnDoubleClick(Widget w, int x, int y, int button)
179 {
180 if (button != MouseState.LEFT) return false;
181
182 int col = GetCol( w );
183
184 if ( !IsValidPos( col ) )
185 return false;
186
187 if (m_IsDebugOutput)
188 {
189 PrintString (m_Root.GetName() + "::OnDoubleClick(" + col.ToString() + ")");
190 }
191
192 if (m_Controller) m_Controller.OnItemDoubleClick(this, w, 0, col);
193
194 return true;
195 }
196
197 //--------------------------------------------------------------------------
198 override bool OnDrop(Widget w, int x, int y, Widget reciever)
199 {
200 if (m_IsDebugOutput)
201 {
202 PrintString (m_Root.GetName() + "::OnDrop()");
203 }
204
205 if (m_Controller)
206 {
207 m_Controller.OnItemDrop(this, w, 0, m_col);
208 }
209
210 if (w)
211 {
212 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast( w.FindAnyWidget("Preview") );
213 if (item_preview)
214 {
215 item_preview.SetView( item_preview.GetItem().GetViewIndex() );
216 }
217 }
218
219 return true;
220 }
221
222 int GetCol( Widget w )
223 {
224 int index;
225
226 if( m_ItemWidgets.GetKeyByValueChecked( w, index ) )
227 return index;
228 else if( m_BackgroundWidgets.GetKeyByValueChecked( w, index ) )
229 return index;
230 else
231 return -1;
232 }
233
234 int GetColFromBg( Widget w )
235 {
236 return m_BackgroundWidgets.GetKeyByValue( w );
237 }
238
239 int GetGridSize()
240 {
241 return m_GridSize;
242 }
243
244 void SetGridSize( int size )
245 {
246 m_GridSize = size;
247 for( int i = 0; i < m_BackgroundWidgets.Count(); i++ )
248 {
249 if( i < size )
250 {
251 m_BackgroundWidgets.Get( i ).Show( true );
252 }
253 else
254 {
255 m_BackgroundWidgets.Get( i ).Show( false );
256 }
257 }
258 }
259
260 bool IsValidPos( int index )
261 {
262 return ( m_GridSize > index && index > -1 );
263 }
264
265 private Widget m_on_drag_item;
266 private int m_col;
267
268 //--------------------------------------------------------------------------
269 override bool OnDrag(Widget w, int x, int y)
270 {
271 w.SetPos(x, y);
272
273 m_col = GetCol( w );
274
275 if ( !IsValidPos( m_col ) )
276 return false;
277
278 if (m_IsDebugOutput)
279 {
280 PrintString (m_Root.GetName() + "::OnDrag(" + m_col.ToString() + ")");
281 }
282
283 if (m_Controller) m_Controller.OnItemDrag(this, w, 0, m_col);
284
285 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast( w.FindAnyWidget("Preview") );
286 if (item_preview)
287 {
288 item_preview.SetView( item_preview.GetItem().GetViewIndex() );
289 }
290 return true;
291 }
292
293 //--------------------------------------------------------------------------
294 override bool OnDraggingOver(Widget w, int x, int y, Widget reciever)
295 {
296 int col = GetCol( reciever );
297
298 if ( !IsValidPos( col ) )
299 {
300 return false;
301 }
302
303 if (m_IsDebugOutput)
304 {
305 PrintString (m_Root.GetName() + "::OnDraggingOver(" + col.ToString() + ")");
306 }
307
308 if (m_Controller) m_Controller.OnItemDraggingOver(this, w, 0, col);
309
310
311 return true;
312 }
313
314 //--------------------------------------------------------------------------
315 override bool OnDropReceived(Widget w, int x, int y, Widget reciever)
316 {
317 int col = GetCol( reciever );
318
319 if ( !IsValidPos( col ) )
320 return false;
321
322 if (m_IsDebugOutput)
323 {
324 PrintString (m_Root.GetName() + "::OnDropReceived(" + col.ToString() + ")");
325 }
326
327 if (m_Controller) m_Controller.OnItemDropReceived(this, w, 0, col);
328
329
330 return true;
331 }
332
333 TItemsMap GetItems() {
334 return m_Items;
335 }
336
337 //--------------------------------------------------------------------------
338 Widget GetItem(int index)
339 {
340 if( m_ItemWidgets.Contains( index ) )
341 return m_ItemWidgets.Get( index );
342 else
343 return null;
344 }
345
346 //--------------------------------------------------------------------------
347 Widget GetItemBackground(int index)
348 {
349 if( m_BackgroundWidgets.Contains( index ) )
350 return m_BackgroundWidgets.Get( index );
351 else
352 return null;
353 }
354
355 Widget GetRoot()
356 {
357 return m_Root;
358 }
359
360 void SetItemColor(InventoryItem item, int color)
361 {
362 if (m_Items.Contains(item))
363 {
364 vector data = m_Items.Get(item);
365 int index = Math.Round(data[0]);
366
367 Widget w = GetItem(index);
368 if (w)
369 w.SetColor(color);
370 }
371 }
372
373 void SetController(InventoryGridController controller) {
374 m_Controller = controller;
375 }
376
377 InventoryGridController GetController() {
378 return m_Controller;
379 }
380
381 //--------------------------------------------------------------------------
382 void GenerateQuickBarBackgroundTiles(int count)
383 {
384 for (int i = 0; i < count; i++)
385 {
386 Widget root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/inventory/inventoryGridBackground.layout", m_Root);
387 TextWidget label_widget = TextWidget.Cast( root_widget.FindAnyWidget( "Label1" ) );
388 TextWidget label_widget2 = TextWidget.Cast( root_widget.FindAnyWidget( "Label2" ) );
389 label_widget.SetText( (i+1).ToString() );
390 label_widget2.SetText( (i+1).ToString() );
391 m_BackgroundWidgets.Insert( i, root_widget );
392 }
393 }
394
395 //--------------------------------------------------------------------------
396 void UpdateQuickbarItems( TItemsMap items )
397 {
398 int i;
399 int c;
400 int index;
401 int width;
402 int height;
403 int row;
404 int col;
405 InventoryItem item;
406 vector data;
407 Widget bck;
408 Widget item_w;
409
410 // remove not actual items
411 c = m_Items.Count();
412 for (i = 0; i < c; i++)
413 {
414 item = m_Items.GetKey(i);
415 if( item != NULL )
416 {
417 bool remove_item = false;
418
419 if (items.Contains(item) == false)
420 {
421 remove_item = true;
422 }
423 else
424 {
425 // check items position actual
426 if ((m_Items.Get(item) - items.Get(item)).LengthSq() > 0.01)
427 {
428 // item has different position or size
429 remove_item = true;
430 }
431 }
432
433 if (remove_item)
434 {
435 RemoveItem(item);
436 c--;
437 i--;
438 }
439 }
440 }
441
442 // add new items
443 for (i = 0; i < items.Count(); i++)
444 {
445 item = items.GetKey(i);
446 data = items.Get(item);
447
448 if (m_Items.Contains(item) == false)
449 {
450 AddItem(item, data, Vector(0,0,0) );
451 }
452 }
453
454 // cache if entity in hands can be added to inventory
455 m_CanAddItemInHandToInventory = m_Controller.CanAddItemInHandToInventory();
456
457 // refresh quickbar
458 for (i = 0; i < items.Count(); i++)
459 {
460 item = items.GetKey(i);
461 data = items.Get(item);
462 RefreshQuickbarItemVariables( item, data );
463 }
464 }
465
466
467 //--------------------------------------------------------------------------
468 void UpdateItems(TItemsMap items, bool show_quantity, bool show_temperature )
469 {
470 int i;
471 int c;
472 int index;
473 int width;
474 int height;
475 int row;
476 int col;
477 InventoryItem item;
478 vector data;
479 Widget bck;
480 Widget item_w;
481
482 // remove not actual items
483 c = m_Items.Count();
484 for (i = 0; i < c; i++)
485 {
486 item = m_Items.GetKey(i);
487
488 bool remove_item = false;
489
490 if (items.Contains(item) == false)
491 {
492 remove_item = true;
493 }
494 else
495 {
496 // check items position actual
497 if ((m_Items.Get(item) - items.Get(item)).LengthSq() > 0.01)
498 {
499 // item has different position or size
500 remove_item = true;
501 }
502 }
503
504 if (remove_item)
505 {
506 RemoveItem(item);
507 c--;
508 i--;
509 }
510 }
511
512 // add new items
513 for (i = 0; i < items.Count(); i++)
514 {
515 item = items.GetKey(i);
516 data = items.Get(item);
517
518 if (m_Items.Contains(item) == false)
519 {
520 AddItem(item, data, Vector(0,0,0) );
521 }
522 }
523
524 // add new items
525 for (i = 0; i < items.Count(); i++)
526 {
527 item = items.GetKey(i);
528 data = items.Get(item);
529 // refresh quantity
530 RefreshItemVariables( item, data, show_quantity, show_temperature );
531 }
532 }
533
534 //--------------------------------------------------------------------------
535 void UpdateQuantityItems()
536 {
537 int i;
538 int index;
539 InventoryItem item;
540 vector data;
541 for (i = 0; i < m_Items.Count(); i++)
542 {
543 item = m_Items.GetKey(i);
544 data = m_Items.Get(item);
545 index = Math.Round(data[0]);
546 RefreshItemVariables( item, data, true, false );
547 }
548 }
549
550 //--------------------------------------------------------------------------
551 void UpdateTemperatureItems()
552 {
553 int i;
554 int index;
555 InventoryItem item;
556 vector data;
557 for (i = 0; i < m_Items.Count(); i++)
558 {
559 item = m_Items.GetKey(i);
560 data = m_Items.Get(item);
561 index = Math.Round(data[0]);
562 RefreshItemVariables( item, data, false, true );
563 }
564 }
565
566 //--------------------------------------------------------------------------
567 void RefreshQuickbarItemVariables(InventoryItem item, vector data)
568 {
569 int index = Math.Round(data[0]);
570 Widget bck = GetItemBackground(index);
571 if ( bck )
572 {
573 Widget item_w = bck.FindAnyWidget("GridItem");
574 if ( item_w )
575 {
576 int color = m_Controller.GetQuickbarItemColor( this, item ); // !!!!!
577 item_w.SetColor( color );
578 }
579 }
580 RefreshItemVariables( item, data, true, false );
581 }
582
583 //--------------------------------------------------------------------------
584 void RefreshItemVariables(InventoryItem item, vector data, bool show_quantity, bool show_temperature )
585 {
586 int index = Math.Round(data[0]);
587 Widget bck = GetItemBackground(index);
588 Widget item_w;
589
590 if ( bck )
591 {
592 item_w = bck.FindAnyWidget("GridItem");
593 if ( item_w )
594 {
595 int has_quantity = m_Controller.HasItemQuantity( item );
596 Widget quantity_panel = bck.FindAnyWidget("QuantityPanel");
597 TextWidget item_quantity = TextWidget.Cast( bck.FindAnyWidget("Quantity") );
598 ProgressBarWidget quantity_progress = ProgressBarWidget.Cast( bck.FindAnyWidget("QuantityBar") );
599 Widget quantity_stack = bck.FindAnyWidget("QuantityStackPanel");
600 if ( has_quantity == QUANTITY_HIDDEN )
601 {
602 quantity_panel.Show( false );
603 }
604 else
605 {
606 quantity_panel.Show( true );
607 if ( has_quantity == QUANTITY_COUNT )
608 {
609 item_quantity.SetText( m_Controller.GetItemQuantityText( item ) );
610 quantity_stack.Show( true );
611 quantity_progress.Show( false );
612 }
613 else if ( has_quantity == QUANTITY_PROGRESS )
614 {
615 float progress_max = quantity_progress.GetMax();
616 int max = m_Controller.GetItemQuantityMax( item );
617 int count = m_Controller.GetItemCount( item );
618 float quantity = m_Controller.GetItemQuantity( item );
619 if ( count > 0 )
620 {
621 max = count;
622 }
623 if ( max > 0 )
624 {
625
626 float value = Math.Round( ( quantity / max ) * 100 );
627 quantity_progress.SetCurrent( value );
628 }
629 quantity_stack.Show( false );
630 quantity_progress.Show( true );
631 }
632 }
633
634 if ( show_temperature )
635 {
636 if ( item && item.IsInherited( InventoryItem ) )
637 {
638 int color = m_Controller.GetItemColor( this, item); // !!!!!
639 if ( color )
640 {
641 item_w.SetColor( color );
642 }
643 }
644 }
645 }
646 }
647 }
648
649 //--------------------------------------------------------------------------
650 void AddItem(InventoryItem item, vector data, vector rotation)
651 {
652 m_Items.Set(item, data);
653
654 int index = Math.Round(data[0]);
655 int width = Math.Round(data[1]);
656 int height = Math.Round(data[2]);
657
658 Widget bck = GetItemBackground(index);
659 Widget item_w_bck = GetGame().GetWorkspace().CreateWidgets("gui/layouts/inventory/inventoryGridItem.layout", bck);
660 Widget item_w = item_w_bck.FindAnyWidget("GridItem");
661
662 bck.FindAnyWidget("LabelTR").Show( true );
663 bck.FindAnyWidget("LabelCC").Show( false );
664
665 m_ItemWidgets.Insert( index, item_w );
666
667 ResetItemWidget(item_w, width, height);
668
669 if ( item )
670 {
671 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast( item_w.FindAnyWidget("Preview") );
672 item_preview.SetItem(item);
673 item_preview.SetModelOrientation( rotation );
674 item_preview.SetView( item_preview.GetItem().GetViewIndex() );
675
676 RefreshItemVariables( item, data, true, true );
677 }
678 }
679
680 //--------------------------------------------------------------------------
681 void SetItem(InventoryItem item, vector data, vector rotation)
682 {
683 m_Items.Set(item, data);
684
685 int index = Math.Round(data[0]);
686 int width = Math.Round(data[1]);
687 int height = Math.Round(data[2]);
688
689 Widget bck = GetItemBackground(index);
690 Widget item_w_bck = bck.FindAnyWidget("GridItemBck");
691
692 if(item_w_bck)
693 {
694 bck.FindAnyWidget("LabelTR").Show( true );
695 bck.FindAnyWidget("LabelCC").Show( false );
696
697 Widget item_w = item_w_bck.FindAnyWidget("GridItem");
698 ResetItemWidget(item_w, width, height);
699
700 if ( item )
701 {
702 ItemPreviewWidget item_preview = ItemPreviewWidget.Cast( item_w.FindAnyWidget("Preview") );
703 item_preview.SetItem(item);
704 item_preview.Show( true );
705 item_preview.SetModelOrientation( rotation );
706 item_preview.SetView( item_preview.GetItem().GetViewIndex() );
707
708 RefreshItemVariables( item, data, true, true );
709 }
710 else
711 {
712 item_preview = ItemPreviewWidget.Cast( item_w.FindAnyWidget("Preview") );
713 item_preview.Show( false );
714 }
715 }
716 }
717
718 //--------------------------------------------------------------------------
719 protected void ResetItemWidget(Widget item_w, int width, int height)
720 {
721 if( item_w )
722 {
723 item_w.SetColor(ITEM_COLOR_NORMAL);
724 }
725 }
726
727 //--------------------------------------------------------------------------
728 void ResetItem(InventoryItem item)
729 {
730 if (m_Items.Contains(item))
731 {
732 vector data = m_Items.Get(item);
733 int index = Math.Round(data[0]);
734 int width = Math.Round(data[1]);
735 int height = Math.Round(data[2]);
736 Widget bck = GetItemBackground(index);
737 Widget item_w = bck.FindAnyWidget("GridItem");
738 ResetItemWidget(item_w, width, height);
739 RefreshItemVariables( item, data, true, true );
740 }
741 }
742
743 //--------------------------------------------------------------------------
744 bool HasItem(InventoryItem item)
745 {
746 return m_Items.Contains(item);
747 }
748
749 //--------------------------------------------------------------------------
750 void RemoveItem(InventoryItem item)
751 {
752 Widget bck;
753 Widget itemW;
754 if ( item && m_Items.Contains( item ) )
755 {
756 vector data = m_Items.Get(item);
757 int index = Math.Round(data[0]);
758 int width = Math.Round(data[1]);
759 int height = Math.Round(data[2]);
760 bck = GetItemBackground(index);
761 if( bck )
762 {
763 itemW = bck.FindAnyWidget("GridCell");
764 if( itemW )
765 delete itemW;
766 bck.FindAnyWidget("LabelTR").Show( false );
767 bck.FindAnyWidget("LabelCC").Show( true );
768 }
769
770 m_Items.Remove( item );
771 m_ItemWidgets.Remove( index );
772 }
773 }
774};
override bool OnMouseEnter(Widget w, int x, int y)
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
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
proto string ToString()
proto native CGame GetGame()
const int QUANTITY_PROGRESS
Definition constants.c:518
const int QUANTITY_COUNT
Definition constants.c:517
const int QUANTITY_HIDDEN
Definition constants.c:516
class array< Class T > PrintString
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
MouseState
Definition ensystem.c:311
Icon x
Icon y
int GetItemSize()
Definition itembase.c:7507
override bool OnDoubleClick(Widget w, int x, int y, int button)
bool OnMouseButtonDown(Widget w, int x, int y, int button)
override bool OnMouseButtonUp(Widget w, int x, int y, int button)
Definition radialmenu.c:668
void SetItem(EntityAI item)
EntityAI GetItem()
ItemBase m_Items[MAX_NUMBER_OF_INGREDIENTS]
Definition recipebase.c:28
Widget m_Root
Definition sizetochild.c:91
void OnWidgetScriptInit(Widget w)
Definition sizetochild.c:94