OSDN Git Service

不要となっていたプロパティを削除
[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) else {
17                 
18                 fatalError("Can not load Taihi.txt")
19         }
20         
21         guard (taihiString as NSString).length == 2 else {
22             
23             fatalError("Taihi string is not 2 charactor")
24         }
25         
26          return taihiString
27     }()
28     
29     var controlSize: NSControl.ControlSize = .regular
30     private var taiString: String {
31         
32         let s = GuardEscapedView.taihiStrings
33         let range = s.index(s.startIndex, offsetBy: 0)
34         
35         return String(s[range])
36     }
37     
38     private var hiString: String {
39         
40         let s = GuardEscapedView.taihiStrings
41         let range = s.index(s.endIndex, offsetBy: -1)
42         
43         return String(s[range])
44     }
45     
46     override func draw(_ dirtyRect: NSRect) {
47         
48         super.draw(dirtyRect)
49
50         let bounds = self.bounds
51         NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
52         NSBezierPath(rect: bounds).fill()
53         
54         switch controlSize {
55             
56         case .regular: drawTaihi(in: bounds)
57             
58         case .small, .mini: drawSmallTaihi(in: bounds)
59             
60         }
61     }
62     
63     private func drawTaihi(in bounds: NSRect) {
64         
65         let rotate = NSAffineTransform()
66         rotate.translateX(by: 0.0, yBy: 65.0)
67         rotate.rotate(byDegrees: -27)
68         rotate.concat()
69         
70         let width = 50
71         let height = 100
72         let x = Int((bounds.width - CGFloat(width)) * 0.5)
73         let y = Int((bounds.height - CGFloat(height)) * 0.5)
74         let rect = NSRect(x: x, y: y, width: width, height: height)
75         let path = NSBezierPath(rect: rect)
76         NSColor.white.setFill()
77         NSColor.black.setStroke()
78         path.fill()
79         path.stroke()
80         
81         let borderRect = rect.insetBy(dx: 3, dy: 3)
82         let borderPath = NSBezierPath(rect: borderRect)
83         borderPath.lineWidth = 2.0
84         borderPath.stroke()
85         
86         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
87         let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
88                            NSAttributedStringKey.font: fontSize ]
89         let tai = NSAttributedString(string: taiString, attributes: attributes)
90         let hi = NSAttributedString(string: hiString, attributes: attributes)
91         
92         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
93         stringRect.origin.y += 4
94         stringRect.origin.x += 1.5
95         stringRect.size.height -= 2
96         tai.draw(in: stringRect)
97         stringRect.size.height *= 0.5
98         hi.draw(in: stringRect)
99     }
100     
101     private func drawSmallTaihi(in bounds: NSRect) {
102         
103         let width = 100
104         let height = 50
105         let x = Int((bounds.width - CGFloat(width)) * 0.5)
106         let y = Int((bounds.height - CGFloat(height)) * 0.5)
107         let rect = NSRect(x: x, y: y, width: width, height: height)
108         let path = NSBezierPath(rect: rect)
109         NSColor.white.setFill()
110         NSColor.black.setStroke()
111         path.fill()
112         path.stroke()
113         
114         let borderRect = rect.insetBy(dx: 3, dy: 3)
115         let borderPath = NSBezierPath(rect: borderRect)
116         borderPath.lineWidth = 2.0
117         borderPath.stroke()
118         
119         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
120         let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
121                            NSAttributedStringKey.font: fontSize ]
122         let tai = NSAttributedString(string: taiString, attributes: attributes)
123         let hi = NSAttributedString(string: hiString, attributes: attributes)
124         
125         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
126         stringRect.origin.y += 5
127         stringRect.origin.x += 5
128         stringRect.size.height -= 2
129         tai.draw(in: stringRect)
130         stringRect.origin.x += stringRect.width * 0.5 + 1
131         hi.draw(in: stringRect)
132     }
133 }