OSDN Git Service

AppDelegateからウインドウに関する部分を分離した
[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     case none = 0
13     case slightly
14     case modest
15     case badly
16 }
17
18 class DamageView: NSView {
19     dynamic var damageType: Int = 0 {
20         willSet {
21             guard let v = DamageType(rawValue: newValue) else {
22                 self.print("Can not set damageType")
23                 return
24             }
25             if innerDamageType != v { needsDisplay = true }
26             innerDamageType = v
27         }
28     }
29     var controlSize: NSControlSize = .regular
30     private var innerDamageType: DamageType = .none
31     private var color: NSColor? {
32         switch innerDamageType {
33         case .none:
34             return nil
35         case .slightly:
36             return #colorLiteral(red: 1.000, green: 0.956, blue: 0.012, alpha: 0.5)
37         case .modest:
38             return NSColor.orange.withAlphaComponent(0.5)
39         case .badly:
40             return NSColor.red.withAlphaComponent(0.5)
41         }
42     }
43     private var borderColor: NSColor? {
44         switch innerDamageType {
45         case .none:
46             return nil
47         case .slightly:
48             return NSColor.orange.withAlphaComponent(0.5)
49         case .modest:
50             return NSColor.orange.withAlphaComponent(0.9)
51         case .badly:
52             return NSColor.red.withAlphaComponent(0.9)
53         }
54     }
55     private var path: NSBezierPath? {
56         switch controlSize {
57         case .regular:
58             return pathForRegular
59         case .small, .mini:
60             return pathForSmall
61         }
62     }
63     private var pathForRegular: NSBezierPath? {
64         let height = bounds.height
65         switch innerDamageType {
66         case .none:
67             return nil
68         case .slightly:
69             return polygon {
70                 [NSPoint]()
71                     .appended { NSPoint(x: 35.0, y: height - 2.0) }
72                     .appended { NSPoint(x: 0.0, y: height - 2.0) }
73                     .appended { NSPoint(x: 0.0, y: height - 35.0) }
74             }
75         case .modest:
76             return polygon {
77                 [NSPoint]()
78                     .appended { NSPoint(x: 50.0, y: height - 2.0) }
79                     .appended { NSPoint(x: 25.0, y: height - 2.0) }
80                     .appended { NSPoint(x: 0.0, y: height - 25.0) }
81                     .appended { NSPoint(x: 0.0, y: height - 50.0) }
82             }
83         case .badly:
84             let p = polygon {
85                 [NSPoint]()
86                     .appended { NSPoint(x: 60.0, y: height - 2.0) }
87                     .appended { NSPoint(x: 53.0, y: height - 2.0) }
88                     .appended { NSPoint(x: 0.0, y: height - 53.0) }
89                     .appended { NSPoint(x: 0.0, y: height - 60.0) }
90             }
91             polygon {
92                 [NSPoint]()
93                     .appended { NSPoint(x: 47.0, y: height - 2.0) }
94                     .appended { NSPoint(x: 23.0, y: height - 2.0) }
95                     .appended { NSPoint(x: 0.0, y: height - 23.0) }
96                     .appended { NSPoint(x: 0.0, y: height - 47.0) }
97             }
98                 .map { p?.append($0) }
99             return p
100         }
101     }
102     private var pathForSmall: NSBezierPath? {
103         let height = bounds.height
104         switch innerDamageType {
105         case .none:
106             return nil
107         case .slightly:
108             return polygon {
109                 [NSPoint]()
110                     .appended { NSPoint(x: 35.0, y: height - 2.0) }
111                     .appended { NSPoint(x: 0.0, y: height - 2.0) }
112                     .appended { NSPoint(x: 0.0, y: height - 35.0) }
113             }
114         case .modest:
115             return polygon {
116                 [NSPoint]()
117                     .appended { NSPoint(x: 50.0, y: height - 2.0) }
118                     .appended { NSPoint(x: 25.0, y: height - 2.0) }
119                     .appended { NSPoint(x: 0.0, y: height - 25.0) }
120                     .appended { NSPoint(x: 0.0, y: height - 50.0) }
121             }
122         case .badly:
123             let p = polygon {
124                 [NSPoint]()
125                     .appended { NSPoint(x: 55.0, y: height - 2.0) }
126                     .appended { NSPoint(x: 48.0, y: height - 2.0) }
127                     .appended { NSPoint(x: 0.0, y: height - 48.0) }
128                     .appended { NSPoint(x: 0.0, y: height - 55.0) }
129             }
130             polygon {
131                 [NSPoint]()
132                     .appended { NSPoint(x: 42.0, y: height - 2.0) }
133                     .appended { NSPoint(x: 20.0, y: height - 2.0) }
134                     .appended { NSPoint(x: 0.0, y: height - 20.0) }
135                     .appended { NSPoint(x: 0.0, y: height - 42.0) }
136                 }
137                 .map { p?.append($0) }
138             return p
139         }
140     }
141     
142     override func draw(_ dirtyRect: NSRect) {
143         super.draw(dirtyRect)
144         
145         color?.setFill()
146         borderColor?.setStroke()
147         path?.fill()
148         path?.stroke()
149     }
150 }