5 // Created by Hori,Masaki on 2017/01/02.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class SuppliesCell: NSCell {
13 private enum ResourceType {
20 private let numberOfCell = 10
22 private var greenColor: NSColor {
24 return ColorSetManager.current[.suppliesCellGreen]
26 private var yellowColor: NSColor {
28 return ColorSetManager.current[.suppliesCellYellow]
30 private var orangeColor: NSColor {
32 return ColorSetManager.current[.suppliesCellOrange]
34 private var redColor: NSColor {
36 return ColorSetManager.current[.suppliesCellRedColor]
38 private var borderColor: NSColor {
40 return ColorSetManager.current[.suppliesCellBorder]
42 private var backgroundColor: NSColor {
44 return ColorSetManager.current[.suppliesCellBackground]
47 @objc dynamic var ship: Ship?
49 private var fuelStatusColor: NSColor {
51 guard let s = ship else {
56 return statusColor(withValue: s.fuel, max: s.maxFuel)
59 private var bullStatusColor: NSColor {
61 guard let s = ship else {
66 return statusColor(withValue: s.bull, max: s.maxBull)
69 private var numberOfFuelColoredCell: Int {
71 guard let s = ship else {
76 return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
79 private var numberOfBullColoredCell: Int {
81 guard let s = ship else {
86 return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
89 override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
91 drawBackground(withFrame: cellFrame)
92 drawFuelInterior(withFrame: cellFrame)
93 drawBullInterior(withFrame: cellFrame)
96 private func drawBackground(withFrame cellFrame: NSRect) {
99 NSBezierPath(rect: cellFrame).fill()
102 private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
104 if position >= border {
106 return backgroundColor
109 return type == .fuel ? fuelStatusColor : bullStatusColor
112 private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
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)
118 (0...numberOfCell).forEach {
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()
127 private func drawFuelInterior(withFrame cellFrame: NSRect) {
129 drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
132 private func drawBullInterior(withFrame cellFrame: NSRect) {
134 drawResource(withFrame: cellFrame, border: numberOfBullColoredCell, type: .bull)
137 private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
144 let ratio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
154 private func statusColor(withValue value: Int, max: Int) -> NSColor {
156 switch numberOfColoredCell(withValue: value, max: max) {
158 case 1, 2, 3: return redColor
160 case 4, 5, 6, 7: return orangeColor
162 case 8, 9: return yellowColor
164 default: return greenColor