Skip to content

Commit 6ecba36

Browse files
committed
Fixed bug in WebXRController Button Up/Down state.
1 parent 5beeca6 commit 6ecba36

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

Packages/webxr/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- WebXRController Button Up/Down state.
810

911
## [0.5.0] - 2020-12-19
1012
### Added

Packages/webxr/Runtime/Scripts/WebXRController.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,51 +196,34 @@ public bool GetButton(ButtonTypes action)
196196
return buttonStates[action].pressed;
197197
}
198198

199-
private bool GetPastButtonState(ButtonTypes action)
200-
{
201-
if (!buttonStates.ContainsKey(action))
202-
return false;
203-
return buttonStates[action].prevPressedState;
204-
}
205-
206199
private void SetButtonState(ButtonTypes action, bool isPressed, float value)
207200
{
208201
if (buttonStates.ContainsKey(action))
209202
{
210-
buttonStates[action].pressed = isPressed;
211-
buttonStates[action].value = value;
203+
buttonStates[action].UpdateState(isPressed, value);
212204
}
213205
else
214206
buttonStates.Add(action, new WebXRControllerButton(isPressed, value));
215207
}
216208

217-
private void SetPastButtonState(ButtonTypes action, bool isPressed)
218-
{
219-
if (!buttonStates.ContainsKey(action))
220-
return;
221-
buttonStates[action].prevPressedState = isPressed;
222-
}
223-
224209
public bool GetButtonDown(ButtonTypes action)
225210
{
226211
TryUpdateButtons();
227-
if (GetButton(action) && !GetPastButtonState(action))
212+
if (!buttonStates.ContainsKey(action))
228213
{
229-
SetPastButtonState(action, true);
230-
return true;
214+
return false;
231215
}
232-
return false;
216+
return buttonStates[action].down;
233217
}
234218

235219
public bool GetButtonUp(ButtonTypes action)
236220
{
237221
TryUpdateButtons();
238-
if (!GetButton(action) && GetPastButtonState(action))
222+
if (!buttonStates.ContainsKey(action))
239223
{
240-
SetPastButtonState(action, false);
241-
return true;
224+
return false;
242225
}
243-
return false;
226+
return buttonStates[action].up;
244227
}
245228

246229
public float GetButtonIndexValue(int index)

Packages/webxr/Runtime/Scripts/WebXRControllerData.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public class WebXRHitPoseData
8484
public Quaternion rotation;
8585
}
8686

87-
public enum WebXRControllerHand {
87+
public enum WebXRControllerHand
88+
{
8889
NONE = 0,
8990
LEFT = 1,
9091
RIGHT = 2
@@ -94,14 +95,41 @@ public enum WebXRControllerHand {
9495
public class WebXRControllerButton
9596
{
9697
public bool pressed;
97-
public bool prevPressedState;
98-
public bool touched;
98+
public bool down;
99+
public bool up;
99100
public float value;
100101

101102
public WebXRControllerButton(bool isPressed, float buttonValue)
102103
{
104+
down = false;
105+
up = false;
106+
pressed = isPressed;
107+
value = buttonValue;
108+
}
109+
110+
public void UpdateState(bool isPressed, float buttonValue)
111+
{
112+
if (isPressed && pressed) // nothing
113+
{
114+
down = false;
115+
up = false;
116+
}
117+
else if (isPressed && !pressed) // up
118+
{
119+
down = true;
120+
up = false;
121+
}
122+
else if (!isPressed && !pressed) // nothing
123+
{
124+
down = false;
125+
up = false;
126+
}
127+
else if (!isPressed && pressed) // down
128+
{
129+
down = false;
130+
up = true;
131+
}
103132
pressed = isPressed;
104-
prevPressedState = false;
105133
value = buttonValue;
106134
}
107135
}

0 commit comments

Comments
 (0)