OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMMaskSelectView.m
1 //
2 //  HMMaskSelectView.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/09/05.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMMaskSelectView.h"
10
11 #import "HMMaskInfomation.h"
12
13
14 const NSInteger kNumberOfMask = 4;
15 static NSRect maskRects[kNumberOfMask];
16
17 static NSSize originalSize;
18
19 @implementation HMMaskSelectView
20
21 + (void)initialize
22 {
23         static dispatch_once_t onceToken;
24         dispatch_once(&onceToken, ^{
25                 maskRects[0] = NSMakeRect(113, 455, 100, 18);
26                 maskRects[1] = NSMakeRect(203, 330, 200, 25);
27                 maskRects[2] = NSMakeRect(95, 378, 100, 18);
28                 maskRects[3] = NSMakeRect(60, 378, 100, 18);
29                 
30                 originalSize = NSMakeSize(800, 480);
31         });
32 }
33
34 - (id)initWithFrame:(NSRect)frame
35 {
36     self = [super initWithFrame:frame];
37     if(self) {
38                 [self buildMask];
39     }
40     return self;
41 }
42
43 - (id)initWithCoder:(NSCoder *)aDecoder
44 {
45         self = [super initWithCoder:aDecoder];
46         if(self) {
47                 [self buildMask];
48         }
49         return self;
50 }
51
52 - (void)buildMask
53 {
54         self.masks = [NSMutableArray new];
55         NSRect frame = self.frame;
56         CGFloat widthRatio = frame.size.width / originalSize.width;
57         CGFloat heightRatio = frame.size.height / originalSize.height;
58         
59         for(NSInteger i = 0; i < kNumberOfMask; i++ ) {
60                 HMMaskInfomation *info = [HMMaskInfomation new];
61                 NSRect maskRect = NSMakeRect(
62                                                                          maskRects[i].origin.x * widthRatio,
63                                                                          maskRects[i].origin.y * heightRatio,
64                                                                          maskRects[i].size.width * widthRatio,
65                                                                          maskRects[i].size.height * heightRatio
66                                                                          );
67                 info.maskRect = maskRect;
68                 [self.masks addObject:info];
69                 if(i == 3) {
70                         info.borderColor = [NSColor colorWithCalibratedRed:0.000 green:0.011 blue:1.000 alpha:1.000];
71                 } else {
72                         info.borderColor = [NSColor redColor];
73                 }
74                 info.maskColor = [NSColor blackColor];
75         }
76 }
77
78 - (void)drawRect:(NSRect)dirtyRect
79 {
80     NSGraphicsContext *context = [NSGraphicsContext currentContext];
81         [context saveGraphicsState];
82         [context setShouldAntialias:NO];
83         
84         CGFloat dashSeed[] = {3.0, 3.0};
85         for(HMMaskInfomation *info in self.masks) {
86                 NSBezierPath *path = [NSBezierPath bezierPathWithRect:info.maskRect];
87                 if(info.enable) {
88                         [info.maskColor set];
89                         [path fill];
90                 }
91                 [path setLineDash:dashSeed count:2 phase:0];
92                 [info.borderColor set];
93                 [path stroke];
94                 [path setLineDash:dashSeed count:2 phase:3.0];
95                 [[NSColor lightGrayColor] set];
96                 [path stroke];
97         }
98         
99         [context restoreGraphicsState];
100         
101 #if 0
102         NSBezierPath *path = [NSBezierPath bezierPath];
103         for(int i = 0; i < 800; i += 50) {
104                 [path moveToPoint:NSMakePoint(i, 0)];
105                 [path lineToPoint:NSMakePoint(i, 480)];
106         }
107         for(int j = 0; j < 480; j += 50) {
108                 [path moveToPoint:NSMakePoint(0, j)];
109                 [path lineToPoint:NSMakePoint(800, j)];
110         }
111         [[NSColor whiteColor] set];
112         [path stroke];
113 #endif
114 }
115
116 - (void)mouseUp:(NSEvent *)event
117 {
118         NSPoint mouse = [event locationInWindow];
119         mouse = [self convertPoint:mouse fromView:nil];
120         
121         for(HMMaskInfomation *info in [self.masks reverseObjectEnumerator]) {
122                 if(NSMouseInRect(mouse, info.maskRect, self.isFlipped)) {
123                         info.enable = !info.enable;
124                         [self setNeedsDisplayInRect:NSInsetRect(info.maskRect, -5, -5)];
125                         break;
126                 }
127         }
128 }
129
130 - (IBAction)disableAllMasks:(id)sender
131 {
132         for(HMMaskInfomation *info in self.masks) {
133                 info.enable = NO;
134         }
135         [self setNeedsDisplay:YES];
136 }
137
138 @end