X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JSActionHat.m;h=2cac39c77c4a22e4148be01fe6e78c52d6a82540;hp=4e11b49b4dcfaafd71ba9cd8b8d810c8c5cb0dd1;hb=0238d141f06420e1a73eccd14ca73a7e29ad2a69;hpb=e68c19b5923618b763543c74bf8dd6f85d4d323e diff --git a/JSActionHat.m b/JSActionHat.m index 4e11b49..2cac39c 100644 --- a/JSActionHat.m +++ b/JSActionHat.m @@ -5,6 +5,8 @@ // Created by Sam McCall on 5/05/09. // +#import "JSActionHat.h" + static BOOL active_eightway[36] = { NO, NO, NO, NO , // center YES, NO, NO, NO , // N @@ -27,51 +29,50 @@ static BOOL active_fourway[20] = { @implementation JSActionHat -- (id)init { +- (id)initWithIndex:(int)index { if ((self = [super init])) { - self.subActions = @[[[SubAction alloc] initWithIndex: 0 name: @"Up" base: self], - [[SubAction alloc] initWithIndex: 1 name: @"Down" base: self], - [[SubAction alloc] initWithIndex: 2 name: @"Left" base: self], - [[SubAction alloc] initWithIndex: 3 name: @"Right" base: self]]; - // TODO(jfw): Should have an indexed name, like everything else. - self.name = @"Hat switch"; + self.children = @[[[JSAction alloc] initWithName:@"Up" base:self], + [[JSAction alloc] initWithName:@"Down" base:self], + [[JSAction alloc] initWithName:@"Left" base:self], + [[JSAction alloc] initWithName:@"Right" base:self]]; + self.name = [NSString stringWithFormat:@"Hat Switch %d", index]; } return self; } - (id)findSubActionForValue:(IOHIDValueRef)value { - int parsed = IOHIDValueGetIntegerValue(value); + long parsed = IOHIDValueGetIntegerValue(value); switch (IOHIDElementGetLogicalMax(IOHIDValueGetElement(value))) { case 7: // 8-way switch, 0-7. switch (parsed) { - case 0: return self.subActions[0]; - case 4: return self.subActions[1]; - case 6: return self.subActions[2]; - case 2: return self.subActions[3]; + case 0: return self.children[0]; + case 4: return self.children[1]; + case 6: return self.children[2]; + case 2: return self.children[3]; default: return nil; } case 8: // 8-way switch, 1-8 (neutral 0). switch (parsed) { - case 1: return self.subActions[0]; - case 5: return self.subActions[1]; - case 7: return self.subActions[2]; - case 3: return self.subActions[3]; + case 1: return self.children[0]; + case 5: return self.children[1]; + case 7: return self.children[2]; + case 3: return self.children[3]; default: return nil; } case 3: // 4-way switch, 0-3. switch (parsed) { - case 0: return self.subActions[0]; - case 2: return self.subActions[1]; - case 3: return self.subActions[2]; - case 1: return self.subActions[3]; + case 0: return self.children[0]; + case 2: return self.children[1]; + case 3: return self.children[2]; + case 1: return self.children[3]; default: return nil; } case 4: // 4-way switch, 1-4 (neutral 0). switch (parsed) { - case 1: return self.subActions[0]; - case 3: return self.subActions[1]; - case 4: return self.subActions[2]; - case 2: return self.subActions[3]; + case 1: return self.children[0]; + case 3: return self.children[1]; + case 4: return self.children[2]; + case 2: return self.children[3]; default: return nil; } default: @@ -80,16 +81,16 @@ static BOOL active_fourway[20] = { } - (void)notifyEvent:(IOHIDValueRef)value { - int parsed = IOHIDValueGetIntegerValue(value); - int size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)); + long parsed = IOHIDValueGetIntegerValue(value); + long size = IOHIDElementGetLogicalMax(IOHIDValueGetElement(value)); // Skip first row in table if 0 is not neutral. if (size & 1) { parsed++; size++; } - BOOL *activeSubactions = (size == 8) ? active_eightway : active_fourway; + BOOL *activechildren = (size == 8) ? active_eightway : active_fourway; for (int i = 0; i < 4; i++) - [self.subActions[i] setActive:activeSubactions[parsed * 4 + i]]; + [self.children[i] setActive:activechildren[parsed * 4 + i]]; } @end