git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

iOS/GBThemePreviewController.m

      1 #import "GBThemePreviewController.h"
      2 #import "GBVerticalLayout.h"
      3 #import "GBHorizontalLayout.h"
      4 #import "GBBackgroundView.h"
      5 
      6 @implementation GBThemePreviewController
      7 {
      8     GBHorizontalLayout *_horizontalLayoutLeft;
      9     GBHorizontalLayout *_horizontalLayoutRight;
     10     GBVerticalLayout *_verticalLayout;
     11     GBBackgroundView *_backgroundView;
     12 }
     13 
     14 - (instancetype)initWithTheme:(GBTheme *)theme
     15 {
     16     self = [super init];
     17     _horizontalLayoutLeft = [[GBHorizontalLayout alloc] initWithTheme:theme cutoutOnRight:false];
     18     _horizontalLayoutRight = _horizontalLayoutLeft.cutout?
     19         [[GBHorizontalLayout alloc] initWithTheme:theme cutoutOnRight:true] :
     20         _horizontalLayoutLeft;
     21     _verticalLayout = [[GBVerticalLayout alloc] initWithTheme:theme];
     22     return self;
     23 }
     24 
     25 - (void)viewDidLoad
     26 {
     27     [super viewDidLoad];
     28     _backgroundView = [[GBBackgroundView alloc] initWithLayout:_verticalLayout];
     29     self.view.backgroundColor = _verticalLayout.theme.backgroundGradientBottom;
     30     [self.view addSubview:_backgroundView];
     31     [_backgroundView enterPreviewMode:true];
     32     _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
     33     
     34     [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
     35     [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
     36                                                                             action:@selector(showPopup)]];
     37 }
     38 
     39 - (UIModalPresentationStyle)modalPresentationStyle
     40 {
     41     return UIModalPresentationFullScreen;
     42 }
     43 
     44 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
     45 {
     46     GBLayout *layout = nil;
     47     switch (orientation) {
     48         default:
     49         case UIInterfaceOrientationUnknown:
     50         case UIInterfaceOrientationPortrait:
     51         case UIInterfaceOrientationPortraitUpsideDown:
     52             layout = _verticalLayout;
     53             break;
     54         case UIInterfaceOrientationLandscapeRight:
     55             layout = _horizontalLayoutLeft;
     56             break;
     57         case UIInterfaceOrientationLandscapeLeft:
     58             layout = _horizontalLayoutRight;
     59             break;
     60     }
     61     
     62     _backgroundView.frame = [layout viewRectForOrientation:orientation];
     63     _backgroundView.layout = layout;
     64 }
     65 
     66 - (void)showPopup
     67 {
     68     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Apply Theme"
     69                                                                    message:[NSString stringWithFormat:@"Apply “%@” as the current theme?", _verticalLayout.theme.name]
     70                                                             preferredStyle:[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad?
     71                                               UIAlertControllerStyleAlert : UIAlertControllerStyleActionSheet];
     72     if (false) {
     73         // No supporter-only themes outside the App Store release
     74     }
     75     else {
     76         [alert addAction:[UIAlertAction actionWithTitle:@"Apply Theme"
     77                                                   style:UIAlertActionStyleDefault
     78                                                 handler:^(UIAlertAction *action) {
     79             [[NSUserDefaults standardUserDefaults] setObject:_verticalLayout.theme.name forKey:@"GBInterfaceTheme"];
     80             [[self presentingViewController] dismissViewControllerAnimated:true completion:nil];
     81         }]];
     82     }
     83     [alert addAction:[UIAlertAction actionWithTitle:@"Exit Preview"
     84                                               style:UIAlertActionStyleDefault
     85                                             handler:^(UIAlertAction *action) {
     86         [[self presentingViewController] dismissViewControllerAnimated:true completion:nil];
     87     }]];
     88     [self presentViewController:alert animated:true completion:^{
     89         alert.view.superview.userInteractionEnabled = true;
     90         [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
     91                                                                                            action:@selector(dismissPopup)]];
     92         for (UIView *view in alert.view.superview.subviews) {
     93             if (view.backgroundColor) {
     94                 view.userInteractionEnabled = true;
     95                 [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
     96                                                                                    action:@selector(dismissPopup)]];
     97 
     98             }
     99         }
    100     }];
    101 }
    102 
    103 - (void)dismissViewController
    104 {
    105     [self dismissViewControllerAnimated:true completion:nil];
    106 }
    107 
    108 - (void)dismissPopup
    109 {
    110     [self dismissViewControllerAnimated:true completion:nil];
    111 }
    112 
    113 - (UIStatusBarStyle)preferredStatusBarStyle
    114 {
    115     if (@available(iOS 13.0, *)) {
    116         return _verticalLayout.theme.isDark? UIStatusBarStyleLightContent : UIStatusBarStyleDarkContent;
    117     }
    118     return _verticalLayout.theme.isDark? UIStatusBarStyleLightContent : UIStatusBarStyleDefault;
    119 }
    120 
    121 - (UIInterfaceOrientationMask)supportedInterfaceOrientations
    122 {
    123     if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    124         return UIInterfaceOrientationMaskAll;
    125     }
    126     if (MAX([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width) <= 568) {
    127         return UIInterfaceOrientationMaskLandscape;
    128     }
    129     return UIInterfaceOrientationMaskAllButUpsideDown;
    130 }
    131 
    132 @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.