SameBoy/Cocoa/GBView.m

785 lines
26 KiB
Mathematica
Raw Permalink Normal View History

#import <IOKit/pwr_mgt/IOPMLib.h>
2020-09-19 16:31:24 +00:00
#import <Carbon/Carbon.h>
2016-03-30 20:07:55 +00:00
#import "GBView.h"
#import "GBViewGL.h"
#import "GBViewMetal.h"
2016-04-13 19:43:16 +00:00
#import "GBButtons.h"
#import "NSString+StringForKey.h"
#import "Document.h"
2016-03-30 20:07:55 +00:00
#define JOYSTICK_HIGH 0x4000
#define JOYSTICK_LOW 0x3800
2020-09-19 16:31:24 +00:00
static const uint8_t workboy_ascii_to_key[] = {
['0'] = GB_WORKBOY_0,
['`'] = GB_WORKBOY_UMLAUT,
['1'] = GB_WORKBOY_1,
['2'] = GB_WORKBOY_2,
['3'] = GB_WORKBOY_3,
['4'] = GB_WORKBOY_4,
['5'] = GB_WORKBOY_5,
['6'] = GB_WORKBOY_6,
['7'] = GB_WORKBOY_7,
['8'] = GB_WORKBOY_8,
['9'] = GB_WORKBOY_9,
['\r'] = GB_WORKBOY_ENTER,
[3] = GB_WORKBOY_ENTER,
['!'] = GB_WORKBOY_EXCLAMATION_MARK,
['$'] = GB_WORKBOY_DOLLAR,
['#'] = GB_WORKBOY_HASH,
['~'] = GB_WORKBOY_TILDE,
['*'] = GB_WORKBOY_ASTERISK,
['+'] = GB_WORKBOY_PLUS,
['-'] = GB_WORKBOY_MINUS,
['('] = GB_WORKBOY_LEFT_PARENTHESIS,
[')'] = GB_WORKBOY_RIGHT_PARENTHESIS,
[';'] = GB_WORKBOY_SEMICOLON,
[':'] = GB_WORKBOY_COLON,
['%'] = GB_WORKBOY_PERCENT,
['='] = GB_WORKBOY_EQUAL,
[','] = GB_WORKBOY_COMMA,
['<'] = GB_WORKBOY_LT,
['.'] = GB_WORKBOY_DOT,
['>'] = GB_WORKBOY_GT,
['/'] = GB_WORKBOY_SLASH,
['?'] = GB_WORKBOY_QUESTION_MARK,
[' '] = GB_WORKBOY_SPACE,
['\''] = GB_WORKBOY_QUOTE,
['@'] = GB_WORKBOY_AT,
['q'] = GB_WORKBOY_Q,
['w'] = GB_WORKBOY_W,
['e'] = GB_WORKBOY_E,
['r'] = GB_WORKBOY_R,
['t'] = GB_WORKBOY_T,
['y'] = GB_WORKBOY_Y,
['u'] = GB_WORKBOY_U,
['i'] = GB_WORKBOY_I,
['o'] = GB_WORKBOY_O,
['p'] = GB_WORKBOY_P,
['a'] = GB_WORKBOY_A,
['s'] = GB_WORKBOY_S,
['d'] = GB_WORKBOY_D,
['f'] = GB_WORKBOY_F,
['g'] = GB_WORKBOY_G,
['h'] = GB_WORKBOY_H,
['j'] = GB_WORKBOY_J,
['k'] = GB_WORKBOY_K,
['l'] = GB_WORKBOY_L,
['z'] = GB_WORKBOY_Z,
['x'] = GB_WORKBOY_X,
['c'] = GB_WORKBOY_C,
['v'] = GB_WORKBOY_V,
['b'] = GB_WORKBOY_B,
['n'] = GB_WORKBOY_N,
['m'] = GB_WORKBOY_M,
};
static const uint8_t workboy_vk_to_key[] = {
[kVK_F1] = GB_WORKBOY_CLOCK,
[kVK_F2] = GB_WORKBOY_TEMPERATURE,
[kVK_F3] = GB_WORKBOY_MONEY,
[kVK_F4] = GB_WORKBOY_CALCULATOR,
[kVK_F5] = GB_WORKBOY_DATE,
[kVK_F6] = GB_WORKBOY_CONVERSION,
[kVK_F7] = GB_WORKBOY_RECORD,
[kVK_F8] = GB_WORKBOY_WORLD,
[kVK_F9] = GB_WORKBOY_PHONE,
[kVK_F10] = GB_WORKBOY_UNKNOWN,
[kVK_Delete] = GB_WORKBOY_BACKSPACE,
[kVK_Shift] = GB_WORKBOY_SHIFT_DOWN,
[kVK_RightShift] = GB_WORKBOY_SHIFT_DOWN,
[kVK_UpArrow] = GB_WORKBOY_UP,
[kVK_DownArrow] = GB_WORKBOY_DOWN,
[kVK_LeftArrow] = GB_WORKBOY_LEFT,
[kVK_RightArrow] = GB_WORKBOY_RIGHT,
[kVK_Escape] = GB_WORKBOY_ESCAPE,
[kVK_ANSI_KeypadDecimal] = GB_WORKBOY_DECIMAL_POINT,
[kVK_ANSI_KeypadClear] = GB_WORKBOY_M,
[kVK_ANSI_KeypadMultiply] = GB_WORKBOY_H,
[kVK_ANSI_KeypadDivide] = GB_WORKBOY_J,
};
2016-03-30 20:07:55 +00:00
@implementation GBView
{
uint32_t *image_buffers[3];
unsigned char current_buffer;
2021-10-23 10:36:58 +00:00
bool mouse_hidden;
2016-07-05 20:34:33 +00:00
NSTrackingArea *tracking_area;
2021-10-23 10:36:58 +00:00
bool _mouseHidingEnabled;
bool axisActive[2];
bool underclockKeyDown;
double clockMultiplier;
double analogClockMultiplier;
bool analogClockMultiplierValid;
NSEventModifierFlags previousModifiers;
JOYController *lastController;
2020-03-26 18:54:18 +00:00
GB_frame_blending_mode_t _frameBlendingMode;
2021-05-30 17:55:04 +00:00
bool _turbo;
2021-11-14 19:27:12 +00:00
bool _mouseControlEnabled;
2016-03-30 20:07:55 +00:00
}
+ (instancetype)alloc
{
return [self allocWithZone:NULL];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (self == [GBView class]) {
2018-06-15 17:03:59 +00:00
if ([GBViewMetal isSupported]) {
return [GBViewMetal allocWithZone: zone];
}
return [GBViewGL allocWithZone: zone];
}
return [super allocWithZone:zone];
}
- (void) createInternalView
{
assert(false && "createInternalView must not be inherited");
}
2016-03-30 20:07:55 +00:00
- (void) _init
2020-03-26 18:54:18 +00:00
{
2021-04-14 12:24:06 +00:00
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
2021-04-14 12:20:01 +00:00
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
2016-07-05 20:34:33 +00:00
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
2021-11-14 19:27:12 +00:00
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseMoved
2016-07-05 20:34:33 +00:00
owner:self
userInfo:nil];
[self addTrackingArea:tracking_area];
clockMultiplier = 1.0;
[self createInternalView];
[self addSubview:self.internalView];
self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[JOYController registerListener:self];
2021-11-14 19:27:12 +00:00
_mouseControlEnabled = true;
2016-03-30 20:07:55 +00:00
}
- (void)screenSizeChanged
{
if (image_buffers[0]) free(image_buffers[0]);
if (image_buffers[1]) free(image_buffers[1]);
if (image_buffers[2]) free(image_buffers[2]);
size_t buffer_size = sizeof(image_buffers[0][0]) * GB_get_screen_width(_gb) * GB_get_screen_height(_gb);
image_buffers[0] = calloc(1, buffer_size);
image_buffers[1] = calloc(1, buffer_size);
image_buffers[2] = calloc(1, buffer_size);
dispatch_async(dispatch_get_main_queue(), ^{
[self setFrame:self.superview.frame];
});
}
- (void) ratioKeepingChanged
{
[self setFrame:self.superview.frame];
}
2020-03-26 18:54:18 +00:00
- (void) setFrameBlendingMode:(GB_frame_blending_mode_t)frameBlendingMode
{
2020-03-26 18:54:18 +00:00
_frameBlendingMode = frameBlendingMode;
2021-10-23 10:36:58 +00:00
[self setNeedsDisplay:true];
}
2020-03-26 18:54:18 +00:00
- (GB_frame_blending_mode_t)frameBlendingMode
{
if (_frameBlendingMode == GB_FRAME_BLENDING_MODE_ACCURATE) {
2020-03-27 14:22:50 +00:00
if (!_gb || GB_is_sgb(_gb)) {
2020-03-26 18:54:18 +00:00
return GB_FRAME_BLENDING_MODE_SIMPLE;
}
return GB_is_odd_frame(_gb)? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN;
}
return _frameBlendingMode;
}
2016-03-30 20:07:55 +00:00
- (unsigned char) numberOfBuffers
{
2020-03-26 18:54:18 +00:00
return _frameBlendingMode? 3 : 2;
2016-03-30 20:07:55 +00:00
}
- (void)dealloc
{
free(image_buffers[0]);
free(image_buffers[1]);
free(image_buffers[2]);
2016-07-05 20:34:33 +00:00
if (mouse_hidden) {
mouse_hidden = false;
[NSCursor unhide];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self setRumble:0];
[JOYController unregisterListener:self];
2016-03-30 20:07:55 +00:00
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
2020-04-24 17:37:57 +00:00
if (!(self = [super initWithCoder:coder])) {
2016-03-30 20:07:55 +00:00
return self;
}
[self _init];
return self;
}
- (instancetype)initWithFrame:(NSRect)frameRect
{
2020-04-24 17:37:57 +00:00
if (!(self = [super initWithFrame:frameRect])) {
2016-03-30 20:07:55 +00:00
return self;
}
[self _init];
return self;
}
2016-07-03 17:58:25 +00:00
- (void)setFrame:(NSRect)frame
{
frame = self.superview.frame;
2018-11-23 17:59:15 +00:00
if (_gb && ![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]) {
double ratio = frame.size.width / frame.size.height;
double width = GB_get_screen_width(_gb);
double height = GB_get_screen_height(_gb);
if (ratio >= width / height) {
double new_width = round(frame.size.height / height * width);
frame.origin.x = floor((frame.size.width - new_width) / 2);
frame.size.width = new_width;
frame.origin.y = 0;
}
else {
double new_height = round(frame.size.width / width * height);
frame.origin.y = floor((frame.size.height - new_height) / 2);
frame.size.height = new_height;
frame.origin.x = 0;
}
2016-07-03 17:58:25 +00:00
}
2016-07-05 20:34:33 +00:00
2016-07-03 17:58:25 +00:00
[super setFrame:frame];
}
2016-03-30 20:07:55 +00:00
- (void) flip
{
if (analogClockMultiplierValid && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBAnalogControls"]) {
clockMultiplier = 1.0;
GB_set_clock_multiplier(_gb, analogClockMultiplier);
if (self.document.partner) {
GB_set_clock_multiplier(self.document.partner.gb, analogClockMultiplier);
}
if (analogClockMultiplier == 1.0) {
analogClockMultiplierValid = false;
}
if (analogClockMultiplier < 2.0 && analogClockMultiplier > 1.0) {
GB_set_turbo_mode(_gb, false, false);
if (self.document.partner) {
GB_set_turbo_mode(self.document.partner.gb, false, false);
}
}
}
else {
if (underclockKeyDown && clockMultiplier > 0.5) {
clockMultiplier -= 1.0/16;
GB_set_clock_multiplier(_gb, clockMultiplier);
if (self.document.partner) {
GB_set_clock_multiplier(self.document.partner.gb, clockMultiplier);
}
}
if (!underclockKeyDown && clockMultiplier < 1.0) {
clockMultiplier += 1.0/16;
GB_set_clock_multiplier(_gb, clockMultiplier);
if (self.document.partner) {
GB_set_clock_multiplier(self.document.partner.gb, clockMultiplier);
}
}
}
if ((!analogClockMultiplierValid && clockMultiplier > 1) ||
_turbo || (analogClockMultiplierValid && analogClockMultiplier > 1)) {
2021-05-30 17:55:04 +00:00
[self.osdView displayText:@"Fast forwarding..."];
}
else if ((!analogClockMultiplierValid && clockMultiplier < 1) ||
(analogClockMultiplierValid && analogClockMultiplier < 1)) {
2021-05-30 17:55:04 +00:00
[self.osdView displayText:@"Slow motion..."];
}
2016-03-30 20:07:55 +00:00
current_buffer = (current_buffer + 1) % self.numberOfBuffers;
}
- (uint32_t *) pixels
{
return image_buffers[(current_buffer + 1) % self.numberOfBuffers];
}
-(void)keyDown:(NSEvent *)theEvent
{
2020-09-19 16:31:24 +00:00
if ([theEvent type] != NSEventTypeFlagsChanged && theEvent.isARepeat) return;
unsigned short keyCode = theEvent.keyCode;
2020-09-19 16:31:24 +00:00
if (GB_workboy_is_enabled(_gb)) {
if (theEvent.keyCode < sizeof(workboy_vk_to_key) && workboy_vk_to_key[theEvent.keyCode]) {
GB_workboy_set_key(_gb, workboy_vk_to_key[theEvent.keyCode]);
return;
}
unichar c = [theEvent type] != NSEventTypeFlagsChanged? [theEvent.charactersIgnoringModifiers.lowercaseString characterAtIndex:0] : 0;
if (c < sizeof(workboy_ascii_to_key) && workboy_ascii_to_key[c]) {
GB_workboy_set_key(_gb, workboy_ascii_to_key[c]);
return;
}
}
2016-04-13 19:43:16 +00:00
bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2018-12-15 16:55:41 +00:00
unsigned player_count = GB_get_player_count(_gb);
if (self.document.partner) {
player_count = 2;
}
for (unsigned player = 0; player < player_count; player++) {
for (GBButton button = 0; button < GBButtonCount; button++) {
NSNumber *key = [defaults valueForKey:button_to_preference_name(button, player)];
if (!key) continue;
if (key.unsignedShortValue == keyCode) {
handled = true;
switch (button) {
case GBTurbo:
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, true, false);
}
else {
GB_set_turbo_mode(_gb, true, self.isRewinding);
}
2021-05-30 17:55:04 +00:00
_turbo = true;
analogClockMultiplierValid = false;
break;
case GBRewind:
if (!self.document.partner) {
self.isRewinding = true;
GB_set_turbo_mode(_gb, false, false);
2021-05-30 17:55:04 +00:00
_turbo = false;
}
break;
case GBUnderclock:
underclockKeyDown = true;
analogClockMultiplierValid = false;
break;
default:
if (self.document.partner) {
if (player == 0) {
GB_set_key_state_for_player(_gb, (GB_key_t)button, 0, true);
}
else {
GB_set_key_state_for_player(self.document.partner.gb, (GB_key_t)button, 0, true);
}
}
else {
GB_set_key_state_for_player(_gb, (GB_key_t)button, player, true);
}
break;
}
2016-04-13 19:43:16 +00:00
}
}
}
2018-03-02 17:37:40 +00:00
if (!handled && [theEvent type] != NSEventTypeFlagsChanged) {
2016-04-13 19:43:16 +00:00
[super keyDown:theEvent];
2016-03-30 20:07:55 +00:00
}
}
-(void)keyUp:(NSEvent *)theEvent
{
unsigned short keyCode = theEvent.keyCode;
2020-09-19 16:31:24 +00:00
if (GB_workboy_is_enabled(_gb)) {
if (keyCode == kVK_Shift || keyCode == kVK_RightShift) {
GB_workboy_set_key(_gb, GB_WORKBOY_SHIFT_UP);
}
else {
GB_workboy_set_key(_gb, GB_WORKBOY_NONE);
}
}
2016-04-13 19:43:16 +00:00
bool handled = false;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2018-12-15 16:55:41 +00:00
unsigned player_count = GB_get_player_count(_gb);
if (self.document.partner) {
player_count = 2;
}
for (unsigned player = 0; player < player_count; player++) {
for (GBButton button = 0; button < GBButtonCount; button++) {
NSNumber *key = [defaults valueForKey:button_to_preference_name(button, player)];
if (!key) continue;
if (key.unsignedShortValue == keyCode) {
handled = true;
switch (button) {
case GBTurbo:
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, false, false);
}
else {
GB_set_turbo_mode(_gb, false, false);
}
2021-05-30 17:55:04 +00:00
_turbo = false;
analogClockMultiplierValid = false;
break;
case GBRewind:
self.isRewinding = false;
break;
case GBUnderclock:
underclockKeyDown = false;
analogClockMultiplierValid = false;
break;
default:
if (self.document.partner) {
if (player == 0) {
GB_set_key_state_for_player(_gb, (GB_key_t)button, 0, false);
}
else {
GB_set_key_state_for_player(self.document.partner.gb, (GB_key_t)button, 0, false);
}
}
else {
GB_set_key_state_for_player(_gb, (GB_key_t)button, player, false);
}
break;
}
2016-04-13 19:43:16 +00:00
}
}
}
2018-03-02 17:37:40 +00:00
if (!handled && [theEvent type] != NSEventTypeFlagsChanged) {
2016-04-13 19:43:16 +00:00
[super keyUp:theEvent];
2016-03-30 20:07:55 +00:00
}
}
2020-04-29 13:05:31 +00:00
- (void)setRumble:(double)amp
2017-12-30 14:23:17 +00:00
{
[lastController setRumbleAmplitude:amp];
}
2018-12-15 16:55:41 +00:00
2021-11-12 16:10:03 +00:00
- (bool)shouldControllerUseJoystickForMotion:(JOYController *)controller
{
2021-11-14 19:27:12 +00:00
if (!_gb) return false;
2021-11-12 16:10:03 +00:00
if (!GB_has_accelerometer(_gb)) return false;
2021-11-14 19:27:12 +00:00
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7JoystickOverride"]) return true;
for (JOYAxes3D *axes in controller.axes3D) {
if (axes.usage == JOYAxes3DUsageOrientation || axes.usage == JOYAxes3DUsageAcceleration) {
return false;
}
}
2021-11-12 16:10:03 +00:00
return true;
}
- (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis
{
2021-11-14 19:27:12 +00:00
if (!_gb) return;
if (![self.window isMainWindow]) return;
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID];
if (!mapping) {
mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName];
}
if ((axis.usage == JOYAxisUsageR1 && !mapping) ||
axis.uniqueID == [mapping[@"AnalogUnderclock"] unsignedLongValue]){
analogClockMultiplier = MIN(MAX(1 - axis.value + 0.05, 1.0 / 3), 1.0);
analogClockMultiplierValid = true;
}
else if ((axis.usage == JOYAxisUsageL1 && !mapping) ||
axis.uniqueID == [mapping[@"AnalogTurbo"] unsignedLongValue]){
analogClockMultiplier = MIN(MAX(axis.value * 3 + 0.95, 1.0), 3.0);
analogClockMultiplierValid = true;
2017-12-30 14:23:17 +00:00
}
}
2021-11-12 16:10:03 +00:00
- (void)controller:(JOYController *)controller movedAxes2D:(JOYAxes2D *)axes
{
2021-11-14 19:27:12 +00:00
if (!_gb) return;
2021-11-12 16:10:03 +00:00
if ([self shouldControllerUseJoystickForMotion:controller]) {
2021-11-14 19:27:12 +00:00
if (!self.mouseControlsActive) {
GB_set_accelerometer_values(_gb, -axes.value.x, -axes.value.y);
}
2021-11-12 16:10:03 +00:00
}
}
- (void)controller:(JOYController *)controller movedAxes3D:(JOYAxes3D *)axes
{
2021-11-14 19:27:12 +00:00
if (!_gb) return;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7JoystickOverride"]) return;
if (self.mouseControlsActive) return;
if (axes.usage == JOYAxes3DUsageOrientation) {
for (JOYAxes3D *axes in controller.axes3D) {
// Only use orientation if there's no acceleration axes
if (axes.usage == JOYAxes3DUsageAcceleration) {
return;
}
}
JOYPoint3D point = axes.normalizedValue;
GB_set_accelerometer_values(_gb, point.x, point.z);
}
else if (axes.usage == JOYAxes3DUsageAcceleration) {
JOYPoint3D point = axes.gUnitsValue;
GB_set_accelerometer_values(_gb, point.x, point.z);
}
}
- (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button
2017-12-30 14:23:17 +00:00
{
2021-11-14 19:27:12 +00:00
if (!_gb) return;
if (![self.window isMainWindow]) return;
2021-11-14 19:27:12 +00:00
_mouseControlEnabled = false;
2021-11-12 16:10:03 +00:00
if (button.type == JOYButtonTypeAxes2DEmulated && [self shouldControllerUseJoystickForMotion:controller]) return;
2018-12-15 16:55:41 +00:00
unsigned player_count = GB_get_player_count(_gb);
if (self.document.partner) {
player_count = 2;
}
2018-12-15 16:55:41 +00:00
IOPMAssertionID assertionID;
IOPMAssertionDeclareUserActivity(CFSTR(""), kIOPMUserActiveLocal, &assertionID);
2018-12-15 16:55:41 +00:00
for (unsigned player = 0; player < player_count; player++) {
NSString *preferred_joypad = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitDefaultControllers"]
objectForKey:n2s(player)];
2018-12-15 16:55:41 +00:00
if (player_count != 1 && // Single player, accpet inputs from all joypads
!(player == 0 && !preferred_joypad) && // Multiplayer, but player 1 has no joypad configured, so it takes inputs from all joypads
![preferred_joypad isEqualToString:controller.uniqueID]) {
2018-12-15 16:55:41 +00:00
continue;
}
dispatch_async(dispatch_get_main_queue(), ^{
[controller setPlayerLEDs:[controller LEDMaskForPlayer:player]];
});
NSDictionary *mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitInstanceMapping"][controller.uniqueID];
if (!mapping) {
mapping = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"JoyKitNameMapping"][controller.deviceName];
}
2018-12-15 16:55:41 +00:00
JOYButtonUsage usage = ((JOYButtonUsage)[mapping[n2s(button.uniqueID)] unsignedIntValue]) ?: button.usage;
if (!mapping && usage >= JOYButtonUsageGeneric0) {
usage = (const JOYButtonUsage[]){JOYButtonUsageY, JOYButtonUsageA, JOYButtonUsageB, JOYButtonUsageX}[(usage - JOYButtonUsageGeneric0) & 3];
}
GB_gameboy_t *effectiveGB = _gb;
unsigned effectivePlayer = player;
if (player && self.document.partner) {
effectiveGB = self.document.partner.gb;
effectivePlayer = 0;
if (controller != self.document.partner.view->lastController) {
[self setRumble:0];
self.document.partner.view->lastController = controller;
}
}
else {
if (controller != lastController) {
[self setRumble:0];
lastController = controller;
}
}
switch (usage) {
case JOYButtonUsageNone: break;
case JOYButtonUsageA: GB_set_key_state_for_player(effectiveGB, GB_KEY_A, effectivePlayer, button.isPressed); break;
case JOYButtonUsageB: GB_set_key_state_for_player(effectiveGB, GB_KEY_B, effectivePlayer, button.isPressed); break;
case JOYButtonUsageC: break;
case JOYButtonUsageStart:
case JOYButtonUsageX: GB_set_key_state_for_player(effectiveGB, GB_KEY_START, effectivePlayer, button.isPressed); break;
case JOYButtonUsageSelect:
case JOYButtonUsageY: GB_set_key_state_for_player(effectiveGB, GB_KEY_SELECT, effectivePlayer, button.isPressed); break;
case JOYButtonUsageR2:
case JOYButtonUsageL2:
case JOYButtonUsageZ: {
self.isRewinding = button.isPressed;
if (button.isPressed) {
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, false, false);
}
else {
GB_set_turbo_mode(_gb, false, false);
}
_turbo = false;
}
break;
2018-12-15 16:55:41 +00:00
}
case JOYButtonUsageL1: {
if (!analogClockMultiplierValid || analogClockMultiplier == 1.0 || !button.isPressed) {
if (self.document.isSlave) {
GB_set_turbo_mode(self.document.partner.gb, button.isPressed, false);
}
else {
GB_set_turbo_mode(_gb, button.isPressed, button.isPressed && self.isRewinding);
}
_turbo = button.isPressed;
}
break;
}
2017-12-30 14:23:17 +00:00
case JOYButtonUsageR1: underclockKeyDown = button.isPressed; break;
case JOYButtonUsageDPadLeft: GB_set_key_state_for_player(effectiveGB, GB_KEY_LEFT, effectivePlayer, button.isPressed); break;
case JOYButtonUsageDPadRight: GB_set_key_state_for_player(effectiveGB, GB_KEY_RIGHT, effectivePlayer, button.isPressed); break;
case JOYButtonUsageDPadUp: GB_set_key_state_for_player(effectiveGB, GB_KEY_UP, effectivePlayer, button.isPressed); break;
case JOYButtonUsageDPadDown: GB_set_key_state_for_player(effectiveGB, GB_KEY_DOWN, effectivePlayer, button.isPressed); break;
default:
break;
2019-06-14 15:06:15 +00:00
}
}
}
2017-12-30 14:23:17 +00:00
2016-03-30 20:07:55 +00:00
- (BOOL)acceptsFirstResponder
{
2021-10-23 10:36:58 +00:00
return true;
2016-03-30 20:07:55 +00:00
}
2016-07-05 20:34:33 +00:00
2021-11-14 19:27:12 +00:00
- (bool)mouseControlsActive
{
return _gb && GB_is_inited(_gb) && GB_has_accelerometer(_gb) &&
_mouseControlEnabled && [[NSUserDefaults standardUserDefaults] boolForKey:@"GBMBC7AllowMouse"];
}
2016-07-05 20:34:33 +00:00
- (void)mouseEntered:(NSEvent *)theEvent
{
if (!mouse_hidden) {
mouse_hidden = true;
2021-11-14 19:27:12 +00:00
if (_mouseHidingEnabled &&
!self.mouseControlsActive) {
2016-07-05 20:34:33 +00:00
[NSCursor hide];
}
}
[super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent *)theEvent
{
if (mouse_hidden) {
mouse_hidden = false;
if (_mouseHidingEnabled) {
[NSCursor unhide];
}
}
[super mouseExited:theEvent];
}
2021-11-14 19:27:12 +00:00
- (void)mouseDown:(NSEvent *)event
{
_mouseControlEnabled = true;
if (self.mouseControlsActive) {
if (event.type == NSEventTypeLeftMouseDown) {
GB_set_key_state(_gb, GB_KEY_A, true);
}
}
}
- (void)mouseUp:(NSEvent *)event
{
if (self.mouseControlsActive) {
if (event.type == NSEventTypeLeftMouseUp) {
GB_set_key_state(_gb, GB_KEY_A, false);
}
}
}
- (void)mouseMoved:(NSEvent *)event
{
if (self.mouseControlsActive) {
NSPoint point = [self convertPoint:[event locationInWindow] toView:nil];
point.x /= self.frame.size.width;
point.x *= 2;
point.x -= 1;
point.y /= self.frame.size.height;
point.y *= 2;
point.y -= 1;
if (GB_get_screen_width(_gb) != 160) { // has border
point.x *= 256 / 160.0;
point.y *= 224 / 114.0;
}
GB_set_accelerometer_values(_gb, -point.x, point.y);
}
}
2021-10-23 10:36:58 +00:00
- (void)setMouseHidingEnabled:(bool)mouseHidingEnabled
2016-07-05 20:34:33 +00:00
{
if (mouseHidingEnabled == _mouseHidingEnabled) return;
_mouseHidingEnabled = mouseHidingEnabled;
if (mouse_hidden && _mouseHidingEnabled) {
[NSCursor hide];
}
if (mouse_hidden && !_mouseHidingEnabled) {
[NSCursor unhide];
}
}
2021-10-23 10:36:58 +00:00
- (bool)isMouseHidingEnabled
2016-07-05 20:34:33 +00:00
{
return _mouseHidingEnabled;
}
2017-12-30 14:23:17 +00:00
- (void) flagsChanged:(NSEvent *)event
{
if (event.modifierFlags > previousModifiers) {
[self keyDown:event];
}
else {
[self keyUp:event];
}
previousModifiers = event.modifierFlags;
}
- (uint32_t *)currentBuffer
{
return image_buffers[current_buffer];
}
- (uint32_t *)previousBuffer
{
return image_buffers[(current_buffer + 2) % self.numberOfBuffers];
}
2021-04-14 12:20:01 +00:00
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSURLPboardType] ) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
if (GB_is_save_state(fileURL.fileSystemRepresentation)) {
2021-04-14 12:20:01 +00:00
return NSDragOperationGeneric;
}
}
return NSDragOperationNone;
}
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSURLPboardType] ) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
return [_document loadStateFile:fileURL.fileSystemRepresentation noErrorOnNotFound:false];
2021-04-14 12:20:01 +00:00
}
return false;
}
2021-10-23 10:26:44 +00:00
- (NSImage *)renderToImage;
{
/* Not going to support this on OpenGL, OpenGL is too much of a terrible API for me
to bother figuring out how the hell something so trivial can be done. */
return nil;
}
2016-03-30 20:07:55 +00:00
@end