forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBSlotButton.m
150 lines (133 loc) · 5.25 KB
/
GBSlotButton.m
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "GBSlotButton.h"
@implementation GBSlotButton
{
UIImageView *_imageView;
}
+ (instancetype)buttonWithLabelText:(NSString *)labelText
{
GBSlotButton *ret = [self buttonWithType:UIButtonTypeCustom];
if (!ret) return nil;
ret.frame = CGRectMake(0, 0, 0x100, 0x100);
ret->_slotSubtitleLabel = [[UILabel alloc] init];
ret->_slotSubtitleLabel.text = @"Empty";
ret->_slotSubtitleLabel.font = [UIFont systemFontOfSize:UIFont.smallSystemFontSize];
if (@available(iOS 13.0, *)) {
ret->_slotSubtitleLabel.textColor = [UIColor secondaryLabelColor];
}
else {
ret->_slotSubtitleLabel.textColor = [UIColor systemGrayColor];
}
[ret->_slotSubtitleLabel sizeToFit];
ret->_slotSubtitleLabel.textAlignment = NSTextAlignmentCenter;
CGRect slotSubtitleLabelRect = ret->_slotSubtitleLabel.frame;
slotSubtitleLabelRect.size.width = 0x100;
slotSubtitleLabelRect.origin.y = 0x100 - slotSubtitleLabelRect.size.height - 8;
ret->_slotSubtitleLabel.frame = slotSubtitleLabelRect;
ret->_slotSubtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[ret addSubview:ret->_slotSubtitleLabel];
ret->_label = [[UILabel alloc] init];
ret->_label.text = labelText;
[ret->_label sizeToFit];
ret->_label.textAlignment = NSTextAlignmentCenter;
CGRect labelRect = ret->_label.frame;
labelRect.size.width = 0x100;
labelRect.origin.y = slotSubtitleLabelRect.origin.y - labelRect.size.height - 4;
ret->_label.frame = labelRect;
ret->_label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[ret addSubview:ret->_label];
ret.backgroundColor = [UIColor clearColor];
ret->_imageView = [[UIImageView alloc] initWithImage:nil];
ret.imageView.layer.cornerRadius = 6;
ret.imageView.layer.masksToBounds = true;
if (@available(iOS 13.0, *)) {
ret.imageView.layer.borderColor = [UIColor tertiaryLabelColor] .CGColor;
}
else {
ret.imageView.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor;
}
ret.imageView.layer.borderWidth = 1;
ret.imageView.layer.backgroundColor = [UIColor whiteColor].CGColor;
[ret addSubview:ret.imageView];
return ret;
}
- (UIImageView *)imageView
{
return _imageView;
}
- (void)setHighlighted:(BOOL)highlighted
{
if (_showingMenu) return;
if (highlighted == self.isHighlighted) return;
[super setHighlighted:highlighted];
[UIView animateWithDuration:0.25 animations:^{
if (highlighted) {
if (@available(iOS 13.0, *)) {
self.backgroundColor = [UIColor tertiarySystemFillColor];
}
else {
self.backgroundColor = [UIColor colorWithRed:118 / 255.0
green:118 / 255.0
blue:128 / 255.0
alpha:0.12];
}
self.imageView.layer.opacity = 11 / 12.0;
}
else {
self.backgroundColor = [UIColor clearColor];
self.imageView.layer.opacity = 1;
}
}];
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
CGRect screenshotRect = self.bounds;
screenshotRect.size.width -= 8;
screenshotRect.origin.x += 4;
screenshotRect.size.height = _label.frame.origin.y - 16;
screenshotRect.origin.y += 8;
double scale = [UIApplication sharedApplication].keyWindow.screen.scale;
double nativeWidth = 160.0 / scale;
double nativeHeight = 144.0 / scale;
if (screenshotRect.size.width > nativeWidth &&
screenshotRect.size.height > nativeHeight) {
self.imageView.layer.magnificationFilter = kCAFilterNearest;
double newWidth = floor(screenshotRect.size.width / nativeWidth) * nativeWidth;
screenshotRect.origin.x += (screenshotRect.size.width - newWidth) / 2;
screenshotRect.size.width = newWidth;
double newHeight = floor(screenshotRect.size.height / nativeHeight) * nativeHeight;
screenshotRect.origin.y += (screenshotRect.size.height - newHeight) / 2;
screenshotRect.size.height = newHeight;
}
else {
self.imageView.layer.magnificationFilter = kCAFilterLinear;
}
double aspect = screenshotRect.size.width / screenshotRect.size.height;
if (aspect > 160.0 / 144.0) {
// Too wide
double newWidth = screenshotRect.size.height / 144 * 160;
screenshotRect.origin.x += (screenshotRect.size.width - newWidth) / 2;
screenshotRect.size.width = newWidth;
}
else {
// Too narrow
double newHeight = screenshotRect.size.width / 160 * 144;
screenshotRect.origin.y += (screenshotRect.size.height - newHeight) / 2;
screenshotRect.size.height = newHeight;
}
screenshotRect.origin.x = round(screenshotRect.origin.x);
screenshotRect.origin.y = round(screenshotRect.origin.y);
self.imageView.frame = screenshotRect;
}
- (void)setShowingMenu:(bool)showingMenu
{
if (showingMenu) {
self.highlighted = true;
_showingMenu = true;
}
else {
_showingMenu = false;
self.highlighted = false;
}
}
@end