DayZ 1.24
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 {
17 m_commands = new array<ref CallQueueContext>;
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;
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
51
52 // align buttons
53 float button_height_percent = 0.02; // button height is 4% of screen height
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
68 m_context_menu_panel_widget.GetScript(spacer);
69 if (spacer)
70 spacer.Update();
71
72 m_context_menu_root_widget.GetSize(w, h);
73 m_context_menu_panel_widget.GetSize(sx, sy);
74 m_context_menu_root_widget.SetSize(w, sy);
75
76 // set position
77 m_context_menu_root_widget.GetScreenSize(w, h);
78 screen_w -= 10;
79 screen_h -= 10;
80
81 int right_edge = x + w - offset_x;
82 if (right_edge > screen_w)
83 x = screen_w - w - offset_x;
84 else
85 x = x + offset_x;
86
87 int bottom_edge = y + h - offset_y;
89 y = y - h - offset_y;
90 else
91 y = y + offset_y;
92
93 m_context_menu_root_widget.SetPos(x, y);
94 m_context_menu_root_widget.Show(true);
95 }
96 //--------------------------------------------------------------------------
97 void SetSize(float x, float y)
98 {
99 m_context_menu_root_widget.SetSize(x, y);
100 }
101
102 //--------------------------------------------------------------------------
103 void ShowBackdrop(bool show)
104 {
105 if (show == true)
106 m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
107 else
108 m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
109 }
110
111 //--------------------------------------------------------------------------
112 void Hide()
113 {
114 m_context_menu_root_widget.Show(false);
115
116 Clear();
117 }
118
119 //--------------------------------------------------------------------------
121 {
122 return m_context_menu_root_widget.IsVisible();
123 }
124
125 //--------------------------------------------------------------------------
126 override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
127 {
128 super.OnMouseLeave(w, enterW, x, y);
129
130 if (!m_builtIn && enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget)
131 {
132 Hide();
133 return true;
134 }
135 return false;
136 }
137
138 //--------------------------------------------------------------------------
139 override bool OnMouseButtonDown(Widget w, int x, int y, int button)
140 {
141 super.OnMouseButtonDown(w, x, y, button);
142
143 if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
144 {
145 CallQueueContext ctx = m_commands.Get(w.GetUserID());
146
147 int actionId = Param3<EntityAI, int, int>.Cast(ctx.m_params).param2;
148 if (actionId == EActions.DELETE)
149 Hide();
150
151 UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
152 if (menu)
153 GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
154
155 ctx.Call();
156
157 return true;
158 }
159
160 return false;
161 }
162
163 //--------------------------------------------------------------------------
164 void Add(string label, Class obj, string fn_name, Param params)
165 {
166 AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
167 }
168
169 void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
170 {
171 int count = Count();
172 ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
173 if (menuButton)
174 {
175 label.ToUpper();
176 menuButton.SetText(label);
177 menuButton.SetTextColor(labelColor);
178 menuButton.Show(true);
179 if (!funcName)
180 menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);
181
182 int itemWidth = label.Length();
183 if (m_max_item_width < itemWidth)
184 m_max_item_width = itemWidth;
185 }
186
187 m_count++;
188 m_commands.Insert(new CallQueueContext(obj, funcName, params));
189 }
190
191 //--------------------------------------------------------------------------
192 void Remove(int index)
193 {
194 if (index < m_commands.Count())
195 {
196 m_commands.RemoveOrdered(index);
197 ButtonWidget menu_button = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(String("Button" + (index + 1).ToString())));
198 menu_button.Show(false);
199 menu_button.SetText("");
200 m_count--;
201 }
202 }
203
204 //--------------------------------------------------------------------------
205 int Count()
206 {
207 return m_commands.Count();
208 }
209
210 //--------------------------------------------------------------------------
211 void Clear()
212 {
213 int i;
214
215 m_commands.Clear();
216
217 if (!m_context_menu_panel_widget)
218 return;
219 Widget child = m_context_menu_panel_widget.GetChildren();
220 while (child)
221 {
223 if (button)
224 button.Show(false);
225 child = child.GetSibling();
226
227
228 }
229 m_count = 0;
230 m_max_item_width = 0;
231 }
232
234 {
235 Clear();
236
238 entity.GetDebugActions(customActions);
239
240 int actionsCount = customActions.Count();
241 for (int i = 0; i < customActions.Count(); i++)
242 {
244 if (actionInfo)
245 {
246 int actionId = actionInfo.param2;
247 int textColor = actionInfo.param4;
248 string actionText = actionInfo.param3;
249
250 if (actionId == EActions.SEPARATOR)
252 else
254 }
255 }
256 }
257
258 //--------------------------------------------------------------------------
260 {
261 m_ContextMenuInstance = new ContextMenu();
262 if (m_ContextMenuInstance)
263 {
264 m_ContextMenuInstance.Init(rootWidget);
265 m_ContextMenuInstance.BuildContextMenu(entity, rootWidget, target);
266
267 m_ContextMenuInstance.SetSize(1, 1);
268 m_ContextMenuInstance.Show(x, y);
269 }
270 }
271
272};
EActions
Definition EActions.c:2
proto string ToString()
Icon x
Icon y
Super root of all classes in Enforce script.
Definition EnScript.c:11
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Definition param.c:12
map: item x vector(index, width, height)
Definition EnWidgets.c:651
void ShowBackdrop(bool show)
void BuildContextMenu(notnull EntityAI entity, notnull Widget rootWidget, Class target)
static void DisplayContextMenu(int x, int y, notnull EntityAI entity, notnull Widget rootWidget, Class target)
void SetSize(float x, float y)
Definition ContextMenu.c:97
static ref ContextMenu m_ContextMenuInstance
Definition ContextMenu.c:4
void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
void Add(string label, Class obj, string fn_name, Param params)
void Show(int x, int y)
Definition ContextMenu.c:41
void Init(Widget layoutRoot, bool builtIn=false)
Definition ContextMenu.c:28
ref array< ref CallQueueContext > m_commands
Definition ContextMenu.c:8
override bool OnMouseButtonDown(Widget w, int x, int y, int button)
proto native CGame GetGame()
string String(string s)
Helper for passing string expression to functions with void parameter. Example: Print(String("Hello "...
Definition EnScript.c:338
MouseState
Definition EnSystem.c:311
proto void GetScreenSize(out int x, out int y)
const int CALL_CATEGORY_GUI
Definition tools.c:9
WidgetFlags
Definition EnWidgets.c:58