OSDN Git Service

MappingConfigurationを簡略化
[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 class GuardEscapedView: NSView {
12     private static var taihiStrings: String = {
13         guard let url = Bundle.main.url(forResource: "Taihi", withExtension: "txt"),
14             let taihiString = try? String(contentsOf: url, encoding: .utf8)
15             else { fatalError("Can not load Taihi.txt") }
16         guard (taihiString as NSString).length == 2 else { fatalError("Taihi string is not 2 charactor") }
17          return taihiString
18     }()
19     
20     var controlSize: NSControlSize = .regular
21     private var taiString: String {
22         let s = GuardEscapedView.taihiStrings
23         let range = s.index(s.startIndex, offsetBy: 0)
24         return String(s[range])
25     }
26     private var hiString: String {
27         let s = GuardEscapedView.taihiStrings
28         let range = s.index(s.endIndex, offsetBy: -1)
29         return String(s[range])
30     }
31     
32     override func draw(_ dirtyRect: NSRect) {
33         super.draw(dirtyRect)
34
35         let bounds = self.bounds
36         NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
37         NSBezierPath(rect: bounds).fill()
38         
39         switch controlSize {
40         case .regular: drawTaihi(in: bounds)
41         case .small, .mini: drawSmallTaihi(in: bounds)
42         }
43     }
44     
45     private func drawTaihi(in bounds: NSRect) {
46         let rotate = NSAffineTransform()
47         rotate.translateX(by: 0.0, yBy: 65.0)
48         rotate.rotate(byDegrees: -27)
49         rotate.concat()
50         
51         let width = 50
52         let height = 100
53         let x = Int((bounds.width - CGFloat(width)) * 0.5)
54         let y = Int((bounds.height - CGFloat(height)) * 0.5)
55         let rect = NSRect(x: x, y: y, width: width, height: height)
56         let path = NSBezierPath(rect: rect)
57         NSColor.white.setFill()
58         NSColor.black.setStroke()
59         path.fill()
60         path.stroke()
61         
62         let borderRect = rect.insetBy(dx: 3, dy: 3)
63         let borderPath = NSBezierPath(rect: borderRect)
64         borderPath.lineWidth = 2.0
65         borderPath.stroke()
66         
67         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
68         let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
69                            NSFontAttributeName: fontSize ]
70         let tai = NSAttributedString(string: taiString, attributes: attributes)
71         let hi = NSAttributedString(string: hiString, attributes: attributes)
72         
73         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
74         stringRect.origin.y += 4
75         stringRect.origin.x += 1.5
76         stringRect.size.height -= 2
77         tai.draw(in: stringRect)
78         stringRect.size.height *= 0.5
79         hi.draw(in: stringRect)
80     }
81     private func drawSmallTaihi(in bounds: NSRect) {
82         let width = 100
83         let height = 50
84         let x = Int((bounds.width - CGFloat(width)) * 0.5)
85         let y = Int((bounds.height - CGFloat(height)) * 0.5)
86         let rect = NSRect(x: x, y: y, width: width, height: height)
87         let path = NSBezierPath(rect: rect)
88         NSColor.white.setFill()
89         NSColor.black.setStroke()
90         path.fill()
91         path.stroke()
92         
93         let borderRect = rect.insetBy(dx: 3, dy: 3)
94         let borderPath = NSBezierPath(rect: borderRect)
95         borderPath.lineWidth = 2.0
96         borderPath.stroke()
97         
98         let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
99         let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
100                            NSFontAttributeName: fontSize ]
101         let tai = NSAttributedString(string: taiString, attributes: attributes)
102         let hi = NSAttributedString(string: hiString, attributes: attributes)
103         
104         var stringRect = borderRect.insetBy(dx: 2, dy: 2)
105         stringRect.origin.y += 5
106         stringRect.origin.x += 5
107         stringRect.size.height -= 2
108         tai.draw(in: stringRect)
109         stringRect.origin.x += stringRect.width * 0.5 + 1
110         hi.draw(in: stringRect)
111     }
112 }