Configurable keys for the Cocoa port

This commit is contained in:
Lior Halphon 2016-04-13 22:43:16 +03:00
parent 8dd1b3c854
commit 2a5375a0c8
13 changed files with 309 additions and 72 deletions

View File

@ -2,6 +2,8 @@
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property IBOutlet NSWindow *preferencesWindow;
- (IBAction)showPreferences: (id) sender;
- (IBAction)toggleDeveloperMode:(id)sender;
@end

View File

@ -5,6 +5,28 @@
@end
@implementation AppDelegate
{
NSWindow *preferences_window;
}
- (void) applicationDidFinishLaunching:(NSNotification *)notification
{
#define KEY(x) ({unichar __x = x; [NSString stringWithCharacters:&(__x) length:1];})
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"GBRight": KEY(NSRightArrowFunctionKey),
@"GBLeft": KEY(NSLeftArrowFunctionKey),
@"GBUp": KEY(NSUpArrowFunctionKey),
@"GBDown": KEY(NSDownArrowFunctionKey),
@"GBA": @"x",
@"GBB": @"z",
@"GBSelect": @"\x7f",
@"GBStart": @"\r",
@"GBTurbo": @" ",
}];
#undef KEY
}
- (IBAction)toggleDeveloperMode:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
@ -15,10 +37,17 @@
{
if ([anItem action] == @selector(toggleDeveloperMode:)) {
[(NSMenuItem*)anItem setState:[[NSUserDefaults standardUserDefaults] boolForKey:@"DeveloperMode"]];
return true;
}
return false;
return true;
}
- (IBAction) showPreferences: (id) sender
{
NSArray *objects;
if (!_preferencesWindow) {
[[NSBundle mainBundle] loadNibNamed:@"Preferences" owner:self topLevelObjects:&objects];
}
[_preferencesWindow makeKeyAndOrderFront:self];
}
@end

24
Cocoa/GBButtons.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef GBButtons_h
#define GBButtons_h
typedef enum : NSUInteger {
GBRight,
GBLeft,
GBUp,
GBDown,
GBA,
GBB,
GBSelect,
GBStart,
GBTurbo,
GBButtonCount
} GBButton;
extern NSString const *GBButtonNames[GBButtonCount];
static inline NSString *button_to_preference_name(GBButton button)
{
return [NSString stringWithFormat:@"GB%@", GBButtonNames[button]];
}
#endif

4
Cocoa/GBButtons.m Normal file
View File

@ -0,0 +1,4 @@
#import <Foundation/Foundation.h>
#import "GBButtons.h"
NSString const *GBButtonNames[] = {@"Right", @"Left", @"Up", @"Down", @"A", @"B", @"Select", @"Start", @"Turbo"};

View File

@ -0,0 +1,5 @@
#import <Cocoa/Cocoa.h>
@interface GBPreferencesWindow : NSWindow <NSTableViewDelegate, NSTableViewDataSource>
@property IBOutlet NSTableView *controlsTableView;
@end

View File

@ -0,0 +1,60 @@
#import "GBPreferencesWindow.h"
#import "NSString+StringForKey.h"
#import "GBButtons.h"
@implementation GBPreferencesWindow
{
bool is_button_being_modified;
NSInteger button_being_modified;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return GBButtonCount;
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if ([tableColumn.identifier isEqualToString:@"keyName"]) {
return GBButtonNames[row];
}
if (is_button_being_modified && button_being_modified == row) {
return @"Select a new key...";
}
return [NSString displayStringForKeyString:[[NSUserDefaults standardUserDefaults] stringForKey:
button_to_preference_name(row)]];
}
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
dispatch_async(dispatch_get_main_queue(), ^{
is_button_being_modified = true;
button_being_modified = row;
tableView.enabled = NO;
[tableView reloadData];
[self makeFirstResponder:self];
});
return NO;
}
-(void)keyDown:(NSEvent *)theEvent
{
if (!is_button_being_modified) {
if (self.firstResponder != self.controlsTableView) {
[super keyDown:theEvent];
}
return;
}
is_button_being_modified = false;
[[NSUserDefaults standardUserDefaults] setObject:theEvent.charactersIgnoringModifiers
forKey:button_to_preference_name(button_being_modified)];
self.controlsTableView.enabled = YES;
[self.controlsTableView reloadData];
[self makeFirstResponder:self.controlsTableView];
}
@end

View File

@ -1,6 +1,7 @@
#import <Carbon/Carbon.h>
#import <OpenGL/gl.h>
#import "GBView.h"
#import "GBButtons.h"
#import "NSString+StringForKey.h"
@implementation GBView
{
@ -76,78 +77,52 @@
-(void)keyDown:(NSEvent *)theEvent
{
unsigned short key = theEvent.keyCode;
switch (key) {
case kVK_RightArrow:
_gb->keys[0] = true;
break;
case kVK_LeftArrow:
_gb->keys[1] = true;
break;
case kVK_UpArrow:
_gb->keys[2] = true;
break;
case kVK_DownArrow:
_gb->keys[3] = true;
break;
case kVK_ANSI_X:
_gb->keys[4] = true;
break;
case kVK_ANSI_Z:
_gb->keys[5] = true;
break;
case kVK_Delete:
_gb->keys[6] = true;
break;
case kVK_Return:
_gb->keys[7] = true;
break;
case kVK_Space:
bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (GBButton i = 0; i < GBButtonCount; i++) {
if ([[defaults stringForKey:button_to_preference_name(i)] isEqualToString:theEvent.charactersIgnoringModifiers]) {
handled = true;
switch (i) {
case GBTurbo:
_gb->turbo = true;
break;
default:
[super keyDown:theEvent];
_gb->keys[i] = true;
break;
}
}
}
if (!handled) {
[super keyDown:theEvent];
}
}
-(void)keyUp:(NSEvent *)theEvent
{
unsigned short key = theEvent.keyCode;
switch (key) {
case kVK_RightArrow:
_gb->keys[0] = false;
break;
case kVK_LeftArrow:
_gb->keys[1] = false;
break;
case kVK_UpArrow:
_gb->keys[2] = false;
break;
case kVK_DownArrow:
_gb->keys[3] = false;
break;
case kVK_ANSI_X:
_gb->keys[4] = false;
break;
case kVK_ANSI_Z:
_gb->keys[5] = false;
break;
case kVK_Delete:
_gb->keys[6] = false;
break;
case kVK_Return:
_gb->keys[7] = false;
break;
case kVK_Space:
bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (GBButton i = 0; i < GBButtonCount; i++) {
if ([[defaults stringForKey:button_to_preference_name(i)] isEqualToString:theEvent.charactersIgnoringModifiers]) {
handled = true;
switch (i) {
case GBTurbo:
_gb->turbo = false;
break;
default:
[super keyUp:theEvent];
_gb->keys[i] = false;
break;
}
}
}
if (!handled) {
[super keyUp:theEvent];
}
}
-(void)reshape

View File

@ -26,7 +26,11 @@
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW">
<connections>
<action selector="showPreferences:" target="Voe-Tx-rLC" id="RcX-51-nzq"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
<menuItem title="Services" id="NMo-om-nkz">
<modifierMask key="keyEquivalentModifierMask"/>
@ -305,9 +309,6 @@
</menuItem>
</items>
</menu>
<connections>
<action selector="loadState:" target="-1" id="WyW-n5-YHf"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="5GS-tt-E0a"/>
<menuItem title="Gameboy" tag="1" id="vc7-yy-ARW">

View File

@ -0,0 +1,22 @@
#ifndef NSKeyboardShortcut_h
#define NSKeyboardShortcut_h
/* This is private API, but it is a very simple and comprehensive way
to convert a key equivalent to its display name. */
@interface NSKeyboardShortcut : NSObject <NSCopying>
+ (id)shortcutWithPreferencesEncoding:(NSString *)encoding;
+ (id)shortcutWithKeyEquivalent:(NSString *)key_equivalent modifierMask:(unsigned long long)mask;
- (id)initWithKeyEquivalent:(NSString *)key_equivalent modifierMask:(unsigned long long)mask;
@property(readonly) unsigned long long modifierMask;
@property(readonly) NSString *keyEquivalent;
@property(readonly) NSString *preferencesEncoding;
@property(readonly) NSString *localizedModifierMaskDisplayName;
@property(readonly) NSString *localizedKeyEquivalentDisplayName;
@property(readonly) NSString *localizedDisplayName;
@end
#endif

View File

@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>
@interface NSString (StringForKey)
+ (NSString *) displayStringForKeyString: (NSString *)key_string;
@end

View File

@ -0,0 +1,12 @@
#import "NSString+StringForKey.h"
#import "NSKeyboardShortcut.h"
@implementation NSString (StringForKey)
+ (NSString *) displayStringForKeyString: (NSString *)key_string
{
return [[NSKeyboardShortcut shortcutWithKeyEquivalent:key_string modifierMask:0] localizedDisplayName];
}
@end

97
Cocoa/Preferences.xib Normal file
View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="9531" systemVersion="14F1509" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9531"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="AppDelegate">
<connections>
<outlet property="preferencesWindow" destination="QvC-M9-y7g" id="kBg-fq-rZh"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g" customClass="GBPreferencesWindow">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="292" height="237"/>
<rect key="screenRect" x="0.0" y="0.0" width="2560" height="1417"/>
<view key="contentView" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="292" height="237"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Utu-t4-cLx">
<rect key="frame" x="18" y="201" width="256" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Button configuration:" id="YqW-Ds-VIC">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<scrollView focusRingType="none" fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" hasHorizontalScroller="NO" hasVerticalScroller="NO" usesPredominantAxisScrolling="NO" horizontalScrollElasticity="none" verticalScrollElasticity="none" translatesAutoresizingMaskIntoConstraints="NO" id="PBp-dj-EIa">
<rect key="frame" x="20" y="20" width="252" height="173"/>
<clipView key="contentView" focusRingType="none" ambiguous="YES" drawsBackground="NO" id="AMs-PO-nid">
<rect key="frame" x="1" y="1" width="250" height="171"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView focusRingType="none" appearanceType="vibrantLight" verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnReordering="NO" columnResizing="NO" multipleSelection="NO" emptySelection="NO" autosaveColumns="NO" typeSelect="NO" id="UDd-IJ-fxX">
<rect key="frame" x="0.0" y="0.0" width="250" height="171"/>
<autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn identifier="keyName" width="116" minWidth="40" maxWidth="1000" id="73A-gb-pzd">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" title="Text Cell" id="mqT-jD-eXS">
<font key="font" metaFont="system"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
</tableColumn>
<tableColumn width="128" minWidth="40" maxWidth="1000" id="5VG-zV-WM6">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" identifier="keyValue" title="Text Cell" id="tn8-0i-1q1">
<font key="font" metaFont="system"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
</tableColumn>
</tableColumns>
<connections>
<outlet property="dataSource" destination="QvC-M9-y7g" id="Msa-aU-MtO"/>
<outlet property="delegate" destination="QvC-M9-y7g" id="CfR-lh-CPe"/>
</connections>
</tableView>
</subviews>
<nil key="backgroundColor"/>
</clipView>
<scroller key="horizontalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="YES" id="31h-at-Znm">
<rect key="frame" x="-100" y="-100" width="209.5" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" verticalHuggingPriority="750" horizontal="NO" id="JkP-U1-jdy">
<rect key="frame" x="-100" y="-100" width="15" height="102"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
</subviews>
</view>
<connections>
<outlet property="controlsTableView" destination="UDd-IJ-fxX" id="a1D-Md-yXv"/>
<outlet property="delegate" destination="-2" id="ASc-vN-Zbq"/>
</connections>
<point key="canvasLocation" x="179" y="403.5"/>
</window>
</objects>
</document>

View File

@ -74,7 +74,8 @@ $(BIN)/Sameboy.app: $(BIN)/Sameboy.app/Contents/MacOS/Sameboy \
$(BIN)/BootROMs/dmg_boot.bin \
$(BIN)/BootROMs/cgb_boot.bin \
$(BIN)/Sameboy.app/Contents/Resources/Base.lproj/Document.nib \
$(BIN)/Sameboy.app/Contents/Resources/Base.lproj/MainMenu.nib
$(BIN)/Sameboy.app/Contents/Resources/Base.lproj/MainMenu.nib \
$(BIN)/Sameboy.app/Contents/Resources/Base.lproj/Preferences.nib
mkdir -p $(BIN)/Sameboy.app/Contents/Resources
cp Cocoa/*.icns $(BIN)/BootROMs/dmg_boot.bin $(BIN)/BootROMs/cgb_boot.bin $(BIN)/Sameboy.app/Contents/Resources/
sed s/@VERSION/$(VERSION)/ < Cocoa/info.plist > $(BIN)/Sameboy.app/Contents/info.plist