Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#35 Fixed - Completed configurable joystick controls
FIXME - Keyboard editor borked
  • Loading branch information
0xe1f committed Apr 9, 2016
1 parent 57f79e9 commit 64dc6ae
Show file tree
Hide file tree
Showing 19 changed files with 690 additions and 1,286 deletions.
19 changes: 15 additions & 4 deletions Cocoa/CMGamepadConfiguration.h
Expand Up @@ -22,10 +22,11 @@
*/
#import <Foundation/Foundation.h>

@interface CMGamepadConfiguration : NSObject<NSCoding, NSCopying>

#define CM_NO_INPUT 0

#define CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_1 (0)
#define CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_2 (1)

#define CM_KIND_MASK 0xf000000
#define CM_VALUE_MASK 0x0ffffff

Expand All @@ -36,9 +37,15 @@

#define CM_KIND_BUTTON 0x1000000
#define CM_KIND_ANALOG 0x2000000
#define CM_KIND_KEY 0x4000000

#define CMMakeButton(index) (CM_KIND_BUTTON|((index)&0x1f))
#define CMMakeAnalog(dir) (CM_KIND_ANALOG|((dir)&0x3))
#define CMMakeKey(key) (CM_KIND_KEY|((key)&0xff))

#define CMKeyCode(code) ((code)&0xff)

@interface CMGamepadConfiguration : NSObject<NSCoding, NSCopying>

@property (nonatomic, assign) NSInteger vendorProductId;

Expand All @@ -49,7 +56,11 @@
@property (nonatomic, assign) NSInteger buttonA;
@property (nonatomic, assign) NSInteger buttonB;

- (void) clear;
+ (CMGamepadConfiguration *) defaultConfiguration;
- (void) reset;
- (NSString *) vendorProductString;

+ (CMGamepadConfiguration *) defaultKeyboardPlayerOneConfiguration;
+ (CMGamepadConfiguration *) defaultKeyboardPlayerTwoConfiguration;
+ (CMGamepadConfiguration *) defaultGamepadConfiguration;

@end
73 changes: 61 additions & 12 deletions Cocoa/CMGamepadConfiguration.m
Expand Up @@ -22,39 +22,88 @@
*/
#import "CMGamepadConfiguration.h"

#import "CMKeyboardManager.h"
#import "CMKeyboardInput.h"

@implementation CMGamepadConfiguration

static CMGamepadConfiguration *_defaultConfig;
static CMGamepadConfiguration *_defaultKeyboardPlayerOneConfig;
static CMGamepadConfiguration *_defaultKeyboardPlayerTwoConfig;
static CMGamepadConfiguration *_defaultGamepadConfig;

+ (void) initialize
{
_defaultConfig = [[CMGamepadConfiguration alloc] init];
_defaultKeyboardPlayerOneConfig = [[CMGamepadConfiguration alloc] init];
_defaultKeyboardPlayerOneConfig->_vendorProductId = CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_1;
[_defaultKeyboardPlayerOneConfig reset];

_defaultKeyboardPlayerTwoConfig = [[CMGamepadConfiguration alloc] init];
_defaultKeyboardPlayerTwoConfig->_vendorProductId = CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_2;
[_defaultKeyboardPlayerTwoConfig reset];

_defaultGamepadConfig = [[CMGamepadConfiguration alloc] init];
_defaultGamepadConfig->_vendorProductId = -1;
[_defaultGamepadConfig reset];
}

- (id)init
{
if ((self = [super init])) {
[self clear];
}

return self;
}

#pragma mark - Public

- (void) clear
- (void) reset
{
switch (_vendorProductId) {
case CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_1:
_up = CMMakeKey(CMKeyUp);
_down = CMMakeKey(CMKeyDown);
_left = CMMakeKey(CMKeyLeft);
_right = CMMakeKey(CMKeyRight);
_buttonA = CMMakeKey(CMKeySpacebar);
_buttonB = CMMakeKey(CMKeyLeftAlt);
break;
case CM_VENDOR_PRODUCT_KEYBOARD_PLAYER_2:
_up = CMMakeKey(CMKeyW);
_down = CMMakeKey(CMKeyS);
_left = CMMakeKey(CMKeyA);
_right = CMMakeKey(CMKeyD);
_buttonA = CMMakeKey(CMKeyLeftBracket);
_buttonB = CMMakeKey(CMKeyRightBracket);
break;
default:
_up = CMMakeAnalog(CM_DIR_UP);
_down = CMMakeAnalog(CM_DIR_DOWN);
_left = CMMakeAnalog(CM_DIR_LEFT);
_right = CMMakeAnalog(CM_DIR_RIGHT);
_buttonA = CMMakeButton(1);
_buttonB = CMMakeButton(2);
break;
}
}

+ (CMGamepadConfiguration *) defaultKeyboardPlayerOneConfiguration
{
return _defaultKeyboardPlayerOneConfig;
}

+ (CMGamepadConfiguration *) defaultKeyboardPlayerTwoConfiguration
{
return _defaultKeyboardPlayerTwoConfig;
}

+ (CMGamepadConfiguration *) defaultGamepadConfiguration
{
_up = CMMakeAnalog(CM_DIR_UP);
_down = CMMakeAnalog(CM_DIR_DOWN);
_left = CMMakeAnalog(CM_DIR_LEFT);
_right = CMMakeAnalog(CM_DIR_RIGHT);
_buttonA = CMMakeButton(1);
_buttonB = CMMakeButton(2);
return _defaultGamepadConfig;
}

+ (CMGamepadConfiguration *) defaultConfiguration
- (NSString *) vendorProductString
{
return _defaultConfig;
return [NSString stringWithFormat:@"%08lx", _vendorProductId];
}

#pragma mark - NSCoding
Expand Down
2 changes: 2 additions & 0 deletions Cocoa/CMGamepadManager.h
Expand Up @@ -57,6 +57,8 @@
- (CMGamepad *) gamepadWithId:(NSInteger) gamepadId;
- (CMGamepad *) gamepadAtIndex:(NSUInteger) index;

- (NSUInteger) gamepadCount;

- (void)addObserver:(id<CMGamepadEventDelegate>)observer;
- (void)removeObserver:(id<CMGamepadEventDelegate>)observer;

Expand Down
5 changes: 5 additions & 0 deletions Cocoa/CMGamepadManager.m
Expand Up @@ -129,6 +129,11 @@ - (CMGamepad *) gamepadAtIndex:(NSUInteger) index
return gp;
}

- (NSUInteger) gamepadCount
{
return [_allGamepads count];
}

#pragma mark - CMGamepadDelegate

- (void)gamepadDidConnect:(CMGamepad *)gamepad
Expand Down
4 changes: 0 additions & 4 deletions Cocoa/CMPreferences.h
Expand Up @@ -34,10 +34,6 @@

- (CMInputDeviceLayout *)keyboardLayout;
- (void)setKeyboardLayout:(CMInputDeviceLayout *)keyboardLayout;
- (CMInputDeviceLayout *)joystickOneLayout;
- (void)setJoystickOneLayout:(CMInputDeviceLayout *)joystickOneLayout;
- (CMInputDeviceLayout *)joystickTwoLayout;
- (void)setJoystickTwoLayout:(CMInputDeviceLayout *)joystickTwoLayout;

- (CMInputDeviceLayout *)defaultKeyboardLayout;
- (CMInputDeviceLayout *)defaultJoystickOneLayout;
Expand Down
26 changes: 1 addition & 25 deletions Cocoa/CMPreferences.m
Expand Up @@ -114,36 +114,12 @@ - (CMInputDeviceLayout *)keyboardLayout;
return [NSKeyedUnarchiver unarchiveObjectWithData:layoutData];
}

- (void)setKeyboardLayout:(CMInputDeviceLayout *)keyboardLayout;
- (void)setKeyboardLayout:(CMInputDeviceLayout *) keyboardLayout;
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:keyboardLayout];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:CMKeyboardLayoutPrefKey];
}

- (CMInputDeviceLayout *)joystickOneLayout;
{
NSData *layoutData = [[NSUserDefaults standardUserDefaults] objectForKey:CMJoystickOneLayoutPrefKey];
return [NSKeyedUnarchiver unarchiveObjectWithData:layoutData];
}

- (void)setJoystickOneLayout:(CMInputDeviceLayout *)joystickOneLayout;
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:joystickOneLayout];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:CMJoystickOneLayoutPrefKey];
}

- (CMInputDeviceLayout *)joystickTwoLayout;
{
NSData *layoutData = [[NSUserDefaults standardUserDefaults] objectForKey:CMJoystickTwoLayoutPrefKey];
return [NSKeyedUnarchiver unarchiveObjectWithData:layoutData];
}

- (void)setJoystickTwoLayout:(CMInputDeviceLayout *)joystickTwoLayout;
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:joystickTwoLayout];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:CMJoystickTwoLayoutPrefKey];
}

- (CMInputDeviceLayout *)defaultKeyboardLayout;
{
NSString *bundleResourcePath = [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"];
Expand Down
7 changes: 4 additions & 3 deletions Controllers/CMConfigureJoystickController.h
Expand Up @@ -23,21 +23,22 @@
#import <Cocoa/Cocoa.h>

#import "CMGamepadManager.h"
#import "CMKeyboardManager.h"

@class CMGamepadConfiguration;

@protocol CMGamepadConfigurationDelegate

@required
- (void) gamepadDidConfigure:(CMGamepad *) gamepad
configuration:(CMGamepadConfiguration *) configuration;
- (void) gamepadConfigurationDidComplete:(CMGamepadConfiguration *) configuration;

@end

@interface CMConfigureJoystickController : NSWindowController<NSWindowDelegate, NSTableViewDataSource, CMGamepadEventDelegate>
@interface CMConfigureJoystickController : NSWindowController<NSWindowDelegate, NSTableViewDataSource, CMGamepadEventDelegate, CMKeyboardEventDelegate>
{
IBOutlet NSButton *saveButton;
IBOutlet NSTableView *tableView;
IBOutlet NSTextField *infoLabel;
}

@property (nonatomic, weak) id delegate;
Expand Down

0 comments on commit 64dc6ae

Please sign in to comment.