Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
UIContextMenu.cs
Go to the documentation of this file.
1using System;
2using DG.Tweening;
3using UnityEngine;
4using UnityEngine.EventSystems;
5using UnityEngine.Events;
6using UnityEngine.UI;
7
8public class UIContextMenu : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler
9{
10 public static UIContextMenu Current;
11
12 public static float timeSinceClosed;
13
14 public static bool closeOnMouseLeave;
15
17
19
21
23
24 public GridLayoutGroup defaultGridLayout;
25
26 public Transform logo;
27
28 public CanvasGroup cg;
29
30 public GameObject[] separators;
31
32 public Action onDestroy;
33
34 public Action onUpdate;
35
37
38 public bool destroyOnHide;
39
40 public bool wasCanceled;
41
43
44 public bool system;
45
46 public bool alwaysPopLeft;
47
48 public Image bg;
49
50 public SoundData soundPop;
51
52 public Vector2 margin;
53
54 public Vector2 fixPos;
55
57
58 public bool hideOnMouseLeave;
59
60 private Vector2 showPos;
61
62 [NonSerialized]
63 public VerticalLayoutGroup layoutGroup;
64
65 [NonSerialized]
66 public RectTransform _rect;
67
68 [NonSerialized]
69 public bool isPointerOver;
70
71 [NonSerialized]
72 public int depth;
73
74 [NonSerialized]
76
77 [NonSerialized]
78 public float timeSinceOpen;
79
80 [NonSerialized]
82
83 [NonSerialized]
84 public float updatesSinceOpen;
85
86 [NonSerialized]
87 public bool isDestroyed;
88
89 public bool isChild => popper != null;
90
92 {
93 get
94 {
95 if (!parent)
96 {
97 return this;
98 }
99 return parent.Root;
100 }
101 }
102
103 private void Awake()
104 {
105 layoutGroup = GetComponent<VerticalLayoutGroup>();
106 _rect = base.transform as RectTransform;
107 base.gameObject.SetActive(value: false);
108 }
109
110 private void Update()
111 {
112 if (onUpdate != null)
113 {
114 onUpdate();
115 }
116 if ((bool)highlightTarget)
117 {
119 }
120 timeSinceOpen += Time.unscaledDeltaTime;
121 if (timeSinceOpen < 0.3f)
122 {
123 return;
124 }
125 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetMouseButtonDown(1))
126 {
127 wasCanceled = true;
128 Hide();
129 return;
130 }
131 updatesSinceOpen += 1f;
132 if (!Current.hideOnMouseLeave && updatesSinceOpen > 10f && !isPointerOver && Input.GetMouseButtonDown(0) && !isChild)
133 {
134 Hide();
135 }
136 else if (updatesSinceOpen > 10f && !isPointerOver && !Input.GetMouseButton(0))
137 {
138 timeSincePointerLeft += Time.unscaledDeltaTime;
140 {
141 if (Input.GetMouseButtonUp(0))
142 {
143 Hide();
144 }
146 {
147 Hide();
148 }
149 }
150 }
151 else
152 {
154 }
155 }
156
157 private void LateUpdate()
158 {
159 if ((bool)highlightTarget)
160 {
162 }
163 }
164
165 public void OnPointerEnter(PointerEventData data)
166 {
167 isPointerOver = true;
168 }
169
170 public void OnPointerExit(PointerEventData data)
171 {
172 isPointerOver = false;
173 }
174
175 public void Show(UIItem i)
176 {
177 Show(i.button1);
178 }
179
180 public void Show(UIButton b)
181 {
183 Show();
184 }
185
186 public void Show(Vector2 pos)
187 {
188 showPos = pos;
189 Show();
190 }
191
192 public void Show()
193 {
194 wasCanceled = false;
195 if (isChild)
196 {
198 }
199 else
200 {
201 hideOnMouseLeave = true;
202 }
203 if (showPos == Vector2.zero)
204 {
206 }
207 base.gameObject.SetActive(value: true);
208 this.RebuildLayout(recursive: true);
209 if (!isChild)
210 {
211 showPos += fixPos;
212 if ((bool)soundPop)
213 {
214 SE.Play(soundPop);
215 }
216 _rect.anchoredPosition = new Vector2(-10000f, -10000f);
217 PositionMenu();
218 Current = this;
219 }
220 if ((bool)TooltipManager.Instance)
221 {
223 }
224 }
225
226 public void Hide()
227 {
229 {
230 return;
231 }
232 isPointerOver = false;
233 if (destroyOnHide)
234 {
235 if (!isDestroyed)
236 {
237 isDestroyed = true;
238 UnityEngine.Object.DestroyImmediate(base.gameObject);
239 if (onDestroy != null)
240 {
241 onDestroy();
242 }
244 {
246 }
247 }
248 }
249 else
250 {
251 if (!base.gameObject.activeSelf)
252 {
253 return;
254 }
255 base.gameObject.SetActive(value: false);
256 }
258 BaseCore.Instance.eventSystem.SetSelectedGameObject(null);
261 }
262
263 public void ClearItems()
264 {
265 layoutGroup.DestroyChildren();
266 }
267
268 public void AddSeparator(int index = 0)
269 {
270 UnityEngine.Object.Instantiate(separators[index]).transform.SetParent(layoutGroup.transform, worldPositionStays: false);
271 }
272
273 public void AddButton(Func<string> funcText, UnityAction action = null)
274 {
275 UIContextMenuItem item = UnityEngine.Object.Instantiate(defaultButton);
276 item.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
277 item.textName.text = funcText();
278 Button button = item.button;
279 if (action != null)
280 {
281 button.onClick.AddListener(action);
282 }
283 button.onClick.AddListener(delegate
284 {
285 if ((bool)item)
286 {
287 item.textName.text = funcText();
288 }
289 });
290 }
291
292 public UIButton AddButton(string idLang = "", Action action = null, bool hideAfter = true)
293 {
294 UIContextMenuItem uIContextMenuItem = UnityEngine.Object.Instantiate(defaultButton);
295 uIContextMenuItem.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
296 uIContextMenuItem.textName.text = Lang.Get(idLang);
297 UIButton uIButton = uIContextMenuItem.button as UIButton;
298 if (hideAfter)
299 {
300 uIButton.onClick.AddListener(Hide);
301 }
302 if ((bool)popper && hideAfter)
303 {
304 uIButton.onClick.AddListener(popper.parent.Hide);
305 }
306 if (action != null)
307 {
308 uIButton.onClick.AddListener(delegate
309 {
310 action();
311 });
312 }
313 return uIButton;
314 }
315
316 public UIContextMenuItem AddToggle(string idLang = "", bool isOn = false, UnityAction<bool> action = null)
317 {
318 UIContextMenuItem uIContextMenuItem = UnityEngine.Object.Instantiate(defaultToggle);
319 uIContextMenuItem.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
320 uIContextMenuItem.textName.text = idLang.lang().ToTitleCase();
321 uIContextMenuItem.toggle.isOn = isOn;
322 if (action != null)
323 {
324 uIContextMenuItem.toggle.onValueChanged.AddListener(action);
325 }
326 return uIContextMenuItem;
327 }
328
329 public UIContextMenuItem AddSlider(string text, Func<float, string> textFunc, float value, Action<float> action, float min = 0f, float max = 1f, bool isInt = false, bool hideOther = true, bool useInput = false)
330 {
331 UIContextMenuItem item = UnityEngine.Object.Instantiate(defaultSlider);
332 return AddSlider(item, text, textFunc, value, action, min, max, isInt, hideOther, useInput);
333 }
334
335 public UIContextMenuItem AddSlider(string id, string text, Func<float, string> textFunc, float value, Action<float> action, float min = 0f, float max = 1f, bool isInt = false, bool hideOther = true, bool useInput = false)
336 {
337 UIContextMenuItem item = Util.Instantiate<UIContextMenuItem>("Items/" + id);
338 return AddSlider(item, text, textFunc, value, action, min, max, isInt, hideOther, useInput);
339 }
340
341 public UIContextMenuItem AddSlider(UIContextMenuItem item, string text, Func<float, string> textFunc, float value, Action<float> action, float min = 0f, float max = 1f, bool isInt = false, bool hideOther = true, bool useInput = false)
342 {
343 UiInputField input = item.GetComponentInChildren<UiInputField>();
344 Slider s = item.slider;
345 item.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
346 item.textName.text = text.lang();
347 s.minValue = min;
348 if (max < 0f)
349 {
350 max = 0f;
351 }
352 s.maxValue = max;
353 s.wholeNumbers = isInt;
354 s.SetValueWithoutNotify(value);
355 item.textSlider.text = textFunc(value);
356 s.onValueChanged.AddListener(delegate(float a)
357 {
358 item.textSlider.text = textFunc(a);
359 action(a);
360 if ((bool)input)
361 {
362 input.text = s.value.ToString() ?? "";
363 input.Select();
364 input.HideCaret();
365 }
366 });
367 if ((bool)input)
368 {
369 input.SetActive(useInput);
370 input.text = s.value.ToString() ?? "";
371 input.onValueChanged.AddListener(delegate(string text)
372 {
373 if (!text.IsEmpty())
374 {
375 float num2 = Mathf.Clamp(text.ToInt(), min, max);
376 s.SetValueWithoutNotify(num2);
377 action(num2);
378 }
379 });
380 input.onUpdate = delegate
381 {
382 if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
383 {
384 action(s.value);
385 Current?.Hide();
386 }
387 else if (!input.text.IsEmpty())
388 {
389 int num = input.text.ToInt();
390 if ((float)num > max)
391 {
392 num = (int)max;
393 input.text = num.ToString() ?? "";
394 s.SetValueWithoutNotify(num);
395 }
396 }
397 };
398 if (useInput)
399 {
400 input.onDisable = delegate
401 {
402 action(s.value);
403 };
404 }
405 if (useInput)
406 {
407 input.Select();
408 }
409 }
410 if (hideOther)
411 {
412 RectTransform blocker = null;
413 EventTrigger component = s.GetComponent<EventTrigger>();
414 component.triggers.Clear();
415 EventTrigger.Entry entry = new EventTrigger.Entry
416 {
417 eventID = EventTriggerType.PointerDown
418 };
419 entry.callback.AddListener(delegate
420 {
421 blocker = Util.Instantiate<RectTransform>("Items/sliderBlocker", this);
422 blocker.SetAsFirstSibling();
423 UIContextMenu[] componentsInParent2 = GetComponentsInParent<UIContextMenu>();
424 for (int j = 0; j < componentsInParent2.Length; j++)
425 {
426 componentsInParent2[j].cg.DOFade(0.01f, 1f);
427 }
428 if ((bool)s && s.gameObject != null)
429 {
430 s.gameObject.AddComponent<CanvasGroup>().ignoreParentGroups = true;
431 }
432 });
433 component.triggers.Add(entry);
434 entry = new EventTrigger.Entry
435 {
436 eventID = EventTriggerType.PointerUp
437 };
438 entry.callback.AddListener(delegate
439 {
440 if (!isPointerOver)
441 {
442 Root.Hide();
443 }
444 else
445 {
446 UIContextMenu[] componentsInParent = GetComponentsInParent<UIContextMenu>();
447 foreach (UIContextMenu obj in componentsInParent)
448 {
449 DOTween.Kill(obj.cg);
450 obj.cg.alpha = 1f;
451 }
452 UnityEngine.Object.Destroy(s.GetComponent<CanvasGroup>());
453 UnityEngine.Object.DestroyImmediate(blocker.gameObject);
454 }
455 });
456 component.triggers.Add(entry);
457 }
458 return item;
459 }
460
461 public GridLayoutGroup AddGridLayout()
462 {
463 GridLayoutGroup gridLayoutGroup = UnityEngine.Object.Instantiate(defaultGridLayout);
464 gridLayoutGroup.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
465 return gridLayoutGroup;
466 }
467
468 public GridLayoutGroup AddGridLayout(string id)
469 {
470 return Util.Instantiate<GridLayoutGroup>("Items/" + id, layoutGroup);
471 }
472
473 public T AddGameObject<T>(T c) where T : Component
474 {
475 c.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
476 return c;
477 }
478
479 public T AddGameObject<T>(string id) where T : Component
480 {
481 return Util.Instantiate<T>("Items/" + id, layoutGroup);
482 }
483
484 public UIContextMenu AddOrGetChild(string idLang = "Child")
485 {
486 foreach (UIContextMenuPopper componentsInDirectChild in base.transform.GetComponentsInDirectChildren<UIContextMenuPopper>())
487 {
488 if (componentsInDirectChild.id == idLang)
489 {
490 return componentsInDirectChild.menu;
491 }
492 }
493 return AddChild(idLang);
494 }
495
496 public UIContextMenu AddChild(string idLang, TextAnchor anchor)
497 {
498 UIContextMenu uIContextMenu = AddChild(idLang);
499 uIContextMenu.layoutGroup.childAlignment = anchor;
500 return uIContextMenu;
501 }
502
503 public UIContextMenu AddChild(string idLang = "Child")
504 {
505 UIContextMenuPopper uIContextMenuPopper = UnityEngine.Object.Instantiate(defaultPopper);
506 uIContextMenuPopper.transform.SetParent(layoutGroup.transform, worldPositionStays: false);
507 uIContextMenuPopper.textName.text = Lang.Get(idLang);
508 uIContextMenuPopper.parent = this;
509 uIContextMenuPopper.id = idLang;
510 UIContextMenu uIContextMenu = uIContextMenuPopper.CreateMenu();
511 uIContextMenu.alwaysPopLeft = alwaysPopLeft;
512 uIContextMenu.ClearItems();
513 uIContextMenuPopper.button.onClick.RemoveAllListeners();
514 return uIContextMenuPopper.menu;
515 }
516
517 private void PositionMenu()
518 {
519 base.gameObject.SetActive(value: true);
520 this.RebuildLayout(recursive: true);
521 _rect.anchoredPosition = showPos;
522 RectTransform obj = _rect.parent as RectTransform;
523 Vector3 localPosition = _rect.localPosition;
524 Vector3 vector = obj.rect.min - _rect.rect.min;
525 Vector3 vector2 = obj.rect.max - _rect.rect.max;
526 localPosition.x = Mathf.Clamp(_rect.localPosition.x, vector.x, vector2.x);
527 localPosition.y = Mathf.Clamp(_rect.localPosition.y, vector.y, vector2.y + margin.y);
528 _rect.localPosition = localPosition;
529 }
530
532 {
533 return SetHighlightTarget(i.button1);
534 }
535
537 {
538 highlightTarget = t;
539 return this;
540 }
541}
virtual void ConsumeInput()
Definition: BaseCore.cs:57
EventSystem eventSystem
Definition: BaseCore.cs:27
static BaseCore Instance
Definition: BaseCore.cs:11
Definition: EInput.cs:8
static Vector3 uiMousePosition
Definition: EInput.cs:369
static void DisableIME()
Definition: EInput.cs:454
Definition: Hide.cs:4
static bool IsPointerOver(Component c)
Definition: Lang.cs:6
static string Get(string id)
Definition: Lang.cs:91
static TooltipManager Instance
void HideTooltips(bool immediate=false)
void DoHighlightTransition(bool instant=false)
Definition: UIButton.cs:526
static void TryShowTip(Transform root=null, bool highlight=true, bool ignoreWhenRightClick=true)
Definition: UIButton.cs:778
void DoNormalTransition(bool instant=true)
Definition: UIButton.cs:531
static UIContextMenuManager Instance
UIContextMenu CreateMenu()
Vector2 showPos
GridLayoutGroup defaultGridLayout
GameObject[] separators
UIContextMenu AddChild(string idLang="Child")
void AddSeparator(int index=0)
UIContextMenu Root
Vector2 margin
Vector2 fixPos
UIButton highlightTarget
UIContextMenuPopper popper
UIContextMenuItem defaultSlider
UIContextMenuItem defaultToggle
UIContextMenuItem AddToggle(string idLang="", bool isOn=false, UnityAction< bool > action=null)
UIContextMenu SetHighlightTarget(UIButton t)
void Show(UIItem i)
GridLayoutGroup AddGridLayout(string id)
static float timeSinceClosed
void PositionMenu()
static UIContextMenu Current
VerticalLayoutGroup layoutGroup
void OnPointerEnter(PointerEventData data)
UIContextMenuItem AddSlider(string text, Func< float, string > textFunc, float value, Action< float > action, float min=0f, float max=1f, bool isInt=false, bool hideOther=true, bool useInput=false)
UIContextMenu AddOrGetChild(string idLang="Child")
bool hideOnMouseLeave
UIContextMenuItem AddSlider(string id, string text, Func< float, string > textFunc, float value, Action< float > action, float min=0f, float max=1f, bool isInt=false, bool hideOther=true, bool useInput=false)
Material matBlur
UIContextMenuPopper defaultPopper
Transform logo
static bool closeOnMouseLeave
UIButton AddButton(string idLang="", Action action=null, bool hideAfter=true)
void Show(Vector2 pos)
UIContextMenu SetHighlightTarget(UIItem i)
SoundData soundPop
RectTransform _rect
float timeSincePointerLeft
UIContextMenuItem defaultButton
Action onDestroy
UIContextMenuItem AddSlider(UIContextMenuItem item, string text, Func< float, string > textFunc, float value, Action< float > action, float min=0f, float max=1f, bool isInt=false, bool hideOther=true, bool useInput=false)
CanvasGroup cg
float updatesSinceOpen
void OnPointerExit(PointerEventData data)
void AddButton(Func< string > funcText, UnityAction action=null)
void Show(UIButton b)
GridLayoutGroup AddGridLayout()
UIContextMenu parent
UIContextMenu AddChild(string idLang, TextAnchor anchor)
float timeSinceOpen
T AddGameObject< T >(T c)
Definition: UIItem.cs:5
UIButton button1
Definition: UIItem.cs:18
void Clear()
Definition: UiInputField.cs:93