-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathEJBindingGamepad.m
More file actions
223 lines (184 loc) · 7.41 KB
/
EJBindingGamepad.m
File metadata and controls
223 lines (184 loc) · 7.41 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#import "EJBindingGamepad.h"
#import "EJBindingGamepadButton.h"
@implementation EJBindingGamepad
@synthesize controller;
- (id)initWithController:(GCController *)controllerp atIndex:(NSUInteger)indexp {
if( self = [super initWithContext:NULL argc:0 argv:NULL] ) {
controller = [controllerp retain];
index = indexp;
connected = YES;
allowsRotation = YES;
controller.playerIndex = index;
}
return self;
}
- (void)createWithJSObject:(JSObjectRef)obj scriptView:(EJJavaScriptView *)view {
[super createWithJSObject:obj scriptView:view];
// We have to hold on to the JS Object here as long as the gamepad is connectd
// and can be reached through navigator.getGamepads(). It will be unprotected
// in (void)disconnet.
JSValueProtect(scriptView.jsGlobalContext, obj);
// Initialize Axes - JS Array with Numbers
if( controller.extendedGamepad ) {
int numAxes = 4;
JSValueRef axesValues[numAxes];
for( int i = 0; i < numAxes; i++ ) {
axesValues[i] = JSValueMakeNumber(view.jsGlobalContext, 0);
}
jsAxes = JSObjectMakeArray(view.jsGlobalContext, numAxes, axesValues, NULL);
}
else {
jsAxes = JSObjectMakeArray(view.jsGlobalContext, 0, NULL, NULL);
}
JSValueProtect(view.jsGlobalContext, jsAxes);
// Initialize Buttons - JS Array with EJBindingGamepad instances. The W3C standard defines
// 17 buttons. Not all are supported by iOS. However, W3C dictates that Gamepad buttons
// should be mapped to the standard layout if possible, so we still provide dummies, so
// the button indices are not mingled.
// See: http://www.w3.org/TR/gamepad/#remapping
// Buttons unsupported by iOS: Select:8, Start:9, LeftStick:10, RightStick:11
NSObject<EJControllerButtonInputProtocol> *mapping[kEJGamepadNumButtons] = {NULL};
if( controller.extendedGamepad ) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
mapping[kEJGamepadButtonA] = gamepad.buttonA;
mapping[kEJGamepadButtonB] = gamepad.buttonB;
mapping[kEJGamepadButtonX] = gamepad.buttonX;
mapping[kEJGamepadButtonY] = gamepad.buttonY;
mapping[kEJGamepadButtonL1] = gamepad.leftShoulder;
mapping[kEJGamepadButtonR1] = gamepad.rightShoulder;
mapping[kEJGamepadButtonL2] = gamepad.leftTrigger;
mapping[kEJGamepadButtonR2] = gamepad.rightTrigger;
mapping[kEJGamepadButtonUp] = gamepad.dpad.up;
mapping[kEJGamepadButtonDown] = gamepad.dpad.down;
mapping[kEJGamepadButtonLeft] = gamepad.dpad.left;
mapping[kEJGamepadButtonRight] = gamepad.dpad.right;
}
else if( controller.gamepad ) {
GCGamepad *gamepad = controller.gamepad;
mapping[kEJGamepadButtonA] = gamepad.buttonA;
mapping[kEJGamepadButtonB] = gamepad.buttonB;
mapping[kEJGamepadButtonX] = gamepad.buttonX;
mapping[kEJGamepadButtonY] = gamepad.buttonY;
mapping[kEJGamepadButtonL1] = gamepad.leftShoulder;
mapping[kEJGamepadButtonR1] = gamepad.rightShoulder;
mapping[kEJGamepadButtonUp] = gamepad.dpad.up;
mapping[kEJGamepadButtonDown] = gamepad.dpad.down;
mapping[kEJGamepadButtonLeft] = gamepad.dpad.left;
mapping[kEJGamepadButtonRight] = gamepad.dpad.right;
}
#if TARGET_OS_TV
else if( controller.microGamepad ) {
GCMicroGamepad *gamepad = controller.microGamepad;
gamepad.reportsAbsoluteDpadValues = YES;
gamepad.allowsRotation = allowsRotation;
mapping[kEJGamepadButtonA] = gamepad.buttonA;
mapping[kEJGamepadButtonX] = gamepad.buttonX;
mapping[kEJGamepadButtonUp] = gamepad.dpad.up;
mapping[kEJGamepadButtonDown] = gamepad.dpad.down;
mapping[kEJGamepadButtonLeft] = gamepad.dpad.left;
mapping[kEJGamepadButtonRight] = gamepad.dpad.right;
}
#endif
// Because Apple likes to fuck with us, the "Pause" button (otherwise known as "Home" or
// "Menu") is not available as GCControllerButtonInput, but rather only reachable through
// the "controllerPausedHandler" callback on the controller. So we provide our own
// ButtonInput instance that conforms to the same protocol as GCControllerButtonInput.
mapping[kEJGamepadButtonHome] = [[EJControllerButtonInputHome alloc] initWithController:controller];
JSValueRef buttonsValues[kEJGamepadNumButtons];
for( int i = 0; i < kEJGamepadNumButtons; i++ ) {
NSObject<EJControllerButtonInputProtocol> *button = mapping[i];
// button may be NULL, but EJBindingGamepadButton doesn't care
EJBindingGamepadButton *binding = [[EJBindingGamepadButton alloc] initWithButton:button];
buttonsValues[i] = [EJBindingGamepadButton
createJSObjectWithContext:scriptView.jsGlobalContext
scriptView:scriptView
instance:binding];
[binding release];
}
jsButtons = JSObjectMakeArray(view.jsGlobalContext, kEJGamepadNumButtons, buttonsValues, NULL);
JSValueProtect(view.jsGlobalContext, jsButtons);
[mapping[kEJGamepadButtonHome] release];
}
- (void)disconnect {
connected = NO;
JSValueUnprotectSafe(scriptView.jsGlobalContext, jsObject);
}
- (void)dealloc {
JSValueUnprotectSafe(scriptView.jsGlobalContext, jsAxes);
JSValueUnprotectSafe(scriptView.jsGlobalContext, jsButtons);
[controller release];
[super dealloc];
}
- (JSObjectRef)jsObject {
return jsObject;
}
EJ_BIND_GET(id, ctx) {
return NSStringToJSValue(ctx, controller.vendorName);
}
EJ_BIND_GET(profile, ctx) {
if( controller.extendedGamepad ) {
return NSStringToJSValue(ctx, @"extendedGamepad");
}
#if TARGET_OS_TV
else if( controller.microGamepad ) {
return NSStringToJSValue(ctx, @"microGamepad");
}
#endif
else {
return NSStringToJSValue(ctx, @"gamepad");
}
}
EJ_BIND_GET(index, ctx) {
return JSValueMakeNumber(ctx, index);
}
EJ_BIND_GET(connected, ctx) {
return JSValueMakeBoolean(ctx, connected);
}
EJ_BIND_GET(timestamp, ctx) {
double time = NSDate.timeIntervalSinceReferenceDate - scriptView.startTime;
return JSValueMakeNumber(ctx, time * 1000.0);
}
EJ_BIND_GET(mapping, ctx) {
return NSStringToJSValue(ctx, @"standard");
}
EJ_BIND_GET(axes, ctx) {
if( controller.extendedGamepad ) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
GCControllerDirectionPad *leftStick = gamepad.leftThumbstick;
GCControllerDirectionPad *rightStick = gamepad.rightThumbstick;
// Note: Y-Axis is inverted. iOS says positive Y means up, W3C says down.
JSObjectSetPropertyAtIndex(ctx, jsAxes, 0, JSValueMakeNumber(ctx, leftStick.xAxis.value), NULL);
JSObjectSetPropertyAtIndex(ctx, jsAxes, 1, JSValueMakeNumber(ctx, -leftStick.yAxis.value), NULL);
JSObjectSetPropertyAtIndex(ctx, jsAxes, 2, JSValueMakeNumber(ctx, rightStick.xAxis.value), NULL);
JSObjectSetPropertyAtIndex(ctx, jsAxes, 3, JSValueMakeNumber(ctx, -rightStick.yAxis.value), NULL);
}
#if TARGET_OS_TV
// Provide the Remote's touch pad a axis in addition to the Up/Down/Left/Right buttons
else if( controller.microGamepad ) {
GCMicroGamepad *gamepad = controller.microGamepad;
JSObjectSetPropertyAtIndex(ctx, jsAxes, 0, JSValueMakeNumber(ctx, gamepad.dpad.xAxis.value), NULL);
JSObjectSetPropertyAtIndex(ctx, jsAxes, 1, JSValueMakeNumber(ctx, -gamepad.dpad.yAxis.value), NULL);
}
#endif
return jsAxes;
}
EJ_BIND_GET(buttons, ctx) {
return jsButtons;
}
EJ_BIND_GET(exitOnMenuPress, ctx) {
return JSValueMakeBoolean(ctx, scriptView.exitOnMenuPress);
}
EJ_BIND_SET(exitOnMenuPress, ctx, value) {
scriptView.exitOnMenuPress = JSValueToBoolean(ctx, value);
}
EJ_BIND_GET(allowsRotation, ctx) {
return JSValueMakeBoolean(ctx, allowsRotation);
}
EJ_BIND_SET(allowsRotation, ctx, value) {
if( controller.microGamepad ) {
allowsRotation = JSValueToBoolean(ctx, value);
GCMicroGamepad *gamepad = controller.microGamepad;
gamepad.allowsRotation = allowsRotation;
}
}
@end