prefer pixel-perfect scaling when filter is set to nearest

This commit is contained in:
Jesús A. Álvarez 2024-03-01 23:31:06 +01:00
parent 65d377884b
commit da3f639ef2

View File

@ -29,7 +29,7 @@ static ScreenView *sharedScreenView = nil;
[self updateVideoLayer]; [self updateVideoLayer];
[self.layer addSublayer:videoLayer]; [self.layer addSublayer:videoLayer];
[[AppDelegate sharedInstance] addObserver:self forKeyPath:@"sharedEmulator" options:NSKeyValueObservingOptionNew context:NULL]; [[AppDelegate sharedInstance] addObserver:self forKeyPath:@"sharedEmulator" options:NSKeyValueObservingOptionNew context:NULL];
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"screenFilter" options:NSKeyValueObservingOptionNew context:NULL]; [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"screenFilter" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
} }
+ (instancetype)sharedScreenView { + (instancetype)sharedScreenView {
@ -57,17 +57,25 @@ static ScreenView *sharedScreenView = nil;
- (void)layoutSubviews { - (void)layoutSubviews {
[super layoutSubviews]; [super layoutSubviews];
CGRect viewBounds = self.bounds; CGRect viewBounds = self.bounds;
CGFloat screenScale = MAX(screenSize.width / viewBounds.size.width, screenSize.height / viewBounds.size.height); CGFloat screenScale = MIN(viewBounds.size.width / screenSize.width, viewBounds.size.height / screenSize.height);
if (screenScale > 0.9 && screenScale <= 1.0) { NSString *screenFilter = [[NSUserDefaults standardUserDefaults] stringForKey:@"screenFilter"];
if ([screenFilter isEqualToString:kCAFilterNearest] && screenScale > 1.0) {
screenScale = floor(screenScale);
} else if (screenScale > 1.0 && screenScale <= 1.1) {
screenScale = 1.0; screenScale = 1.0;
} }
screenBounds = CGRectMake(0, 0, screenSize.width / screenScale, screenSize.height / screenScale); screenBounds = CGRectMake(0, 0, screenSize.width * screenScale, screenSize.height * screenScale);
screenBounds.origin.x = (viewBounds.size.width - screenBounds.size.width)/2; screenBounds.origin.x = (viewBounds.size.width - screenBounds.size.width)/2;
screenBounds = CGRectIntegral(screenBounds); screenBounds = CGRectIntegral(screenBounds);
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad && (viewBounds.size.height - screenBounds.size.height) >= 30.0) {
// move under multitasking indicator on iPad
screenBounds.origin.y += 30;
}
videoLayer.frame = screenBounds; videoLayer.frame = screenBounds;
screenBounds.origin.y = self.frame.origin.y; screenBounds.origin.y += self.frame.origin.y;
BOOL scaleIsIntegral = (floor(screenScale) == screenScale); BOOL scaleIsIntegral = (floor(screenScale) == screenScale);
NSString *screenFilter = scaleIsIntegral ? kCAFilterNearest : [[NSUserDefaults standardUserDefaults] stringForKey:@"screenFilter"]; if (scaleIsIntegral) screenFilter = kCAFilterNearest;
videoLayer.magnificationFilter = screenFilter; videoLayer.magnificationFilter = screenFilter;
videoLayer.minificationFilter = screenFilter; videoLayer.minificationFilter = screenFilter;
} }
@ -75,13 +83,19 @@ static ScreenView *sharedScreenView = nil;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([object isEqual:[NSUserDefaults standardUserDefaults]]) { if ([object isEqual:[NSUserDefaults standardUserDefaults]]) {
if ([keyPath isEqualToString:@"screenFilter"]) { if ([keyPath isEqualToString:@"screenFilter"]) {
NSString *oldValue = change[NSKeyValueChangeOldKey];
NSString *value = change[NSKeyValueChangeNewKey]; NSString *value = change[NSKeyValueChangeNewKey];
videoLayer.magnificationFilter = value; videoLayer.magnificationFilter = value;
videoLayer.minificationFilter = value; videoLayer.minificationFilter = value;
if ([value isEqualToString:kCAFilterNearest] || [oldValue isEqualToString:kCAFilterNearest]) {
[self setNeedsLayout];
[self layoutIfNeeded];
}
} }
} else if (object == [AppDelegate sharedInstance] && [keyPath isEqualToString:@"sharedEmulator"]) { } else if (object == [AppDelegate sharedInstance] && [keyPath isEqualToString:@"sharedEmulator"]) {
[self updateVideoLayer]; [self updateVideoLayer];
[self layoutSubviews]; [self setNeedsLayout];
[self layoutIfNeeded];
} }
} }