Dayz Build 1.29.163047, Scripts Rev. 123548
Dayz Code Explorer by Zeroy
Loading...
Searching...
No Matches
contextmenu.c
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2class ContextMenu extends ScriptedWidgetEventHandler
3{
4 protected static ref ContextMenu m_ContextMenuInstance;
5
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 //--------------------------------------------------------------------------
16 {
18 m_count = 0;
19 }
20 //--------------------------------------------------------------------------
22 {
23 Clear();
24
26 }
27 //--------------------------------------------------------------------------
28 void Init(Widget layoutRoot, bool builtIn = false)
29 {
30 m_builtIn = builtIn;
32 {
33 m_context_menu_root_widget = g_Game.GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
34 m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
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
105 }
106 //--------------------------------------------------------------------------
107 void SetSize(float x, float y)
108 {
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 //--------------------------------------------------------------------------
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 = g_Game.GetUIManager().GetMenu();
166 if (menu)
167 g_Game.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
194 if (funcName == "")
195 {
196 menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
197 }
198 else
199 {
200 menuButton.ClearFlags(WidgetFlags.IGNOREPOINTER);
201 }
202
203 int itemWidth = label.Length();
204 if (m_max_item_width < itemWidth)
205 m_max_item_width = itemWidth;
206 }
207
208 m_count++;
209 m_commands.Insert(new CallQueueContext(obj, funcName, params));
210 }
211
212 //--------------------------------------------------------------------------
213 void Remove(int index)
214 {
215 if (index < m_commands.Count())
216 {
217 m_commands.RemoveOrdered(index);
218 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
219 menu_button.Show( false );
220 menu_button.SetText( "" );
221 m_count--;
222 }
223 }
224
225 //--------------------------------------------------------------------------
226 int Count()
227 {
228 return m_commands.Count();
229 }
230
231 //--------------------------------------------------------------------------
232 void Clear()
233 {
234 int i;
235
236 m_commands.Clear();
237
239 return;
240 Widget child = m_context_menu_panel_widget.GetChildren();
241 while(child)
242 {
243 ButtonWidget button = ButtonWidget.Cast(child);
244 if(button)
245 {
246 button.Show(false);
247 }
248 child = child.GetSibling();
249
250
251 }
252 m_count = 0;
254 }
255
256 void BuildContextMenu(notnull EntityAI entity, notnull Widget rootWidget, Class target)
257 {
258 Clear();
259
260 TSelectableActionInfoArrayEx customActions = new TSelectableActionInfoArrayEx();
261 entity.GetDebugActions(customActions);
262
263 int actionsCount = customActions.Count();
264 for (int i = 0; i < customActions.Count(); i++)
265 {
266 TSelectableActionInfoWithColor actionInfo = TSelectableActionInfoWithColor.Cast(customActions.Get(i));
267 if (actionInfo)
268 {
269 int actionId = actionInfo.param2;
270 int textColor = actionInfo.param4;
271 string actionText = actionInfo.param3;
272
273 if (actionId == EActions.SEPARATOR)
274 AddEx(actionText, textColor, null, "", null);
275 else
276 AddEx(actionText, textColor, target, "OnSelectAction", new Param3<EntityAI, int, int>(entity, actionId, textColor));
277 }
278 }
279 }
280
281 //--------------------------------------------------------------------------
282 static void DisplayContextMenu(int x, int y, notnull EntityAI entity, notnull Widget rootWidget, Class target)
283 {
286 {
287 m_ContextMenuInstance.Init(rootWidget);
288 m_ContextMenuInstance.BuildContextMenu(entity, rootWidget, target);
289
290 m_ContextMenuInstance.SetSize(1,1);
292 }
293 }
294
295};
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition entityai.c:104
void Call()
Definition tools.c:30
ref Param m_params
Definition tools.c:19
Super root of all classes in Enforce script.
Definition enscript.c:11
Base Param Class with no parameters.
Definition param.c:12
map: item x vector(index, width, height)
Definition enwidgets.c:657
ref array< ref CallQueueContext > m_commands
Definition contextmenu.c:6
void Init(Widget layoutRoot, bool builtIn=false)
Definition contextmenu.c:28
void BuildContextMenu(notnull EntityAI entity, notnull Widget rootWidget, Class target)
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
void Show(int x, int y)
Definition contextmenu.c:41
void Add(string label, Class obj, string fn_name, Param params)
static void DisplayContextMenu(int x, int y, notnull EntityAI entity, notnull Widget rootWidget, Class target)
static ref ContextMenu m_ContextMenuInstance
Definition contextmenu.c:4
void SetSize(float x, float y)
void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
void ShowBackdrop(bool show)
override bool OnMouseButtonDown(Widget w, int x, int y, int button)
Xbox menu.
Definition dayzgame.c:64
override void Refresh()
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
DayZGame g_Game
Definition dayzgame.c:3942
EActions
Definition eactions.c:2
proto string ToString()
string String(string s)
Helper for passing string expression to functions with void parameter. Example: Print(String("Hello "...
Definition enscript.c:339
MouseState
Definition ensystem.c:311
proto void GetScreenSize(out int x, out int y)
proto int ToUpper()
Changes string to uppercase.
proto native int Length()
Returns length of string.
const int CALL_CATEGORY_GUI
Definition tools.c:9
WidgetFlags
Definition enwidgets.c:58
Icon x
Icon y