OSDN Git Service

UIを調整
[kcd/KCD.git] / KCD / HMGuardEscapedView.m
1 //
2 //  HMGuardEscapedView.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2015/08/10.
6 //  Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMGuardEscapedView.h"
10
11
12 static NSString *taiString = nil;
13 static NSString *hiString = nil;
14
15 @implementation HMGuardEscapedView
16
17 + (void)initialize
18 {
19         static dispatch_once_t onceToken;
20         dispatch_once(&onceToken, ^{
21                 NSBundle *mainBundle = [NSBundle mainBundle];
22                 NSString *path  = [mainBundle pathForResource:@"Taihi" ofType:@"txt"];
23                 NSError *error = nil;
24                 NSString *taihiString = [[NSString alloc] initWithContentsOfFile:path
25                                                                                                                                 encoding:NSUTF8StringEncoding
26                                                                                                                                    error:&error];
27                 if(!taihiString) {
28                         if(error) {
29                                 NSLog(@"Could not find Taihi.txt. Error -> %@", error);
30                                 NSBeep();
31                                 return;
32                         }
33                         NSLog(@"Could not find Taihi.txt");
34                         NSBeep();
35                         return;
36                 }
37                 
38                 if(taihiString.length != 2) {
39                         NSLog(@"Taihi.txt length is not two.");
40                         NSBeep();
41                         return;
42                 }
43                 taiString = [taihiString substringToIndex:1];
44                 hiString = [taihiString substringFromIndex:1];
45         });
46 }
47 - (void)drawRect:(NSRect)dirtyRect {
48     [super drawRect:dirtyRect];
49         
50         NSRect bounds = self.bounds;
51         
52         NSBezierPath *fillPath = [NSBezierPath bezierPathWithRect:bounds];
53         [[NSColor colorWithCalibratedWhite:0.9 alpha:0.8] set];
54         [fillPath fill];
55         
56         switch(self.controlSize) {
57                 case NSRegularControlSize:
58                         [self drawTaihiInrect:bounds];
59                         break;
60                 case NSSmallControlSize:
61                 case NSMiniControlSize:
62                         [self drawSmallTaihiInrect:bounds];
63                         break;
64         }
65 }
66
67 - (void)drawTaihiInrect:(NSRect)bounds
68 {
69         
70         NSAffineTransform *rotate = [NSAffineTransform transform];
71         [rotate translateXBy:0.0 yBy:65.0];
72         [rotate rotateByDegrees:-27];
73         [rotate concat];
74         
75         const CGFloat width = 50;
76         const CGFloat height = 100;
77         NSRect rect = NSMakeRect(
78                                                          (NSInteger)((bounds.size.width - width) * 0.5),
79                                                          (NSInteger)((bounds.size.height - height) * 0.5),
80                                                          width, height);
81         
82         NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
83         
84         [[NSColor whiteColor] set];
85         [path fill];
86         
87         [[NSColor blackColor] set];
88         [path stroke];
89         
90         rect = NSInsetRect(rect, 3, 3);
91         path = [NSBezierPath bezierPathWithRect:rect];
92         path.lineWidth = 2;
93         [path stroke];
94         
95         NSDictionary *taiAttr = @{
96                                                           NSForegroundColorAttributeName : [NSColor lightGrayColor],
97                                                           NSFontAttributeName : [NSFont boldSystemFontOfSize:width - 10],
98                                                           };
99         NSAttributedString *tai = [[NSAttributedString alloc] initWithString:taiString attributes:taiAttr];
100         
101         NSAttributedString *hi = [[NSAttributedString alloc] initWithString:hiString attributes:taiAttr];
102         
103         rect = NSInsetRect(rect, 2, 2);
104         rect.origin.y += 4;
105         rect.size.height -= 2;
106         [tai drawInRect:rect];
107         rect.size.height *= 0.5;
108         [hi drawInRect:rect];
109 }
110
111 - (void)drawSmallTaihiInrect:(NSRect)bounds
112 {
113         const CGFloat width = 100;
114         const CGFloat height = 50;
115         NSRect rect = NSMakeRect(
116                                                          (NSInteger)((bounds.size.width - width) * 0.5),
117                                                          (NSInteger)((bounds.size.height - height) * 0.5),
118                                                          width, height);
119         
120         NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
121         
122         [[NSColor whiteColor] set];
123         [path fill];
124         
125         [[NSColor blackColor] set];
126         [path stroke];
127         
128         rect = NSInsetRect(rect, 3, 3);
129         path = [NSBezierPath bezierPathWithRect:rect];
130         path.lineWidth = 2;
131         [path stroke];
132         
133         NSDictionary *taiAttr = @{
134                                                           NSForegroundColorAttributeName : [NSColor lightGrayColor],
135                                                           NSFontAttributeName : [NSFont boldSystemFontOfSize:height - 14],
136                                                           };
137         NSAttributedString *tai = [[NSAttributedString alloc] initWithString:taiString attributes:taiAttr];
138         
139         NSAttributedString *hi = [[NSAttributedString alloc] initWithString:hiString attributes:taiAttr];
140         
141         rect = NSInsetRect(rect, 2, 2);
142         rect.origin.y += 4;
143         rect.origin.x += 4;
144         rect.size.height -= 2;
145         [tai drawInRect:rect];
146         rect.origin.x += rect.size.width * 0.5;
147         [hi drawInRect:rect];
148 }
149
150 @end