OSDN Git Service

カラーセットを使用するように
[kcd/KCD.git] / KCD / SuppliesCell.swift
1 //
2 //  SuppliesCell.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/01/02.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class SuppliesCell: NSCell {
12     
13     private enum ResourceType {
14         
15         case fuel
16         
17         case bull
18     }
19     
20     private let numberOfCell = 10
21     
22     private var greenColor: NSColor {
23         
24         return ColorSet.current[.suppliesCellGreen]
25     }
26     private var yellowColor: NSColor {
27         
28         return ColorSet.current[.suppliesCellYellow]
29     }
30     private var orangeColor: NSColor {
31         
32         return ColorSet.current[.suppliesCellOrange]
33     }
34     private var redColor: NSColor {
35         
36         return ColorSet.current[.suppliesCellRedColor]
37     }
38     private var borderColor: NSColor {
39         
40         return ColorSet.current[.suppliesCellBorder]
41     }
42     private var backgroundColor: NSColor {
43         
44         return ColorSet.current[.suppliesCellBackground]
45     }
46     
47     @objc dynamic var ship: Ship?
48     
49     private var fuelStatusColor: NSColor {
50         
51         guard let s = ship else {
52             
53             return redColor
54         }
55         
56         return statusColor(withValue: s.fuel, max: s.maxFuel)
57     }
58     
59     private var bullStatusColor: NSColor {
60         
61         guard let s = ship else {
62             
63             return redColor
64         }
65         
66         return statusColor(withValue: s.bull, max: s.maxBull)
67     }
68     
69     private var numberOfFuelColoredCell: Int {
70         
71         guard let s = ship else {
72             
73             return 0
74         }
75         
76         return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
77     }
78     
79     private var numberOfBullColoredCell: Int {
80         
81         guard let s = ship else {
82             
83             return 0
84         }
85         
86         return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
87     }
88     
89     override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
90         
91         drawBackground(withFrame: cellFrame)
92         drawFuelInterior(withFrame: cellFrame)
93         drawBullInterior(withFrame: cellFrame)
94     }
95     
96     private func drawBackground(withFrame cellFrame: NSRect) {
97         
98         borderColor.set()
99         NSBezierPath(rect: cellFrame).fill()
100     }
101     
102     private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
103         
104         if position >= border {
105             
106             return backgroundColor
107         }
108         
109         return type == .fuel ? fuelStatusColor : bullStatusColor
110     }
111     
112     private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
113         
114         let height = (cellFrame.height - 3.0) / 2.0
115         let width = (cellFrame.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
116         let y = (type == .fuel ? height + 2.0 : 1.0)
117         
118         (0...numberOfCell).forEach {
119             
120             let x = CGFloat(1 + $0) + width * CGFloat($0)
121             let cellRect = NSRect(x: x, y: y, width: width, height: height)
122             color(of: type, position: $0, border: border).set()
123             NSBezierPath(rect: cellRect).fill()
124         }
125     }
126     
127     private func drawFuelInterior(withFrame cellFrame: NSRect) {
128         
129         drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
130     }
131     
132     private func drawBullInterior(withFrame cellFrame: NSRect) {
133         
134         drawResource(withFrame: cellFrame, border: numberOfBullColoredCell, type: .bull)
135     }
136     
137     private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
138         
139         if value >= max {
140             
141             return 10
142         }
143         
144         let ratio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
145         
146         if ratio > 9 {
147             
148             return 9
149         }
150         
151         return Int(ratio)
152     }
153     
154     private func statusColor(withValue value: Int, max: Int) -> NSColor {
155         
156         switch numberOfColoredCell(withValue: value, max: max) {
157             
158         case 1, 2, 3: return redColor
159             
160         case 4, 5, 6, 7: return orangeColor
161             
162         case 8, 9: return yellowColor
163             
164         default: return greenColor
165             
166         }
167     }
168 }