git.y1.nz

SameBoy

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

iOS/GBPrinterFeedController.m

      1 #import "GBPrinterFeedController.h"
      2 #import "GBViewController.h"
      3 #import "GBActivityViewController.h"
      4 
      5 @implementation GBPrinterFeedController
      6 {
      7     UIImage *_image;
      8 }
      9 
     10 - (instancetype)initWithImage:(UIImage *)image
     11 {
     12     _image = image;
     13     UIViewController *scrollViewController = [[UIViewController alloc] init];
     14     scrollViewController.title = @"Printer Feed";
     15     if (@available(iOS 13.0, *)) {
     16         scrollViewController.view.backgroundColor = [UIColor systemBackgroundColor];
     17     }
     18     else {
     19         scrollViewController.view.backgroundColor = [UIColor whiteColor];
     20     }
     21     
     22     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewController.view.bounds];
     23     
     24     scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
     25     scrollView.scrollEnabled = true;
     26     scrollView.pagingEnabled = false;
     27     scrollView.showsVerticalScrollIndicator = true;
     28     scrollView.showsHorizontalScrollIndicator = false;
     29     [scrollViewController.view addSubview:scrollView];
     30     
     31     CGSize size = image.size;
     32     while (size.width < 320) {
     33         size.width *= 2;
     34         size.height *= 2;
     35     }
     36     UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
     37     imageView.contentMode = UIViewContentModeScaleToFill;
     38     imageView.frame = (CGRect){{0, 0}, size};
     39     imageView.layer.magnificationFilter = kCAFilterNearest;
     40     
     41     scrollView.contentSize = size;
     42     [scrollView addSubview:imageView];
     43     
     44     CGSize contentSize = size;
     45     self.preferredContentSize = contentSize;
     46     
     47     self = [self initWithRootViewController:scrollViewController];
     48     UIBarButtonItem *close = [[UIBarButtonItem alloc] initWithTitle:@"Close"
     49                                                               style:UIBarButtonItemStylePlain
     50                                                              target:self
     51                                                              action:@selector(dismissFromParent)];
     52     [self.visibleViewController.navigationItem setLeftBarButtonItem:close];
     53     [scrollViewController setToolbarItems:@[
     54         [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
     55                                                       target:self
     56                                                       action:@selector(presentShareSheet)],
     57         [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
     58                                                       target:nil
     59                                                       action:nil],
     60         [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
     61                                                       target:self
     62                                                       action:@selector(emptyFeed)],
     63 
     64     ] animated:false];
     65     [self setToolbarHidden:false animated:false];
     66     return self;
     67 }
     68 
     69 - (UIView *)viewToMask
     70 {
     71     UIView *targetView = self.view;
     72     while (targetView.superview != targetView.window &&
     73            targetView.superview.superview != targetView.window){
     74         targetView = targetView.superview;
     75     }
     76     return targetView;
     77 }
     78 
     79 - (void)viewWillLayoutSubviews
     80 {
     81     [super viewWillLayoutSubviews];
     82     if ([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad) {
     83         UIView *targetView = self.view;
     84         CGRect frame = targetView.frame;
     85         frame.origin.x = ([UIScreen mainScreen].bounds.size.width - 320) / 2;
     86         frame.size.width = 320;
     87         targetView.frame = frame;
     88         
     89         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(frame.origin.x, 0,
     90                                                                                     320, 4096)
     91                                                        byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
     92                                                              cornerRadii:CGSizeMake(10, 10)];
     93         
     94         CAShapeLayer *maskLayer = [CAShapeLayer layer];
     95         maskLayer.frame = self.view.bounds;
     96         maskLayer.path = maskPath.CGPath;
     97 
     98         self.viewToMask.layer.mask = maskLayer;
     99         
    100     }
    101 }
    102 
    103 - (void)presentShareSheet
    104 {
    105     NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"Game Boy Printer Image.png"]];
    106     [UIImagePNGRepresentation(_image) writeToURL:url atomically:false];
    107     [self presentViewController:[[GBActivityViewController alloc] initWithActivityItems:@[url]
    108                                                                   applicationActivities:nil]
    109                        animated:true
    110                      completion:nil];
    111 }
    112 
    113 - (void)emptyFeed
    114 {
    115     [(GBViewController *)UIApplication.sharedApplication.delegate emptyPrinterFeed];
    116 }
    117 
    118 - (void)dismissFromParent
    119 {
    120     [self.presentingViewController dismissViewControllerAnimated:true completion:nil];
    121 }
    122 
    123 - (UIModalPresentationStyle)modalPresentationStyle
    124 {
    125     return UIModalPresentationFormSheet;
    126 }
    127 @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.