OSDN Git Service

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