Hide mouse cursor when running (Cocoa)

This commit is contained in:
Lior Halphon 2016-07-05 23:34:33 +03:00
parent 1268bf3a35
commit dce0e5fdeb
3 changed files with 63 additions and 0 deletions

View File

@ -94,6 +94,7 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
- (void) vblank
{
self.view.mouseHidingEnabled = YES;
[self.view flip];
GB_set_pixels_output(&gb, self.view.pixels);
}
@ -107,11 +108,13 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) {
GB_apu_copy_buffer(&gb, buffer, nFrames);
} andSampleRate:96000];
self.view.mouseHidingEnabled = YES;
[self.audioClient start];
while (running) {
GB_run(&gb);
}
[self.audioClient stop];
self.view.mouseHidingEnabled = NO;
GB_save_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]);
stopping = false;
}
@ -309,6 +312,10 @@ static uint32_t rgbEncode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
return;
}
pendingLogLines++;
/* Make sure mouse is not hidden while debugging */
self.view.mouseHidingEnabled = NO;
NSString *nsstring = @(string); // For ref-counting
dispatch_async(dispatch_get_main_queue(), ^{
NSFont *font = [NSFont userFixedPitchFontOfSize:12];

View File

@ -8,4 +8,5 @@
@property GB_gameboy_t *gb;
@property (nonatomic) BOOL shouldBlendFrameWithPrevious;
@property GBShader *shader;
@property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled;
@end

View File

@ -8,6 +8,9 @@
{
uint32_t *image_buffers[3];
unsigned char current_buffer;
BOOL mouse_hidden;
NSTrackingArea *tracking_area;
BOOL _mouseHidingEnabled;
}
- (void) awakeFromNib
@ -42,6 +45,11 @@
_shouldBlendFrameWithPrevious = 1;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterChanged) name:@"GBFilterChanged" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
owner:self
userInfo:nil];
[self addTrackingArea:tracking_area];
}
- (void) filterChanged
@ -71,6 +79,10 @@
free(image_buffers[0]);
free(image_buffers[1]);
free(image_buffers[2]);
if (mouse_hidden) {
mouse_hidden = false;
[NSCursor unhide];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithCoder:(NSCoder *)coder
@ -111,6 +123,7 @@
frame.origin.x = 0;
}
}
[super setFrame:frame];
}
@ -223,4 +236,46 @@
{
return YES;
}
- (void)mouseEntered:(NSEvent *)theEvent
{
if (!mouse_hidden) {
mouse_hidden = true;
if (_mouseHidingEnabled) {
[NSCursor hide];
}
}
[super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent *)theEvent
{
if (mouse_hidden) {
mouse_hidden = false;
if (_mouseHidingEnabled) {
[NSCursor unhide];
}
}
[super mouseExited:theEvent];
}
- (void)setMouseHidingEnabled:(BOOL)mouseHidingEnabled
{
if (mouseHidingEnabled == _mouseHidingEnabled) return;
_mouseHidingEnabled = mouseHidingEnabled;
if (mouse_hidden && _mouseHidingEnabled) {
[NSCursor hide];
}
if (mouse_hidden && !_mouseHidingEnabled) {
[NSCursor unhide];
}
}
- (BOOL)isMouseHidingEnabled
{
return _mouseHidingEnabled;
}
@end