Dayz Explorer 1.28.160049
Loading...
Searching...
No Matches
contextmenu.c
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2class ContextMenu extends ScriptedWidgetEventHandler
3{
4 const int ITEMS_COUNT = 27;
5 private Widget m_context_menu_root_widget;
6 private Widget m_context_menu_panel_widget;
7 private ref array<ref CallQueueContext> m_commands;
8 private int m_max_item_width;
9 private int m_count;
10 //--------------------------------------------------------------------------
11 void ContextMenu()
12 {
13 m_commands = new array<ref CallQueueContext>;
14 m_count = 0;
15 }
16 //--------------------------------------------------------------------------
17 void ~ContextMenu()
18 {
19 Clear();
20
21 delete m_context_menu_root_widget;
22 }
23 //--------------------------------------------------------------------------
24 void Init(Widget layoutRoot)
25 {
26 m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
27 m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
28 m_context_menu_root_widget.Show(false);
29 m_context_menu_root_widget.SetHandler(this);
30 }
31
32 //--------------------------------------------------------------------------
33 void Show(int x, int y)
34 {
35 if ( m_count == 0) return;
36 int screen_w, screen_h;
37 float w, h;
38 float sx, sy;
39 int offset_x;// = -20;
40 int offset_y;// = -10;
41
42 GetScreenSize(screen_w, screen_h);
43
44 // align buttons
45 float button_height_percent = 0.04; // button height is 4% of screen height
46 float button_height = screen_h * button_height_percent;
47 for ( int i = 0; i < m_count; i++)
48 {
49 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
50 menu_button.SetSize(0.92, button_height);
51 }
52
53
54 AutoHeightSpacer spacer;
55 m_context_menu_panel_widget.GetScript(spacer);
56 if ( spacer )
57 {
58 spacer.Update();
59 }
60
61 m_context_menu_root_widget.GetSize(w, h);
62 m_context_menu_panel_widget.GetSize(sx, sy);
63 m_context_menu_root_widget.SetSize(w, sy);
64
65 // set position
66 m_context_menu_root_widget.GetScreenSize(w,h);
67 screen_w -= 10;
68 screen_h -= 10;
69
70 int right_edge = x + w - offset_x;
71 if (right_edge > screen_w)
72 {
73 x = screen_w - w - offset_x;
74 }
75 else
76 {
77 x = x + offset_x;
78 }
79
80 int bottom_edge = y + h - offset_y;
81 if (bottom_edge > screen_h)
82 {
83 y = y - h - offset_y;
84 }
85 else
86 {
87 y = y + offset_y;
88 }
89
90 m_context_menu_root_widget.SetPos(x, y);
91 m_context_menu_root_widget.Show(true);
92 }
93
94 //--------------------------------------------------------------------------
95 void ShowBackdrop(bool show)
96 {
97 if (show == true)
98 {
99 m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
100 }
101 else
102 {
103 m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
104 }
105 }
106
107 //--------------------------------------------------------------------------
108 void Hide()
109 {
110 m_context_menu_root_widget.Show(false);
111 Clear();
112 }
113
114 //--------------------------------------------------------------------------
115 bool IsVisible()
116 {
117 return m_context_menu_root_widget.IsVisible();
118 }
119
120 //--------------------------------------------------------------------------
121 override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
122 {
123 super.OnMouseLeave(w, enterW, x, y);
124
125 if ( enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
126 {
127 Hide();
128 return true;
129 }
130 return false;
131 }
132
133 //--------------------------------------------------------------------------
134 override bool OnMouseButtonDown(Widget w, int x, int y, int button)
135 {
136 super.OnMouseButtonDown(w, x, y, button);
137
138 if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
139 {
140 CallQueueContext ctx = m_commands.Get( w.GetUserID() );
141 ctx.Call();
142 Hide();
143
144 UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
145 if (menu) GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
146 return true;
147 }
148 return false;
149 }
150
151 //--------------------------------------------------------------------------
152 void Add(string label, Class obj, string fn_name, Param params /*= NULL*/)
153 {
154 int count = Count();
155 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (count+1).ToString() ) ) );
156 if ( menu_button )
157 {
158 label.ToUpper();
159 menu_button.SetText( label );
160 menu_button.Show( true );
161
162 int item_width = label.Length();
163 if (m_max_item_width < item_width)
164 {
165 m_max_item_width = item_width;
166 }
167 }
168 m_count = m_count + 1;
169
170 m_commands.Insert(new CallQueueContext(obj, fn_name, params));
171 }
172
173 //--------------------------------------------------------------------------
174 void Remove(int index)
175 {
176 if (index < m_commands.Count())
177 {
178 m_commands.RemoveOrdered(index);
179 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
180 menu_button.Show( false );
181 menu_button.SetText( "" );
182 m_count = m_count - 1;
183 }
184 }
185
186 //--------------------------------------------------------------------------
187 int Count()
188 {
189 return m_commands.Count();
190 }
191
192 //--------------------------------------------------------------------------
193 void Clear()
194 {
195 int i;
196
197 m_commands.Clear();
198
199 for ( i = 0; i < ITEMS_COUNT; i++)
200 {
201 ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
202 if( menu_button )
203 {
204 menu_button.Show( false );
205 menu_button.SetText( "" );
206 }
207 }
208 m_count = 0;
209 m_max_item_width = 0;
210 }
211};
override bool OnMouseLeave(Widget w, Widget enterW, int x, int 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
Result for an object found in CGame.IsBoxCollidingGeometryProxy.
void Hide()
Definition dayzgame.c:170
override Widget Init()
Definition dayzgame.c:127
void Show()
Definition dayzgame.c:162
proto string ToString()
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:339
MouseState
Definition ensystem.c:311
proto void GetScreenSize(out int x, out int y)
const int CALL_CATEGORY_GUI
Definition tools.c:9
Icon x
Icon y
bool OnMouseButtonDown(Widget w, int x, int y, int button)
void Clear(bool clearFile=false)
void Add(string name, string value)