OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / GuardEscapedView.swift
1 //
2 //  GuardEscapedView.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/03.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class GuardEscapedView: NSView {
12     
13     private static var taihiStrings: String = {
14         
15         guard let url = Bundle.main.url(forResource: "Taihi", withExtension: "txt"),
16             let taihiString = try? String(contentsOf: url, encoding: .utf8)
17             else { fatalError("Can not load Taihi.txt") }
18         
19         guard (taihiString as NSString).length == 2
20             else { fatalError("Taihi string is not 2 charactor") }
21         
22          return taihiString
23     }()
24     
25     var controlSize: NSControl.ControlSize = .regular
26     private var taiString: String {
27         
28         let s = GuardEscapedView.taihiStrings
29         let range = s.index(s.startIndex, offsetBy: 0)
30         
31         return String(s[range])
32     }
33     
34     private var hiString: String {
35         
36         let s = GuardEscapedView.taihiStrings
37         let range = s.index(s.endIndex, offsetBy: -1)
38         
39         return String(s[range])
40     }
41     
42     override func draw(_ dirtyRect: NSRect) {
43         
44         super.draw(dirtyRect)
45
46         let bounds = self.bounds
47         NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
48         NSBezierPath(rect: bounds).fill()
49         
50         switch controlSize {
51         case .regular: drawTaihi(in: bounds)
52         case .small, .mini: drawSmallTaihi(in: bounds)
53         }
54     }
55     
56     private func drawTaihi(in bounds: NSRect) {
57         
58         let rotate = NSAffineTransform()
59         rotate.translateX(by: 0.0, yBy: 65.0)
60         rotate.rotate(byDegrees: -27)
61         rotate.concat()
62         
63         let width = 50
64         let height = 100
65         let x = Int((bounds.width - CGFloat(width)) * 0.5)
66         let y = Int((bounds.height - CGFloat(height)) * 0.5)
67         let rect = NSRect(x: x, y: y, width: width, height: height)
68         let path = NSBezierPath(rect: rect)
69         NSColor.white.setFill()
70         NSColor.black.setStroke()
71         path.fill()
72         path.stroke()
73         
74         let borderRect = rect.insetBy(dx: 3, dy: 3)
75         let borderPath = NSBezierPath(rect: borderRect)
76         borderPath.lineWidth = 2.0
77         borderPath.stroke()
78         
79         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
80         let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
81                            NSAttributedStringKey.font: fontSize ]
82         let tai = NSAttributedString(string: taiString, attributes: attributes)
83         let hi = NSAttributedString(string: hiString, attributes: attributes)
84         
85         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
86         stringRect.origin.y += 4
87         stringRect.origin.x += 1.5
88         stringRect.size.height -= 2
89         tai.draw(in: stringRect)
90         stringRect.size.height *= 0.5
91         hi.draw(in: stringRect)
92     }
93     
94     private func drawSmallTaihi(in bounds: NSRect) {
95         
96         let width = 100
97         let height = 50
98         let x = Int((bounds.width - CGFloat(width)) * 0.5)
99         let y = Int((bounds.height - CGFloat(height)) * 0.5)
100         let rect = NSRect(x: x, y: y, width: width, height: height)
101         let path = NSBezierPath(rect: rect)
102         NSColor.white.setFill()
103         NSColor.black.setStroke()
104         path.fill()
105         path.stroke()
106         
107         let borderRect = rect.insetBy(dx: 3, dy: 3)
108         let borderPath = NSBezierPath(rect: borderRect)
109         borderPath.lineWidth = 2.0
110         borderPath.stroke()
111         
112         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
113         let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
114                            NSAttributedStringKey.font: fontSize ]
115         let tai = NSAttributedString(string: taiString, attributes: attributes)
116         let hi = NSAttributedString(string: hiString, attributes: attributes)
117         
118         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
119         stringRect.origin.y += 5
120         stringRect.origin.x += 5
121         stringRect.size.height -= 2
122         tai.draw(in: stringRect)
123         stringRect.origin.x += stringRect.width * 0.5 + 1
124         hi.draw(in: stringRect)
125     }
126 }