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  //--------------------------------------------------------------------------
12  void ContextMenu()
13  {
14  m_commands = new array<ref CallQueueContext>;
15  m_count = 0;
16  }
17  //--------------------------------------------------------------------------
18  void ~ContextMenu()
19  {
20  Clear();
21 
22  delete m_context_menu_root_widget;
23  }
24  //--------------------------------------------------------------------------
25  void Init(Widget layoutRoot)
26  {
27  if (!m_context_menu_root_widget)
28  {
29  m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
30  m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
31  m_context_menu_root_widget.Show(false);
32  m_context_menu_root_widget.SetHandler(this);
33  }
34  }
35 
36  //--------------------------------------------------------------------------
37  void Show(int x, int y)
38  {
39  if ( m_count == 0) return;
40  int screen_w, screen_h;
41  float w, h;
42  float sx, sy;
43  int offset_x;// = -20;
44  int offset_y;// = -10;
45 
46  GetScreenSize(screen_w, screen_h);
47 
48  // align buttons
49  float button_height_percent = 0.02; // button height is 4% of screen height
50  float button_height = screen_h * button_height_percent;
51 
52  for ( int i = 0; i < m_count; i++)
53  {
54  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
55  if(menu_button)
56  {
57  menu_button.SetSize(0.90, button_height);
58  menu_button.Show(true);
59  }
60  }
61 
62 
63  AutoHeightSpacer spacer;
64  m_context_menu_panel_widget.GetScript(spacer);
65  if ( spacer )
66  {
67  spacer.Update();
68  }
69 
70  m_context_menu_root_widget.GetSize(w, h);
71  m_context_menu_panel_widget.GetSize(sx, sy);
72  m_context_menu_root_widget.SetSize(w, sy);
73 
74  // set position
75  m_context_menu_root_widget.GetScreenSize(w,h);
76  screen_w -= 10;
77  screen_h -= 10;
78 
79  int right_edge = x + w - offset_x;
80  if (right_edge > screen_w)
81  {
82  x = screen_w - w - offset_x;
83  }
84  else
85  {
86  x = x + offset_x;
87  }
88 
89  int bottom_edge = y + h - offset_y;
90  if (bottom_edge > screen_h)
91  {
92  y = y - h - offset_y;
93  }
94  else
95  {
96  y = y + offset_y;
97  }
98 
99  m_context_menu_root_widget.SetPos(x, y);
100  m_context_menu_root_widget.Show(true);
101  }
102 
103  //--------------------------------------------------------------------------
104  void ShowBackdrop(bool show)
105  {
106  if (show == true)
107  {
108  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
109  }
110  else
111  {
112  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
113  }
114  }
115 
116  //--------------------------------------------------------------------------
117  void Hide()
118  {
119  m_context_menu_root_widget.Show(false);
120 
121  Clear();
122  }
123 
124  //--------------------------------------------------------------------------
125  bool IsVisible()
126  {
127  return m_context_menu_root_widget.IsVisible();
128  }
129 
130  //--------------------------------------------------------------------------
131  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
132  {
133  super.OnMouseLeave(w, enterW, x, y);
134 
135  if ( enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
136  {
137  Hide();
138  return true;
139  }
140  return false;
141  }
142 
143  //--------------------------------------------------------------------------
144  override bool OnMouseButtonDown(Widget w, int x, int y, int button)
145  {
146  super.OnMouseButtonDown(w, x, y, button);
147 
148  if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
149  {
150  CallQueueContext ctx = m_commands.Get(w.GetUserID());
151 
152  int actionId = Param3<ItemBase, int, int>.Cast(ctx.m_params).param2;
153  if (actionId == EActions.DELETE)
154  Hide();
155 
156  UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
157  if (menu)
158  GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
159 
160  ctx.Call();
161 
162  return true;
163  }
164 
165  return false;
166  }
167 
168  //--------------------------------------------------------------------------
169  void Add(string label, Class obj, string fn_name, Param params)
170  {
171  AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
172  }
173 
174  void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
175  {
176  int count = Count();
177  ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
178  if (menuButton)
179  {
180  label.ToUpper();
181  menuButton.SetText(label);
182  menuButton.SetTextColor(labelColor);
183  menuButton.Show(true);
184  if (!funcName)
185  menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
186 
187  int itemWidth = label.Length();
188  if (m_max_item_width < itemWidth)
189  m_max_item_width = itemWidth;
190  }
191 
192  m_count++;
193  m_commands.Insert(new CallQueueContext(obj, funcName, params));
194  }
195 
196  //--------------------------------------------------------------------------
197  void Remove(int index)
198  {
199  if (index < m_commands.Count())
200  {
201  m_commands.RemoveOrdered(index);
202  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
203  menu_button.Show( false );
204  menu_button.SetText( "" );
205  m_count--;
206  }
207  }
208 
209  //--------------------------------------------------------------------------
210  int Count()
211  {
212  return m_commands.Count();
213  }
214 
215  //--------------------------------------------------------------------------
216  void Clear()
217  {
218  int i;
219 
220  m_commands.Clear();
221 
222  Widget child = m_context_menu_panel_widget.GetChildren();
223  while(child)
224  {
225  ButtonWidget button = ButtonWidget.Cast(child);
226  if(button)
227  {
228  button.Show(false);
229  }
230  child = child.GetSibling();
231 
232 
233  }
234  m_count = 0;
235  m_max_item_width = 0;
236  }
237 };
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
Param3
Definition: entityai.c:95
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()
EActions
EActions
Definition: eactions.c:1
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