Skip to content

Commit 4459bb8

Browse files
committed
Add ControllerButtonMask to get buttins input simultaneously
* ViveInput.GetAnyPress(HandRole.RightHand, new ControllerButtonMask(ControllerButton.Trigger, ControllerButton.Pad)) - return true if right controller trigger or pad button pressed * ViveInput.GetAllPress(HandRole.RightHand, new ControllerButtonMask(ControllerButton.Trigger, ControllerButton.Pad)) - return true if both right controller trigger and pad button pressed
1 parent cbca4a6 commit 4459bb8

File tree

5 files changed

+337
-0
lines changed

5 files changed

+337
-0
lines changed

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveInput/ControllerState.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ private interface ICtrlState
3030
Vector2 GetPadPressVector();
3131
Vector2 GetPadTouchVector();
3232
Vector2 GetScrollDelta(ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY);
33+
ulong PreviousButtonPressed { get; }
34+
ulong CurrentButtonPressed { get; }
3335
}
3436

3537
private class CtrlState : ICtrlState
@@ -49,6 +51,8 @@ public virtual void RemoveListener(ControllerButton button, RoleValueEventListen
4951
public virtual Vector2 GetPadPressVector() { return Vector2.zero; }
5052
public virtual Vector2 GetPadTouchVector() { return Vector2.zero; }
5153
public virtual Vector2 GetScrollDelta(ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY) { return Vector2.zero; }
54+
public virtual ulong PreviousButtonPressed { get { return 0ul; } }
55+
public virtual ulong CurrentButtonPressed { get { return 0ul; } }
5256
}
5357

5458
private sealed class RCtrlState : CtrlState
@@ -79,6 +83,10 @@ private sealed class RCtrlState : CtrlState
7983
private const float hairDelta = 0.1f; // amount trigger must be pulled or released to change state
8084
private float hairTriggerLimit;
8185

86+
public override ulong PreviousButtonPressed { get { return prevButtonPressed; } }
87+
88+
public override ulong CurrentButtonPressed { get { return currButtonPressed; } }
89+
8290
public RCtrlState(Type roleEnumType, int roleValue)
8391
{
8492
m_map = ViveRole.GetMap(roleEnumType);
@@ -537,6 +545,8 @@ public virtual void RemoveListener(ControllerButton button, RoleValueEventListen
537545
public virtual Vector2 GetPadPressVector() { return Vector2.zero; }
538546
public virtual Vector2 GetPadTouchVector() { return Vector2.zero; }
539547
public virtual Vector2 GetScrollDelta(ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY) { return Vector2.zero; }
548+
public virtual ulong PreviousButtonPressed { get { return 0ul; } }
549+
public virtual ulong CurrentButtonPressed { get { return 0ul; } }
540550

541551
public virtual void AddListener(ControllerButton button, RoleEventListener<TRole> listener, ButtonEventType type = ButtonEventType.Click) { }
542552
public virtual void RemoveListener(ControllerButton button, RoleEventListener<TRole> listener, ButtonEventType type = ButtonEventType.Click) { }
@@ -555,6 +565,10 @@ private sealed class RGCtrolState<TRole> : GCtrlState<TRole>
555565

556566
private RoleEventListener<TRole>[][] listeners;
557567

568+
public override ulong PreviousButtonPressed { get { return m_state.PreviousButtonPressed; } }
569+
570+
public override ulong CurrentButtonPressed { get { return m_state.CurrentButtonPressed; } }
571+
558572
public RGCtrolState(TRole role)
559573
{
560574
var info = ViveRoleEnum.GetInfo<TRole>();
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
2+
3+
using HTC.UnityPlugin.Utility;
4+
using System.Collections.Generic;
5+
using UnityEditor;
6+
using UnityEngine;
7+
8+
namespace HTC.UnityPlugin.Vive
9+
{
10+
[CustomPropertyDrawer(typeof(ControllerButtonMask))]
11+
public class ControllerButtonMaskDrawer : PropertyDrawer
12+
{
13+
private static GUIStyle s_popup;
14+
private static GUIContent s_tempContent;
15+
private static List<bool> s_displayedMask;
16+
private static EnumUtils.EnumDisplayInfo s_enumInfo = EnumUtils.GetDisplayInfo(typeof(ControllerButton));
17+
18+
private bool m_foldoutOpen = false;
19+
20+
static ControllerButtonMaskDrawer()
21+
{
22+
s_popup = new GUIStyle(EditorStyles.popup);
23+
s_tempContent = new GUIContent();
24+
s_displayedMask = new List<bool>();
25+
}
26+
27+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
28+
{
29+
if (!m_foldoutOpen || s_enumInfo == null)
30+
{
31+
return EditorGUIUtility.singleLineHeight;
32+
}
33+
else
34+
{
35+
return EditorGUIUtility.singleLineHeight * (s_enumInfo.displayedMaskNames.Length + 2);
36+
}
37+
}
38+
39+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
40+
{
41+
EditorGUI.BeginProperty(position, label, property);
42+
43+
position = EditorGUI.PrefixLabel(position, new GUIContent(property.displayName));
44+
45+
// get display mask value
46+
s_displayedMask.Clear();
47+
var maskProperty = property.FindPropertyRelative("raw");
48+
var enumDisplayLength = s_enumInfo.displayedMaskLength;
49+
var realMask = (ulong)maskProperty.longValue;
50+
var firstSelected = string.Empty;
51+
for (int i = 0; i < enumDisplayLength; ++i)
52+
{
53+
if (EnumUtils.GetFlag(realMask, s_enumInfo.displayedMaskValues[i]))
54+
{
55+
s_displayedMask.Add(true);
56+
if (string.IsNullOrEmpty(firstSelected)) { firstSelected = s_enumInfo.displayedMaskNames[i]; }
57+
}
58+
else
59+
{
60+
s_displayedMask.Add(false);
61+
}
62+
}
63+
64+
var flagsCount = 0;
65+
for (var i = 0; i < EnumUtils.ULONG_MASK_FIELD_LENGTH; ++i)
66+
{
67+
if (EnumUtils.GetFlag(realMask, i)) { ++flagsCount; }
68+
}
69+
70+
if (EditorGUI.showMixedValue)
71+
{
72+
s_tempContent.text = " - ";
73+
}
74+
else if (flagsCount == 0)
75+
{
76+
s_tempContent.text = "None";
77+
}
78+
else if (flagsCount == 1)
79+
{
80+
s_tempContent.text = firstSelected;
81+
}
82+
else if (flagsCount < enumDisplayLength)
83+
{
84+
s_tempContent.text = "Mixed...";
85+
}
86+
else
87+
{
88+
s_tempContent.text = "All";
89+
}
90+
91+
var controlPos = position;
92+
controlPos.height = EditorGUIUtility.singleLineHeight;
93+
var id = GUIUtility.GetControlID(FocusType.Passive, controlPos);
94+
95+
switch (Event.current.GetTypeForControl(id))
96+
{
97+
case EventType.MouseDown:
98+
if (controlPos.Contains(Event.current.mousePosition))
99+
{
100+
GUIUtility.hotControl = id;
101+
GUIUtility.keyboardControl = id;
102+
Event.current.Use();
103+
}
104+
break;
105+
case EventType.MouseUp:
106+
if (GUIUtility.hotControl == id)
107+
{
108+
GUIUtility.hotControl = 0;
109+
GUIUtility.keyboardControl = 0;
110+
Event.current.Use();
111+
m_foldoutOpen = !m_foldoutOpen;
112+
}
113+
break;
114+
case EventType.Repaint:
115+
s_popup.Draw(position, s_tempContent, id, false);
116+
break;
117+
}
118+
119+
if (m_foldoutOpen)
120+
{
121+
position.y += EditorGUIUtility.singleLineHeight;
122+
123+
var halfWidth = position.width * 0.5f;
124+
if (GUI.Button(new Rect(position.x, position.y, halfWidth - 1, EditorGUIUtility.singleLineHeight), "All"))
125+
{
126+
realMask = ~0ul;
127+
//m_foldoutOpen = false;
128+
}
129+
130+
//Draw the None button
131+
if (GUI.Button(new Rect(position.x + halfWidth + 1, position.y, halfWidth - 1, EditorGUIUtility.singleLineHeight), "None"))
132+
{
133+
realMask = 0ul;
134+
//m_foldoutOpen = false;
135+
}
136+
137+
for (int i = 0; i < enumDisplayLength; ++i)
138+
{
139+
position.y += EditorGUIUtility.singleLineHeight;
140+
var toggled = EditorGUI.ToggleLeft(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), s_enumInfo.displayedMaskNames[i], s_displayedMask[i]);
141+
if (s_displayedMask[i] != toggled)
142+
{
143+
s_displayedMask[i] = toggled;
144+
EnumUtils.SetFlag(ref realMask, s_enumInfo.displayedMaskValues[i], toggled);
145+
//m_foldoutOpen = false;
146+
}
147+
}
148+
149+
maskProperty.longValue = (long)realMask;
150+
}
151+
152+
property.serializedObject.ApplyModifiedProperties();
153+
154+
EditorGUI.EndProperty();
155+
}
156+
}
157+
}

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveInput/Editor/ControllerButtonMaskDrawer.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
2+
3+
using HTC.UnityPlugin.Utility;
4+
using System;
5+
6+
namespace HTC.UnityPlugin.Vive
7+
{
8+
[Serializable]
9+
public struct ControllerButtonMask
10+
{
11+
public ulong raw;
12+
13+
public ControllerButtonMask(ControllerButton button, params ControllerButton[] buttons)
14+
{
15+
raw = GetRawMask(button, buttons);
16+
}
17+
18+
public bool IsSet(ControllerButton button) { return button >= 0 ? (raw & (1ul << (int)button)) > 0ul : false; }
19+
20+
public bool IsAnySet(ControllerButton button, params ControllerButton[] buttons) { var m = GetRawMask(button, buttons); return (raw & m) > 0ul; }
21+
22+
public bool IsAllSet(ControllerButton button, params ControllerButton[] buttons) { var m = GetRawMask(button, buttons); return (raw & m) == m; }
23+
24+
public void Set(ControllerButton button, params ControllerButton[] buttons) { raw |= GetRawMask(button, buttons); }
25+
26+
public void Unset(ControllerButton button, params ControllerButton[] buttons) { raw &= ~GetRawMask(button, buttons); }
27+
28+
public bool GetAnyPress(ulong pressed) { return (pressed & raw) > 0ul; }
29+
30+
public bool GetAllPress(ulong pressed) { return (pressed & raw) == raw; }
31+
32+
public static ulong GetRawMask(ControllerButton button, params ControllerButton[] buttons)
33+
{
34+
var value = button >= 0 ? 1ul << (int)button : 0ul;
35+
if (buttons != null && buttons.Length > 0) { foreach (var b in buttons) { if (b >= 0) { value |= 1ul << (int)b; } } }
36+
return value;
37+
}
38+
39+
public static ControllerButtonMask All { get { return new ControllerButtonMask() { raw = ~0ul }; } }
40+
}
41+
42+
public partial class ViveInput : SingletonBehaviour<ViveInput>
43+
{
44+
public static bool GetAnyPress<TRole>(TRole role, ControllerButtonMask mask, bool usePrevState = false)
45+
{
46+
return mask.GetAnyPress(usePrevState ? GetState(role).PreviousButtonPressed : GetState(role).CurrentButtonPressed);
47+
}
48+
49+
//public static bool GetAnyPressDown<TRole>(TRole role, ControllerButtonMask mask)
50+
//{
51+
// var state = GetState(role);
52+
// return !mask.GetAnyPress(state.PreviousButtonPressed) && mask.GetAnyPress(state.CurrentButtonPressed);
53+
//}
54+
55+
//public static bool GetAnyPressUp<TRole>(TRole role, ControllerButtonMask mask)
56+
//{
57+
// var state = GetState(role);
58+
// return mask.GetAllPress(state.PreviousButtonPressed) && !mask.GetAllPress(state.CurrentButtonPressed);
59+
//}
60+
61+
public static bool GetAllPress<TRole>(TRole role, ControllerButtonMask mask, bool usePrevState = false)
62+
{
63+
return mask.GetAllPress(usePrevState ? GetState(role).PreviousButtonPressed : GetState(role).CurrentButtonPressed);
64+
}
65+
66+
//public static bool GetAllPressDown<TRole>(TRole role, ControllerButtonMask mask)
67+
//{
68+
// var state = GetState(role);
69+
// return !mask.GetAllPress(state.PreviousButtonPressed) && mask.GetAllPress(state.CurrentButtonPressed);
70+
//}
71+
72+
//public static bool GetAllPressUp<TRole>(TRole role, ControllerButtonMask mask)
73+
//{
74+
// var state = GetState(role);
75+
// return mask.GetAnyPress(state.PreviousButtonPressed) && !mask.GetAnyPress(state.CurrentButtonPressed);
76+
//}
77+
78+
public static bool GetAnyPress(ViveRoleProperty role, ControllerButtonMask mask, bool usePrevState = false)
79+
{
80+
return GetAnyPress(role.roleType, role.roleValue, mask, usePrevState);
81+
}
82+
83+
//public static bool GetAnyPressDown(ViveRoleProperty role, ControllerButtonMask mask)
84+
//{
85+
// return GetAnyPressDown(role.roleType, role.roleValue, mask);
86+
//}
87+
88+
//public static bool GetAnyPressUp(ViveRoleProperty role, ControllerButtonMask mask)
89+
//{
90+
// return GetAnyPressUp(role.roleType, role.roleValue, mask);
91+
//}
92+
93+
public static bool GetAllPress(ViveRoleProperty role, ControllerButtonMask mask, bool usePrevState = false)
94+
{
95+
return GetAllPress(role.roleType, role.roleValue, mask, usePrevState);
96+
}
97+
98+
//public static bool GetAllPressDown(ViveRoleProperty role, ControllerButtonMask mask)
99+
//{
100+
// return GetAllPressDown(role.roleType, role.roleValue, mask);
101+
//}
102+
103+
//public static bool GetAllPressUp(ViveRoleProperty role, ControllerButtonMask mask)
104+
//{
105+
// return GetAllPressUp(role.roleType, role.roleValue, mask);
106+
//}
107+
108+
public static bool GetAnyPress(Type roleType, int roleValue, ControllerButtonMask mask, bool usePrevState = false)
109+
{
110+
return mask.GetAnyPress(usePrevState ? GetState(roleType, roleValue).PreviousButtonPressed : GetState(roleType, roleValue).CurrentButtonPressed);
111+
}
112+
113+
//public static bool GetAnyPressDown(Type roleType, int roleValue, ControllerButtonMask mask)
114+
//{
115+
// var state = GetState(roleType, roleValue);
116+
// return !mask.GetAnyPress(state.PreviousButtonPressed) && mask.GetAnyPress(state.CurrentButtonPressed);
117+
//}
118+
119+
//public static bool GetAnyPressUp(Type roleType, int roleValue, ControllerButtonMask mask)
120+
//{
121+
// var state = GetState(roleType, roleValue);
122+
// return mask.GetAllPress(state.PreviousButtonPressed) && !mask.GetAllPress(state.CurrentButtonPressed);
123+
//}
124+
125+
public static bool GetAllPress(Type roleType, int roleValue, ControllerButtonMask mask, bool usePrevState = false)
126+
{
127+
return mask.GetAllPress(usePrevState ? GetState(roleType, roleValue).PreviousButtonPressed : GetState(roleType, roleValue).CurrentButtonPressed);
128+
}
129+
130+
//public static bool GetAllPressDown(Type roleType, int roleValue, ControllerButtonMask mask)
131+
//{
132+
// var state = GetState(roleType, roleValue);
133+
// return !mask.GetAllPress(state.PreviousButtonPressed) && mask.GetAllPress(state.CurrentButtonPressed);
134+
//}
135+
136+
//public static bool GetAllPressUp(Type roleType, int roleValue, ControllerButtonMask mask)
137+
//{
138+
// var state = GetState(roleType, roleValue);
139+
// return mask.GetAnyPress(state.PreviousButtonPressed) && !mask.GetAnyPress(state.CurrentButtonPressed);
140+
//}
141+
}
142+
}

Assets/HTC.UnityPlugin/ViveInputUtility/Scripts/ViveInput/ViveInputButtonMask.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)