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
|
using System.Collections.Generic;
using Rewired.ControllerExtensions;
using UnityEngine;
namespace Rewired.Demos;
[AddComponentMenu("")]
public class DualShock4SpecialFeaturesExample : MonoBehaviour
{
private class Touch
{
public GameObject go;
public int touchId = -1;
}
private const int maxTouches = 2;
public int playerId;
public Transform touchpadTransform;
public GameObject lightObject;
public Transform accelerometerTransform;
private List<Touch> touches;
private Queue<Touch> unusedTouches;
private bool isFlashing;
private GUIStyle textStyle;
private Player player => ReInput.players.GetPlayer(playerId);
private void Awake()
{
InitializeTouchObjects();
}
private void Update()
{
if (!ReInput.isReady)
{
return;
}
IDualShock4Extension firstDS = GetFirstDS4(player);
if (firstDS != null)
{
base.transform.rotation = firstDS.GetOrientation();
HandleTouchpad(firstDS);
Vector3 accelerometerValue = firstDS.GetAccelerometerValue();
accelerometerTransform.LookAt(accelerometerTransform.position + accelerometerValue);
}
if (player.GetButtonDown("CycleLight"))
{
SetRandomLightColor();
}
if (player.GetButtonDown("ResetOrientation"))
{
ResetOrientation();
}
if (player.GetButtonDown("ToggleLightFlash"))
{
if (isFlashing)
{
StopLightFlash();
}
else
{
StartLightFlash();
}
isFlashing = !isFlashing;
}
if (player.GetButtonDown("VibrateLeft"))
{
firstDS.SetVibration(0, 1f, 1f);
}
if (player.GetButtonDown("VibrateRight"))
{
firstDS.SetVibration(1, 1f, 1f);
}
}
private void OnGUI()
{
if (textStyle == null)
{
textStyle = new GUIStyle(GUI.skin.label);
textStyle.fontSize = 20;
textStyle.wordWrap = true;
}
if (GetFirstDS4(player) != null)
{
GUILayout.BeginArea(new Rect(200f, 100f, (float)Screen.width - 400f, (float)Screen.height - 200f));
GUILayout.Label("Rotate the Dual Shock 4 to see the model rotate in sync.", textStyle);
GUILayout.Label("Touch the touchpad to see them appear on the model.", textStyle);
ActionElementMap firstElementMapWithAction = player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Joystick, "ResetOrientation", skipDisabledMaps: true);
if (firstElementMapWithAction != null)
{
GUILayout.Label("Press " + firstElementMapWithAction.elementIdentifierName + " to reset the orientation. Hold the gamepad facing the screen with sticks pointing up and press the button.", textStyle);
}
firstElementMapWithAction = player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Joystick, "CycleLight", skipDisabledMaps: true);
if (firstElementMapWithAction != null)
{
GUILayout.Label("Press " + firstElementMapWithAction.elementIdentifierName + " to change the light color.", textStyle);
}
firstElementMapWithAction = player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Joystick, "ToggleLightFlash", skipDisabledMaps: true);
if (firstElementMapWithAction != null)
{
GUILayout.Label("Press " + firstElementMapWithAction.elementIdentifierName + " to start or stop the light flashing.", textStyle);
}
firstElementMapWithAction = player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Joystick, "VibrateLeft", skipDisabledMaps: true);
if (firstElementMapWithAction != null)
{
GUILayout.Label("Press " + firstElementMapWithAction.elementIdentifierName + " vibrate the left motor.", textStyle);
}
firstElementMapWithAction = player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Joystick, "VibrateRight", skipDisabledMaps: true);
if (firstElementMapWithAction != null)
{
GUILayout.Label("Press " + firstElementMapWithAction.elementIdentifierName + " vibrate the right motor.", textStyle);
}
GUILayout.EndArea();
}
}
private void ResetOrientation()
{
GetFirstDS4(player)?.ResetOrientation();
}
private void SetRandomLightColor()
{
IDualShock4Extension firstDS = GetFirstDS4(player);
if (firstDS != null)
{
Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1f);
firstDS.SetLightColor(color);
lightObject.GetComponent<MeshRenderer>().material.color = color;
}
}
private void StartLightFlash()
{
if (GetFirstDS4(player) is DualShock4Extension dualShock4Extension)
{
dualShock4Extension.SetLightFlash(0.5f, 0.5f);
}
}
private void StopLightFlash()
{
if (GetFirstDS4(player) is DualShock4Extension dualShock4Extension)
{
dualShock4Extension.StopLightFlash();
}
}
private IDualShock4Extension GetFirstDS4(Player player)
{
foreach (Joystick joystick in player.controllers.Joysticks)
{
IDualShock4Extension extension = joystick.GetExtension<IDualShock4Extension>();
if (extension != null)
{
return extension;
}
}
return null;
}
private void InitializeTouchObjects()
{
touches = new List<Touch>(2);
unusedTouches = new Queue<Touch>(2);
for (int i = 0; i < 2; i++)
{
Touch touch = new Touch();
touch.go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
touch.go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
touch.go.transform.SetParent(touchpadTransform, worldPositionStays: true);
touch.go.GetComponent<MeshRenderer>().material.color = ((i == 0) ? Color.red : Color.green);
touch.go.SetActive(value: false);
unusedTouches.Enqueue(touch);
}
}
private void HandleTouchpad(IDualShock4Extension ds4)
{
for (int num = touches.Count - 1; num >= 0; num--)
{
Touch touch = touches[num];
if (!ds4.IsTouchingByTouchId(touch.touchId))
{
touch.go.SetActive(value: false);
unusedTouches.Enqueue(touch);
touches.RemoveAt(num);
}
}
for (int i = 0; i < ds4.maxTouches; i++)
{
if (ds4.IsTouching(i))
{
int touchId = ds4.GetTouchId(i);
Touch touch2 = touches.Find((Touch x) => x.touchId == touchId);
if (touch2 == null)
{
touch2 = unusedTouches.Dequeue();
touches.Add(touch2);
}
touch2.touchId = touchId;
touch2.go.SetActive(value: true);
ds4.GetTouchPosition(i, out var position);
touch2.go.transform.localPosition = new Vector3(position.x - 0.5f, 0.5f + touch2.go.transform.localScale.y * 0.5f, position.y - 0.5f);
}
}
}
}
|