5
5
6
6
namespace Unity . RenderStreaming
7
7
{
8
- enum KeyEventType
8
+ enum KeyboardEventType
9
9
{
10
- KeyDown = 0 ,
11
- KeyUp ,
12
- KeyPress
10
+ KeyUp = 0 ,
11
+ KeyDown = 1 ,
13
12
}
14
13
enum EventType
15
14
{
16
15
Keyboard = 0 ,
17
- MouseDown = 2 ,
18
- MouseMove = 4 ,
19
- MouseWheel = 5 ,
20
- TouchMove = 6
16
+ Mouse = 1 ,
17
+ MouseWheel = 2 ,
18
+ Touch = 3
21
19
}
22
20
23
21
public static class RemoteInput
24
22
{
25
- static Keyboard keyboard ;
26
- static Mouse mouse ;
27
- static Touchscreen touch ;
23
+ public static Keyboard Keyboard { get ; private set ; }
24
+ public static Mouse Mouse { get ; private set ; }
25
+ public static Touchscreen Touch { get ; private set ; }
28
26
29
27
static TDevice GetOrAddDevice < TDevice > ( ) where TDevice : InputDevice
30
28
{
@@ -36,59 +34,98 @@ static TDevice GetOrAddDevice<TDevice>() where TDevice : InputDevice
36
34
return InputSystem . AddDevice < TDevice > ( ) ;
37
35
}
38
36
39
- static RemoteInput ( )
37
+ public static void Initialize ( )
40
38
{
41
- keyboard = GetOrAddDevice < Keyboard > ( ) ;
42
- mouse = GetOrAddDevice < Mouse > ( ) ;
43
- touch = GetOrAddDevice < Touchscreen > ( ) ;
39
+ Keyboard = GetOrAddDevice < Keyboard > ( ) ;
40
+ Mouse = GetOrAddDevice < Mouse > ( ) ;
41
+ Touch = GetOrAddDevice < Touchscreen > ( ) ;
44
42
}
45
43
46
44
public static void ProcessInput ( byte [ ] bytes )
47
45
{
48
46
switch ( ( EventType ) bytes [ 0 ] )
49
47
{
50
48
case EventType . Keyboard :
51
- ProcessKeyEvent ( ( char ) bytes [ 1 ] ) ;
49
+ var type = ( KeyboardEventType ) bytes [ 1 ] ;
50
+ var repeat = bytes [ 2 ] == 1 ;
51
+ var key = ( char ) bytes [ 3 ] ;
52
+ ProcessKeyEvent ( type , repeat , key ) ;
52
53
break ;
53
- case EventType . MouseDown :
54
- ProcessMouseDownEvent ( bytes [ 1 ] ) ;
55
- break ;
56
- case EventType . MouseMove :
54
+ case EventType . Mouse :
57
55
var deltaX = BitConverter . ToInt16 ( bytes , 1 ) ;
58
56
var deltaY = BitConverter . ToInt16 ( bytes , 3 ) ;
59
- ProcessMouseMoveEvent ( deltaX , deltaY , bytes [ 5 ] ) ;
57
+ var button = bytes [ 5 ] ;
58
+ ProcessMouseMoveEvent ( deltaX , deltaY , button ) ;
60
59
break ;
61
- case EventType . TouchMove :
62
- var pageX = BitConverter . ToInt16 ( bytes , 1 ) ;
63
- var pageY = BitConverter . ToInt16 ( bytes , 3 ) ;
64
- ProcessTouchMoveEvent ( pageX , pageY ) ;
60
+ case EventType . MouseWheel :
61
+ var scrollX = BitConverter . ToSingle ( bytes , 1 ) ;
62
+ var scrollY = BitConverter . ToSingle ( bytes , 5 ) ;
63
+ ProcessMouseWheelEvent ( scrollX , scrollY ) ;
64
+ break ;
65
+ case EventType . Touch :
66
+ var phase = ( PointerPhase ) bytes [ 1 ] ;
67
+ var length = bytes [ 2 ] ;
68
+ var index = 3 ;
69
+ for ( int i = 0 ; i < length ; i ++ )
70
+ {
71
+ var pageX = BitConverter . ToInt16 ( bytes , index ) ;
72
+ var pageY = BitConverter . ToInt16 ( bytes , index + 2 ) ;
73
+ var force = BitConverter . ToSingle ( bytes , index + 4 ) ;
74
+ ProcessTouchMoveEvent ( i , phase , pageX , pageY , force ) ;
75
+ index += 8 ;
76
+ }
65
77
break ;
66
-
67
78
}
68
79
}
69
80
70
- static void ProcessKeyEvent ( char keyCode )
81
+ public static void Reset ( )
71
82
{
72
- InputSystem . QueueStateEvent ( keyboard , new KeyboardState ( ( Key ) keyCode ) ) ;
73
- InputSystem . QueueTextEvent ( keyboard , keyCode ) ;
83
+ InputSystem . QueueStateEvent ( Mouse , new MouseState ( ) ) ;
84
+ InputSystem . QueueStateEvent ( Keyboard , new KeyboardState ( ) ) ;
85
+ InputSystem . QueueStateEvent ( Touch , new TouchState ( ) ) ;
86
+ InputSystem . Update ( ) ;
87
+ }
88
+
89
+ static void ProcessKeyEvent ( KeyboardEventType state , bool repeat , char keyCode )
90
+ {
91
+ switch ( state )
92
+ {
93
+ case KeyboardEventType . KeyDown :
94
+ if ( ! repeat )
95
+ {
96
+ InputSystem . QueueStateEvent ( Keyboard , new KeyboardState ( ( Key ) keyCode ) ) ;
97
+ }
98
+ InputSystem . QueueTextEvent ( Keyboard , keyCode ) ;
99
+ break ;
100
+ case KeyboardEventType . KeyUp :
101
+ InputSystem . QueueStateEvent ( Keyboard , new KeyboardState ( ) ) ;
102
+ break ;
103
+ }
74
104
InputSystem . Update ( ) ;
75
105
}
76
106
77
107
static void ProcessMouseMoveEvent ( short deltaX , short deltaY , byte button )
78
108
{
79
- InputSystem . QueueStateEvent ( mouse , new MouseState { delta = new Vector2Int ( deltaX , deltaY ) , buttons = button } ) ;
109
+ InputSystem . QueueStateEvent ( Mouse , new MouseState { delta = new Vector2Int ( deltaX , deltaY ) , buttons = button } ) ;
80
110
InputSystem . Update ( ) ;
81
111
}
82
112
83
- static void ProcessMouseDownEvent ( byte button )
113
+ static void ProcessMouseWheelEvent ( float scrollX , float scrollY )
84
114
{
85
- InputSystem . QueueStateEvent ( mouse , new MouseState { buttons = button } ) ;
115
+ InputSystem . QueueStateEvent ( Mouse , new MouseState { scroll = new Vector2 ( scrollX , scrollY ) } ) ;
86
116
InputSystem . Update ( ) ;
87
117
}
88
118
89
- static void ProcessTouchMoveEvent ( short pageX , short pageY )
119
+ static void ProcessTouchMoveEvent ( int index , PointerPhase phase , short pageX , short pageY , float force )
90
120
{
91
- InputSystem . QueueStateEvent ( touch , new TouchState { position = new Vector2Int ( pageX , pageY ) } ) ;
121
+ InputSystem . QueueDeltaStateEvent ( Touch . allTouchControls [ index ] ,
122
+ new TouchState
123
+ {
124
+ touchId = index ,
125
+ phase = phase ,
126
+ position = new Vector2Int ( pageX , pageY ) ,
127
+ pressure = force
128
+ } ) ;
92
129
InputSystem . Update ( ) ;
93
130
}
94
131
}
0 commit comments