summaryrefslogtreecommitdiff
path: root/VRCSDK3Avatars/Assets/Resources/GestureManager/Scripts/Editor/Modules/Vrc3/AvatarTools.cs
blob: 108261b0b7f696be9e6ad82207c2362117037e8b (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#if VRC_SDK_VRCSDK3
using System.Collections.Generic;
using System.Linq;
using GestureManager.Scripts.Core.Editor;
using GestureManager.Scripts.Editor.Modules.Vrc3.DummyModes;
using UnityEditor;
using UnityEngine;
using VRC.Dynamics;
using VRC.Utility;

namespace GestureManager.Scripts.Editor.Modules.Vrc3
{
    public class AvatarTools
    {
        private UpdateSceneCamera _sceneCamera;
        private UpdateSceneCamera SceneCamera => _sceneCamera ?? (_sceneCamera = new UpdateSceneCamera());

        private ClickableContacts _clickableContacts;
        private ClickableContacts ContactsClickable => _clickableContacts ?? (_clickableContacts = new ClickableContacts());

        private TestAnimation _testAnimation;
        private TestAnimation AnimationTest => _testAnimation ?? (_testAnimation = new TestAnimation());

        private Customization _customization;
        private Customization CustomizationTool => _customization ?? (_customization = new Customization());

        internal void Gui(ModuleVrc3 module)
        {
            GUILayout.Label("Gesture Manager Tools", GestureManagerStyles.Header);
            GUILayout.Label("A collection of some small utility functions~", GestureManagerStyles.SubHeader);
            GUILayout.Space(10);
            SceneCamera.Display(module);
            ContactsClickable.Display(module);
            GUILayout.Label("Extra Tools", GestureManagerStyles.Header);
            AnimationTest.Display(module);
            GUILayout.Label("Customization", GestureManagerStyles.Header);
            CustomizationTool.Display(module);
        }

        internal void OnUpdate(ModuleVrc3 module)
        {
            SceneCamera.OnUpdate(module);
            AnimationTest.OnUpdate(module);
            ContactsClickable.OnUpdate(module);
        }

        internal void OnLateUpdate(ModuleVrc3 module)
        {
            SceneCamera.OnLateUpdate(module);
            AnimationTest.OnLateUpdate(module);
            ContactsClickable.OnLateUpdate(module);
        }

        private class UpdateSceneCamera : GmgDynamicFunction
        {
            private static Camera _camera;
            private readonly BoolProperty _isActive = new BoolProperty("GM3 SceneCamera");

            protected override string Name => "Scene Camera";
            protected override string Description => "This will match your game view with your scene view!\nClick the button to setup the main camera automatically~";
            protected override bool Active => _isActive.Property;

            public UpdateSceneCamera()
            {
                if (_isActive.Property) AutoToggle();
            }

            protected override void Update(ModuleVrc3 module)
            {
                var sceneView = SceneView.lastActiveSceneView;
                if (!sceneView) return;
                var camera = sceneView.camera;
                if (!camera || !_camera) return;
                var sceneTransform = camera.transform;
                var transform = _camera.transform;
                var positionVector = sceneTransform.position;
                transform.rotation = sceneTransform.rotation;
                transform.position = new Vector3(positionVector.x, positionVector.y + 0.001f, positionVector.z);
            }

            protected override void Gui(ModuleVrc3 module)
            {
                if (GmgLayoutHelper.ButtonObjectField("Scene Camera: ", _camera, _camera ? 'X' : 'A', camera => _camera = camera)) AutoToggle();
                _isActive.Property = _camera != null;
            }

            private static void AutoToggle() => _camera = _camera ? null : Camera.main;
        }

        private class ClickableContacts : GmgDynamicFunction
        {
            private readonly BoolProperty _isActive = new BoolProperty("GM3 ClickableContacts");
            private readonly StringProperty _tag = new StringProperty("GM3 ClickableContacts Tag");

            private readonly Camera _camera = Camera.main;
            private readonly HashSet<ContactReceiver> _activeContact = new HashSet<ContactReceiver>();

            protected override string Name => "Clickable Contacts";
            protected override string Description => "Click and trigger Avatar Contacts with your mouse!\nLike you can do with PhysBones~";
            protected override bool Active => _isActive.Property;

            protected override void Update(ModuleVrc3 module) => LateUpdate(module);

            protected override void LateUpdate(ModuleVrc3 module)
            {
                if (Input.GetMouseButton(0)) OnClick(module);
                if (Input.GetMouseButtonUp(0)) Disable();
            }

            private void OnClick(ModuleVrc3 module)
            {
                if (!_camera) return;
                var ray = _camera.ScreenPointToRay(Input.mousePosition);
                CheckRay(module, ray.origin, ray.origin + ray.direction * 1000f);
            }

            private void CheckRay(ModuleVrc3 module, Vector3 s, Vector3 e)
            {
                var manager = ContactManager.Inst;
                if (!manager) return;
                foreach (var receiver in module.Receivers.Where(receiver => string.IsNullOrEmpty(_tag.Property) || receiver.collisionTags.Contains(_tag.Property))) OnContactValue(receiver, ValueFor(manager, receiver, s, e));
            }

            private void OnContactValue(ContactReceiver receiver, float value)
            {
                if (value == 0f) Disable(receiver);
                else Enable(receiver, value);
            }

            private static float ValueFor(ContactManager manager, ContactReceiver receiver, Vector3 s, Vector3 e)
            {
                var isProximity = receiver.receiverType == ContactReceiver.ReceiverType.Proximity;
                var distance = DistanceFrom(manager, receiver, s, e, out var radius);
                if (isProximity) distance -= radius;
                if (isProximity) return Mathf.Clamp(-distance / radius, 0f, 1f);
                return distance < 0 ? 1f : 0f;
            }

            private static float DistanceFrom(ContactManager manager, ContactBase receiver, Vector3 s, Vector3 e, out float radius)
            {
                receiver.InitShape();
                manager.collision.UpdateShapeData(receiver.shape);
                var shape = manager.collision.GetShapeData(receiver.shape);
                var scaleVector = receiver.transform.lossyScale;
                radius = receiver.radius * Mathf.Max(scaleVector.x, scaleVector.y, scaleVector.z);
                Vector3 result0;
                Vector3 result1;
                if (receiver.shapeType == ContactBase.ShapeType.Sphere) PhysicsUtil.ClosestPointsBetweenLineSegments(s, e, shape.outPos0, shape.outPos0, out result0, out result1);
                else PhysicsUtil.ClosestPointsBetweenLineSegments(s, e, shape.outPos0, shape.outPos1, out result0, out result1);
                return (result0 - result1).magnitude - radius;
            }

            private void Enable(ContactReceiver receiver, float value)
            {
                if (_activeContact.Contains(receiver)) return;
                _activeContact.Add(receiver);
                receiver.SetParameter(value);
            }

            private void Disable(ContactReceiver receiver)
            {
                if (!_activeContact.Contains(receiver)) return;
                _activeContact.Remove(receiver);
                receiver.SetParameter(0f);
            }

            private void Disable()
            {
                foreach (var receiver in _activeContact) receiver.SetParameter(0f);
                _activeContact.Clear();
            }

            protected override void Gui(ModuleVrc3 module)
            {
                _isActive.Property = GmgLayoutHelper.ToggleRight("Enabled: ", _isActive.Property);
                _tag.Property = GmgLayoutHelper.PlaceHolderTextField("Tag: ", _tag.Property, " <leave blank to ignore tags> ");
            }
        }

        private class TestAnimation : GmgDynamicFunction
        {
            private bool _testMode;
            private AnimationClip _selectingCustomAnim;

            protected override string Name => "Test Animation";
            protected override string Description => "Use this tool to preview any animation in your project.\nYou can preview Emotes or Gestures.";
            protected override bool Active => _testMode;

            protected override void Gui(ModuleVrc3 module)
            {
                _testMode = module.Manager.OnCustomAnimation;

                var isEditMode = module.DummyMode is Vrc3EditMode;
                using (new GUILayout.HorizontalScope())
                {
                    _selectingCustomAnim = GmgLayoutHelper.ObjectField("Animation: ", _selectingCustomAnim, module.Manager.SetCustomAnimation);

                    GUI.enabled = _selectingCustomAnim && !isEditMode;
                    switch (_testMode)
                    {
                        case true when GUILayout.Button("Stop", GestureManagerStyles.GuiGreenButton):
                            module.Manager.StopCustomAnimation();
                            break;
                        case false when GUILayout.Button("Play", GUILayout.Width(100)):
                            module.Manager.PlayCustomAnimation(_selectingCustomAnim);
                            break;
                    }

                    GUI.enabled = true;
                }
            }
        }

        private class Customization : GmgDynamicFunction
        {
            protected override string Name => "Radial Menu";
            protected override string Description => "Customize the colors of the radial menu!";
            protected override bool Active => false;

            private Color _customMain = RadialMenuUtility.Colors.CustomMain;
            private Color _customBorder = RadialMenuUtility.Colors.CustomBorder;
            private Color _customSelected = RadialMenuUtility.Colors.CustomSelected;

            protected override void Gui(ModuleVrc3 module)
            {
                _customMain = GmgLayoutHelper.ResetColorField("Main Color: ", _customMain, RadialMenuUtility.Colors.Default.Main);
                _customBorder = GmgLayoutHelper.ResetColorField("Border Color: ", _customBorder, RadialMenuUtility.Colors.Default.Border);
                _customSelected = GmgLayoutHelper.ResetColorField("Selected Color:", _customSelected, RadialMenuUtility.Colors.Default.Selected);

                GUILayout.Space(10);

                if (GUILayout.Button("Save")) Save(module);
            }

            private void Save(ModuleVrc3 module)
            {
                RadialMenuUtility.Colors.SaveColors(_customMain, _customBorder, _customSelected);
                module.ReloadRadials();
            }
        }

        private abstract class GmgDynamicFunction
        {
            protected abstract string Name { get; }
            protected abstract string Description { get; }
            protected abstract bool Active { get; }

            internal void Display(ModuleVrc3 module)
            {
                using (new GmgLayoutHelper.GuiBackground(Active ? Color.green : GUI.backgroundColor))
                using (new GUILayout.VerticalScope(GestureManagerStyles.EmoteError))
                {
                    GUILayout.Label(Name, GestureManagerStyles.ToolHeader);
                    GUILayout.Label(Description, GestureManagerStyles.SubHeader);
                    GUILayout.Space(10);
                    Gui(module);
                }
            }

            internal void OnUpdate(ModuleVrc3 module)
            {
                if (Active) Update(module);
            }

            internal void OnLateUpdate(ModuleVrc3 module)
            {
                if (Active) LateUpdate(module);
            }

            protected abstract void Gui(ModuleVrc3 module);

            protected virtual void Update(ModuleVrc3 module)
            {
            }

            protected virtual void LateUpdate(ModuleVrc3 module)
            {
            }
        }

        private class BoolProperty
        {
            private readonly string _key;

            public BoolProperty(string key) => _key = key;

            internal bool Property
            {
                get => EditorPrefs.GetBool(_key);
                set => EditorPrefs.SetBool(_key, value);
            }
        }

        private class StringProperty
        {
            private readonly string _key;

            public StringProperty(string key) => _key = key;

            internal string Property
            {
                get => EditorPrefs.GetString(_key);
                set => EditorPrefs.SetString(_key, value);
            }
        }
    }
}
#endif