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
21 private let greenColor = NSColor(calibratedWhite: 0.39, alpha: 1.0)
22 private let yellowColor = NSColor(calibratedWhite: 0.55, alpha: 1.0)
23 private let orangeColor = NSColor(calibratedWhite: 0.7, alpha: 1.0)
24 private let redColor = NSColor(calibratedWhite: 0.79, alpha: 1.0)
25 private let borderColor = NSColor(calibratedWhite: 0.632, alpha: 1.0)
26 private let backgroundColor = NSColor(calibratedWhite: 0.948, alpha: 1.0)
28 @objc dynamic var ship: Ship?
30 private var fuelStatusColor: NSColor {
32 guard let s = ship else {
37 return statusColor(withValue: s.fuel, max: s.maxFuel)
40 private var bullStatusColor: NSColor {
42 guard let s = ship else {
47 return statusColor(withValue: s.bull, max: s.maxBull)
50 private var numberOfFuelColoredCell: Int {
52 guard let s = ship else {
57 return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
60 private var numberOfBullColoredCell: Int {
62 guard let s = ship else {
67 return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
70 override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
72 drawBackground(withFrame: cellFrame)
73 drawFuelInterior(withFrame: cellFrame)
74 drawBullInterior(withFrame: cellFrame)
77 private func drawBackground(withFrame cellFrame: NSRect) {
80 NSBezierPath(rect: cellFrame).fill()
83 private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
85 if position >= border {
87 return backgroundColor
90 return type == .fuel ? fuelStatusColor : bullStatusColor
93 private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
95 let height = (cellFrame.height - 3.0) / 2.0
96 let width = (cellFrame.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
97 let y = (type == .fuel ? height + 2.0 : 1.0)
99 (0...numberOfCell).forEach {
101 let x = CGFloat(1 + $0) + width * CGFloat($0)
102 let cellRect = NSRect(x: x, y: y, width: width, height: height)
103 color(of: type, position: $0, border: border).set()
104 NSBezierPath(rect: cellRect).fill()
108 private func drawFuelInterior(withFrame cellFrame: NSRect) {
110 drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
113 private func drawBullInterior(withFrame cellFrame: NSRect) {
115 drawResource(withFrame: cellFrame, border: numberOfBullColoredCell, type: .bull)
118 private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
125 let ratio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
135 private func statusColor(withValue value: Int, max: Int) -> NSColor {
137 switch numberOfColoredCell(withValue: value, max: max) {
139 case 1, 2, 3: return redColor
141 case 4, 5, 6, 7: return orangeColor
143 case 8, 9: return yellowColor
145 default: return greenColor