2016-05-01 17:05:36 +00:00
|
|
|
//
|
|
|
|
// ScreenView.m
|
|
|
|
// Mini vMac
|
|
|
|
//
|
|
|
|
// Created by Jesús A. Álvarez on 30/04/2016.
|
2018-04-28 10:12:43 +00:00
|
|
|
// Copyright © 2016-2018 namedfork. All rights reserved.
|
2016-05-01 17:05:36 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "ScreenView.h"
|
2016-05-28 11:01:13 +00:00
|
|
|
#import "AppDelegate.h"
|
2016-05-01 17:05:36 +00:00
|
|
|
|
|
|
|
static ScreenView *sharedScreenView = nil;
|
|
|
|
|
|
|
|
@implementation ScreenView
|
|
|
|
{
|
|
|
|
CGImageRef screenImage;
|
|
|
|
CGRect screenBounds;
|
2016-05-01 21:44:47 +00:00
|
|
|
CGSize screenSize;
|
2016-05-20 22:12:15 +00:00
|
|
|
CALayer *videoLayer;
|
2016-05-01 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)awakeFromNib {
|
|
|
|
[super awakeFromNib];
|
|
|
|
sharedScreenView = self;
|
2016-05-20 22:12:15 +00:00
|
|
|
videoLayer = [CALayer layer];
|
2017-11-30 19:09:08 +00:00
|
|
|
NSString *screenFilter = [[NSUserDefaults standardUserDefaults] stringForKey:@"screenFilter"];
|
|
|
|
videoLayer.magnificationFilter = screenFilter;
|
|
|
|
videoLayer.minificationFilter = screenFilter;
|
2020-09-24 22:29:30 +00:00
|
|
|
[self updateVideoLayer];
|
2016-05-20 22:12:15 +00:00
|
|
|
[self.layer addSublayer:videoLayer];
|
2020-09-24 22:29:30 +00:00
|
|
|
[[AppDelegate sharedInstance] addObserver:self forKeyPath:@"sharedEmulator" options:NSKeyValueObservingOptionNew context:NULL];
|
2024-03-01 22:31:06 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"screenFilter" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
|
2016-05-01 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)sharedScreenView {
|
|
|
|
return sharedScreenView;
|
|
|
|
}
|
|
|
|
|
2016-05-01 21:44:47 +00:00
|
|
|
- (CGRect)screenBounds {
|
|
|
|
return screenBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)screenSize {
|
|
|
|
return screenSize;
|
|
|
|
}
|
|
|
|
|
2020-09-24 22:29:30 +00:00
|
|
|
- (void)updateVideoLayer {
|
2020-09-25 20:17:04 +00:00
|
|
|
videoLayer.contents = nil;
|
2020-09-24 22:29:30 +00:00
|
|
|
if ([AppDelegate sharedEmulator]) {
|
|
|
|
[AppDelegate sharedEmulator].screenLayer = videoLayer;
|
|
|
|
screenSize = [AppDelegate sharedEmulator].screenSize;
|
|
|
|
} else {
|
|
|
|
screenSize = CGSizeMake(1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 17:05:36 +00:00
|
|
|
- (void)layoutSubviews {
|
2016-05-20 22:12:15 +00:00
|
|
|
[super layoutSubviews];
|
2016-05-01 17:05:36 +00:00
|
|
|
CGRect viewBounds = self.bounds;
|
2024-03-01 22:31:06 +00:00
|
|
|
CGFloat screenScale = MIN(viewBounds.size.width / screenSize.width, viewBounds.size.height / screenSize.height);
|
|
|
|
NSString *screenFilter = [[NSUserDefaults standardUserDefaults] stringForKey:@"screenFilter"];
|
|
|
|
if ([screenFilter isEqualToString:kCAFilterNearest] && screenScale > 1.0) {
|
|
|
|
screenScale = floor(screenScale);
|
|
|
|
} else if (screenScale > 1.0 && screenScale <= 1.1) {
|
2017-11-30 19:35:38 +00:00
|
|
|
screenScale = 1.0;
|
|
|
|
}
|
2024-03-01 22:31:06 +00:00
|
|
|
screenBounds = CGRectMake(0, 0, screenSize.width * screenScale, screenSize.height * screenScale);
|
2016-05-01 17:05:36 +00:00
|
|
|
screenBounds.origin.x = (viewBounds.size.width - screenBounds.size.width)/2;
|
|
|
|
screenBounds = CGRectIntegral(screenBounds);
|
2024-03-01 22:31:06 +00:00
|
|
|
|
|
|
|
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad && (viewBounds.size.height - screenBounds.size.height) >= 30.0) {
|
|
|
|
// move under multitasking indicator on iPad
|
|
|
|
screenBounds.origin.y += 30;
|
|
|
|
}
|
2016-05-20 22:12:15 +00:00
|
|
|
videoLayer.frame = screenBounds;
|
2024-03-01 22:31:06 +00:00
|
|
|
screenBounds.origin.y += self.frame.origin.y;
|
2017-11-30 19:34:59 +00:00
|
|
|
BOOL scaleIsIntegral = (floor(screenScale) == screenScale);
|
2024-03-01 22:31:06 +00:00
|
|
|
if (scaleIsIntegral) screenFilter = kCAFilterNearest;
|
2017-11-30 19:34:59 +00:00
|
|
|
videoLayer.magnificationFilter = screenFilter;
|
|
|
|
videoLayer.minificationFilter = screenFilter;
|
2016-05-01 17:05:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 19:09:08 +00:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
|
|
|
|
if ([object isEqual:[NSUserDefaults standardUserDefaults]]) {
|
|
|
|
if ([keyPath isEqualToString:@"screenFilter"]) {
|
2024-03-01 22:31:06 +00:00
|
|
|
NSString *oldValue = change[NSKeyValueChangeOldKey];
|
2017-11-30 19:09:08 +00:00
|
|
|
NSString *value = change[NSKeyValueChangeNewKey];
|
|
|
|
videoLayer.magnificationFilter = value;
|
|
|
|
videoLayer.minificationFilter = value;
|
2024-03-01 22:31:06 +00:00
|
|
|
if ([value isEqualToString:kCAFilterNearest] || [oldValue isEqualToString:kCAFilterNearest]) {
|
|
|
|
[self setNeedsLayout];
|
|
|
|
[self layoutIfNeeded];
|
|
|
|
}
|
2017-11-30 19:09:08 +00:00
|
|
|
}
|
2020-09-24 22:29:30 +00:00
|
|
|
} else if (object == [AppDelegate sharedInstance] && [keyPath isEqualToString:@"sharedEmulator"]) {
|
|
|
|
[self updateVideoLayer];
|
2024-03-01 22:31:06 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
[self layoutIfNeeded];
|
2017-11-30 19:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"screenFilter" context:NULL];
|
|
|
|
}
|
2020-09-24 22:29:30 +00:00
|
|
|
|
2016-05-01 17:05:36 +00:00
|
|
|
@end
|