-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopupButton.cs
134 lines (114 loc) · 4.03 KB
/
PopupButton.cs
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
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cuboid.UI
{
public class PopupButton : MonoBehaviour
{
[SerializeField] private Button _button;
private IDisposable _createdPopupButton;
public void CreatePopupButton<T>(IBinding<T> binding, Action<T> onSetValue = null) where T : Enum, new()
{
PopupButtonInternal<T> button = new(_button, binding);
_createdPopupButton = button;
button.OnSetValue = onSetValue;
}
private void OnDestroy()
{
if (_createdPopupButton != null)
{
_createdPopupButton.Dispose();
}
}
/// <summary>
///
/// </summary>
public class PopupButtonInternal<T> : IDisposable, ICanSetBinding<T> where T : Enum, new()
{
private IBinding<T> _binding;
private Action<T> _onValueChanged;
private Button _button;
public Action<T> OnSetValue;
// constructor
public PopupButtonInternal(Button button, IBinding<T> binding)
{
_onValueChanged = OnValueChanged;
_button = button;
_button.ActiveData = new Button.Data()
{
OnPressed = () => OpenPopup()
};
SetBinding(binding);
}
public void SetBinding(IBinding<T> binding)
{
Unregister();
_binding = binding;
Register();
}
private void OnValueChanged(T value)
{
// update the text
EnumUtils.GetEnumData(value, out string text, out Sprite icon);
_button.ActiveData.Icon = icon;
_button.ActiveData.Text = text;
_button.DataChanged();
}
private void OpenPopup()
{
List<Button.Data> buttons = new List<Button.Data>();
Type enumType = typeof(T);
Array values = Enum.GetValues(enumType);
foreach (T value in values)
{
EnumUtils.GetEnumData(value, out string text, out Sprite icon);
bool active = _binding.Value.Equals(value);
buttons.Add(new Button.Data()
{
Text = text,
OnPressed = () =>
{
_binding.Value = value;
OnSetValue?.Invoke(_binding.Value);
PopupsController.Instance.CloseLastPopup();
},
Icon = (icon == null && active) ? Icons.Data.Check : icon,
Variant = active ? ButtonColors.Variant.Soft : ButtonColors.Variant.Plain
});
}
PrettyTypeNameAttribute prettyTypeName = enumType.GetCustomAttribute<PrettyTypeNameAttribute>();
string title = prettyTypeName != null ? prettyTypeName.Name : enumType.Name;
// open the popup, currently a simple context menu
PopupsController.Instance.OpenContextMenu(new ContextMenu.ContextMenuData()
{
Title = title,
Buttons = buttons
});
}
private void Register()
{
if (_binding != null)
{
_binding.Register(_onValueChanged);
}
}
private void Unregister()
{
if (_binding != null)
{
_binding.Unregister(_onValueChanged);
}
}
public void Dispose()
{
Unregister();
}
}
}
}