From 79b05c3cf1c75bc4f7078acde43aaf4215c6408a Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Wed, 13 Mar 2013 14:44:58 +0100 Subject: [PATCH] Fix localization changing mapping logical names. --- Classes/NJDevice.h | 8 ++--- Classes/NJDevice.m | 28 ++------------- Classes/NJDeviceController.m | 24 ++++++------- Classes/NJInput.h | 13 ++++--- Classes/NJInput.m | 36 ++++--------------- Classes/NJInputAnalog.m | 20 +++++------ Classes/NJInputButton.m | 10 +++--- Classes/NJInputHat.m | 21 +++++++---- Classes/NJInputPathElement.h | 25 ++++++++----- Classes/NJInputPathElement.m | 54 +++++++++++++++++++++++++++++ Classes/NJOutputController.m | 2 +- Enjoyable.xcodeproj/project.pbxproj | 4 +++ Info.plist | 2 +- 13 files changed, 135 insertions(+), 112 deletions(-) create mode 100644 Classes/NJInputPathElement.m diff --git a/Classes/NJDevice.h b/Classes/NJDevice.h index ec607ac..9f2cb97 100644 --- a/Classes/NJDevice.h +++ b/Classes/NJDevice.h @@ -10,16 +10,14 @@ @class NJInput; -@interface NJDevice : NSObject +@interface NJDevice : NJInputPathElement + +- (id)initWithDevice:(IOHIDDeviceRef)device; @property (nonatomic, assign) int index; @property (nonatomic, copy) NSString *productName; @property (nonatomic, assign) IOHIDDeviceRef device; -@property (nonatomic, copy) NSArray *children; -@property (nonatomic, readonly) NSString *name; -@property (readonly) NSString *uid; -- (id)initWithDevice:(IOHIDDeviceRef)device; - (NJInput *)handlerForEvent:(IOHIDValueRef)value; - (NJInput *)inputForEvent:(IOHIDValueRef)value; diff --git a/Classes/NJDevice.m b/Classes/NJDevice.m index e2a0efa..a5efd96 100644 --- a/Classes/NJDevice.m +++ b/Classes/NJDevice.m @@ -66,7 +66,7 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) { } - (id)initWithDevice:(IOHIDDeviceRef)dev { - if ((self = [super init])) { + if ((self = [super initWithName:nil did:nil base:nil])) { self.device = dev; self.productName = (__bridge NSString *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDProductKey)); vendorId = [(__bridge NSNumber *)IOHIDDeviceGetProperty(dev, CFSTR(kIOHIDVendorIDKey)) intValue]; @@ -89,7 +89,7 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) { } - (NJInput *)findInputByCookie:(IOHIDElementCookie)cookie { - for (NJInput *child in _children) + for (NJInput *child in self.children) if (child.cookie == cookie) return child; return nil; @@ -106,28 +106,4 @@ static NSArray *InputsForElement(IOHIDDeviceRef device, id base) { return [self findInputByCookie:cookie]; } -- (BOOL)isEqual:(id)object { - return [object isKindOfClass:NJDevice.class] - && [[object uid] isEqualToString:self.uid]; -} - -- (NSUInteger)hash { - return self.uid.hash; -} - -- (id )elementForUID:(NSString *)uid { - if ([uid isEqualToString:self.uid]) - return self; - else if (![uid hasPrefix:self.uid]) - return nil; - else { - for (id elem in self.children) { - id ret = [elem elementForUID:uid]; - if (ret) - return ret; - } - } - return nil; -} - @end diff --git a/Classes/NJDeviceController.m b/Classes/NJDeviceController.m index 2f1736b..6fc4d13 100644 --- a/Classes/NJDeviceController.m +++ b/Classes/NJDeviceController.m @@ -81,7 +81,7 @@ [_continuousOutputsTick invalidate]; } -- (void)expandRecursive:(id )pathElement { +- (void)expandRecursive:(NJInputPathElement *)pathElement { if (pathElement) { [self expandRecursive:pathElement.base]; [outlineView expandItem:pathElement]; @@ -250,34 +250,34 @@ static int findAvailableIndex(NSArray *list, NJDevice *dev) { } - (NJInput *)selectedInput { - id item = [outlineView itemAtRow:outlineView.selectedRow]; - return (!item.children && item.base) ? item : nil; + NJInputPathElement *item = [outlineView itemAtRow:outlineView.selectedRow]; + return (NJInput *)((!item.children && item.base) ? item : nil); } - (NSInteger)outlineView:(NSOutlineView *)outlineView - numberOfChildrenOfItem:(id )item { + numberOfChildrenOfItem:(NJInputPathElement *)item { return item ? item.children.count : _devices.count; } - (BOOL)outlineView:(NSOutlineView *)outlineView - isItemExpandable:(id )item { + isItemExpandable:(NJInputPathElement *)item { return item ? [[item children] count] > 0: YES; } - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index - ofItem:(id )item { + ofItem:(NJInputPathElement *)item { return item ? item.children[index] : _devices[index]; } - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn - byItem:(id )item { + byItem:(NJInputPathElement *)item { return item ? item.name : @"root"; } - (void)outlineViewSelectionDidChange:(NSNotification *)notification { - id item = [outlineView itemAtRow:outlineView.selectedRow]; + NJInputPathElement *item = [outlineView itemAtRow:outlineView.selectedRow]; if (item) [NSUserDefaults.standardUserDefaults setObject:item.uid forKey:@"selected input"]; @@ -285,17 +285,17 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn } - (BOOL)outlineView:(NSOutlineView *)outlineView - isGroupItem:(id )item { + isGroupItem:(NJInputPathElement *)item { return [item isKindOfClass:NJDevice.class]; } - (BOOL)outlineView:(NSOutlineView *)outlineView_ - shouldSelectItem:(id )item { + shouldSelectItem:(NJInputPathElement *)item { return ![self outlineView:outlineView_ isGroupItem:item]; } - (void)outlineViewItemDidExpand:(NSNotification *)notification { - id item = notification.userInfo[@"NSObject"]; + NJInputPathElement *item = notification.userInfo[@"NSObject"]; NSString *uid = item.uid; if (![_expanded containsObject:uid]) [_expanded addObject:uid]; @@ -306,7 +306,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn } - (void)outlineViewItemDidCollapse:(NSNotification *)notification { - id item = notification.userInfo[@"NSObject"]; + NJInputPathElement *item = notification.userInfo[@"NSObject"]; [_expanded removeObject:item.uid]; [NSUserDefaults.standardUserDefaults setObject:_expanded forKey:@"expanded rows"]; diff --git a/Classes/NJInput.h b/Classes/NJInput.h index d63be1b..b9d2dca 100644 --- a/Classes/NJInput.h +++ b/Classes/NJInput.h @@ -8,17 +8,16 @@ #import "NJInputPathElement.h" -@interface NJInput : NSObject +@interface NJInput : NJInputPathElement + +- (id)initWithName:(NSString *)name + did:(NSString *)did + cookie:(IOHIDElementCookie)cookie + base:(NJInputPathElement *)base; @property (nonatomic, assign) IOHIDElementCookie cookie; -@property (nonatomic, copy) NSArray *children; -@property (nonatomic, weak) id base; -@property (nonatomic, copy) NSString *name; @property (nonatomic, assign) BOOL active; @property (nonatomic, assign) float magnitude; -@property (readonly) NSString *uid; - -- (id)initWithName:(NSString *)newName base:(id )newBase; - (void)notifyEvent:(IOHIDValueRef)value; - (id)findSubInputForValue:(IOHIDValueRef)value; diff --git a/Classes/NJInput.m b/Classes/NJInput.m index 15b4687..78376e6 100644 --- a/Classes/NJInput.m +++ b/Classes/NJInput.m @@ -9,10 +9,12 @@ @implementation NJInput -- (id)initWithName:(NSString *)newName base:(id )newBase { - if ((self = [super init])) { - self.name = newName; - self.base = newBase; +- (id)initWithName:(NSString *)name + did:(NSString *)did + cookie:(IOHIDElementCookie)cookie + base:(NJInputPathElement *)base { + if ((self = [super initWithName:name did:did base:base])) { + self.cookie = cookie; } return self; } @@ -21,34 +23,8 @@ return nil; } -- (NSString *)uid { - return [NSString stringWithFormat:@"%@~%@", _base.uid, _name]; -} - - (void)notifyEvent:(IOHIDValueRef)value { [self doesNotRecognizeSelector:_cmd]; } -- (BOOL)isEqual:(id)object { - return [object isKindOfClass:NJInput.class] - && [[object uid] isEqualToString:self.uid]; -} - -- (NSUInteger)hash { - return self.uid.hash; -} - -- (id )elementForUID:(NSString *)uid { - if ([uid isEqualToString:self.uid]) - return self; - else { - for (id elem in self.children) { - id ret = [elem elementForUID:uid]; - if (ret) - return ret; - } - } - return nil; -} - @end diff --git a/Classes/NJInputAnalog.m b/Classes/NJInputAnalog.m index 41a39f2..af64954 100644 --- a/Classes/NJInputAnalog.m +++ b/Classes/NJInputAnalog.m @@ -14,16 +14,20 @@ static float normalize(long p, long min, long max) { } @implementation NJInputAnalog { - float magnitude; long rawMin; long rawMax; } - (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)rawMax_ { - if ((self = [super init])) { - self.name = [[NSString alloc] initWithFormat:NSLocalizedString(@"axis %d", @"axis name"), index]; - self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"axis low", @"axis low trigger") base:self], - [[NJInput alloc] initWithName:NSLocalizedString(@"axis high", @"axis high trigger") base:self]]; + NSString *name = [[NSString alloc] initWithFormat:NSLocalizedString(@"axis %d", @"axis name"), index]; + NSString *did = [[NSString alloc] initWithFormat:@"Axis %d", index]; + if ((self = [super initWithName:name did:did base:nil])) { + self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"axis low", @"axis low trigger") + did:@"Low" + base:self], + [[NJInput alloc] initWithName:NSLocalizedString(@"axis high", @"axis high trigger") + did:@"High" + base:self]]; rawMax = rawMax_; rawMin = rawMin_; } @@ -41,15 +45,11 @@ static float normalize(long p, long min, long max) { } - (void)notifyEvent:(IOHIDValueRef)value { - magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); + float magnitude = self.magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); [self.children[0] setMagnitude:fabsf(MIN(magnitude, 0))]; [self.children[1] setMagnitude:fabsf(MAX(magnitude, 0))]; [self.children[0] setActive:magnitude < -DEAD_ZONE]; [self.children[1] setActive:magnitude > DEAD_ZONE]; } -- (float)magnitude { - return magnitude; -} - @end diff --git a/Classes/NJInputButton.m b/Classes/NJInputButton.m index caa4d8e..caa759b 100644 --- a/Classes/NJInputButton.m +++ b/Classes/NJInputButton.m @@ -12,12 +12,12 @@ } - (id)initWithName:(NSString *)name idx:(int)idx max:(long)max { - if ((self = [super init])) { + NSString *fullname = [NSString stringWithFormat:NSLocalizedString(@"button %d", @"button name"), idx]; + if (name.length) + fullname = [fullname stringByAppendingFormat:@"- %@", name]; + NSString *did = [[NSString alloc] initWithFormat:@"Button %d", idx]; + if ((self = [super initWithName:fullname did:did base:nil])) { _max = max; - self.name = [NSString stringWithFormat:NSLocalizedString(@"button %d", @"button name"), idx]; - - if (name.length) - self.name = [self.name stringByAppendingFormat:@"- %@", name]; } return self; } diff --git a/Classes/NJInputHat.m b/Classes/NJInputHat.m index d267b71..46ea94f 100644 --- a/Classes/NJInputHat.m +++ b/Classes/NJInputHat.m @@ -30,12 +30,21 @@ static BOOL active_fourway[20] = { @implementation NJInputHat - (id)initWithIndex:(int)index { - if ((self = [super init])) { - self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"hat up", @"hat switch up state") base:self], - [[NJInput alloc] initWithName:NSLocalizedString(@"hat down", @"hat switch down state") base:self], - [[NJInput alloc] initWithName:NSLocalizedString(@"hat left", @"hat switch left state") base:self], - [[NJInput alloc] initWithName:NSLocalizedString(@"hat right", @"hat switch right state") base:self]]; - self.name = [NSString stringWithFormat:NSLocalizedString(@"hat switch %d", @"hat switch name"), index]; + NSString *name = [NSString stringWithFormat:NSLocalizedString(@"hat switch %d", @"hat switch name"), index]; + NSString *did = [NSString stringWithFormat:@"Hat Switch %d", index]; + if ((self = [super initWithName:name did:did base:nil])) { + self.children = @[[[NJInput alloc] initWithName:NSLocalizedString(@"hat up", @"hat switch up state") + did:@"Up" + base:self], + [[NJInput alloc] initWithName:NSLocalizedString(@"hat down", @"hat switch down state") + did:@"Down" + base:self], + [[NJInput alloc] initWithName:NSLocalizedString(@"hat left", @"hat switch left state") + did:@"Left" + base:self], + [[NJInput alloc] initWithName:NSLocalizedString(@"hat right", @"hat switch right state") + did:@"Right" + base:self]]; } return self; } diff --git a/Classes/NJInputPathElement.h b/Classes/NJInputPathElement.h index f3f27c2..3ad50c9 100644 --- a/Classes/NJInputPathElement.h +++ b/Classes/NJInputPathElement.h @@ -1,14 +1,21 @@ -#import +// +// NJInputPathElement.h +// Enjoyable +// +// Created by Joe Wreschnig on 3/13/13. +// +// +@interface NJInputPathElement : NSObject -@protocol NJInputPathElement +- (id)initWithName:(NSString *)name + did:(NSString *)did + base:(NJInputPathElement *)base; -// TODO: It's time this became a real base class rather than a protocol. +@property (nonatomic, weak) NJInputPathElement *base; +@property (nonatomic, copy) NSString *name; +@property (nonatomic, readonly) NSString *uid; +@property (nonatomic, strong) NSArray *children; -- (NSArray *)children; -- (id ) base; -- (NSString *)name; -- (NSString *)uid; - -- (id )elementForUID:(NSString *)uid; +- (NJInputPathElement *)elementForUID:(NSString *)uid; @end diff --git a/Classes/NJInputPathElement.m b/Classes/NJInputPathElement.m new file mode 100644 index 0000000..eb5bfa7 --- /dev/null +++ b/Classes/NJInputPathElement.m @@ -0,0 +1,54 @@ +// +// NJInputPathElement.m +// Enjoyable +// +// Created by Joe Wreschnig on 3/13/13. +// +// + +#include "NJInputPathElement.h" + +@implementation NJInputPathElement { + NSString *_did; +} + +- (id)initWithName:(NSString *)name + did:(NSString *)did + base:(NJInputPathElement *)base { + if ((self = [super init])) { + self.name = name; + self.base = base; + _did = did; + } + return self; +} + +- (BOOL)isEqual:(id)object { + return [object isKindOfClass:NJInputPathElement.class] + && [[object uid] isEqualToString:self.uid]; +} + +- (NSUInteger)hash { + return self.uid.hash; +} + +- (NSString *)uid { + return [NSString stringWithFormat:@"%@~%@", _base.uid, _did]; +} + +- (NJInputPathElement *)elementForUID:(NSString *)uid { + if ([uid isEqualToString:self.uid]) + return self; + else if (![uid hasPrefix:self.uid]) + return nil; + else { + for (NJInputPathElement *elem in self.children) { + NJInputPathElement *ret = [elem elementForUID:uid]; + if (ret) + return ret; + } + } + return nil; +} + +@end diff --git a/Classes/NJOutputController.m b/Classes/NJOutputController.m index 6ae58d4..4c8ce64 100644 --- a/Classes/NJOutputController.m +++ b/Classes/NJOutputController.m @@ -229,7 +229,7 @@ } else { self.enabled = YES; NSString *inpFullName = input.name; - for (id cur = input.base; cur; cur = cur.base) { + for (NJInputPathElement *cur = input.base; cur; cur = cur.base) { inpFullName = [[NSString alloc] initWithFormat:@"%@ ▸ %@", cur.name, inpFullName]; } title.stringValue = inpFullName; diff --git a/Enjoyable.xcodeproj/project.pbxproj b/Enjoyable.xcodeproj/project.pbxproj index ea3ba90..eee41a1 100644 --- a/Enjoyable.xcodeproj/project.pbxproj +++ b/Enjoyable.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ EED4CE7716EE195100C65AA8 /* Sparkle.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EED4CE7616EE195100C65AA8 /* Sparkle.framework */; }; EED4CE7816EE195B00C65AA8 /* Sparkle.framework in Copy Sparkle Framework */ = {isa = PBXBuildFile; fileRef = EED4CE7616EE195100C65AA8 /* Sparkle.framework */; }; EEE703DC16F089FE002FDD69 /* NJHIDManager.m in Sources */ = {isa = PBXBuildFile; fileRef = EEE703DB16F089FE002FDD69 /* NJHIDManager.m */; }; + EEE703DE16F0B3F6002FDD69 /* NJInputPathElement.m in Sources */ = {isa = PBXBuildFile; fileRef = EEE703DD16F0B3F6002FDD69 /* NJInputPathElement.m */; }; EEE73B1616EA42E5009D9D99 /* NSRunningApplication+NJPossibleNames.m in Sources */ = {isa = PBXBuildFile; fileRef = EEE73B1516EA42E5009D9D99 /* NSRunningApplication+NJPossibleNames.m */; }; EEF17D1916E8E21A00D7DC4D /* com.yukkurigames.Enjoyable.mapping.icns in Resources */ = {isa = PBXBuildFile; fileRef = EEF17D1716E8E21A00D7DC4D /* com.yukkurigames.Enjoyable.mapping.icns */; }; EEF17D1F16E8E23A00D7DC4D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = EEF17D1B16E8E23A00D7DC4D /* InfoPlist.strings */; }; @@ -82,6 +83,7 @@ EED4CE7616EE195100C65AA8 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = ""; }; EEE703DA16F089FE002FDD69 /* NJHIDManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NJHIDManager.h; path = Classes/NJHIDManager.h; sourceTree = ""; }; EEE703DB16F089FE002FDD69 /* NJHIDManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NJHIDManager.m; path = Classes/NJHIDManager.m; sourceTree = ""; }; + EEE703DD16F0B3F6002FDD69 /* NJInputPathElement.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NJInputPathElement.m; path = Classes/NJInputPathElement.m; sourceTree = ""; }; EEE73B1416EA42E5009D9D99 /* NSRunningApplication+NJPossibleNames.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSRunningApplication+NJPossibleNames.h"; path = "Categories/NSRunningApplication+NJPossibleNames.h"; sourceTree = ""; }; EEE73B1516EA42E5009D9D99 /* NSRunningApplication+NJPossibleNames.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSRunningApplication+NJPossibleNames.m"; path = "Categories/NSRunningApplication+NJPossibleNames.m"; sourceTree = ""; }; EEF17D1716E8E21A00D7DC4D /* com.yukkurigames.Enjoyable.mapping.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = com.yukkurigames.Enjoyable.mapping.icns; path = Resources/com.yukkurigames.Enjoyable.mapping.icns; sourceTree = ""; }; @@ -170,6 +172,7 @@ EEF17D4A16E8E2EF00D7DC4D /* NJMapping.h */, EEF17D4B16E8E2EF00D7DC4D /* NJMapping.m */, EEF17D4716E8E2EF00D7DC4D /* NJInputPathElement.h */, + EEE703DD16F0B3F6002FDD69 /* NJInputPathElement.m */, EEF17D3B16E8E2EF00D7DC4D /* NJDevice.h */, EEF17D3C16E8E2EF00D7DC4D /* NJDevice.m */, EEF17D3F16E8E2EF00D7DC4D /* NJInput.h */, @@ -432,6 +435,7 @@ EEE73B1616EA42E5009D9D99 /* NSRunningApplication+NJPossibleNames.m in Sources */, EED4CE6E16ED692400C65AA8 /* NJMappingMenuController.m in Sources */, EEE703DC16F089FE002FDD69 /* NJHIDManager.m in Sources */, + EEE703DE16F0B3F6002FDD69 /* NJInputPathElement.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Info.plist b/Info.plist index be1810b..f3cc683 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 244 + 248 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.30.2