mirror of
https://github.com/TomHarte/CLK.git
synced 2025-08-09 20:25:19 +00:00
Switched the Objective-C code to using dynamic_cast alone to decide whether to post keyboard or joystick events.
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
#import "CSAtari2600.h"
|
||||
@@ -17,3 +16,5 @@
|
||||
#import "CSOpenGLView.h"
|
||||
#import "CSAudioQueue.h"
|
||||
#import "CSBestEffortUpdater.h"
|
||||
|
||||
#include "KeyCodes.h"
|
||||
|
@@ -187,48 +187,22 @@ class MachineDocument:
|
||||
}
|
||||
|
||||
// MARK: Input management
|
||||
fileprivate func withKeyboardMachine(_ action: (CSKeyboardMachine) -> ()) {
|
||||
if let keyboardMachine = self.machine as? CSKeyboardMachine {
|
||||
action(keyboardMachine)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func withJoystickMachine(_ action: (CSJoystickMachine) -> ()) {
|
||||
if let joystickMachine = self.machine as? CSJoystickMachine {
|
||||
action(joystickMachine)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func sendJoystickEvent(_ machine: CSJoystickMachine, keyCode: UInt16, isPressed: Bool) {
|
||||
switch keyCode {
|
||||
case 123: machine.setDirection(.left, onPad: 0, isPressed: isPressed)
|
||||
case 126: machine.setDirection(.up, onPad: 0, isPressed: isPressed)
|
||||
case 124: machine.setDirection(.right, onPad: 0, isPressed: isPressed)
|
||||
case 125: machine.setDirection(.down, onPad: 0, isPressed: isPressed)
|
||||
default: machine.setButtonAt(0, onPad: 0, isPressed: isPressed)
|
||||
}
|
||||
}
|
||||
|
||||
func windowDidResignKey(_ notification: Notification) {
|
||||
self.withKeyboardMachine { $0.clearAllKeys() }
|
||||
self.machine.clearAllKeys()
|
||||
}
|
||||
|
||||
func keyDown(_ event: NSEvent) {
|
||||
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: true) }
|
||||
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: true) }
|
||||
self.machine.setKey(event.keyCode, isPressed: true)
|
||||
}
|
||||
|
||||
func keyUp(_ event: NSEvent) {
|
||||
self.withKeyboardMachine { $0.setKey(event.keyCode, isPressed: false) }
|
||||
self.withJoystickMachine { sendJoystickEvent($0, keyCode: event.keyCode, isPressed: false) }
|
||||
self.machine.setKey(event.keyCode, isPressed: false)
|
||||
}
|
||||
|
||||
func flagsChanged(_ newModifiers: NSEvent) {
|
||||
self.withKeyboardMachine {
|
||||
$0.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
|
||||
$0.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
|
||||
$0.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
|
||||
$0.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
|
||||
}
|
||||
self.machine.setKey(VK_Shift, isPressed: newModifiers.modifierFlags.contains(.shift))
|
||||
self.machine.setKey(VK_Control, isPressed: newModifiers.modifierFlags.contains(.control))
|
||||
self.machine.setKey(VK_Command, isPressed: newModifiers.modifierFlags.contains(.command))
|
||||
self.machine.setKey(VK_Option, isPressed: newModifiers.modifierFlags.contains(.option))
|
||||
}
|
||||
}
|
||||
|
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// CSJoystickMachine.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 03/10/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
typedef NS_ENUM(NSInteger, CSJoystickDirection)
|
||||
{
|
||||
CSJoystickDirectionUp,
|
||||
CSJoystickDirectionDown,
|
||||
CSJoystickDirectionLeft,
|
||||
CSJoystickDirectionRight
|
||||
};
|
||||
|
||||
@protocol CSJoystickMachine <NSObject>
|
||||
|
||||
- (void)setButtonAtIndex:(NSUInteger)button onPad:(NSUInteger)pad isPressed:(BOOL)isPressed;
|
||||
- (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed;
|
||||
|
||||
@end
|
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// CSKeyboardMachine.h
|
||||
// Clock Signal
|
||||
//
|
||||
// Created by Thomas Harte on 05/06/2016.
|
||||
// Copyright © 2016 Thomas Harte. All rights reserved.
|
||||
//
|
||||
|
||||
#import "KeyCodes.h"
|
||||
|
||||
@protocol CSKeyboardMachine <NSObject>
|
||||
|
||||
- (void)setKey:(uint16_t)key isPressed:(BOOL)isPressed;
|
||||
- (void)clearAllKeys;
|
||||
|
||||
@end
|
@@ -35,6 +35,9 @@
|
||||
- (void)setView:(CSOpenGLView *)view aspectRatio:(float)aspectRatio;
|
||||
- (void)drawViewForPixelSize:(CGSize)pixelSize onlyIfDirty:(BOOL)onlyIfDirty;
|
||||
|
||||
- (void)setKey:(uint16_t)key isPressed:(BOOL)isPressed;
|
||||
- (void)clearAllKeys;
|
||||
|
||||
@property (nonatomic, strong) CSAudioQueue *audioQueue;
|
||||
@property (nonatomic, readonly) CSOpenGLView *view;
|
||||
@property (nonatomic, weak) id<CSMachineDelegate> delegate;
|
||||
@@ -46,9 +49,4 @@
|
||||
|
||||
- (void)paste:(NSString *)string;
|
||||
|
||||
// This is an informal implementation of the CSKeyboardMachine protocol; specific machines
|
||||
// can decide whether they opt in as keyboard machines.
|
||||
- (void)setKey:(uint16_t)key isPressed:(BOOL)isPressed;
|
||||
- (void)clearAllKeys;
|
||||
|
||||
@end
|
||||
|
@@ -10,10 +10,11 @@
|
||||
#import "CSMachine+Subclassing.h"
|
||||
#import "CSMachine+Target.h"
|
||||
|
||||
#import "CSKeyboardMachine.h"
|
||||
|
||||
#include "KeyCodes.h"
|
||||
#include "Typer.hpp"
|
||||
#include "ConfigurationTarget.hpp"
|
||||
#include "JoystickMachine.hpp"
|
||||
#include "KeyboardMachine.hpp"
|
||||
|
||||
@interface CSMachine()
|
||||
- (void)speaker:(Outputs::Speaker *)speaker didCompleteSamples:(const int16_t *)samples length:(int)length;
|
||||
@@ -187,8 +188,7 @@ struct MachineDelegate: CRTMachine::Machine::Delegate, public LockProtectedDeleg
|
||||
|
||||
- (void)setKey:(uint16_t)key isPressed:(BOOL)isPressed {
|
||||
auto keyboard_machine = dynamic_cast<KeyboardMachine::Machine *>(_machine);
|
||||
if(!keyboard_machine) return;
|
||||
|
||||
if(keyboard_machine) {
|
||||
@synchronized(self) {
|
||||
Inputs::Keyboard &keyboard = keyboard_machine->get_keyboard();
|
||||
|
||||
@@ -248,15 +248,44 @@ struct MachineDelegate: CRTMachine::Machine::Delegate, public LockProtectedDeleg
|
||||
}
|
||||
#undef BIND
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
auto joystick_machine = dynamic_cast<JoystickMachine::Machine *>(_machine);
|
||||
if(joystick_machine) {
|
||||
@synchronized(self) {
|
||||
std::vector<std::unique_ptr<Inputs::Joystick>> &joysticks = joystick_machine->get_joysticks();
|
||||
if(!joysticks.empty()) {
|
||||
switch(key) {
|
||||
case VK_LeftArrow: joysticks[0]->set_digital_input(Inputs::Joystick::DigitalInput::Left, isPressed); break;
|
||||
case VK_RightArrow: joysticks[0]->set_digital_input(Inputs::Joystick::DigitalInput::Right, isPressed); break;
|
||||
case VK_UpArrow: joysticks[0]->set_digital_input(Inputs::Joystick::DigitalInput::Up, isPressed); break;
|
||||
case VK_DownArrow: joysticks[0]->set_digital_input(Inputs::Joystick::DigitalInput::Down, isPressed); break;
|
||||
default:
|
||||
joysticks[0]->set_digital_input(Inputs::Joystick::DigitalInput::Fire, isPressed); break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clearAllKeys {
|
||||
auto keyboard_machine = dynamic_cast<KeyboardMachine::Machine *>(_machine);
|
||||
if(!keyboard_machine) return;
|
||||
|
||||
if(keyboard_machine) {
|
||||
@synchronized(self) {
|
||||
keyboard_machine->get_keyboard().reset_all_keys();
|
||||
}
|
||||
}
|
||||
|
||||
auto joystick_machine = dynamic_cast<JoystickMachine::Machine *>(_machine);
|
||||
if(joystick_machine) {
|
||||
@synchronized(self) {
|
||||
for(auto &joystick : joystick_machine->get_joysticks()) {
|
||||
joystick->reset_all_inputs();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
@@ -7,9 +7,8 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
|
||||
@interface CSAmstradCPC : CSMachine <CSKeyboardMachine>
|
||||
@interface CSAmstradCPC : CSMachine
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
@@ -8,9 +8,8 @@
|
||||
|
||||
#include "CSMachine.h"
|
||||
#include "Atari2600Inputs.h"
|
||||
#import "CSJoystickMachine.h"
|
||||
|
||||
@interface CSAtari2600 : CSMachine <CSJoystickMachine>
|
||||
@interface CSAtari2600 : CSMachine
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
@@ -24,26 +24,6 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setDirection:(CSJoystickDirection)direction onPad:(NSUInteger)pad isPressed:(BOOL)isPressed {
|
||||
// Atari2600DigitalInput input;
|
||||
// switch(direction)
|
||||
// {
|
||||
// case CSJoystickDirectionUp: input = pad ? Atari2600DigitalInputJoy2Up : Atari2600DigitalInputJoy1Up; break;
|
||||
// case CSJoystickDirectionDown: input = pad ? Atari2600DigitalInputJoy2Down : Atari2600DigitalInputJoy1Down; break;
|
||||
// case CSJoystickDirectionLeft: input = pad ? Atari2600DigitalInputJoy2Left : Atari2600DigitalInputJoy1Left; break;
|
||||
// case CSJoystickDirectionRight: input = pad ? Atari2600DigitalInputJoy2Right : Atari2600DigitalInputJoy1Right; break;
|
||||
// }
|
||||
// @synchronized(self) {
|
||||
// _atari2600->set_digital_input(input, isPressed ? true : false);
|
||||
// }
|
||||
}
|
||||
|
||||
- (void)setButtonAtIndex:(NSUInteger)button onPad:(NSUInteger)pad isPressed:(BOOL)isPressed {
|
||||
// @synchronized(self) {
|
||||
// _atari2600->set_digital_input(pad ? Atari2600DigitalInputJoy2Fire : Atari2600DigitalInputJoy1Fire, isPressed ? true : false);
|
||||
// }
|
||||
}
|
||||
|
||||
- (void)setResetLineEnabled:(BOOL)enabled {
|
||||
@synchronized(self) {
|
||||
_atari2600->set_reset_switch(enabled ? true : false);
|
||||
|
@@ -7,10 +7,9 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
@interface CSElectron : CSMachine <CSKeyboardMachine, CSFastLoading>
|
||||
@interface CSElectron : CSMachine <CSFastLoading>
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
@@ -7,10 +7,9 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
@interface CSOric : CSMachine <CSKeyboardMachine, CSFastLoading>
|
||||
@interface CSOric : CSMachine <CSFastLoading>
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
@@ -7,7 +7,6 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
typedef NS_ENUM(NSInteger, CSVic20Country)
|
||||
@@ -26,7 +25,7 @@ typedef NS_ENUM(NSInteger, CSVic20MemorySize)
|
||||
CSVic20MemorySize32Kb,
|
||||
};
|
||||
|
||||
@interface CSVic20 : CSMachine <CSKeyboardMachine, CSFastLoading>
|
||||
@interface CSVic20 : CSMachine <CSFastLoading>
|
||||
|
||||
- (instancetype)init;
|
||||
|
||||
|
@@ -7,10 +7,9 @@
|
||||
//
|
||||
|
||||
#import "CSMachine.h"
|
||||
#import "CSKeyboardMachine.h"
|
||||
#import "CSFastLoading.h"
|
||||
|
||||
@interface CSZX8081 : CSMachine <CSKeyboardMachine, CSFastLoading>
|
||||
@interface CSZX8081 : CSMachine <CSFastLoading>
|
||||
|
||||
@property (nonatomic, assign) BOOL useFastLoadingHack;
|
||||
|
||||
|
Reference in New Issue
Block a user