forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGBBackgroundView.m
518 lines (463 loc) · 18.3 KB
/
GBBackgroundView.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#import "GBBackgroundView.h"
#import "GBViewMetal.h"
#import "GBHapticManager.h"
#import "GBMenuViewController.h"
#import "GBViewController.h"
#import "GBROMManager.h"
double CGPointSquaredDistance(CGPoint a, CGPoint b)
{
double deltaX = a.x - b.x;
double deltaY = a.y - b.y;
return deltaX * deltaX + deltaY * deltaY;
}
double CGPointAngle(CGPoint a, CGPoint b)
{
double deltaX = a.x - b.x;
double deltaY = a.y - b.y;
return atan2(deltaY, deltaX);
}
static void positionView(UIImageView *view, CGPoint position)
{
double center = view.image.size.width / 2 * [UIScreen mainScreen].scale;
view.frame = (CGRect){
{
round(position.x - center) / [UIScreen mainScreen].scale,
round(position.y - center) / [UIScreen mainScreen].scale
},
view.image.size
};
}
static GB_key_mask_t angleToKeyMask(double angle)
{
signed quantizedAngle = round(angle / M_PI * 16);
if (quantizedAngle < 0) {
quantizedAngle += 32;
}
switch (quantizedAngle) {
case 32:
case 0: return GB_KEY_RIGHT_MASK;
case 1: return GB_KEY_RIGHT_MASK;
case 2: return GB_KEY_RIGHT_MASK;
case 3: return GB_KEY_RIGHT_MASK | GB_KEY_DOWN_MASK;
case 4: return GB_KEY_RIGHT_MASK | GB_KEY_DOWN_MASK;
case 5: return GB_KEY_DOWN_MASK;
case 6: return GB_KEY_DOWN_MASK;
case 7: return GB_KEY_DOWN_MASK;
case 8: return GB_KEY_DOWN_MASK;
case 9: return GB_KEY_DOWN_MASK;
case 10: return GB_KEY_DOWN_MASK;
case 11: return GB_KEY_LEFT_MASK | GB_KEY_DOWN_MASK;
case 12: return GB_KEY_LEFT_MASK | GB_KEY_DOWN_MASK;
case 13: return GB_KEY_LEFT_MASK;
case 14: return GB_KEY_LEFT_MASK;
case 15: return GB_KEY_LEFT_MASK;
case 16: return GB_KEY_LEFT_MASK;
case 17: return GB_KEY_LEFT_MASK;
case 18: return GB_KEY_LEFT_MASK;
case 19: return GB_KEY_LEFT_MASK | GB_KEY_UP_MASK;
case 20: return GB_KEY_LEFT_MASK | GB_KEY_UP_MASK;
case 21: return GB_KEY_UP_MASK;
case 22: return GB_KEY_UP_MASK;
case 23: return GB_KEY_UP_MASK;
case 24: return GB_KEY_UP_MASK;
case 25: return GB_KEY_UP_MASK;
case 26: return GB_KEY_UP_MASK;
case 27: return GB_KEY_RIGHT_MASK | GB_KEY_UP_MASK;
case 28: return GB_KEY_RIGHT_MASK | GB_KEY_UP_MASK;
case 29: return GB_KEY_RIGHT_MASK;
case 30: return GB_KEY_RIGHT_MASK;
case 31: return GB_KEY_RIGHT_MASK;
}
return 0;
}
@implementation GBBackgroundView
{
NSMutableSet<UITouch *> *_touches;
UITouch *_swipePadTouch;
CGPoint _padSwipeOrigin;
UITouch *_screenTouch;
CGPoint _screenSwipeOrigin;
bool _screenSwiped;
bool _inDynamicSpeedMode;
UIImageView *_dpadView;
UIImageView *_dpadShadowView;
UIImageView *_aButtonView;
UIImageView *_bButtonView;
UIImageView *_startButtonView;
UIImageView *_selectButtonView;
UILabel *_screenLabel;
UIVisualEffectView *_overlayView;
UIView *_overlayViewContents;
NSTimer *_fadeTimer;
GB_key_mask_t _lastMask;
}
- (instancetype)init
{
self = [super initWithImage:nil];
if (!self) return nil;
_touches = [NSMutableSet set];
_screenLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_screenLabel.text = @"Tap the Game Boy screen to open the menu and load a ROM from the library.";
_screenLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
_screenLabel.textAlignment = NSTextAlignmentCenter;
_screenLabel.textColor = [UIColor whiteColor];
_screenLabel.lineBreakMode = NSLineBreakByWordWrapping;
_screenLabel.numberOfLines = 0;
[self addSubview:_screenLabel];
_dpadView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dpad"]];
_dpadShadowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dpadShadow"]];
_dpadShadowView.hidden = true;
_aButtonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button"]];
_bButtonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button"]];
_startButtonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button2"]];
_selectButtonView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button2"]];
_gbView = [[GBViewMetal alloc] initWithFrame:CGRectZero];
[self addSubview:_dpadView];
[self addSubview:_aButtonView];
[self addSubview:_bButtonView];
[self addSubview:_startButtonView];
[self addSubview:_selectButtonView];
[self addSubview:_gbView];
[_dpadView addSubview:_dpadShadowView];
UIVisualEffect *effect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyle)UIBlurEffectStyleDark];
_overlayView = [[UIVisualEffectView alloc] initWithEffect:effect];
_overlayView.frame = CGRectMake(8, 8, 32, 32);
_overlayView.layer.cornerRadius = 12;
_overlayView.layer.masksToBounds = true;
_overlayView.alpha = 0;
if (@available(iOS 13.0, *)) {
_overlayViewContents = [[UIImageView alloc] init];
_overlayViewContents.tintColor = [UIColor whiteColor];
_overlayViewContents.contentMode = UIViewContentModeCenter;
}
else {
_overlayViewContents = [[UILabel alloc] init];
((UILabel *)_overlayViewContents).font = [UIFont systemFontOfSize:UIFont.systemFontSize weight:UIFontWeightMedium];
((UILabel *)_overlayViewContents).textColor = [UIColor whiteColor];
}
_overlayViewContents.frame = CGRectMake(8, 8, 160, 20.5);
[_overlayView.contentView addSubview:_overlayViewContents];
[_gbView addSubview:_overlayView];
return self;
}
- (GBViewController *)viewController
{
return (GBViewController *)[UIApplication sharedApplication].delegate;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
static const double dpadRadius = 75;
CGPoint dpadLocation = _layout.dpadLocation;
double factor = [UIScreen mainScreen].scale;
dpadLocation.x /= factor;
dpadLocation.y /= factor;
for (UITouch *touch in touches) {
CGPoint point = [touch locationInView:self];
if (CGRectContainsPoint(self.gbView.frame, point) && !_screenTouch) {
if (self.viewController.runMode != GBRunModeNormal) {
self.viewController.runMode = GBRunModeNormal;
[self fadeOverlayOut];
}
else {
_screenTouch = touch;
_screenSwipeOrigin = point;
_screenSwiped = false;
_inDynamicSpeedMode = false;
_overlayView.alpha = 0;
[_fadeTimer invalidate];
_fadeTimer = nil;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBDynamicSpeed"]) {
self.viewController.runMode = GBRunModePaused;
[self displayOverlayWithImage:@"pause" orTitle:@"Paused"];
}
}
}
if (_usesSwipePad && !_swipePadTouch) {
if (fabs(point.x - dpadLocation.x) <= dpadRadius &&
fabs(point.y - dpadLocation.y) <= dpadRadius) {
_swipePadTouch = touch;
_padSwipeOrigin = point;
}
}
}
[_touches unionSet:touches];
[self touchesChanged];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if ([touches containsObject:_swipePadTouch]) {
_swipePadTouch = nil;
}
if ([touches containsObject:_screenTouch]) {
_screenTouch = nil;
if (self.viewController.runMode == GBRunModePaused) {
self.viewController.runMode = GBRunModeNormal;
[self fadeOverlayOut];
}
if (!_screenSwiped) {
[self.window.rootViewController presentViewController:[GBMenuViewController menu] animated:true completion:nil];
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBSwipeLock"]) {
if (self.viewController.runMode != GBRunModeNormal) {
self.viewController.runMode = GBRunModeNormal;
[self fadeOverlayOut];
}
}
}
[_touches minusSet:touches];
[self touchesChanged];
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self touchesChanged];
}
- (void)touchesChanged
{
if (!GB_is_inited(_gbView.gb)) return;
GB_key_mask_t mask = 0;
double factor = [UIScreen mainScreen].scale;
double buttonRadiusSquared = 36 * 36 * factor * factor;
double dpadRadius = 75 * factor;
bool dpadHandled = false;
if (_usesSwipePad) {
dpadHandled = true;
if (_swipePadTouch) {
CGPoint point = [_swipePadTouch locationInView:self];
double squaredDistance = CGPointSquaredDistance(point, _padSwipeOrigin);
if (squaredDistance > 16 * 16) {
double angle = CGPointAngle(point, _padSwipeOrigin);
mask |= angleToKeyMask(angle);
if (squaredDistance > 24 * 24) {
double deltaX = point.x - _padSwipeOrigin.x;
double deltaY = point.y - _padSwipeOrigin.y;
double distance = sqrt(squaredDistance);
_padSwipeOrigin.x = point.x - deltaX / distance * 24;
_padSwipeOrigin.y = point.y - deltaY / distance * 24;
}
}
}
}
for (UITouch *touch in _touches) {
if (touch == _swipePadTouch) continue;
CGPoint point = [touch locationInView:self];
if (touch == _screenTouch) {
if (_inDynamicSpeedMode) {
double delta = point.x - _screenSwipeOrigin.x;
if (fabs(delta) < 32) {
self.viewController.runMode = GBRunModePaused;
[self displayOverlayWithImage:@"pause" orTitle:@"Paused"];
continue;
}
double speed = fabs(delta / _gbView.frame.size.width * 3);
if (delta > 0) {
if (speed > 1) {
[self displayOverlayWithImage:@"forward" orTitle:@"Fast-forwarding…"];
}
else {
[self displayOverlayWithImage:@"play" orTitle:@"Forward…"];
}
GB_set_clock_multiplier(_gbView.gb, speed);
self.viewController.runMode = GBRunModeTurbo;
}
else {
[self displayOverlayWithImage:@"backward" orTitle:@"Rewinding…"];
GB_set_clock_multiplier(_gbView.gb, speed);
self.viewController.runMode = GBRunModeRewind;
}
continue;
}
if (_screenSwiped) continue;
if (point.x - _screenSwipeOrigin.x > 32) {
[self turboSwipe];
}
else if (point.x - _screenSwipeOrigin.x < -32) {
[self rewindSwipe];
}
else if (point.y - _screenSwipeOrigin.y > 32) {
[self saveSwipe];
}
else if (point.y - _screenSwipeOrigin.y < -32) {
[self loadSwipe];
}
continue;
}
point.x *= factor;
point.y *= factor;
if (CGPointSquaredDistance(point, _layout.aLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_A_MASK;
}
else if (CGPointSquaredDistance(point, _layout.bLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_B_MASK;
}
else if (CGPointSquaredDistance(point, _layout.startLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_START_MASK;
}
else if (CGPointSquaredDistance(point, _layout.selectLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_SELECT_MASK;
}
else if (!dpadHandled &&
fabs(point.x - _layout.dpadLocation.x) <= dpadRadius &&
fabs(point.y - _layout.dpadLocation.y) <= dpadRadius) {
dpadHandled = true; // Don't handle the dpad twice
double angle = CGPointAngle(point, _layout.dpadLocation);
mask |= angleToKeyMask(angle);
}
}
if (mask != _lastMask) {
_aButtonView.image = [UIImage imageNamed:(mask & GB_KEY_A_MASK)? @"buttonPressed" : @"button"];
_bButtonView.image = [UIImage imageNamed:(mask & GB_KEY_B_MASK)? @"buttonPressed" : @"button"];
_startButtonView.image = [UIImage imageNamed:(mask & GB_KEY_START_MASK) ? @"button2Pressed" : @"button2"];
_selectButtonView.image = [UIImage imageNamed:(mask & GB_KEY_SELECT_MASK)? @"button2Pressed" : @"button2"];
bool hidden = false;
bool diagonal = false;
double rotation = 0;
switch (mask & (GB_KEY_RIGHT_MASK | GB_KEY_DOWN_MASK | GB_KEY_LEFT_MASK | GB_KEY_UP_MASK)) {
case GB_KEY_RIGHT_MASK: break;
case GB_KEY_RIGHT_MASK | GB_KEY_DOWN_MASK: diagonal = true; break;
case GB_KEY_DOWN_MASK: rotation = M_PI_2; break;
case GB_KEY_LEFT_MASK | GB_KEY_DOWN_MASK: diagonal = true; rotation = M_PI_2; break;
case GB_KEY_LEFT_MASK: rotation = M_PI; break;
case GB_KEY_LEFT_MASK | GB_KEY_UP_MASK: diagonal = true; rotation = M_PI; break;
case GB_KEY_UP_MASK: rotation = -M_PI_2; break;
case GB_KEY_RIGHT_MASK | GB_KEY_UP_MASK: diagonal = true; rotation = -M_PI_2; break;
default:
hidden = true;
}
_dpadShadowView.hidden = hidden;
if (!hidden) {
if (_usesSwipePad) {
_dpadShadowView.image = [UIImage imageNamed:diagonal? @"swipepadShadowDiagonal" : @"swipepadShadow"];
}
else {
_dpadShadowView.image = [UIImage imageNamed:diagonal? @"dpadShadowDiagonal" : @"dpadShadow"];
}
_dpadShadowView.transform = CGAffineTransformMakeRotation(rotation);
}
GB_set_key_mask(_gbView.gb, mask);
if ((mask & ~_lastMask) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBButtonHaptics"])) {
[[GBHapticManager sharedManager] doTapHaptic];
}
_lastMask = mask;
}
}
- (BOOL)isMultipleTouchEnabled
{
return true;
}
- (BOOL)isUserInteractionEnabled
{
return true;
}
- (void)setLayout:(GBLayout *)layout
{
_layout = layout;
self.image = layout.background;
positionView(_dpadView, layout.dpadLocation);
positionView(_aButtonView, layout.aLocation);
positionView(_bButtonView, layout.bLocation);
positionView(_startButtonView, layout.startLocation);
positionView(_selectButtonView, layout.selectLocation);
CGRect screenFrame = layout.screenRect;
screenFrame.origin.x /= [UIScreen mainScreen].scale;
screenFrame.origin.y /= [UIScreen mainScreen].scale;
screenFrame.size.width /= [UIScreen mainScreen].scale;
screenFrame.size.height /= [UIScreen mainScreen].scale;
_gbView.frame = screenFrame;
screenFrame.origin.x += 8;
screenFrame.origin.y += 8;
screenFrame.size.width -= 16;
screenFrame.size.height -= 16;
_screenLabel.frame = screenFrame;
}
- (void)setUsesSwipePad:(bool)usesSwipePad
{
_usesSwipePad = usesSwipePad;
_dpadView.image = [UIImage imageNamed:usesSwipePad? @"swipepad" : @"dpad"];
}
- (void)displayOverlayWithImage:(NSString *)imageName orTitle:(NSString *)title
{
if (@available(iOS 13.0, *)) {
((UIImageView *)_overlayViewContents).image = [UIImage systemImageNamed:imageName
withConfiguration:[UIImageSymbolConfiguration configurationWithWeight:UIImageSymbolWeightMedium]];
}
else {
((UILabel *)_overlayViewContents).text = title;
}
[_overlayViewContents sizeToFit];
CGRect frame = _overlayViewContents.frame;
frame.size.width = MAX(frame.size.width, 25);
frame.size.height = MAX(frame.size.height, 22);
_overlayViewContents.frame = frame;
frame.origin = (CGPoint){8, 8};
frame.size.width += 16;
frame.size.height += 16;
_overlayView.frame = frame;
_overlayView.alpha = 1.0;
}
- (void)fadeOverlayOut
{
[UIView animateWithDuration:1 animations:^{
_overlayView.alpha = 0;
}];
[_fadeTimer invalidate];
_fadeTimer = nil;
}
- (void)turboSwipe
{
_screenSwiped = true;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBDynamicSpeed"]) {
_inDynamicSpeedMode = true;
}
[self displayOverlayWithImage:@"forward" orTitle:@"Fast-forwarding…"];
self.viewController.runMode = GBRunModeTurbo;
}
- (void)rewindSwipe
{
_screenSwiped = true;
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"GBDynamicSpeed"]) {
_inDynamicSpeedMode = true;
}
[self displayOverlayWithImage:@"backward" orTitle:@"Rewinding…"];
self.viewController.runMode = GBRunModeRewind;
}
- (NSString *)swipeStateFile
{
return [[GBROMManager sharedManager] stateFile:1];
}
- (void)saveSwipe
{
_screenSwiped = true;
self.viewController.runMode = GBRunModeNormal;
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBSwipeState"]) {
[self fadeOverlayOut];
return;
}
[self displayOverlayWithImage:@"square.and.arrow.down" orTitle:@"Saved state to Slot 1"];
_fadeTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:false block:^(NSTimer *timer) {
[self fadeOverlayOut];
}];
[self.viewController stop];
[self.viewController saveStateToFile:self.swipeStateFile];
[self.viewController start];
}
- (void)loadSwipe
{
_screenSwiped = true;
self.viewController.runMode = GBRunModeNormal;
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBSwipeState"]) {
[self fadeOverlayOut];
return;
}
[self displayOverlayWithImage:@"square.and.arrow.up" orTitle:@"Loaded state from Slot 1"];
_fadeTimer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:false block:^(NSTimer *timer) {
[self fadeOverlayOut];
}];
[self.viewController stop];
[self.viewController loadStateFromFile:self.swipeStateFile];
[self.viewController start];
}
@end