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
|
#if VRC_SDK_VRCSDK3
using UnityEngine;
using UnityEngine.UIElements;
using GestureManager.Scripts.Core.Editor;
using UIEPosition = UnityEngine.UIElements.Position;
namespace GestureManager.Scripts.Editor.Modules.Vrc3
{
public class Vrc3WeightSlider : Vrc3VisualRender
{
private const float FadeSpeed = 0.02f;
private readonly Vrc3WeightController _controller;
private readonly string _target;
private VisualElement _slider;
private VisualElement _right;
private VisualElement _left;
private VisualElement _dot;
private VisualElement _textHolder;
private TextElement _textWeight;
internal bool Drag;
internal bool Active;
private int _afk;
private bool _out;
private float _weight = 1f;
private bool Xin(Vector2 p) => Rect.xMin < p.x && Rect.xMax > p.x;
public Vrc3WeightSlider(Vrc3WeightController controller, string target)
{
_controller = controller;
_target = target;
style.justifyContent = Justify.Center;
style.position = UIEPosition.Absolute;
pickingMode = PickingMode.Ignore;
CreateWeightController();
}
private void CreateWeightController()
{
_slider = this.MyAdd(new VisualElement { pickingMode = PickingMode.Ignore, style = { opacity = 0f, position = UIEPosition.Absolute, justifyContent = Justify.Center } });
_right = _slider.MyAdd(new VisualElement { pickingMode = PickingMode.Ignore, style = { backgroundColor = Color.gray, height = 10, position = UIEPosition.Absolute, right = 0 } }).MyBorder(1, 5, Color.black);
_left = _slider.MyAdd(new VisualElement { pickingMode = PickingMode.Ignore, style = { backgroundColor = RadialMenuUtility.Colors.CustomSelected, height = 10, width = 10, position = UIEPosition.Absolute, left = 0 } }).MyBorder(1, 5, Color.black);
_dot = _slider.MyAdd(RadialMenuUtility.Prefabs.NewCircle(16, RadialMenuUtility.Colors.RadialCenter, RadialMenuUtility.Colors.CustomMain, RadialMenuUtility.Colors.CustomBorder, UIEPosition.Absolute));
_textHolder = this.MyAdd(new VisualElement { pickingMode = PickingMode.Ignore, style = { opacity = 1f, position = UIEPosition.Absolute, justifyContent = Justify.Center, unityTextAlign = TextAnchor.MiddleCenter, alignItems = Align.Center, flexDirection = FlexDirection.Row } });
_textHolder.MyAdd(new TextElement { pickingMode = PickingMode.Ignore, text = "Weight: ", style = { fontSize = 12, height = 15 } });
_textWeight = _textHolder.MyAdd(new TextElement { pickingMode = PickingMode.Ignore, text = "100%", style = { fontSize = 15, color = RadialMenuUtility.Colors.CustomSelected, height = 15 } });
}
public override void Render(VisualElement root, Rect rect)
{
base.Render(root, rect);
if (Event.current.type == EventType.MouseUp) Drag = false;
style.width = style.width.value.value - 80;
style.left = rect.xMin + 40;
style.top = rect.y - 5;
_slider.style.width = style.width;
_textHolder.style.width = style.width;
HandleFade(Event.current.mousePosition);
if (!GUI.enabled) return;
if (Drag) Update(Event.current.mousePosition);
SetWeight(_controller.Module.GetParam(_target)?.Get() ?? 0f);
rect.center += new Vector2((rect.width - _slider.style.width.value.value) / 2 - 10, -10);
rect.width = _slider.style.width.value.value + 20f;
rect.height += 10f;
if (Event.current.type == EventType.MouseDown) CheckDragStart(rect);
}
private void CheckDragStart(Rect rect)
{
if (!rect.Contains(Event.current.mousePosition)) return;
_controller.DisableDrag();
Drag = true;
UpdatePosition(Event.current.mousePosition, false);
}
protected override bool RenderCondition(ModuleVrc3 module, RadialMenu menu) => menu.ToolBar.Selected == 0;
private void Update(Vector2 mousePosition)
{
if (Event.current.type == EventType.MouseDrag) UpdatePosition(mousePosition, false);
else if (!Xin(mousePosition)) UpdatePosition(mousePosition, true);
else if (_out && --_afk == 0) Drag = false;
}
private void UpdatePosition(Vector2 mousePosition, bool outer)
{
_out = outer;
var position = mousePosition.x - layout.x;
var weight = Mathf.Clamp(position / layout.width, 0f, 1f);
_controller.Module.GetParam(_target)?.Set(_controller.Module, weight);
_afk = 5;
}
private void HandleFade(Vector2 mousePosition)
{
if (GUI.enabled)
{
Active = _controller.Dragging || Mathf.Abs(mousePosition.y - Rect.y) < 16 && Xin(mousePosition);
if (_controller.Active) FadeOn();
else FadeOff();
}
else FadeDisable();
}
private void FadeDisable()
{
_slider.style.opacity = 0f;
_textHolder.style.opacity = 0.5f;
}
private void FadeOff()
{
_slider.style.opacity = Mathf.Clamp(_slider.style.opacity.value - FadeSpeed, 0f, 1f);
_textHolder.style.opacity = Mathf.Clamp(_textHolder.style.opacity.value + FadeSpeed, 0f, 1f);
}
private void FadeOn()
{
_slider.style.opacity = Mathf.Clamp(_slider.style.opacity.value + FadeSpeed, 0f, 1f);
_textHolder.style.opacity = Mathf.Clamp(_textHolder.style.opacity.value - FadeSpeed, 0f, 1f);
}
private void SetWeight(float weight)
{
_weight = weight;
ShowWeight();
}
internal void ShowWeight()
{
_left.style.width = _weight * style.width.value.value;
_right.style.width = style.width.value.value - _left.style.width.value.value;
_dot.style.left = _weight * style.width.value.value - 8;
_textWeight.text = (int)(_weight * 100f) + "%";
}
}
}
#endif
|