OSDN Git Service

6b4fabb71b26c58838c2565ac098566fe596a6ef
[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 class SuppliesCell: NSCell {
12     private enum ResourceType {
13         case fuel
14         case bull
15     }
16     
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)
24     
25     dynamic var shipStatus: KCShipObject?
26     private var fuelStatusColor: NSColor {
27         guard let s = shipStatus else { return redColor }
28         return statusColor(withValue: s.fuel, max: s.maxFuel)
29     }
30     private var bullStatusColor: NSColor {
31         guard let s = shipStatus else { return redColor }
32         return statusColor(withValue: s.bull, max: s.maxBull)
33     }
34     private var numberOfFuelColoredCell: Int {
35         guard let s = shipStatus else { return 0 }
36         return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
37     }
38     private var numberOgBullColoredCell: Int {
39         guard let s = shipStatus else { return 0 }
40         return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
41     }
42     
43     override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
44         drawBackground(withFrame: cellFrame)
45         drawFuelInterior(withFrame: cellFrame)
46         drawBullInterior(withFrame: cellFrame)
47     }
48     
49     private func drawBackground(withFrame cellFrame: NSRect) {
50         let path = NSBezierPath(rect: cellFrame)
51         borderColor.set()
52         path.fill()
53     }
54     private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
55         if position >= border { return backgroundColor }
56         return type == .fuel ? fuelStatusColor : bullStatusColor
57     }
58     private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
59         let height: CGFloat = (NSHeight(cellFrame) - 3.0) / 2.0
60         let width: CGFloat = (NSWidth(cellFrame) - CGFloat(numberOfCell) - 1.0) / CGFloat(numberOfCell)
61         let y: CGFloat = type == .fuel ? height + 2.0 : 1.0
62         for i in 0...10 {
63             let x: CGFloat = CGFloat(1 + i) + width * CGFloat(i)
64             let cellRect = NSMakeRect(x, y, width, height)
65             color(of: type, position: i, border: border).set()
66             NSBezierPath(rect: cellRect).fill()
67         }
68     }
69     private func drawFuelInterior(withFrame cellFrame: NSRect) {
70         drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
71     }
72     private func drawBullInterior(withFrame cellFrame: NSRect) {
73         drawResource(withFrame: cellFrame, border: numberOgBullColoredCell, type: .bull)
74     }
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 }
79         return Int(retio)
80     }
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
87         }
88     }
89 }