Clean-up of Joystick class. Refactor constructor to avoid mandatory 'post-constructor...
[enjoyable.git] / JSActionAnalog.m
1 //
2 // JSActionAnalog.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 @implementation JSActionAnalog
9
10 - (id) initWithIndex: (int)newIndex {
11 if(self = [super init]) {
12 subActions = @[[[SubAction alloc] initWithIndex: 0 name: @"Low" base: self],
13 [[SubAction alloc] initWithIndex: 1 name: @"High" base: self],
14 [[SubAction alloc] initWithIndex: 2 name: @"Analog" base: self]];
15 index = newIndex;
16 name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)];
17 }
18 return self;
19 }
20
21 -(id) findSubActionForValue: (IOHIDValueRef) value {
22 if ([subActions[2] active]) {
23 return subActions[2]; // TODO?
24 }
25
26 //Target* target = [[base->configsController currentConfig] getTargetForAction: [subActions objectAtIndex: 0]];
27
28 int raw = IOHIDValueGetIntegerValue(value);
29 double parsed = [self getRealValue: raw];
30
31 if(parsed < -0.3) // fixed?!
32 return subActions[0];
33 else if(parsed > 0.3)
34 return subActions[1];
35 return NULL;
36 }
37
38 -(void) notifyEvent: (IOHIDValueRef) value {
39 // Analog action is always active
40 [subActions[2] setActive: true];
41
42 int raw = IOHIDValueGetIntegerValue(value);
43 double parsed = [self getRealValue: raw];
44
45 [subActions[0] setActive: (parsed < -0.3)];
46 [subActions[1] setActive: (parsed > 0.3)];
47 }
48
49 -(double) getRealValue: (int)value {
50 double parsed = offset + scale * value;
51 return parsed;
52 }
53
54 @synthesize offset, scale;
55
56
57 @end