Skip to content

Commit f425761

Browse files
committed
initial commit
1 parent f10158c commit f425761

14 files changed

+8928
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vs/
2+
obj/
3+
bin/

OpenVR.NET.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30907.101
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenVR.NET", "OpenVR.NET\OpenVR.NET.csproj", "{D4884028-3692-4714-AE17-0743F7A03628}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D4884028-3692-4714-AE17-0743F7A03628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D4884028-3692-4714-AE17-0743F7A03628}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D4884028-3692-4714-AE17-0743F7A03628}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D4884028-3692-4714-AE17-0743F7A03628}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {32023769-617E-4ABE-955A-29B9CEB22F56}
24+
EndGlobalSection
25+
EndGlobal

OpenVR.NET/Events.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace OpenVR.NET {
4+
public static class Events {
5+
public static void Log ( string messgae ) { }
6+
public static void Message ( string messgae ) { }
7+
public static void Warning ( string messgae ) { }
8+
public static void Error ( string messgae ) { }
9+
public static void Exception ( Exception e, string messgae ) { }
10+
}
11+
}

OpenVR.NET/Extensions.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Numerics;
3+
using Valve.VR;
4+
5+
namespace OpenVR.NET {
6+
public static class Extensions {
7+
public static Vector3 ExtractPosition ( this HmdMatrix34_t mat ) {
8+
return new Vector3( mat.m3, mat.m7, -mat.m11 );
9+
}
10+
11+
public static Quaternion ExtractRotation ( this HmdMatrix34_t mat ) {
12+
static float CopySign ( float a, float b ) {
13+
if ( MathF.Sign( a ) != MathF.Sign( b ) )
14+
return -a;
15+
else return a;
16+
}
17+
18+
Quaternion q = default;
19+
q.W = -MathF.Sqrt( MathF.Max( 0, 1 + mat.m0 + mat.m5 + mat.m10 ) ) / 2;
20+
q.X = MathF.Sqrt( MathF.Max( 0, 1 + mat.m0 - mat.m5 - mat.m10 ) ) / 2;
21+
q.Y = MathF.Sqrt( MathF.Max( 0, 1 - mat.m0 + mat.m5 - mat.m10 ) ) / 2;
22+
q.Z = MathF.Sqrt( MathF.Max( 0, 1 - mat.m0 - mat.m5 + mat.m10 ) ) / 2;
23+
var l = q.Length();
24+
q.X = CopySign( q.X, mat.m9 - mat.m6 ) / l;
25+
q.Y = CopySign( q.Y, mat.m8 - mat.m2 ) / l;
26+
q.Z = CopySign( q.Z, mat.m4 - mat.m1 ) / l;
27+
28+
return q;
29+
}
30+
}
31+
}

OpenVR.NET/Manifests/Action.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace OpenVR.NET.Manifests {
5+
/// <summary>
6+
/// A controller binding. Contains what type of input it uses and what enum name it has.
7+
/// </summary>
8+
public class Action<T> : Action where T : Enum {
9+
public T Name { get; set; }
10+
public override string GetReadableName () => Name.ToString();
11+
public override ControllerComponent CreateComponent ( ulong handle ) {
12+
switch ( Type ) {
13+
case ActionType.Boolean:
14+
return new ControllerButton { Handle = handle, Name = Name };
15+
case ActionType.Vector1:
16+
return null;
17+
case ActionType.Vector2:
18+
return null;
19+
case ActionType.Vector3:
20+
return null;
21+
case ActionType.Vibration:
22+
return null;
23+
case ActionType.Skeleton:
24+
return null;
25+
case ActionType.Pose:
26+
return null;
27+
default:
28+
throw new InvalidOperationException( $"No controller component exists for {Type}" );
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Base type of <see cref="Action{T}"/> as Enums are not covariant. Don't use this.
35+
/// </summary>
36+
public abstract class Action {
37+
public abstract string GetReadableName ();
38+
public ActionType Type { get; set; }
39+
public Requirement Requirement { get; set; }
40+
/// <summary>
41+
/// Locale names. The key should follow http://www.lingoes.net/en/translator/langcode.htm (but lowercase snake case).
42+
/// </summary>
43+
public Dictionary<string, string> Localizations = new();
44+
45+
/// <summary>
46+
/// The full action name. This is usually set by <see cref="VrManager"/>
47+
/// </summary>
48+
public string FullPath { get; set; }
49+
50+
public abstract ControllerComponent CreateComponent ( ulong handle );
51+
}
52+
}

OpenVR.NET/Manifests/ActionGroup.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace OpenVR.NET.Manifests {
5+
/// <summary>
6+
/// A group of <see cref="Action{TAction}"/>. Used to set the <see cref="ActionGroupType"/> of child <see cref="Action{T}"/>
7+
/// </summary>
8+
public class ActionGroup<TGroup, TAction> : ActionGroup where TAction : Enum where TGroup : Enum {
9+
public TGroup Name { get; set; }
10+
public List<Action<TAction>> Actions = new();
11+
12+
public override string GetReadableName ()
13+
=> Name.ToString();
14+
15+
public override IEnumerable<Action> EnumerateActions ()
16+
=> Actions;
17+
}
18+
19+
/// <summary>
20+
/// Base type of <see cref="ActionGroup{TGroup, TAction}"/> as Enums are not covariant. Don't use this.
21+
/// </summary>
22+
public abstract class ActionGroup {
23+
public ActionGroupType Type { get; set; }
24+
public abstract string GetReadableName ();
25+
/// <summary>
26+
/// The full group name. This is usually set by <see cref="VrManager"/>
27+
/// </summary>
28+
public string FullPath;
29+
public abstract IEnumerable<Action> EnumerateActions ();
30+
/// <summary>
31+
/// Locale names. The key should follow http://www.lingoes.net/en/translator/langcode.htm (but lowercase snake case).
32+
/// </summary>
33+
public Dictionary<string, string> Localizations = new();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Valve.VR;
4+
5+
namespace OpenVR.NET.Manifests {
6+
/// <summary>
7+
/// Represents an input or output of a controller
8+
/// </summary>
9+
public abstract class ControllerComponent {
10+
/// <summary>
11+
/// A unique name for this component, usually an enum
12+
/// </summary>
13+
public object Name { get; init; }
14+
public ulong Handle { get; init; }
15+
16+
public abstract void Update ();
17+
}
18+
19+
public abstract class ControllerComponent<T> : ControllerComponent where T : IEquatable<T> {
20+
private T value;
21+
public T Value {
22+
get => value;
23+
protected set {
24+
if ( value.Equals( this.value ) ) return;
25+
this.value = value;
26+
ValueChanged?.Invoke( value );
27+
}
28+
}
29+
30+
public event System.Action<T> ValueChanged;
31+
public void BindValueChanged ( System.Action<T> action, bool runOnceImmediately = false ) {
32+
ValueChanged += action;
33+
if ( runOnceImmediately ) action( value );
34+
}
35+
}
36+
37+
// TODO leftright actions can be restricted to a given side
38+
public class ControllerButton : ControllerComponent<bool> {
39+
public override void Update () {
40+
InputDigitalActionData_t data = default;
41+
var error = Valve.VR.OpenVR.Input.GetDigitalActionData( Handle, ref data, (uint)Marshal.SizeOf<InputDigitalActionData_t>(), Valve.VR.OpenVR.k_ulInvalidActionHandle );
42+
if ( error != EVRInputError.None ) {
43+
Events.Error( $"Cannot read input: {error}" );
44+
return;
45+
}
46+
47+
Value = data.bState;
48+
}
49+
}
50+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace OpenVR.NET.Manifests {
2+
public class DefaultBinding {
3+
public string ControllerType;
4+
public string Path;
5+
}
6+
}

OpenVR.NET/Manifests/Enums.cs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace OpenVR.NET.Manifests {
2+
public enum Requirement {
3+
/// <summary>
4+
/// The user will be warned if this is not bound
5+
/// </summary>
6+
Suggested,
7+
/// <summary>
8+
/// The bindings cannot be saved witohut this bound
9+
/// </summary>
10+
Mandatory,
11+
/// <summary>
12+
/// No warning are produced if this is not bound
13+
/// </summary>
14+
Optional
15+
}
16+
17+
public enum ActionType {
18+
/// <summary>
19+
/// Boolean actions are used for triggers and buttons
20+
/// </summary>
21+
Boolean,
22+
/// <summary>
23+
/// Vecotr1 actions are read from triggers, 1D trackpads and joysticks
24+
/// </summary>
25+
Vector1,
26+
/// <summary>
27+
/// Vector2 actions are read from trackpads and joysticks
28+
/// </summary>
29+
Vector2,
30+
Vector3,
31+
/// <summary>
32+
/// Vibration is used to activate haptics
33+
/// </summary>
34+
Vibration,
35+
Pose,
36+
Skeleton
37+
}
38+
39+
public enum ActionGroupType {
40+
/// <summary>
41+
/// These actions can be bound on each controller separately
42+
/// </summary>
43+
LeftRight,
44+
/// <summary>
45+
/// These actions can be bound only on one controller
46+
/// </summary>
47+
Single,
48+
/// <summary>
49+
/// These actions are hidden
50+
/// </summary>
51+
Hidden
52+
}
53+
}

OpenVR.NET/Manifests/Manifest.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace OpenVR.NET.Manifests {
5+
/// <summary>
6+
/// A manifest of how controller buttons and such should be renamed into enum values.
7+
/// </summary>
8+
public class Manifest<TGroup, TAction> : Manifest where TAction : Enum where TGroup : Enum {
9+
public List<ActionGroup<TGroup, TAction>> Groups = new();
10+
11+
public override IEnumerable<ActionGroup> EnumerateGroups ()
12+
=> Groups;
13+
}
14+
15+
/// <summary>
16+
/// Base type of <see cref="Manifest{TGroup, TAction}"/> as Enums are not covariant. Don't use this.
17+
/// </summary>
18+
public abstract class Manifest {
19+
public abstract IEnumerable<ActionGroup> EnumerateGroups ();
20+
public List<DefaultBinding> DefaultBindings = new();
21+
/// <summary>
22+
/// A unique name to identify the program. Should be in the style of myCompany.myProjectName to avoid collisions
23+
/// </summary>
24+
public string Name;
25+
public LaunchType LaunchType;
26+
/// <summary>
27+
/// The path to the binary. If not set, will be determined automatically based on the entry assembly location.
28+
/// </summary>
29+
public string Path;
30+
public bool IsDashBoardOverlay;
31+
// ActionsManifestPath is autogenerated
32+
/// <summary>
33+
/// Locale names. The key should follow http://www.lingoes.net/en/translator/langcode.htm (but lowercase snake case).
34+
/// </summary>
35+
public List<ManifestLocale> Localizations = new();
36+
}
37+
38+
public enum LaunchType {
39+
Binary
40+
}
41+
42+
public struct ManifestLocale {
43+
public string LanguageTag;
44+
45+
public ManifestLocale ( string languageTag ) : this() {
46+
LanguageTag = languageTag;
47+
}
48+
49+
public string Name;
50+
public string Description;
51+
}
52+
}

0 commit comments

Comments
 (0)