File: GBOSDView.m

package info (click to toggle)
sameboy 1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,632 kB
  • sloc: ansic: 29,954; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,064; xml: 111
file content (104 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#import "GBOSDView.h"

@implementation GBOSDView
{
    bool _usesSGBScale;
    NSString *_text;
    double _animation;
    NSTimer *_timer;
}

- (void)setUsesSGBScale:(bool)usesSGBScale
{
    _usesSGBScale = usesSGBScale;
    [self setNeedsDisplay:true];
}

- (bool)usesSGBScale
{
    return _usesSGBScale;
}

- (void)displayText:(NSString *)text
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBOSDEnabled"]) return;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (![_text isEqualToString:text]) {
            [self setNeedsDisplay:true];
        }
        _text = text;
        self.alphaValue = 1.0;
        _animation = 2.5;
        // Longer strings should appear longer
        if ([_text rangeOfString:@"\n"].location != NSNotFound) {
            _animation += 4;
        }
        [_timer invalidate];
        self.hidden = false;
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(animate) userInfo:nil repeats:true];
    });
}

- (void)animate
{
    _animation -= 0.1;
    if (_animation < 1.0) {
        self.alphaValue = _animation;
    };
    if (_animation == 0) {
        self.hidden = true;
        [_timer invalidate];
        _text = nil;
    }
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    if (!_text.length) return;
    
    double fontSize = 8;
    NSSize size = self.frame.size;
    if (_usesSGBScale) {
        fontSize *= MIN(size.width / 256, size.height / 224);
    }
    else {
        fontSize *= MIN(size.width / 160, size.height / 144);
    }
    
    NSFont *font = [NSFont boldSystemFontOfSize:fontSize];
    
    /* The built in stroke attribute uses an inside stroke, which is typographically terrible.
       We'll use a naïve manual stroke instead which looks better. */

    NSDictionary *attributes = @{
        NSFontAttributeName: font,
        NSForegroundColorAttributeName: [NSColor blackColor],
    };
        
    NSAttributedString *text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];

    [text drawAtPoint:NSMakePoint(fontSize + 1, fontSize + 0)];
    [text drawAtPoint:NSMakePoint(fontSize - 1, fontSize + 0)];
    [text drawAtPoint:NSMakePoint(fontSize + 0, fontSize + 1)];
    [text drawAtPoint:NSMakePoint(fontSize + 0, fontSize - 1)];
    
    // The uses of sqrt(2)/2, which is more correct, results in severe ugly-looking rounding errors
    if (self.window.screen.backingScaleFactor > 1) {
        [text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize + 0.5)];
        [text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize + 0.5)];
        [text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize - 0.5)];
        [text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize - 0.5)];
    }
    
    attributes = @{
        NSFontAttributeName: font,
        NSForegroundColorAttributeName: [NSColor whiteColor],
    };

    text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];
    
    [text drawAtPoint:NSMakePoint(fontSize, fontSize)];
}

@end