forked from MichaeliusAChapelo/VR-Annotation-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputMapping.cs
More file actions
108 lines (95 loc) · 3.87 KB
/
Copy pathInputMapping.cs
File metadata and controls
108 lines (95 loc) · 3.87 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Maps an button to a specific input command.
/// </summary>
public static class InputMapping
{
public static bool IsRightHanded = true;
/// <summary>
/// Private holder for whether a button was pressed.
/// </summary>
private class ButtonPress
{
// Internal fields
internal readonly OVRInput.RawButton Button;
internal bool ThisFrame, PreviousFrame;
// Internal constructor
internal ButtonPress(OVRInput.RawButton button)
{
this.Button = button;
ThisFrame = PreviousFrame = false;
}
}
/// <summary>
/// List of user-mapped input.
/// </summary>
public enum AnnotationInput
{
ExportAndClear,
SizeUp,
SizeDown,
Annotate,
DestroyAnnotation,
ResetTransform,
SwitchDominantHand,
//StartMenu
}
/// <summary>
/// Dictionary that maps input to buttons presses.
/// </summary>
private static readonly Dictionary<AnnotationInput, ButtonPress> ButtonMappings = new Dictionary<AnnotationInput, ButtonPress>()
{
{AnnotationInput.SizeUp, new ButtonPress(OVRInput.RawButton.Y)},
{AnnotationInput.SizeDown, new ButtonPress(OVRInput.RawButton.X)},
{AnnotationInput.DestroyAnnotation, new ButtonPress(OVRInput.RawButton.B)},
{AnnotationInput.ResetTransform, new ButtonPress(OVRInput.RawButton.A)},
{AnnotationInput.ExportAndClear, new ButtonPress(OVRInput.RawButton.Start)}, // could bring up a OVR menu with the same toggle
{AnnotationInput.Annotate, new ButtonPress(OVRInput.RawButton.RIndexTrigger) },
{AnnotationInput.SwitchDominantHand, new ButtonPress(OVRInput.RawButton.LIndexTrigger) },
};
/// <summary>
/// Swaps two input mappings.
/// </summary>
private static void SwapButtons(AnnotationInput a, AnnotationInput b)
{
ButtonPress aButtonPress = ButtonMappings[a];
ButtonMappings[a] = ButtonMappings[b];
ButtonMappings[b] = aButtonPress;
}
/// <summary>
/// Switches dominant hand.
/// </summary>
public static void SwitchDominantHand()
{
IsRightHanded = !IsRightHanded;
SwapButtons(AnnotationInput.SwitchDominantHand, AnnotationInput.Annotate);
//SwapButtons(AnnotationInput.SizeUp, AnnotationInput.DestroyAnnotation);
//SwapButtons(AnnotationInput.SizeDown, AnnotationInput.ResetTransform);
}
/// <summary>
/// Returns true if the given button corresponding to the mapped input was pressed this frame.
/// </summary>
/// <param name="input">Mapped input.</param>
/// <returns></returns>
public static bool PressedThisFrame(AnnotationInput input)
{
ButtonPress bp = ButtonMappings[input];
return !bp.PreviousFrame && bp.ThisFrame;
}
/// <summary>
/// Returns true if the given button corresponding to the mapped input is held this frame.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool HeldThisFrame(AnnotationInput input) { return ButtonMappings[input].ThisFrame; }
/// <summary>
/// Updates all the button reads this frame. Use in the beginning of an update function.
/// </summary>
public static void UpdateThisFrame() { foreach (ButtonPress b in ButtonMappings.Values) b.ThisFrame = OVRInput.Get(b.Button); }
/// <summary>
/// Updates all previous button reads from frame, readying for next frame. Use at the end of an update function.
/// </summary>
public static void UpdatePreviousFrame() { foreach (ButtonPress b in ButtonMappings.Values) b.PreviousFrame = b.ThisFrame; }
}