git.y1.nz

SameBoy

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

iOS/GBSlotButton.m

      1 #import "GBSlotButton.h"
      2 
      3 @implementation GBSlotButton
      4 {
      5     UIImageView *_imageView;
      6 }
      7 
      8 + (instancetype)buttonWithLabelText:(NSString *)labelText
      9 {
     10     GBSlotButton *ret = [self buttonWithType:UIButtonTypeCustom];
     11     if (!ret) return nil;
     12     ret.frame = CGRectMake(0, 0, 0x100, 0x100);
     13     
     14     ret->_slotSubtitleLabel = [[UILabel alloc] init];
     15     ret->_slotSubtitleLabel.text = @"Empty";
     16     ret->_slotSubtitleLabel.font = [UIFont systemFontOfSize:UIFont.smallSystemFontSize];
     17     if (@available(iOS 13.0, *)) {
     18         ret->_slotSubtitleLabel.textColor = [UIColor secondaryLabelColor];
     19     }
     20     else {
     21         ret->_slotSubtitleLabel.textColor = [UIColor systemGrayColor];
     22     }
     23     [ret->_slotSubtitleLabel sizeToFit];
     24     ret->_slotSubtitleLabel.textAlignment = NSTextAlignmentCenter;
     25     CGRect slotSubtitleLabelRect = ret->_slotSubtitleLabel.frame;
     26     slotSubtitleLabelRect.size.width = 0x100;
     27     slotSubtitleLabelRect.origin.y = 0x100 - slotSubtitleLabelRect.size.height - 8;
     28     ret->_slotSubtitleLabel.frame = slotSubtitleLabelRect;
     29     ret->_slotSubtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
     30     [ret addSubview:ret->_slotSubtitleLabel];
     31     
     32     ret->_label = [[UILabel alloc] init];
     33     ret->_label.text = labelText;
     34     [ret->_label sizeToFit];
     35     ret->_label.textAlignment = NSTextAlignmentCenter;
     36     CGRect labelRect = ret->_label.frame;
     37     labelRect.size.width = 0x100;
     38     labelRect.origin.y = slotSubtitleLabelRect.origin.y - labelRect.size.height - 4;
     39     ret->_label.frame = labelRect;
     40     ret->_label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
     41     [ret addSubview:ret->_label];
     42     
     43     ret.backgroundColor = [UIColor clearColor];
     44     
     45     ret->_imageView = [[UIImageView alloc] initWithImage:nil];
     46     ret.imageView.layer.cornerRadius = 6;
     47     ret.imageView.layer.masksToBounds = true;
     48     if (@available(iOS 13.0, *)) {
     49         ret.imageView.layer.borderColor = [UIColor tertiaryLabelColor] .CGColor;
     50     }
     51     else {
     52         ret.imageView.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor;
     53     }
     54     ret.imageView.layer.borderWidth = 1;
     55     ret.imageView.layer.backgroundColor = [UIColor whiteColor].CGColor;
     56     [ret addSubview:ret.imageView];
     57     
     58     return ret;
     59 }
     60 
     61 - (UIImageView *)imageView
     62 {
     63     return _imageView;
     64 }
     65 
     66 - (void)setHighlighted:(BOOL)highlighted
     67 {
     68     if (_showingMenu) return;
     69     if (highlighted == self.isHighlighted) return;
     70     [super setHighlighted:highlighted];
     71     
     72     [UIView animateWithDuration:0.25 animations:^{
     73         if (highlighted) {
     74             if (@available(iOS 13.0, *)) {
     75                 self.backgroundColor = [UIColor tertiarySystemFillColor];
     76             }
     77             else {
     78                 self.backgroundColor = [UIColor colorWithRed:118 / 255.0
     79                                                        green:118 / 255.0
     80                                                         blue:128 / 255.0
     81                                                        alpha:0.12];
     82             }
     83             self.imageView.layer.opacity = 11 / 12.0;
     84         }
     85         else {
     86             self.backgroundColor = [UIColor clearColor];
     87             self.imageView.layer.opacity = 1;
     88         }
     89     }];
     90 }
     91 
     92 - (void)setFrame:(CGRect)frame
     93 {
     94     [super setFrame:frame];
     95     CGRect screenshotRect = self.bounds;
     96     screenshotRect.size.width -= 8;
     97     screenshotRect.origin.x += 4;
     98     screenshotRect.size.height = _label.frame.origin.y - 16;
     99     screenshotRect.origin.y += 8;
    100     
    101     double scale = [UIApplication sharedApplication].keyWindow.screen.scale;
    102     double nativeWidth = 160.0 / scale;
    103     double nativeHeight = 144.0 / scale;
    104     
    105     if (screenshotRect.size.width > nativeWidth &&
    106         screenshotRect.size.height > nativeHeight) {
    107         self.imageView.layer.magnificationFilter = kCAFilterNearest;
    108         double newWidth = floor(screenshotRect.size.width / nativeWidth) * nativeWidth;
    109         screenshotRect.origin.x += (screenshotRect.size.width - newWidth) / 2;
    110         screenshotRect.size.width = newWidth;
    111         
    112         double newHeight = floor(screenshotRect.size.height / nativeHeight) * nativeHeight;
    113         screenshotRect.origin.y += (screenshotRect.size.height - newHeight) / 2;
    114         screenshotRect.size.height = newHeight;
    115     }
    116     else {
    117         self.imageView.layer.magnificationFilter = kCAFilterLinear;
    118     }
    119     
    120     double aspect = screenshotRect.size.width / screenshotRect.size.height;
    121     if (aspect > 160.0 / 144.0) {
    122         // Too wide
    123         double newWidth = screenshotRect.size.height / 144 * 160;
    124         screenshotRect.origin.x += (screenshotRect.size.width - newWidth) / 2;
    125         screenshotRect.size.width = newWidth;
    126     }
    127     else {
    128         // Too narrow
    129         double newHeight = screenshotRect.size.width / 160 * 144;
    130         screenshotRect.origin.y += (screenshotRect.size.height - newHeight) / 2;
    131         screenshotRect.size.height = newHeight;
    132     }
    133     screenshotRect.origin.x = round(screenshotRect.origin.x);
    134     screenshotRect.origin.y = round(screenshotRect.origin.y);
    135     self.imageView.frame = screenshotRect;
    136 }
    137 
    138 - (void)setShowingMenu:(bool)showingMenu
    139 {
    140     if (showingMenu) {
    141         self.highlighted = true;
    142         _showingMenu = true;
    143     }
    144     else {
    145         _showingMenu = false;
    146         self.highlighted = false;
    147     }
    148 }
    149 
    150 @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.