git.y1.nz

SameBoy

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

iOS/GBColorWell.m

      1 #import "GBColorWell.h"
      2 
      3 @implementation GBColorWell
      4 {
      5     UIView *_proxyView;
      6 }
      7 
      8 - (instancetype)initWithFrame:(CGRect)frame
      9 {
     10     self = [super initWithFrame:frame];
     11     self.opaque = false;
     12     [self addTarget:self action:@selector(setNeedsDisplay) forControlEvents:UIControlEventValueChanged];
     13     return self;
     14 }
     15 
     16 - (void)drawRect:(CGRect)rect
     17 {
     18     if (self.enabled) {
     19         [[UIColor systemFillColor] set];
     20     }
     21     else {
     22         [[UIColor quaternarySystemFillColor] set];
     23     }
     24     rect = self.bounds;
     25     [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:6] fill];
     26     
     27     rect.size.width -= 6;
     28     rect.size.height -= 6;
     29     rect.origin.x += 3;
     30     rect.origin.y += 3;
     31     
     32     if (!self.enabled) {
     33         rect.size.width -= 2;
     34         rect.size.height -= 2;
     35         rect.origin.x += 1;
     36         rect.origin.y += 1;
     37     }
     38     
     39     CGContextRef context = UIGraphicsGetCurrentContext();
     40     
     41     CGContextSaveGState(context);
     42     CGContextSetShadowWithColor(context, (CGSize){0, 0}, 2, [UIColor colorWithWhite:0 alpha:self.enabled? 0.25 : 0.125].CGColor);
     43     [self.selectedColor set];
     44     [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:3] fill];
     45     CGContextRestoreGState(context);
     46 }
     47 
     48 - (void)addSubview:(UIView *)view
     49 {
     50     if (!_proxyView) {
     51         _proxyView = [[UIView alloc] initWithFrame:self.bounds];
     52         [super addSubview:_proxyView];
     53         _proxyView.layer.mask = [CALayer layer];
     54     }
     55     [_proxyView addSubview:view];
     56 }
     57 
     58 - (void)setSelectedColor:(UIColor *)selectedColor
     59 {
     60     [super setSelectedColor:selectedColor];
     61     [self setNeedsDisplay];
     62 }
     63 
     64 - (UIColor *)selectedColor
     65 {
     66     UIColor *orig = [super selectedColor];
     67     CGFloat red, green, blue;
     68     [orig getRed:&red green:&green blue:&blue alpha:nil];
     69     red   = MIN(MAX(red,   0), 1);
     70     green = MIN(MAX(green, 0), 1);
     71     blue  = MIN(MAX(blue,  0), 1);
     72     return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
     73 }
     74 
     75 @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.