git.y1.nz

SameBoy

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

Cocoa/GBImageView.m

      1 #import "GBImageView.h"
      2 
      3 @implementation GBImageViewGridConfiguration
      4 - (instancetype)initWithColor:(NSColor *)color size:(NSUInteger)size
      5 {
      6     self = [super init];
      7     self.color = color;
      8     self.size = size;
      9     return self;
     10 }
     11 @end
     12 
     13 @interface GBGridView : NSView
     14 @end
     15 
     16 @implementation GBGridView
     17 
     18 - (void)drawRect:(NSRect)dirtyRect
     19 {
     20     GBImageView *parent = (GBImageView *)self.superview;
     21 
     22     CGFloat y_ratio = parent.frame.size.height / parent.image.size.height;
     23     CGFloat x_ratio = parent.frame.size.width / parent.image.size.width;
     24     for (GBImageViewGridConfiguration *conf in parent.verticalGrids) {
     25         [conf.color set];
     26         for (CGFloat y = conf.size * y_ratio; y < self.frame.size.height; y += conf.size * y_ratio) {
     27             NSBezierPath *line = [NSBezierPath bezierPath];
     28             [line moveToPoint:NSMakePoint(0, y - 0.5)];
     29             [line lineToPoint:NSMakePoint(self.frame.size.width, y - 0.5)];
     30             [line setLineWidth:1.0];
     31             [line stroke];
     32         }
     33     }
     34     
     35     for (GBImageViewGridConfiguration *conf in parent.horizontalGrids) {
     36         [conf.color set];
     37         for (CGFloat x = conf.size * x_ratio; x < self.frame.size.width; x += conf.size * x_ratio) {
     38             NSBezierPath *line = [NSBezierPath bezierPath];
     39             [line moveToPoint:NSMakePoint(x + 0.5, 0)];
     40             [line lineToPoint:NSMakePoint(x + 0.5, self.frame.size.height)];
     41             [line setLineWidth:1.0];
     42             [line stroke];
     43         }
     44     }
     45     
     46     if (parent.displayScrollRect) {
     47         // CGRectInfinite in NSBezierPath is broken in newer macOS versions
     48         NSBezierPath *path = [NSBezierPath bezierPathWithRect:CGRectMake(-0x4000, -0x4000, 0x8000, 0x8000)];
     49         for (unsigned x = 0; x < 2; x++) {
     50             for (unsigned y = 0; y < 2; y++) {
     51                 NSRect rect = parent.scrollRect;
     52                 rect.origin.x *= x_ratio;
     53                 rect.origin.y *= y_ratio;
     54                 rect.size.width *= x_ratio;
     55                 rect.size.height *= y_ratio;
     56                 rect.origin.y = self.frame.size.height - rect.origin.y - rect.size.height;
     57                 
     58                 rect.origin.x -= self.frame.size.width * x;
     59                 rect.origin.y += self.frame.size.height * y;
     60                 
     61                 
     62                 NSBezierPath *subpath = [NSBezierPath bezierPathWithRect:rect];
     63                 [path appendBezierPath:subpath];
     64             }
     65         }
     66         [path setWindingRule:NSEvenOddWindingRule];
     67         [path setLineWidth:4.0];
     68         [path setLineJoinStyle:NSRoundLineJoinStyle];
     69         [[NSColor colorWithWhite:0.2 alpha:0.5] set];
     70         [path fill];
     71         [path addClip];
     72         [[NSColor colorWithWhite:0.0 alpha:0.6] set];
     73         [path stroke];
     74     }
     75 }
     76 @end
     77 
     78 @implementation GBImageView
     79 {
     80     NSTrackingArea *_trackingArea;
     81     GBGridView *_gridView;
     82     NSRect _scrollRect;
     83 }
     84 
     85 - (instancetype)initWithCoder:(NSCoder *)coder
     86 {
     87     self = [super initWithCoder:coder];
     88     self.wantsLayer = true;
     89     _gridView = [[GBGridView alloc] initWithFrame:self.bounds];
     90     _gridView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
     91     [self addSubview:_gridView];
     92     return self;
     93 }
     94 
     95 -(void)viewWillDraw
     96 {
     97     [super viewWillDraw];
     98     for (CALayer *layer in self.layer.sublayers) {
     99         layer.magnificationFilter = kCAFilterNearest;
    100     }
    101 }
    102 
    103 - (void)setHorizontalGrids:(NSArray *)horizontalGrids
    104 {
    105     self->_horizontalGrids = horizontalGrids;
    106     [_gridView setNeedsDisplay:true];
    107 }
    108 
    109 - (void)setVerticalGrids:(NSArray *)verticalGrids
    110 {
    111     self->_verticalGrids = verticalGrids;
    112     [_gridView setNeedsDisplay:true];
    113 }
    114 
    115 - (void)setDisplayScrollRect:(bool)displayScrollRect
    116 {
    117     self->_displayScrollRect = displayScrollRect;
    118     [_gridView setNeedsDisplay:true];
    119 }
    120 
    121 - (void)updateTrackingAreas
    122 {
    123     if (_trackingArea != nil) {
    124         [self removeTrackingArea:_trackingArea];
    125     }
    126     
    127     _trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
    128                                                  options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingMouseMoved
    129                                                    owner:self
    130                                                 userInfo:nil];
    131     [self addTrackingArea:_trackingArea];
    132 }
    133 
    134 - (void)mouseExited:(NSEvent *)theEvent
    135 {
    136     if ([self.delegate respondsToSelector:@selector(mouseDidLeaveImageView:)]) {
    137         [self.delegate mouseDidLeaveImageView:self];
    138     }
    139 }
    140 
    141 - (void)mouseMoved:(NSEvent *)theEvent
    142 {
    143     if ([self.delegate respondsToSelector:@selector(imageView:mouseMovedToX:Y:)]) {
    144         NSPoint location = [self convertPoint:theEvent.locationInWindow fromView:nil];
    145         location.x /= self.bounds.size.width;
    146         location.y /= self.bounds.size.height;
    147         location.y = 1 - location.y;
    148         location.x *= self.image.size.width;
    149         location.y *= self.image.size.height;
    150         [self.delegate imageView:self mouseMovedToX:(NSUInteger)location.x Y:(NSUInteger)location.y];
    151     }
    152 }
    153 
    154 - (void)setScrollRect:(NSRect)scrollRect
    155 {
    156     if (memcmp(&scrollRect, &_scrollRect, sizeof(scrollRect)) != 0) {
    157         _scrollRect = scrollRect;
    158         [_gridView setNeedsDisplay:true];
    159     }
    160 }
    161 
    162 - (NSRect)scrollRect
    163 {
    164     return _scrollRect;
    165 }
    166 
    167 @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.