OSDN Git Service

fc7fb7f5f2568c2f982dd7e093564f11a69f22aa
[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     dynamic var damageType: Int = 0 {
22         
23         willSet {
24             guard let v = DamageType(rawValue: newValue) else {
25                 self.print("Can not set damageType")
26                 return
27             }
28             
29             if innerDamageType != v { needsDisplay = true }
30             
31             innerDamageType = v
32         }
33     }
34     
35     var controlSize: NSControlSize = .regular
36     private var innerDamageType: DamageType = .none
37     private var color: NSColor? {
38         
39         switch innerDamageType {
40         case .none:
41             return nil
42             
43         case .slightly:
44             return #colorLiteral(red: 1.000, green: 0.956, blue: 0.012, alpha: 0.5)
45             
46         case .modest:
47             return NSColor.orange.withAlphaComponent(0.5)
48             
49         case .badly:
50             return NSColor.red.withAlphaComponent(0.5)
51         }
52     }
53     
54     private var borderColor: NSColor? {
55         
56         switch innerDamageType {
57         case .none:
58             return nil
59             
60         case .slightly:
61             return NSColor.orange.withAlphaComponent(0.5)
62             
63         case .modest:
64             return NSColor.orange.withAlphaComponent(0.9)
65             
66         case .badly:
67             return NSColor.red.withAlphaComponent(0.9)
68         }
69     }
70     
71     private var path: NSBezierPath? {
72         
73         switch controlSize {
74         case .regular:
75             return pathForRegular
76             
77         case .small, .mini:
78             return pathForSmall
79         }
80     }
81     
82     private var pathForRegular: NSBezierPath? {
83         
84         let height = bounds.height
85         
86         switch innerDamageType {
87         case .none:
88             return nil
89             
90         case .slightly:
91             return polygon {
92                 [NSPoint]()
93                     .appended { NSPoint(x: 35.0, y: height - 2.0) }
94                     .appended { NSPoint(x: 0.0, y: height - 2.0) }
95                     .appended { NSPoint(x: 0.0, y: height - 35.0) }
96             }
97             
98         case .modest:
99             return polygon {
100                 [NSPoint]()
101                     .appended { NSPoint(x: 50.0, y: height - 2.0) }
102                     .appended { NSPoint(x: 25.0, y: height - 2.0) }
103                     .appended { NSPoint(x: 0.0, y: height - 25.0) }
104                     .appended { NSPoint(x: 0.0, y: height - 50.0) }
105             }
106             
107         case .badly:
108             let p = polygon {
109                 [NSPoint]()
110                     .appended { NSPoint(x: 60.0, y: height - 2.0) }
111                     .appended { NSPoint(x: 53.0, y: height - 2.0) }
112                     .appended { NSPoint(x: 0.0, y: height - 53.0) }
113                     .appended { NSPoint(x: 0.0, y: height - 60.0) }
114             }
115             polygon {
116                 [NSPoint]()
117                     .appended { NSPoint(x: 47.0, y: height - 2.0) }
118                     .appended { NSPoint(x: 23.0, y: height - 2.0) }
119                     .appended { NSPoint(x: 0.0, y: height - 23.0) }
120                     .appended { NSPoint(x: 0.0, y: height - 47.0) }
121             }
122                 .map { p?.append($0) }
123             return p
124         }
125     }
126     
127     private var pathForSmall: NSBezierPath? {
128         
129         let height = bounds.height
130         
131         switch innerDamageType {
132         case .none:
133             return nil
134             
135         case .slightly:
136             return polygon {
137                 [NSPoint]()
138                     .appended { NSPoint(x: 35.0, y: height - 2.0) }
139                     .appended { NSPoint(x: 0.0, y: height - 2.0) }
140                     .appended { NSPoint(x: 0.0, y: height - 35.0) }
141             }
142             
143         case .modest:
144             return polygon {
145                 [NSPoint]()
146                     .appended { NSPoint(x: 50.0, y: height - 2.0) }
147                     .appended { NSPoint(x: 25.0, y: height - 2.0) }
148                     .appended { NSPoint(x: 0.0, y: height - 25.0) }
149                     .appended { NSPoint(x: 0.0, y: height - 50.0) }
150             }
151             
152         case .badly:
153             let p = polygon {
154                 [NSPoint]()
155                     .appended { NSPoint(x: 55.0, y: height - 2.0) }
156                     .appended { NSPoint(x: 48.0, y: height - 2.0) }
157                     .appended { NSPoint(x: 0.0, y: height - 48.0) }
158                     .appended { NSPoint(x: 0.0, y: height - 55.0) }
159             }
160             polygon {
161                 [NSPoint]()
162                     .appended { NSPoint(x: 42.0, y: height - 2.0) }
163                     .appended { NSPoint(x: 20.0, y: height - 2.0) }
164                     .appended { NSPoint(x: 0.0, y: height - 20.0) }
165                     .appended { NSPoint(x: 0.0, y: height - 42.0) }
166                 }
167                 .map { p?.append($0) }
168             return p
169         }
170     }
171     
172     override func draw(_ dirtyRect: NSRect) {
173         
174         super.draw(dirtyRect)
175         
176         color?.setFill()
177         borderColor?.setStroke()
178         path?.fill()
179         path?.stroke()
180     }
181 }