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