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@implementation GBImageView
14{
15    NSTrackingArea *trackingArea;
16}
17- (void)drawRect:(NSRect)dirtyRect
18{
19    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
20    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
21    [super drawRect:dirtyRect];
22    CGFloat y_ratio = self.frame.size.height / self.image.size.height;
23    CGFloat x_ratio = self.frame.size.width / self.image.size.width;
24    for (GBImageViewGridConfiguration *conf in self.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 self.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 (self.displayScrollRect) {
47        NSBezierPath *path = [NSBezierPath bezierPathWithRect:CGRectInfinite];
48        for (unsigned x = 0; x < 2; x++) {
49            for (unsigned y = 0; y < 2; y++) {
50                NSRect rect = self.scrollRect;
51                rect.origin.x *= x_ratio;
52                rect.origin.y *= y_ratio;
53                rect.size.width *= x_ratio;
54                rect.size.height *= y_ratio;
55                rect.origin.y = self.frame.size.height - rect.origin.y - rect.size.height;
56
57                rect.origin.x -= self.frame.size.width * x;
58                rect.origin.y += self.frame.size.height * y;
59
60
61                NSBezierPath *subpath = [NSBezierPath bezierPathWithRect:rect];
62                [path appendBezierPath:subpath];
63            }
64        }
65        [path setWindingRule:NSEvenOddWindingRule];
66        [path setLineWidth:4.0];
67        [path setLineJoinStyle:NSRoundLineJoinStyle];
68        [[NSColor colorWithWhite:0.2 alpha:0.5] set];
69        [path fill];
70        [path addClip];
71        [[NSColor colorWithWhite:0.0 alpha:0.6] set];
72        [path stroke];
73    }
74}
75
76- (void)setHorizontalGrids:(NSArray *)horizontalGrids
77{
78    self->_horizontalGrids = horizontalGrids;
79    [self setNeedsDisplay];
80}
81
82- (void)setVerticalGrids:(NSArray *)verticalGrids
83{
84    self->_verticalGrids = verticalGrids;
85    [self setNeedsDisplay];
86}
87
88- (void)setDisplayScrollRect:(bool)displayScrollRect
89{
90    self->_displayScrollRect = displayScrollRect;
91    [self setNeedsDisplay];
92}
93
94- (void)updateTrackingAreas
95{
96    if (trackingArea != nil) {
97        [self removeTrackingArea:trackingArea];
98    }
99
100    trackingArea = [ [NSTrackingArea alloc] initWithRect:[self bounds]
101                                                 options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingMouseMoved
102                                                   owner:self
103                                                userInfo:nil];
104    [self addTrackingArea:trackingArea];
105}
106
107- (void)mouseExited:(NSEvent *)theEvent
108{
109    if ([self.delegate respondsToSelector:@selector(mouseDidLeaveImageView:)]) {
110        [self.delegate mouseDidLeaveImageView:self];
111    }
112}
113
114- (void)mouseMoved:(NSEvent *)theEvent
115{
116    if ([self.delegate respondsToSelector:@selector(imageView:mouseMovedToX:Y:)]) {
117        NSPoint location = [self convertPoint:theEvent.locationInWindow fromView:nil];
118        location.x /= self.bounds.size.width;
119        location.y /= self.bounds.size.height;
120        location.y = 1 - location.y;
121        location.x *= self.image.size.width;
122        location.y *= self.image.size.height;
123        [self.delegate imageView:self mouseMovedToX:(NSUInteger)location.x Y:(NSUInteger)location.y];
124    }
125}
126
127@end
128