Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
contextmenu.c
Go to the documentation of this file.
1 //--------------------------------------------------------------------------
2 class ContextMenu extends ScriptedWidgetEventHandler
3 {
4  private Widget m_context_menu_root_widget;
5  private Widget m_context_menu_panel_widget;
6  private ref array<ref CallQueueContext> m_commands;
7  private int m_max_item_width;
8  private int m_count;
9  const int ITEMS_COUNT = 27;
10  //--------------------------------------------------------------------------
11  void ContextMenu()
12  {
13  m_commands = new array<ref CallQueueContext>;
14  m_count = 0;
15  }
16  //--------------------------------------------------------------------------
17  void ~ContextMenu()
18  {
19  Clear();
20 
21  delete m_context_menu_root_widget;
22  }
23  //--------------------------------------------------------------------------
24  void Init(Widget layoutRoot)
25  {
26  if(!m_context_menu_root_widget)
27  {
28  m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
29  m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
30  m_context_menu_root_widget.Show(false);
31  m_context_menu_root_widget.SetHandler(this);
32  }
33  }
34 
35  //--------------------------------------------------------------------------
36  void Show(int x, int y)
37  {
38  if ( m_count == 0) return;
39  int screen_w, screen_h;
40  float w, h;
41  float sx, sy;
42  int offset_x;// = -20;
43  int offset_y;// = -10;
44 
45  GetScreenSize(screen_w, screen_h);
46 
47  // align buttons
48  float button_height_percent = 0.02; // button height is 4% of screen height
49  float button_height = screen_h * button_height_percent;
50 
51  for ( int i = 0; i < m_count; i++)
52  {
53  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
54  if(menu_button)
55  {
56  menu_button.SetSize(0.90, button_height);
57  menu_button.Show(true);
58  }
59  }
60 
61 
62  AutoHeightSpacer spacer;
63  m_context_menu_panel_widget.GetScript(spacer);
64  if ( spacer )
65  {
66  spacer.Update();
67  }
68 
69  m_context_menu_root_widget.GetSize(w, h);
70  m_context_menu_panel_widget.GetSize(sx, sy);
71  m_context_menu_root_widget.SetSize(w, sy);
72 
73  // set position
74  m_context_menu_root_widget.GetScreenSize(w,h);
75  screen_w -= 10;
76  screen_h -= 10;
77 
78  int right_edge = x + w - offset_x;
79  if (right_edge > screen_w)
80  {
81  x = screen_w - w - offset_x;
82  }
83  else
84  {
85  x = x + offset_x;
86  }
87 
88  int bottom_edge = y + h - offset_y;
89  if (bottom_edge > screen_h)
90  {
91  y = y - h - offset_y;
92  }
93  else
94  {
95  y = y + offset_y;
96  }
97 
98  m_context_menu_root_widget.SetPos(x, y);
99  m_context_menu_root_widget.Show(true);
100  }
101 
102  //--------------------------------------------------------------------------
103  void ShowBackdrop(bool show)
104  {
105  if (show == true)
106  {
107  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
108  }
109  else
110  {
111  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
112  }
113  }
114 
115  //--------------------------------------------------------------------------
116  void Hide()
117  {
118  m_context_menu_root_widget.Show(false);
119 
120  Clear();
121  }
122 
123  //--------------------------------------------------------------------------
124  bool IsVisible()
125  {
126  return m_context_menu_root_widget.IsVisible();
127  }
128 
129  //--------------------------------------------------------------------------
130  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
131  {
132  super.OnMouseLeave(w, enterW, x, y);
133 
134  if ( enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
135  {
136  Hide();
137  return true;
138  }
139  return false;
140  }
141 
142  //--------------------------------------------------------------------------
143  override bool OnMouseButtonDown(Widget w, int x, int y, int button)
144  {
145  super.OnMouseButtonDown(w, x, y, button);
146 
147  if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
148  {
149  CallQueueContext ctx = m_commands.Get( w.GetUserID() );
150  ctx.Call();
151  Hide();
152 
153  UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
154  if (menu) GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
155  return true;
156  }
157  return false;
158  }
159 
160  //--------------------------------------------------------------------------
161  void Add(string label, Class obj, string fn_name, Param params)
162  {
163  AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
164  }
165 
166  void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
167  {
168  int count = Count();
169  ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
170  if (menuButton)
171  {
172  label.ToUpper();
173  menuButton.SetText(label);
174  menuButton.SetTextColor(labelColor);
175  menuButton.Show(true);
176  if (!funcName)
177  menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
178 
179  int itemWidth = label.Length();
180  if (m_max_item_width < itemWidth)
181  m_max_item_width = itemWidth;
182  }
183 
184  m_count++;
185  m_commands.Insert(new CallQueueContext(obj, funcName, params));
186  }
187 
188  //--------------------------------------------------------------------------
189  void Remove(int index)
190  {
191  if (index < m_commands.Count())
192  {
193  m_commands.RemoveOrdered(index);
194  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
195  menu_button.Show( false );
196  menu_button.SetText( "" );
197  m_count--;
198  }
199  }
200 
201  //--------------------------------------------------------------------------
202  int Count()
203  {
204  return m_commands.Count();
205  }
206 
207  //--------------------------------------------------------------------------
208  void Clear()
209  {
210  int i;
211 
212  m_commands.Clear();
213 
214  Widget child = m_context_menu_panel_widget.GetChildren();
215  while(child)
216  {
217  ButtonWidget button = ButtonWidget.Cast(child);
218  if(button)
219  {
220  button.Show(false);
221  }
222  child = child.GetSibling();
223 
224 
225  }
226  m_count = 0;
227  m_max_item_width = 0;
228  }
229 };
GetGame
proto native CGame GetGame()
UIScriptedMenu
Definition: dayzgame.c:63
AutoHeightSpacer
Definition: autoheightspacer.c:2
Param
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Definition: param.c:11
Add
void Add(string name, string value)
Definition: universaltemperaturesource.c:224
String
string String(string s)
Helper for passing string expression to functions with void parameter. Example: Print(String("Hello "...
Definition: enscript.c:339
Show
proto native void Show(bool show, bool immedUpdate=true)
Remove
void Remove(Object object)
Definition: actiontargets.c:95
Clear
protected void Clear(bool clearFile=false)
Definition: scriptconsoleenfscripttab.c:95
GetScreenSize
proto void GetScreenSize(out int x, out int y)
y
Icon y
ToString
proto string ToString()
CALL_CATEGORY_GUI
const int CALL_CATEGORY_GUI
Definition: tools.c:9
array< ref CallQueueContext >
CallQueue Class provide "lazy" calls - when we don't want to execute function immediately but later d...
Definition: tools.c:66
OnMouseLeave
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
Definition: uihintpanel.c:276
Init
class InventoryGridController extends ScriptedWidgetEventHandler Init
Definition: uihintpanel.c:46
IsVisible
proto native bool IsVisible()
MouseState
MouseState
Definition: ensystem.c:310
CallQueueContext
Definition: tools.c:15
x
Icon x
OnMouseButtonDown
bool OnMouseButtonDown(Widget w, int x, int y, int button)
Definition: pluginitemdiagnostic.c:117
WidgetFlags
WidgetFlags
Definition: enwidgets.c:57
Widget
Definition: enwidgets.c:189
Hide
void Hide()
Definition: dayzgame.c:165
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
ScriptedWidgetEventHandler
map: item x vector(index, width, height)
Definition: enwidgets.c:650
Count
@ Count
Definition: randomgeneratorsyncmanager.c:7