SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
Cocoa/GBOSDView.m
1 #import "GBOSDView.h"
2
3 @implementation GBOSDView
4 {
5 bool _usesSGBScale;
6 NSString *_text;
7 double _animation;
8 NSTimer *_timer;
9 }
10
11 - (void)setUsesSGBScale:(bool)usesSGBScale
12 {
13 _usesSGBScale = usesSGBScale;
14 [self setNeedsDisplay:true];
15 }
16
17 - (bool)usesSGBScale
18 {
19 return _usesSGBScale;
20 }
21
22 - (void)displayText:(NSString *)text
23 {
24 if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBOSDEnabled"]) return;
25 dispatch_async(dispatch_get_main_queue(), ^{
26 if (![_text isEqualToString:text]) {
27 [self setNeedsDisplay:true];
28 }
29 _text = text;
30 self.alphaValue = 1.0;
31 _animation = 2.5;
32 // Longer strings should appear longer
33 if ([_text rangeOfString:@"\n"].location != NSNotFound) {
34 _animation += 4;
35 }
36 [_timer invalidate];
37 self.hidden = false;
38 _timer = [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(animate) userInfo:nil repeats:true];
39 });
40 }
41
42 - (void)animate
43 {
44 _animation -= 0.1;
45 if (_animation < 1.0) {
46 self.alphaValue = _animation;
47 };
48 if (_animation == 0) {
49 self.hidden = true;
50 [_timer invalidate];
51 _text = nil;
52 }
53 }
54
55 - (void)drawRect:(NSRect)dirtyRect
56 {
57 [super drawRect:dirtyRect];
58 if (!_text.length) return;
59
60 double fontSize = 8;
61 NSSize size = self.frame.size;
62 if (_usesSGBScale) {
63 fontSize *= MIN(size.width / 256, size.height / 224);
64 }
65 else {
66 fontSize *= MIN(size.width / 160, size.height / 144);
67 }
68
69 NSFont *font = [NSFont boldSystemFontOfSize:fontSize];
70
71 /* The built in stroke attribute uses an inside stroke, which is typographically terrible.
72 We'll use a naïve manual stroke instead which looks better. */
73
74 NSDictionary *attributes = @{
75 NSFontAttributeName: font,
76 NSForegroundColorAttributeName: [NSColor blackColor],
77 };
78
79 NSAttributedString *text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];
80
81 [text drawAtPoint:NSMakePoint(fontSize + 1, fontSize + 0)];
82 [text drawAtPoint:NSMakePoint(fontSize - 1, fontSize + 0)];
83 [text drawAtPoint:NSMakePoint(fontSize + 0, fontSize + 1)];
84 [text drawAtPoint:NSMakePoint(fontSize + 0, fontSize - 1)];
85
86 // The uses of sqrt(2)/2, which is more correct, results in severe ugly-looking rounding errors
87 if (self.window.screen.backingScaleFactor > 1) {
88 [text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize + 0.5)];
89 [text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize + 0.5)];
90 [text drawAtPoint:NSMakePoint(fontSize - 0.5, fontSize - 0.5)];
91 [text drawAtPoint:NSMakePoint(fontSize + 0.5, fontSize - 0.5)];
92 }
93
94 attributes = @{
95 NSFontAttributeName: font,
96 NSForegroundColorAttributeName: [NSColor whiteColor],
97 };
98
99 text = [[NSAttributedString alloc] initWithString:_text attributes:attributes];
100
101 [text drawAtPoint:NSMakePoint(fontSize, fontSize)];
102 }
103
104 @end
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.