OSDN Git Service

Doutakuを導入
[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: Polygon? {
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: Polygon? {
80         
81         let height = bounds.height
82         
83         switch innerDamageType {
84         case .none:
85             return nil
86             
87         case .slightly:
88             return Polygon()
89                 .move(to: NSPoint(x: 35.0, y: height - 2.0))
90                 .line(to: NSPoint(x: 0.0, y: height - 2.0))
91                 .line(to: NSPoint(x: 0.0, y: height - 35.0))
92                 .close()
93             
94         case .modest:
95             return Polygon()
96                 .move(to: NSPoint(x: 50.0, y: height - 2.0))
97                 .line(to: NSPoint(x: 25.0, y: height - 2.0))
98                 .line(to: NSPoint(x: 0.0, y: height - 25.0))
99                 .line(to: NSPoint(x: 0.0, y: height - 50.0))
100                 .close()
101             
102         case .badly:
103             return Polygon()
104                 .move(to: NSPoint(x: 60.0, y: height - 2.0))
105                 .line(to: NSPoint(x: 53.0, y: height - 2.0))
106                 .line(to: NSPoint(x: 0.0, y: height - 53.0))
107                 .line(to: NSPoint(x: 0.0, y: height - 60.0))
108                 .close()
109                 .move(to: NSPoint(x: 47.0, y: height - 2.0))
110                 .line(to: NSPoint(x: 23.0, y: height - 2.0))
111                 .line(to: NSPoint(x: 0.0, y: height - 23.0))
112                 .line(to: NSPoint(x: 0.0, y: height - 47.0))
113                 .close()
114         }
115     }
116     
117     private var pathForSmall: Polygon? {
118         
119         let height = bounds.height
120         
121         switch innerDamageType {
122         case .none:
123             return nil
124             
125         case .slightly:
126             return Polygon()
127                 .move(to: NSPoint(x: 35.0, y: height - 2.0))
128                 .line(to: NSPoint(x: 0.0, y: height - 2.0))
129                 .line(to: NSPoint(x: 0.0, y: height - 35.0))
130                 .close()
131             
132         case .modest:
133             return Polygon()
134                 .move(to: NSPoint(x: 50.0, y: height - 2.0))
135                 .line(to: NSPoint(x: 25.0, y: height - 2.0))
136                 .line(to: NSPoint(x: 0.0, y: height - 25.0))
137                 .line(to: NSPoint(x: 0.0, y: height - 50.0))
138                 .close()
139             
140         case .badly:
141             return Polygon()
142                 .move(to: NSPoint(x: 55.0, y: height - 2.0))
143                 .line(to: NSPoint(x: 48.0, y: height - 2.0))
144                 .line(to: NSPoint(x: 0.0, y: height - 48.0))
145                 .line(to: NSPoint(x: 0.0, y: height - 55.0))
146                 .close()
147                 .move(to: NSPoint(x: 42.0, y: height - 2.0))
148                 .line(to: NSPoint(x: 20.0, y: height - 2.0))
149                 .line(to: NSPoint(x: 0.0, y: height - 20.0))
150                 .line(to: NSPoint(x: 0.0, y: height - 42.0))
151                 .close()
152         }
153     }
154     
155     override func draw(_ dirtyRect: NSRect) {
156         
157         super.draw(dirtyRect)
158         
159         color?.setFill()
160         borderColor?.setStroke()
161         path?.fill()
162         path?.stroke()
163     }
164 }