OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / DamageView.swift
1 //
2 //  DamageView.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/01.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 enum DamageType: Int {
12     
13     case none = 0
14     case slightly
15     case modest
16     case badly
17 }
18
19 final class DamageView: NSView {
20     
21     @objc dynamic var damageType: Int = 0 {
22         
23         willSet {
24             guard let v = DamageType(rawValue: newValue) else {
25                 
26                 print("Can not set damageType")
27                 return
28             }
29             
30             if innerDamageType != v { needsDisplay = true }
31             
32             innerDamageType = v
33         }
34     }
35     
36     var controlSize: NSControl.ControlSize = .regular
37     private var innerDamageType: DamageType = .none
38     private var color: NSColor? {
39         
40         switch innerDamageType {
41         case .none:
42             return nil
43             
44         case .slightly:
45             return #colorLiteral(red: 1.000, green: 0.956, blue: 0.012, alpha: 0.5)
46             
47         case .modest:
48             return NSColor.orange.withAlphaComponent(0.5)
49             
50         case .badly:
51             return NSColor.red.withAlphaComponent(0.5)
52         }
53     }
54     
55     private var borderColor: NSColor? {
56         
57         switch innerDamageType {
58         case .none:
59             return nil
60             
61         case .slightly:
62             return NSColor.orange.withAlphaComponent(0.5)
63             
64         case .modest:
65             return NSColor.orange.withAlphaComponent(0.9)
66             
67         case .badly:
68             return NSColor.red.withAlphaComponent(0.9)
69         }
70     }
71     
72     private var path: NSBezierPath? {
73         
74         switch controlSize {
75         case .regular:
76             return pathForRegular
77             
78         case .small, .mini:
79             return pathForSmall
80         }
81     }
82     
83     private var pathForRegular: NSBezierPath? {
84         
85         let height = bounds.height
86         
87         switch innerDamageType {
88         case .none:
89             return nil
90             
91         case .slightly:
92             return polygon(points:
93                 [
94                     NSPoint(x: 35.0, y: height - 2.0),
95                     NSPoint(x: 0.0, y: height - 2.0),
96                     NSPoint(x: 0.0, y: height - 35.0)
97                 ])
98             
99         case .modest:
100             return polygon(points:
101                 [
102                     NSPoint(x: 50.0, y: height - 2.0),
103                     NSPoint(x: 25.0, y: height - 2.0),
104                     NSPoint(x: 0.0, y: height - 25.0),
105                     NSPoint(x: 0.0, y: height - 50.0)
106                 ])
107             
108         case .badly:
109             let p = polygon(points:
110                 [
111                     NSPoint(x: 60.0, y: height - 2.0),
112                     NSPoint(x: 53.0, y: height - 2.0),
113                     NSPoint(x: 0.0, y: height - 53.0),
114                     NSPoint(x: 0.0, y: height - 60.0)
115                 ])
116             polygon(points:
117                 [
118                     NSPoint(x: 47.0, y: height - 2.0),
119                     NSPoint(x: 23.0, y: height - 2.0),
120                     NSPoint(x: 0.0, y: height - 23.0),
121                     NSPoint(x: 0.0, y: height - 47.0)
122                 ])
123                 .map { p?.append($0) }
124             return p
125         }
126     }
127     
128     private var pathForSmall: NSBezierPath? {
129         
130         let height = bounds.height
131         
132         switch innerDamageType {
133         case .none:
134             return nil
135             
136         case .slightly:
137             return polygon(points:
138                 [
139                     NSPoint(x: 35.0, y: height - 2.0),
140                     NSPoint(x: 0.0, y: height - 2.0),
141                     NSPoint(x: 0.0, y: height - 35.0)
142                 ])
143             
144         case .modest:
145             return polygon(points:
146                 [
147                     NSPoint(x: 50.0, y: height - 2.0),
148                     NSPoint(x: 25.0, y: height - 2.0),
149                     NSPoint(x: 0.0, y: height - 25.0),
150                     NSPoint(x: 0.0, y: height - 50.0)
151                 ])
152             
153         case .badly:
154             let p = polygon(points:
155                 [
156                     NSPoint(x: 55.0, y: height - 2.0),
157                     NSPoint(x: 48.0, y: height - 2.0),
158                     NSPoint(x: 0.0, y: height - 48.0),
159                     NSPoint(x: 0.0, y: height - 55.0)
160                 ])
161             polygon(points:
162                 [
163                     NSPoint(x: 42.0, y: height - 2.0),
164                     NSPoint(x: 20.0, y: height - 2.0),
165                     NSPoint(x: 0.0, y: height - 20.0),
166                     NSPoint(x: 0.0, y: height - 42.0)
167                 ])
168                 .map { p?.append($0) }
169             return p
170         }
171     }
172     
173     override func draw(_ dirtyRect: NSRect) {
174         
175         super.draw(dirtyRect)
176         
177         color?.setFill()
178         borderColor?.setStroke()
179         path?.fill()
180         path?.stroke()
181     }
182 }