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 {
19 private let numberOfCell = 10
20 private let greenColor = NSColor(calibratedWhite: 0.39, alpha: 1.0)
21 private let yellowColor = NSColor(calibratedWhite: 0.55, alpha: 1.0)
22 private let orangeColor = NSColor(calibratedWhite: 0.7, alpha: 1.0)
23 private let redColor = NSColor(calibratedWhite: 0.79, alpha: 1.0)
24 private let borderColor = NSColor(calibratedWhite: 0.632, alpha: 1.0)
25 private let backgroundColor = NSColor(calibratedWhite: 0.948, alpha: 1.0)
27 @objc dynamic var shipStatus: Ship?
29 private var fuelStatusColor: NSColor {
31 guard let s = shipStatus else { return redColor }
33 return statusColor(withValue: s.fuel, max: s.maxFuel)
36 private var bullStatusColor: NSColor {
38 guard let s = shipStatus else { return redColor }
40 return statusColor(withValue: s.bull, max: s.maxBull)
43 private var numberOfFuelColoredCell: Int {
45 guard let s = shipStatus else { return 0 }
47 return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
50 private var numberOgBullColoredCell: Int {
52 guard let s = shipStatus else { return 0 }
54 return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
57 override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
59 drawBackground(withFrame: cellFrame)
60 drawFuelInterior(withFrame: cellFrame)
61 drawBullInterior(withFrame: cellFrame)
64 private func drawBackground(withFrame cellFrame: NSRect) {
67 NSBezierPath(rect: cellFrame).fill()
70 private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
72 if position >= border { return backgroundColor }
74 return type == .fuel ? fuelStatusColor : bullStatusColor
77 private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
79 let height: CGFloat = (cellFrame.height - 3.0) / 2.0
80 let width: CGFloat = (cellFrame.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
81 let y: CGFloat = type == .fuel ? height + 2.0 : 1.0
83 (0...numberOfCell).forEach {
85 let x: CGFloat = CGFloat(1 + $0) + width * CGFloat($0)
86 let cellRect = NSRect(x: x, y: y, width: width, height: height)
87 color(of: type, position: $0, border: border).set()
88 NSBezierPath(rect: cellRect).fill()
92 private func drawFuelInterior(withFrame cellFrame: NSRect) {
94 drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
97 private func drawBullInterior(withFrame cellFrame: NSRect) {
99 drawResource(withFrame: cellFrame, border: numberOgBullColoredCell, type: .bull)
102 private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
104 if value >= max { return 10 }
106 let retio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
108 if retio > 9 { return 9 }
113 private func statusColor(withValue value: Int, max: Int) -> NSColor {
115 switch numberOfColoredCell(withValue: value, max: max) {
116 case 1, 2, 3: return redColor
117 case 4, 5, 6, 7: return orangeColor
118 case 8, 9: return yellowColor
119 default: return greenColor