2class ContextMenu
extends ScriptedWidgetEventHandler
4 protected static ref ContextMenu m_ContextMenuInstance;
6 Widget m_context_menu_root_widget;
7 private Widget m_context_menu_panel_widget;
9 private int m_max_item_width;
11 private bool m_builtIn =
false;
12 const int ITEMS_COUNT = 27;
25 delete m_context_menu_root_widget;
28 void Init(Widget layoutRoot,
bool builtIn =
false)
31 if (!m_context_menu_root_widget)
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);
43 if ( m_count == 0)
return;
44 int screen_w, screen_h;
53 float button_height_percent = 0.02;
54 float button_height = screen_h * button_height_percent;
56 for (
int i = 0; i < m_count; i++)
58 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget(
String(
"Button" + (i+1).
ToString() ) ) );
61 menu_button.SetSize(0.90, button_height);
62 menu_button.Show(
true);
68 m_context_menu_panel_widget.GetScript(spacer);
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);
79 m_context_menu_root_widget.GetScreenSize(w,h);
83 int right_edge =
x + w - offset_x;
84 if (right_edge > screen_w)
86 x = screen_w - w - offset_x;
93 int bottom_edge =
y + h - offset_y;
94 if (bottom_edge > screen_h)
103 m_context_menu_root_widget.SetPos(
x,
y);
104 m_context_menu_root_widget.Show(
true);
107 void SetSize(
float x,
float y)
109 m_context_menu_root_widget.SetSize(
x,
y);
113 void ShowBackdrop(
bool show)
117 m_context_menu_root_widget.FindAnyWidget(
"BackdropImageWidget").Show(
true);
121 m_context_menu_root_widget.FindAnyWidget(
"BackdropImageWidget").Show(
false);
128 m_context_menu_root_widget.Show(
false);
136 return m_context_menu_root_widget.IsVisible();
142 super.OnMouseLeave(w, enterW,
x,
y);
144 if ( !m_builtIn && enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
155 super.OnMouseButtonDown(w,
x,
y, button);
157 if (button ==
MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
178 void Add(
string label,
Class obj,
string fn_name,
Param params)
180 AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
183 void AddEx(
string label,
int labelColor,
Class obj,
string funcName,
Param params)
186 ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(
string.Format(
"Button%1", count + 1)));
190 menuButton.SetText(label);
191 menuButton.SetTextColor(labelColor);
192 menuButton.Show(
true);
196 menuButton.SetFlags(menuButton.GetFlags() |
WidgetFlags.IGNOREPOINTER);
203 int itemWidth = label.Length();
204 if (m_max_item_width < itemWidth)
205 m_max_item_width = itemWidth;
213 void Remove(
int index)
215 if (index < m_commands.Count())
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(
"" );
228 return m_commands.Count();
238 if (!m_context_menu_panel_widget)
240 Widget child = m_context_menu_panel_widget.GetChildren();
243 ButtonWidget button = ButtonWidget.Cast(child);
248 child = child.GetSibling();
253 m_max_item_width = 0;
256 void BuildContextMenu(notnull
EntityAI entity, notnull Widget rootWidget,
Class target)
260 TSelectableActionInfoArrayEx customActions =
new TSelectableActionInfoArrayEx();
261 entity.GetDebugActions(customActions);
263 int actionsCount = customActions.Count();
264 for (
int i = 0; i < customActions.Count(); i++)
269 int actionId = actionInfo.param2;
270 int textColor = actionInfo.param4;
271 string actionText = actionInfo.param3;
274 AddEx(actionText, textColor, null,
"", null);
282 static void DisplayContextMenu(
int x,
int y, notnull
EntityAI entity, notnull Widget rootWidget,
Class target)
284 m_ContextMenuInstance =
new ContextMenu();
285 if (m_ContextMenuInstance)
287 m_ContextMenuInstance.Init(rootWidget);
288 m_ContextMenuInstance.BuildContextMenu(entity, rootWidget, target);
290 m_ContextMenuInstance.SetSize(1,1);
291 m_ContextMenuInstance.Show(
x,
y);
Param4< int, int, string, int > TSelectableActionInfoWithColor
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
Super root of all classes in Enforce script.
Base Param Class with no parameters. Used as general purpose parameter overloaded with Param1 to Para...
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
proto native CGame GetGame()
string String(string s)
Helper for passing string expression to functions with void parameter. Example: Print(String("Hello "...
proto void GetScreenSize(out int x, out int y)
bool OnMouseButtonDown(Widget w, int x, int y, int button)
void Clear(bool clearFile=false)
void Add(string name, string value)