git.y1.nz

SameBoy

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

iOS/GBHorizontalLayout.m

      1 #define GBLayoutInternal
      2 #import "GBHorizontalLayout.h"
      3 
      4 @implementation GBHorizontalLayout
      5 
      6 - (instancetype)initWithTheme:(GBTheme *)theme cutoutOnRight:(bool)cutoutOnRight
      7 {
      8     self = [super initWithTheme:theme];
      9     if (!self) return nil;
     10     
     11     CGSize resolution = {self.resolution.height, self.resolution.width};
     12     
     13     CGRect screenRect = {0,};
     14     screenRect.size.height = self.hasFractionalPixels? (resolution.height - self.homeBar) : floor((resolution.height - self.homeBar) / 144) * 144;
     15     screenRect.size.width = screenRect.size.height / 144 * 160;
     16     
     17     screenRect.origin.x = (resolution.width - screenRect.size.width) / 2;
     18     screenRect.origin.y = (resolution.height - self.homeBar - screenRect.size.height) / 2;
     19     self.fullScreenRect = screenRect;
     20     
     21     double horizontalMargin, verticalMargin;
     22     while (true) {
     23         horizontalMargin = (resolution.width - screenRect.size.width) / 2;
     24         verticalMargin = (resolution.height - self.homeBar - screenRect.size.height) / 2;
     25         if (horizontalMargin / self.factor < 164) {
     26             if (self.hasFractionalPixels) {
     27                 screenRect.size.width = resolution.width - 164 * self.factor * 2;
     28                 screenRect.size.height = screenRect.size.width / 160 * 144;
     29                 continue;
     30             }
     31             screenRect.size.width -= 160;
     32             screenRect.size.height -= 144;
     33             continue;
     34         }
     35         break;
     36     }
     37     
     38     double screenBorderWidth = MIN(screenRect.size.width / 40, 16 * self.factor);
     39 
     40     screenRect.origin.x = (resolution.width - screenRect.size.width) / 2;
     41     bool drawSameBoyLogo = false;
     42     if (verticalMargin * 2 > screenBorderWidth * 7) {
     43         drawSameBoyLogo = true;
     44         screenRect.origin.y = (resolution.height - self.homeBar - screenRect.size.height - screenBorderWidth * 5) / 2;
     45     }
     46     else {
     47         screenRect.origin.y = (resolution.height - self.homeBar - screenRect.size.height) / 2;
     48     }
     49         
     50     self.screenRect = screenRect;
     51     
     52     self.dpadLocation = (CGPoint){
     53         round((screenRect.origin.x - screenBorderWidth) / 2) + (cutoutOnRight? 0 : self.cutout / 2),
     54         round(resolution.height * 3 / 8)
     55     };
     56         
     57     double longWing = (resolution.width - screenRect.size.width) / 2 - screenBorderWidth * 5;
     58     double shortWing = longWing - self.cutout;
     59     double buttonRadius = 36 * self.factor;
     60     CGSize buttonsDelta = [self buttonDeltaForMaxHorizontalDistance:(cutoutOnRight? shortWing : longWing) - buttonRadius * 2];
     61     CGPoint buttonsCenter = {
     62         (resolution.width + screenRect.size.width + screenRect.origin.x) / 2 - (cutoutOnRight? self.cutout / 2 : 0),
     63         self.dpadLocation.y,
     64     };
     65     
     66     self.abComboLocation = buttonsCenter;
     67     
     68     self.aLocation = (CGPoint) {
     69         round(buttonsCenter.x + buttonsDelta.width / 2),
     70         round(buttonsCenter.y - buttonsDelta.height / 2)
     71     };
     72     
     73     self.bLocation = (CGPoint) {
     74         round(buttonsCenter.x - buttonsDelta.width / 2),
     75         round(buttonsCenter.y + buttonsDelta.height / 2)
     76     };
     77 
     78     self.selectLocation = (CGPoint){
     79         self.dpadLocation.x + (cutoutOnRight? self.cutout / 2 : 0),
     80         MIN(round(resolution.height * 3 / 4), self.dpadLocation.y + 180 * self.factor)
     81     };
     82     
     83     self.startLocation = (CGPoint){
     84         buttonsCenter.x -  (cutoutOnRight? 0 : self.cutout / 2 ),
     85         self.selectLocation.y
     86     };
     87 
     88     
     89     if (theme.renderingPreview) {
     90         UIGraphicsBeginImageContextWithOptions((CGSize){resolution.width / 8, resolution.height / 8}, true, 1);
     91         CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1 / 8.0, 1 / 8.0);
     92     }
     93     else {
     94         UIGraphicsBeginImageContextWithOptions(resolution, true, 1);
     95     }
     96     [self drawBackground];
     97     [self drawScreenBezels];
     98     
     99     [self drawThemedLabelsWithBlock:^{
    100         if (drawSameBoyLogo) {
    101             double bezelBottom = screenRect.origin.y + screenRect.size.height + screenBorderWidth;
    102             double freeSpace = resolution.height - bezelBottom;
    103             [self drawLogoInVerticalRange:(NSRange){bezelBottom + screenBorderWidth * 2, freeSpace - screenBorderWidth * 4}
    104                            controlPadding:0];
    105         }
    106         
    107         [self drawLabels];
    108     }];
    109     
    110     self.background = UIGraphicsGetImageFromCurrentImageContext();
    111     UIGraphicsEndImageContext();
    112     return self;
    113 }
    114 
    115 - (instancetype)initWithTheme:(GBTheme *)theme
    116 {
    117     assert(false);
    118     __builtin_unreachable();
    119 }
    120 
    121 - (CGRect)viewRectForOrientation:(UIInterfaceOrientation)orientation
    122 {
    123     return CGRectMake(0, 0, self.background.size.width / self.factor, self.background.size.height / self.factor);
    124 }
    125 
    126 - (CGSize)size
    127 {
    128     return (CGSize){self.resolution.height, self.resolution.width};
    129 }
    130 
    131 @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.