OSDN Git Service

改修工廠メニューを更新
[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         
17         case bull
18     }
19     
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)
27     
28     @objc dynamic var ship: Ship?
29     
30     private var fuelStatusColor: NSColor {
31         
32         guard let s = ship else {
33             
34             return redColor
35         }
36         
37         return statusColor(withValue: s.fuel, max: s.maxFuel)
38     }
39     
40     private var bullStatusColor: NSColor {
41         
42         guard let s = ship else {
43             
44             return redColor
45         }
46         
47         return statusColor(withValue: s.bull, max: s.maxBull)
48     }
49     
50     private var numberOfFuelColoredCell: Int {
51         
52         guard let s = ship else {
53             
54             return 0
55         }
56         
57         return numberOfColoredCell(withValue: s.fuel, max: s.maxFuel)
58     }
59     
60     private var numberOfBullColoredCell: Int {
61         
62         guard let s = ship else {
63             
64             return 0
65         }
66         
67         return numberOfColoredCell(withValue: s.bull, max: s.maxBull)
68     }
69     
70     override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
71         
72         drawBackground(withFrame: cellFrame)
73         drawFuelInterior(withFrame: cellFrame)
74         drawBullInterior(withFrame: cellFrame)
75     }
76     
77     private func drawBackground(withFrame cellFrame: NSRect) {
78         
79         borderColor.set()
80         NSBezierPath(rect: cellFrame).fill()
81     }
82     
83     private func color(of type: ResourceType, position: Int, border: Int) -> NSColor {
84         
85         if position >= border {
86             
87             return backgroundColor
88         }
89         
90         return type == .fuel ? fuelStatusColor : bullStatusColor
91     }
92     
93     private func drawResource(withFrame cellFrame: NSRect, border: Int, type: ResourceType) {
94         
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)
98         
99         (0...numberOfCell).forEach {
100             
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()
105         }
106     }
107     
108     private func drawFuelInterior(withFrame cellFrame: NSRect) {
109         
110         drawResource(withFrame: cellFrame, border: numberOfFuelColoredCell, type: .fuel)
111     }
112     
113     private func drawBullInterior(withFrame cellFrame: NSRect) {
114         
115         drawResource(withFrame: cellFrame, border: numberOfBullColoredCell, type: .bull)
116     }
117     
118     private func numberOfColoredCell(withValue value: Int, max: Int) -> Int {
119         
120         if value >= max {
121             
122             return 10
123         }
124         
125         let ratio = ceil( Double(value) / Double(max) * Double(numberOfCell) )
126         
127         if ratio > 9 {
128             
129             return 9
130         }
131         
132         return Int(ratio)
133     }
134     
135     private func statusColor(withValue value: Int, max: Int) -> NSColor {
136         
137         switch numberOfColoredCell(withValue: value, max: max) {
138             
139         case 1, 2, 3: return redColor
140             
141         case 4, 5, 6, 7: return orangeColor
142             
143         case 8, 9: return yellowColor
144             
145         default: return greenColor
146             
147         }
148     }
149 }