File size: 7,767 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//--------------------------------------------------------------------------
class ContextMenu extends ScriptedWidgetEventHandler
{
	protected static ref ContextMenu m_ContextMenuInstance;
	
	Widget m_context_menu_root_widget;
	private Widget m_context_menu_panel_widget;
	private ref array<ref CallQueueContext> m_commands;
	private int m_max_item_width;
	private int m_count;
	private bool m_builtIn = false;
	const int ITEMS_COUNT = 27;
	
	//--------------------------------------------------------------------------
	void ContextMenu()
	{
		m_commands = new array<ref CallQueueContext>;
		m_count = 0;
	}
	//--------------------------------------------------------------------------
	void ~ContextMenu()
	{
		Clear();
	
		delete m_context_menu_root_widget;
	}
	//--------------------------------------------------------------------------
	void Init(Widget layoutRoot, bool builtIn = false)
	{
		m_builtIn = builtIn;
		if (!m_context_menu_root_widget)
		{
			m_context_menu_root_widget = GetGame().GetWorkspace().CreateWidgets("gui/layouts/day_z_inventory_context_menu.layout", layoutRoot);
			m_context_menu_panel_widget = m_context_menu_root_widget.FindAnyWidget("PanelWidget");
			m_context_menu_root_widget.Show(false);
			m_context_menu_root_widget.SetHandler(this);
		}
	}
	
	//--------------------------------------------------------------------------
	void Show(int x, int y)
	{
		if ( m_count == 0) return;
		int screen_w, screen_h;
		float w, h;
		float sx, sy;
		int offset_x;// = -20;
		int offset_y;// = -10;
	
		GetScreenSize(screen_w, screen_h);
	
		// align buttons
		float button_height_percent = 0.02;  // button height is 4% of screen height
		float button_height = screen_h * button_height_percent;
		
		for ( int i = 0; i < m_count; i++)
		{
			ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + (i+1).ToString() ) ) );
			if(menu_button)
			{
					menu_button.SetSize(0.90, button_height);
					menu_button.Show(true);
			}
		}
	
	
		AutoHeightSpacer spacer;
		m_context_menu_panel_widget.GetScript(spacer);
		if ( spacer )
		{
			spacer.Update();	
		}
	
		m_context_menu_root_widget.GetSize(w, h);
		m_context_menu_panel_widget.GetSize(sx, sy);
		m_context_menu_root_widget.SetSize(w, sy);
	
		// set position
		m_context_menu_root_widget.GetScreenSize(w,h);
		screen_w -= 10;
		screen_h -= 10;
	
		int right_edge = x + w - offset_x;
		if (right_edge > screen_w) 
		{
			x = screen_w - w - offset_x;
		}
		else
		{
			x = x + offset_x;
		}
	
		int bottom_edge = y + h - offset_y;
		if (bottom_edge > screen_h)
		{
			y = y - h - offset_y;
		}
		else
		{
			y = y + offset_y;
		}
	
		m_context_menu_root_widget.SetPos(x, y);
		m_context_menu_root_widget.Show(true);
	}	
	//--------------------------------------------------------------------------
	void SetSize(float x, float y)
	{
		m_context_menu_root_widget.SetSize(x, y);
	}

	//--------------------------------------------------------------------------
	void ShowBackdrop(bool show)
	{
		if (show == true)
		{
			m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(true);
		}
		else
		{
			m_context_menu_root_widget.FindAnyWidget("BackdropImageWidget").Show(false);
		}
	}
	
	//--------------------------------------------------------------------------
	void Hide()
	{
		m_context_menu_root_widget.Show(false);
	
		Clear();
	}
	
	//--------------------------------------------------------------------------
	bool IsVisible()
	{
		return m_context_menu_root_widget.IsVisible();
	}
	
	//--------------------------------------------------------------------------
	override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
	{
		super.OnMouseLeave(w, enterW, x, y);
		
		if ( !m_builtIn && enterW && m_context_menu_panel_widget && enterW != m_context_menu_panel_widget && enterW.GetParent() != m_context_menu_panel_widget )
		{
			Hide();
			return true;
		}
		return false;
	}
	
	//--------------------------------------------------------------------------
	override bool OnMouseButtonDown(Widget w, int x, int y, int button)
	{
		super.OnMouseButtonDown(w, x, y, button);
		
		if (button == MouseState.LEFT && w.GetUserID() > -1 && w.GetUserID() < m_commands.Count())
		{
			CallQueueContext ctx = m_commands.Get(w.GetUserID());

			int actionId = Param3<EntityAI, int, int>.Cast(ctx.m_params).param2;
			if (actionId == EActions.DELETE)
				Hide();
			
			UIScriptedMenu menu = GetGame().GetUIManager().GetMenu();
			if (menu)
				GetGame().GetCallQueue(CALL_CATEGORY_GUI).Call(menu.Refresh);
			
			ctx.Call();

			return true;
		}

		return false;
	}
	
	//--------------------------------------------------------------------------
	void Add(string label, Class obj, string fn_name, Param params)
	{
		 AddEx(label, FadeColors.LIGHT_GREY, obj, fn_name, params);
	}
	
	void AddEx(string label, int labelColor, Class obj, string funcName, Param params)
	{
		int count = Count();
		ButtonWidget menuButton = ButtonWidget.Cast(m_context_menu_root_widget.FindAnyWidget(string.Format("Button%1", count + 1)));
		if (menuButton)
		{
			label.ToUpper();
			menuButton.SetText(label);
			menuButton.SetTextColor(labelColor);
			menuButton.Show(true);
			if (!funcName)
				menuButton.SetFlags(menuButton.GetFlags() | WidgetFlags.IGNOREPOINTER);	

			int itemWidth = label.Length();
			if (m_max_item_width < itemWidth)
				m_max_item_width = itemWidth;
		}
		
		m_count++;
		m_commands.Insert(new CallQueueContext(obj, funcName, params));
	}
	
	//--------------------------------------------------------------------------
	void Remove(int index)
	{
		if (index < m_commands.Count())
		{
			m_commands.RemoveOrdered(index);
			ButtonWidget menu_button = ButtonWidget.Cast( m_context_menu_root_widget.FindAnyWidget( String( "Button" + ( index + 1 ).ToString() ) ) );
			menu_button.Show( false );
			menu_button.SetText( "" );
			m_count--;
		}
	}
	
	//--------------------------------------------------------------------------
	int Count()
	{
		return m_commands.Count();
	}
	
	//--------------------------------------------------------------------------
	void Clear()
	{	
		int i;
		
		m_commands.Clear();
		
		if (!m_context_menu_panel_widget)
			return;
		Widget child = m_context_menu_panel_widget.GetChildren();
		while(child)
		{
			ButtonWidget button = ButtonWidget.Cast(child);
			if(button)
			{
				button.Show(false);
			}
			child = child.GetSibling();
			
			
		}
		m_count = 0;
		m_max_item_width = 0;
	}

	void BuildContextMenu(notnull EntityAI entity, notnull Widget rootWidget, Class target)
	{
		Clear();

		TSelectableActionInfoArrayEx customActions = new TSelectableActionInfoArrayEx();
		entity.GetDebugActions(customActions);
	
		int actionsCount = customActions.Count();
		for (int i = 0; i < customActions.Count(); i++)
		{
			TSelectableActionInfoWithColor actionInfo = TSelectableActionInfoWithColor.Cast(customActions.Get(i));
			if (actionInfo)
			{
				int actionId 		= actionInfo.param2;
				int textColor		= actionInfo.param4;
				string actionText 	= actionInfo.param3;
	
				if (actionId == EActions.SEPARATOR)
					AddEx(actionText, textColor, null, "", null);
				else
					AddEx(actionText, textColor, target, "OnSelectAction", new Param3<EntityAI, int, int>(entity, actionId, textColor));
			}
		}
	}
	
	//--------------------------------------------------------------------------
	static void DisplayContextMenu(int x, int y, notnull EntityAI entity, notnull Widget rootWidget, Class target)
	{
		m_ContextMenuInstance = new ContextMenu();
		if (m_ContextMenuInstance)
		{
			m_ContextMenuInstance.Init(rootWidget);
			m_ContextMenuInstance.BuildContextMenu(entity, rootWidget, target);

			m_ContextMenuInstance.SetSize(1,1);
			m_ContextMenuInstance.Show(x, y);
		}
	}
	
};