using
UnityEngine;
using
Valve.VR;
public
struct
ClickedEventArgs
{
public
uint
controllerIndex;
public
uint
flags;
public
float
padX, padY;
}
public
delegate
void
ClickedEventHandler(
object
sender, ClickedEventArgs e);
public
class
SteamVR_TrackedController : MonoBehaviour
{
public
uint
controllerIndex;
public
VRControllerState_t controllerState;
public
bool
triggerPressed =
false
;
public
bool
steamPressed =
false
;
public
bool
menuPressed =
false
;
public
bool
padPressed =
false
;
public
bool
padTouched =
false
;
public
bool
gripped =
false
;
public
event
ClickedEventHandler MenuButtonClicked;
public
event
ClickedEventHandler MenuButtonUnclicked;
public
event
ClickedEventHandler TriggerClicked;
public
event
ClickedEventHandler TriggerUnclicked;
public
event
ClickedEventHandler SteamClicked;
public
event
ClickedEventHandler PadClicked;
public
event
ClickedEventHandler PadUnclicked;
public
event
ClickedEventHandler PadTouched;
public
event
ClickedEventHandler PadUntouched;
public
event
ClickedEventHandler Gripped;
public
event
ClickedEventHandler Ungripped;
void
Start()
{
if
(
this
.GetComponent() ==
null
)
{
gameObject.AddComponent();
}
this
.GetComponent().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
if
(
this
.GetComponent() !=
null
)
{
this
.GetComponent().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
}
}
///
/// 引发扳机按下事件
///
public
virtual
void
OnTriggerClicked(ClickedEventArgs e)
{
if
(TriggerClicked !=
null
)
TriggerClicked(
this
, e);
}
///
/// 引发扳机松开事件
///
public
virtual
void
OnTriggerUnclicked(ClickedEventArgs e)
{
if
(TriggerUnclicked !=
null
)
TriggerUnclicked(
this
, e);
}
///
/// 引发菜单点击事件
///
public
virtual
void
OnMenuClicked(ClickedEventArgs e)
{
if
(MenuButtonClicked !=
null
)
MenuButtonClicked(
this
, e);
}
///
/// 引发菜单松开事件
///
public
virtual
void
OnMenuUnclicked(ClickedEventArgs e)
{
if
(MenuButtonUnclicked !=
null
)
MenuButtonUnclicked(
this
, e);
}
///
/// 引发系统按钮点击事件
///
public
virtual
void
OnSteamClicked(ClickedEventArgs e)
{
if
(SteamClicked !=
null
)
SteamClicked(
this
, e);
}
///
/// 引发触摸板点击事件
///
public
virtual
void
OnPadClicked(ClickedEventArgs e)
{
if
(PadClicked !=
null
)
PadClicked(
this
, e);
}
///
/// 引发触摸板未点击事件
///
public
virtual
void
OnPadUnclicked(ClickedEventArgs e)
{
if
(PadUnclicked !=
null
)
PadUnclicked(
this
, e);
}
///
/// 引发触摸板触摸事件
///
public
virtual
void
OnPadTouched(ClickedEventArgs e)
{
if
(PadTouched !=
null
)
PadTouched(
this
, e);
}
///
/// 引发触摸板没有触摸事件
///
public
virtual
void
OnPadUntouched(ClickedEventArgs e)
{
if
(PadUntouched !=
null
)
PadUntouched(
this
, e);
}
///
/// 引发握紧事件
///
public
virtual
void
OnGripped(ClickedEventArgs e)
{
if
(Gripped !=
null
)
Gripped(
this
, e);
}
///
/// 引发未握紧事件
///
public
virtual
void
OnUngripped(ClickedEventArgs e)
{
if
(Ungripped !=
null
)
Ungripped(
this
, e);
}
void
Update()
{
var system = OpenVR.System;
if
(system !=
null
&& system.GetControllerState(controllerIndex,
ref
controllerState))
{
ulong
trigger = controllerState.ulButtonPressed & (1UL << ((
int
)EVRButtonId.k_EButton_SteamVR_Trigger));
if
(trigger > 0L && !triggerPressed)
{
triggerPressed =
true
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnTriggerClicked(e);
}
else
if
(trigger == 0L && triggerPressed)
{
triggerPressed =
false
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnTriggerUnclicked(e);
}
ulong
grip = controllerState.ulButtonPressed & (1UL << ((
int
)EVRButtonId.k_EButton_Grip));
if
(grip > 0L && !gripped)
{
gripped =
true
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnGripped(e);
}
else
if
(grip == 0L && gripped)
{
gripped =
false
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnUngripped(e);
}
ulong
pad = controllerState.ulButtonPressed & (1UL << ((
int
)EVRButtonId.k_EButton_SteamVR_Touchpad));
if
(pad > 0L && !padPressed)
{
padPressed =
true
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnPadClicked(e);
}
else
if
(pad == 0L && padPressed)
{
padPressed =
false
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnPadUnclicked(e);
}
ulong
menu = controllerState.ulButtonPressed & (1UL << ((
int
)EVRButtonId.k_EButton_ApplicationMenu));
if
(menu > 0L && !menuPressed)
{
menuPressed =
true
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnMenuClicked(e);
}
else
if
(menu == 0L && menuPressed)
{
menuPressed =
false
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnMenuUnclicked(e);
}
pad = controllerState.ulButtonTouched & (1UL << ((
int
)EVRButtonId.k_EButton_SteamVR_Touchpad));
if
(pad > 0L && !padTouched)
{
padTouched =
true
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnPadTouched(e);
}
else
if
(pad == 0L && padTouched)
{
padTouched =
false
;
ClickedEventArgs e;
e.controllerIndex = controllerIndex;
e.flags = (
uint
)controllerState.ulButtonPressed;
e.padX = controllerState.rAxis0.x;
e.padY = controllerState.rAxis0.y;
OnPadUntouched(e);
}
}
}
}