5 // Created by Hori,Masaki on 2017/01/02.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 class SuppliesCell: NSCell {
12 private enum ResourceType {
17 private let numberOfCell = 10
18 private let greenColor = NSColor(calibratedWhite: 0.39, alpha: 1.0)
19 private let yellowColor = NSColor(calibratedWhite: 0.55, alpha: 1.0)
20 private let orangeColor = NSColor(calibratedWhite: 0.7, alpha: 1.0)
21 private let redColor = NSColor(calibratedWhite: 0.79, alpha: 1.0)
22 private let borderColor = NSColor(calibratedWhite: 0.632, alpha: 1.0)
23 private let backgroundColor = NSColor(calibratedWhite: 0.948, alpha: 1.0)
25 dynamic var shipStatus: Ship?
26 private var fuelStatusColor: NSColor {
27 guard let s = shipStatus else { return redColor }
28 return statusColor(withValue: s.fuel, max: s.maxFuel)
30 private var bullStatusColor: NSColor {
31 guard let s = shipStatus else { return redColor }
32 return statusColor(withValue: s.bull, max: s.maxBull)
34 private var numberOfFuelColoredCell: Int {
35 guard let s = shipStatus else { return 0 }
36 return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
38 private var numberOgBullColoredCell: Int {
39 guard let s = shipStatus else { return 0 }
40 return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
43 override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
44 drawBackground(withFrame: cellFrame)
45 drawFuelInterior(withFrame: cellFrame)
46 drawBullInterior(withFrame: cellFrame)
49 private func drawBackground(withFrame cellFrame: NSRect) {
50 let path = NSBezierPath(rect: cellFrame)
54 private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
55 if position >= border { return backgroundColor }
56 return type == .fuel ? fuelStatusColor : bullStatusColor
58 private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
59 let height: CGFloat = (cellFrame.height - 3.0) / 2.0
60 let width: CGFloat = (cellFrame.width - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
61 let y: CGFloat = type == .fuel ? height + 2.0 : 1.0
63 let x: CGFloat = CGFloat(1 + i) + width * CGFloat(i)
64 let cellRect = NSRect(x: x, y: y, width: width, height: height)
65 color(of: type, position: i, border: border).set()
66 NSBezierPath(rect: cellRect).fill()
69 private func drawFuelInterior(withFrame cellFrame: NSRect) {
70 drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
72 private func drawBullInterior(withFrame cellFrame: NSRect) {
73 drawResource(withFrame: cellFrame, border: numberOgBullColoredCell, type: .bull)
75 private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
76 if value >= max { return 10 }
77 let retio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
78 if retio > 9 { return 9 }
81 private func statusColor(withValue value: Int, max: Int) -> NSColor {
82 switch numberOfColoredCell(withValue: value, max: max) {
83 case 1, 2, 3: return redColor
84 case 4, 5, 6, 7: return orangeColor
85 case 8, 9: return yellowColor
86 default: return greenColor