Formal protocol for the interface shared between Joysticks and JSActions, use new...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 2 Mar 2013 12:33:11 +0000 (13:33 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 2 Mar 2013 12:33:11 +0000 (13:33 +0100)
ApplicationController.h
Enjoyable.xcodeproj/project.pbxproj
JSAction.h
JSAction.m
Joystick.h
Joystick.m
JoystickController.m
NJActionPathElement.h [new file with mode: 0644]
TargetController.m

index d9a875a..a5dcb6a 100644 (file)
@@ -10,7 +10,7 @@
 @class TargetController;
 @class ConfigsController;
 
-@interface ApplicationController : NSObject {
+@interface ApplicationController : NSObject <NSApplicationDelegate> {
     IBOutlet NSDrawer *drawer;
     IBOutlet NSWindow *mainWindow;
     IBOutlet NSToolbarItem *activeButton;
index 35f48d8..c46eb77 100644 (file)
@@ -89,6 +89,7 @@
                EE1D7C9116E01E7000B000EB /* NSView+FirstResponder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+FirstResponder.m"; sourceTree = "<group>"; };
                EE1D7C9416E0ECCF00B000EB /* NSError+Description.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Description.h"; sourceTree = "<group>"; };
                EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Description.m"; sourceTree = "<group>"; };
+               EEF86B7316E2241000674B87 /* NJActionPathElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJActionPathElement.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
                                8BEFAD9B15C46BFF00823AEC /* TargetMouseScroll.m */,
                                8BEFAD9E15C476DC00823AEC /* TargetToggleMouseScope.h */,
                                8BEFAD9F15C476DC00823AEC /* TargetToggleMouseScope.m */,
+                               EEF86B7316E2241000674B87 /* NJActionPathElement.h */,
                        );
                        name = Classes;
                        sourceTree = "<group>";
index 0e25847..dc00a8d 100644 (file)
@@ -6,7 +6,9 @@
 //  Copyright 2009 University of Otago. All rights reserved.
 //
 
-@interface JSAction : NSObject
+#import "NJActionPathElement.h"
+
+@interface JSAction : NSObject <NJActionPathElement>
 
 @property (nonatomic, assign) IOHIDElementCookie cookie;
 @property (nonatomic, copy) NSArray *children;
@@ -16,7 +18,7 @@
 @property (nonatomic, readonly) float magnitude;
 @property (readonly) NSString *uid;
 
-- (id)initWithName:(NSString *)newName base:(JSAction *)newBase;
+- (id)initWithName:(NSString *)newName base:(id <NJActionPathElement>)newBase;
 
 - (void)notifyEvent:(IOHIDValueRef)value;
 - (id)findSubActionForValue:(IOHIDValueRef)value;
index 6903c56..1cdbeb2 100644 (file)
@@ -9,7 +9,7 @@
 
 @implementation JSAction
 
-- (id)initWithName:(NSString *)newName base:(JSAction *)newBase {
+- (id)initWithName:(NSString *)newName base:(id <NJActionPathElement>)newBase {
     if ((self = [super init])) {
         self.name = newName;
         self.base = newBase;
index 4b60ba8..e5566f1 100644 (file)
@@ -6,9 +6,11 @@
 //  Copyright 2009 University of Otago. All rights reserved.
 //
 
+#import "NJActionPathElement.h"
+
 @class JSAction;
 
-@interface Joystick : NSObject
+@interface Joystick : NSObject <NJActionPathElement>
 
 @property (nonatomic, assign) int index;
 @property (nonatomic, copy) NSString *productName;
@@ -18,7 +20,7 @@
 @property (readonly) NSString *uid;
 
 - (id)initWithDevice:(IOHIDDeviceRef)device;
-- (id)handlerForEvent:(IOHIDValueRef)value;
+- (JSAction *)handlerForEvent:(IOHIDValueRef)value;
 - (JSAction *)actionForEvent:(IOHIDValueRef)value;
 
 @end
index 3b43e4e..4753caf 100644 (file)
@@ -95,7 +95,7 @@ static NSArray *ActionsForElement(IOHIDDeviceRef device, id base) {
     return nil;
 }
 
-- (id)handlerForEvent:(IOHIDValueRef) value {
+- (JSAction *)handlerForEvent:(IOHIDValueRef)value {
     JSAction *mainAction = [self actionForEvent:value];
     return [mainAction findSubActionForValue:value];
 }
index 967a935..ab59c82 100644 (file)
     CFRelease(hidManager);
 }
 
-- (void)expandRecursive:(id)handler {
-    if ([handler base])
-        [self expandRecursive:[handler base]];
-    [outlineView expandItem:handler];
+- (void)expandRecursive:(id <NJActionPathElement>)pathElement {
+    if (pathElement) {
+        [self expandRecursive:pathElement.base];
+        [outlineView expandItem:pathElement];
+    }
 }
 
 - (void)addRunningTarget:(Target *)target {
@@ -188,26 +189,30 @@ static void remove_callback(void *ctx, IOReturn inResult, void *inSender, IOHIDD
 }
 
 - (JSAction *)selectedAction {
-    id item = [outlineView itemAtRow:outlineView.selectedRow];
-    return [item children] ? nil : item;
+    id <NJActionPathElement> item = [outlineView itemAtRow:outlineView.selectedRow];
+    return (!item.children && item.base) ? item : nil;
 }
 
-- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
-    return item ? [[item children] count] : _joysticks.count;
+- (NSInteger)outlineView:(NSOutlineView *)outlineView
+  numberOfChildrenOfItem:(id <NJActionPathElement>)item {
+    return item ? item.children.count : _joysticks.count;
 }
 
-- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
+- (BOOL)outlineView:(NSOutlineView *)outlineView
+   isItemExpandable:(id <NJActionPathElement>)item {
     return item ? [[item children] count] > 0: YES;
 }
 
-- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
-    return item ? [item children][index] : _joysticks[index];
+- (id)outlineView:(NSOutlineView *)outlineView
+            child:(NSInteger)index
+           ofItem:(id <NJActionPathElement>)item {
+    return item ? item.children[index] : _joysticks[index];
 }
 
-- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item  {
-    if(item == nil)
-        return @"root";
-    return [item name];
+- (id)outlineView:(NSOutlineView *)outlineView
+objectValueForTableColumn:(NSTableColumn *)tableColumn
+           byItem:(id <NJActionPathElement>)item  {
+    return item ? item.name : @"root";
 }
 
 - (void)outlineViewSelectionDidChange:(NSNotification *)notification {
diff --git a/NJActionPathElement.h b/NJActionPathElement.h
new file mode 100644 (file)
index 0000000..f5eb0ca
--- /dev/null
@@ -0,0 +1,9 @@
+#import <Foundation/Foundation.h>
+
+@protocol NJActionPathElement <NSObject>
+
+- (NSArray *)children;
+- (id <NJActionPathElement>) base;
+- (NSString *)name;
+
+@end
index 75f58ab..1f285ec 100644 (file)
     } else {
         self.enabled = YES;
         NSString *actFullName = action.name;
-        for (JSAction *cur = action.base; cur; cur = cur.base) {
+        for (id <NJActionPathElement> cur = action.base; cur; cur = cur.base) {
             actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName];
         }
         title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName];