Improved Context Menus
Context menus now support dropdown options Context menus can now be created for UI elements with new dedicated cancellation click catcher Game manager creates context menus based on the object being clicked, no need for duplicate code in every context that object appears
This commit is contained in:
parent
36f0e4b364
commit
0ae4a388f5
14 changed files with 1062 additions and 226 deletions
|
|
@ -9,7 +9,13 @@ using UnityEngine.UI;
|
|||
public class ContextMenu : MonoBehaviour
|
||||
{
|
||||
public Canvas Canvas;
|
||||
public RectTransform OptionPrefab;
|
||||
public RectTransform Root;
|
||||
public ClickCatcher CancelClickCatcher;
|
||||
public ContextMenuOption OptionPrefab;
|
||||
public ContextMenu DropMenuPrefab;
|
||||
public ContextMenu Parent;
|
||||
public bool ForceDirectionRight;
|
||||
public Vector3 ForcePosition;
|
||||
|
||||
public RectTransform ContentRoot;
|
||||
|
||||
|
|
@ -17,31 +23,100 @@ public class ContextMenu : MonoBehaviour
|
|||
public float PaddingHeight;
|
||||
|
||||
private List<GameObject> _options = new List<GameObject>();
|
||||
private bool _hasDropdown;
|
||||
private bool _dropdownRight;
|
||||
private GameObject _dropdown;
|
||||
private ContextMenuOption _currentDropdown;
|
||||
|
||||
public void SetOptions(IEnumerable<(string text, Action action)> options)
|
||||
private void Start()
|
||||
{
|
||||
if (CancelClickCatcher != null)
|
||||
CancelClickCatcher.OnClick += data =>
|
||||
{
|
||||
End();
|
||||
};
|
||||
}
|
||||
|
||||
private void End()
|
||||
{
|
||||
if (_dropdown != null)
|
||||
Destroy(_dropdown);
|
||||
CancelClickCatcher.gameObject.SetActive(false);
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
var rect = transform as RectTransform;
|
||||
var pivot = rect.pivot;
|
||||
pivot.x = Input.mousePosition.x > Screen.width - rect.sizeDelta.x * Canvas.scaleFactor ? 1 : 0;
|
||||
pivot.y = Input.mousePosition.y < (PaddingHeight + options.Count() * OptionHeight) * Canvas.scaleFactor ? 0 : 1;
|
||||
rect.pivot = pivot;
|
||||
rect.position = Input.mousePosition;
|
||||
|
||||
// Destroy previous options
|
||||
foreach(var option in _options)
|
||||
Destroy(option);
|
||||
_options.Clear();
|
||||
|
||||
foreach (var option in options)
|
||||
_hasDropdown = false;
|
||||
}
|
||||
|
||||
public void AddOption(string text, Action action)
|
||||
{
|
||||
var optionButton = Instantiate(OptionPrefab, ContentRoot);
|
||||
optionButton.Label.text = text;
|
||||
optionButton.Button.onClick.AddListener(() =>
|
||||
{
|
||||
var optionButton = Instantiate(OptionPrefab, ContentRoot);
|
||||
optionButton.GetComponentInChildren<TextMeshProUGUI>().text = option.text;
|
||||
optionButton.GetComponent<Button>().onClick.AddListener(() =>
|
||||
action();
|
||||
if(Parent!=null)
|
||||
Parent.End();
|
||||
else
|
||||
End();
|
||||
});
|
||||
optionButton.Hover.OnEnter += data =>
|
||||
{
|
||||
if (_dropdown != null)
|
||||
Destroy(_dropdown);
|
||||
};
|
||||
optionButton.Arrow.gameObject.SetActive(false);
|
||||
_options.Add(optionButton.gameObject);
|
||||
}
|
||||
|
||||
public void AddDropdown(string text, IEnumerable<(string text, Action action)> options)
|
||||
{
|
||||
_hasDropdown = true;
|
||||
var optionButton = Instantiate(OptionPrefab, ContentRoot);
|
||||
optionButton.Label.text = text;
|
||||
optionButton.Hover.OnEnter += data =>
|
||||
{
|
||||
if (_currentDropdown != optionButton)
|
||||
{
|
||||
option.action();
|
||||
gameObject.SetActive(false);
|
||||
});
|
||||
_options.Add(optionButton.gameObject);
|
||||
}
|
||||
if(_dropdown!=null)
|
||||
Destroy(_dropdown);
|
||||
var drop = Instantiate(DropMenuPrefab, Root);
|
||||
_dropdown = drop.gameObject;
|
||||
foreach (var option in options)
|
||||
drop.AddOption(option.text, option.action);
|
||||
drop.Parent = this;
|
||||
drop.ForceDirectionRight = _dropdownRight;
|
||||
Vector3[] corners = new Vector3[4];
|
||||
((RectTransform) optionButton.transform).GetWorldCorners(corners);
|
||||
drop.ForcePosition = corners[_dropdownRight ? 2 : 1];
|
||||
drop.Show();
|
||||
}
|
||||
};
|
||||
_options.Add(optionButton.gameObject);
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
var rect = transform as RectTransform;
|
||||
var pivot = rect.pivot;
|
||||
var canvas = Parent != null ? Parent.Canvas : Canvas;
|
||||
if (Parent!=null)
|
||||
pivot.x = ForceDirectionRight ? 0 : 1;
|
||||
else
|
||||
pivot.x = Input.mousePosition.x > Screen.width - rect.sizeDelta.x * canvas.scaleFactor ? 1 : 0;
|
||||
var scaleFactor = canvas.scaleFactor;
|
||||
_dropdownRight = Input.mousePosition.x < Screen.width - rect.sizeDelta.x * scaleFactor * 2;
|
||||
var pivotTop = (Parent==null ? Input.mousePosition.y : ForcePosition.y) > (PaddingHeight + _options.Count * OptionHeight) * scaleFactor;
|
||||
pivot.y = pivotTop ? 1 : 0;
|
||||
rect.pivot = pivot;
|
||||
rect.position = Parent!=null ? ForcePosition - (pivotTop ? Vector3.zero : Vector3.up * (OptionHeight * scaleFactor)) : Input.mousePosition;
|
||||
if (Parent==null)
|
||||
CancelClickCatcher.gameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
Assets/Scripts/UI/ContextMenuOption.cs
Normal file
13
Assets/Scripts/UI/ContextMenuOption.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ContextMenuOption : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI Label;
|
||||
public Image Arrow;
|
||||
public Button Button;
|
||||
public MouseHover Hover;
|
||||
}
|
||||
11
Assets/Scripts/UI/ContextMenuOption.cs.meta
Normal file
11
Assets/Scripts/UI/ContextMenuOption.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 741dca7ca8716dd438516c4f8bafd441
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
27
Assets/Scripts/UI/MouseHover.cs
Normal file
27
Assets/Scripts/UI/MouseHover.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class MouseHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
public event Action<PointerEventData> OnEnter;
|
||||
public event Action<PointerEventData> OnExit;
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
OnEnter?.Invoke(eventData);
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
OnExit?.Invoke(eventData);
|
||||
}
|
||||
|
||||
public void ClearListeners()
|
||||
{
|
||||
OnEnter = null;
|
||||
OnExit = null;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/MouseHover.cs.meta
Normal file
11
Assets/Scripts/UI/MouseHover.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6435d6126b6f8694292cb94466cf1df4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue