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  protected static ref ContextMenu m_ContextMenuInstance;
5 
6  Widget m_context_menu_root_widget;
7  private Widget m_context_menu_panel_widget;
8  private ref array<ref CallQueueContext> m_commands;
9  private int m_max_item_width;
10  private int m_count;
11  private bool m_builtIn = false;
12  const int ITEMS_COUNT = 27;
13 
14  //--------------------------------------------------------------------------
15  void ContextMenu()
16  {
17  m_commands = new array<ref CallQueueContext>;
18  m_count = 0;
19  }
20  //--------------------------------------------------------------------------
21  void ~ContextMenu()
22  {
23  Clear();
24 
25  delete m_context_menu_root_widget;
26  }
27  //--------------------------------------------------------------------------
28  void Init(Widget layoutRoot, bool builtIn = false)
29  {
30  m_builtIn = builtIn;
31  if (!m_context_menu_root_widget)
32  {
33  m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
34  m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
35  m_context_menu_root_widget.Show(false);
36  m_context_menu_root_widget.SetHandler(this);
37  }
38  }
39 
40  //--------------------------------------------------------------------------
41  void Show(int x, int y)
42  {
43  if ( m_count == 0) return;
44  int screen_w, screen_h;
45  float w, h;
46  float sx, sy;
47  int offset_x;// = -20;
48  int offset_y;// = -10;
49 
50  GetScreenSize(screen_w, screen_h);
51 
52  // align buttons
53  float button_height_percent = 0.02; // button height is 4% of screen height
54  float button_height = screen_h * button_height_percent;
55 
56  for ( int i = 0; i < m_count; i++)
57  {
58  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
59  if(menu_button)
60  {
61  menu_button.SetSize(0.90, button_height);
62  menu_button.Show(true);
63  }
64  }
65 
66 
67  AutoHeightSpacer spacer;
68  m_context_menu_panel_widget.GetScript(spacer);
69  if ( spacer )
70  {
71  spacer.Update();
72  }
73 
74  m_context_menu_root_widget.GetSize(w, h);
75  m_context_menu_panel_widget.GetSize(sx, sy);
76  m_context_menu_root_widget.SetSize(w, sy);
77 
78  // set position
79  m_context_menu_root_widget.GetScreenSize(w,h);
80  screen_w -= 10;
81  screen_h -= 10;
82 
83  int right_edge = x + w - offset_x;
84  if (right_edge > screen_w)
85  {
86  x = screen_w - w - offset_x;
87  }
88  else
89  {
90  x = x + offset_x;
91  }
92 
93  int bottom_edge = y + h - offset_y;
94  if (bottom_edge > screen_h)
95  {
96  y = y - h - offset_y;
97  }
98  else
99  {
100  y = y + offset_y;
101  }
102 
103  m_context_menu_root_widget.SetPos(x, y);
104  m_context_menu_root_widget.Show(true);
105  }
106  //--------------------------------------------------------------------------
107  void SetSize(float x, float y)
108  {
109  m_context_menu_root_widget.SetSize(x, y);
110  }
111 
112  //--------------------------------------------------------------------------
113  void ShowBackdrop(bool show)
114  {
115  if (show == true)
116  {
117  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
118  }
119  else
120  {
121  m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
122  }
123  }
124 
125  //--------------------------------------------------------------------------
126  void Hide()
127  {
128  m_context_menu_root_widget.Show(false);
129 
130  Clear();
131  }
132 
133  //--------------------------------------------------------------------------
134  bool IsVisible()
135  {
136  return m_context_menu_root_widget.IsVisible();
137  }
138 
139  //--------------------------------------------------------------------------
140  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
141  {
142  super.OnMouseLeave(w, enterW, x, y);
143 
144  if ( !m_builtIn && enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
145  {
146  Hide();
147  return true;
148  }
149  return false;
150  }
151 
152  //--------------------------------------------------------------------------
153  override bool OnMouseButtonDown(Widget w, int x, int y, int button)
154  {
155  super.OnMouseButtonDown(w, x, y, button);
156 
157  if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
158  {
159  CallQueueContext ctx = m_commands.Get(w.GetUserID());
160 
161  int actionId = Param3<EntityAI, int, int>.Cast(ctx.m_params).param2;
162  if (actionId == EActions.DELETE)
163  Hide();
164 
165  UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
166  if (menu)
167  GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
168 
169  ctx.Call();
170 
171  return true;
172  }
173 
174  return false;
175  }
176 
177  //--------------------------------------------------------------------------
178  void Add(string label, Class obj, string fn_name, Param params)
179  {
180  AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
181  }
182 
183  void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
184  {
185  int count = Count();
186  ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
187  if (menuButton)
188  {
189  label.ToUpper();
190  menuButton.SetText(label);
191  menuButton.SetTextColor(labelColor);
192  menuButton.Show(true);
193  if (!funcName)
194  menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
195 
196  int itemWidth = label.Length();
197  if (m_max_item_width < itemWidth)
198  m_max_item_width = itemWidth;
199  }
200 
201  m_count++;
202  m_commands.Insert(new CallQueueContext(obj, funcName, params));
203  }
204 
205  //--------------------------------------------------------------------------
206  void Remove(int index)
207  {
208  if (index < m_commands.Count())
209  {
210  m_commands.RemoveOrdered(index);
211  ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
212  menu_button.Show( false );
213  menu_button.SetText( "" );
214  m_count--;
215  }
216  }
217 
218  //--------------------------------------------------------------------------
219  int Count()
220  {
221  return m_commands.Count();
222  }
223 
224  //--------------------------------------------------------------------------
225  void Clear()
226  {
227  int i;
228 
229  m_commands.Clear();
230 
231  if (!m_context_menu_panel_widget)
232  return;
233  Widget child = m_context_menu_panel_widget.GetChildren();
234  while(child)
235  {
236  ButtonWidget button = ButtonWidget.Cast(child);
237  if(button)
238  {
239  button.Show(false);
240  }
241  child = child.GetSibling();
242 
243 
244  }
245  m_count = 0;
246  m_max_item_width = 0;
247  }
248 
249  void BuildContextMenu(notnull EntityAI entity, notnull Widget rootWidget, Class target)
250  {
251  Clear();
252 
253  TSelectableActionInfoArrayEx customActions = new TSelectableActionInfoArrayEx();
254  entity.GetDebugActions(customActions);
255 
256  int actionsCount = customActions.Count();
257  for (int i = 0; i < customActions.Count(); i++)
258  {
259  TSelectableActionInfoWithColor actionInfo = TSelectableActionInfoWithColor.Cast(customActions.Get(i));
260  if (actionInfo)
261  {
262  int actionId = actionInfo.param2;
263  int textColor = actionInfo.param4;
264  string actionText = actionInfo.param3;
265 
266  if (actionId == EActions.SEPARATOR)
267  AddEx(actionText, textColor, null, "", null);
268  else
269  AddEx(actionText, textColor, target, "OnSelectAction", new Param3<EntityAI, int, int>(entity, actionId, textColor));
270  }
271  }
272  }
273 
274  //--------------------------------------------------------------------------
275  static void DisplayContextMenu(int x, int y, notnull EntityAI entity, notnull Widget rootWidget, Class target)
276  {
277  m_ContextMenuInstance = new ContextMenu();
278  if (m_ContextMenuInstance)
279  {
280  m_ContextMenuInstance.Init(rootWidget);
281  m_ContextMenuInstance.BuildContextMenu(entity, rootWidget, target);
282 
283  m_ContextMenuInstance.SetSize(1,1);
284  m_ContextMenuInstance.Show(x, y);
285  }
286  }
287 
288 };
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
SetSize
proto native void SetSize(float w, float h, bool immedUpdate=true)
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
TSelectableActionInfoWithColor
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition: entityai.c:97
EntityAI
Definition: building.c:5
ScriptedWidgetEventHandler
map: item x vector(index, width, height)
Definition: enwidgets.c:650
Count
@ Count
Definition: randomgeneratorsyncmanager.c:7