OSDN Git Service

guard の書き方を統一した
[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 final class SuppliesCell: NSCell {
12     
13     private enum ResourceType {
14         
15         case fuel
16         case bull
17     }
18     
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)
26     
27     @objc dynamic var shipStatus: Ship?
28     
29     private var fuelStatusColor: NSColor {
30         
31         guard let s = shipStatus else { return redColor }
32         
33         return statusColor(withValue: s.fuel, max: s.maxFuel)
34     }
35     
36     private var bullStatusColor: NSColor {
37         
38         guard let s = shipStatus else { return redColor }
39         
40         return statusColor(withValue: s.bull, max: s.maxBull)
41     }
42     
43     private var numberOfFuelColoredCell: Int {
44         
45         guard let s = shipStatus else { return 0 }
46         
47         return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
48     }
49     
50     private var numberOgBullColoredCell: Int {
51         
52         guard let s = shipStatus else { return 0 }
53         
54         return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
55     }
56     
57     override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
58         
59         drawBackground(withFrame: cellFrame)
60         drawFuelInterior(withFrame: cellFrame)
61         drawBullInterior(withFrame: cellFrame)
62     }
63     
64     private func drawBackground(withFrame cellFrame: NSRect) {
65         
66         borderColor.set()
67         NSBezierPath(rect: cellFrame).fill()
68     }
69     
70     private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
71         
72         if position >= border { return backgroundColor }
73         
74         return type == .fuel ? fuelStatusColor : bullStatusColor
75     }
76     
77     private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
78         
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
82         
83         (0...numberOfCell).forEach {
84             
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()
89         }
90     }
91     
92     private func drawFuelInterior(withFrame cellFrame: NSRect) {
93         
94         drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
95     }
96     
97     private func drawBullInterior(withFrame cellFrame: NSRect) {
98         
99         drawResource(withFrame: cellFrame, border: numberOgBullColoredCell, type: .bull)
100     }
101     
102     private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
103         
104         if value >= max { return 10 }
105         
106         let retio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
107         
108         if retio > 9 { return 9 }
109         
110         return Int(retio)
111     }
112     
113     private func statusColor(withValue value: Int, max: Int) -> NSColor {
114         
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
120         }
121     }
122 }