OSDN Git Service

guard の書き方を統一した
[kcd/KCD.git] / KCD / AirPlanInfoView.swift
1 //
2 //  AirPlanInfoView.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/04/29.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 final class AirPlanInfoView: NSTableCellView {
12     
13     enum Condition: Int {
14         
15         case normal = 1
16         case tired = 2
17         case bad = 3
18     }
19     
20     @IBOutlet var planNameVew: SlotItemLevelView!
21     @IBOutlet var conditionBox: NSBox!
22     @IBOutlet var needSupplyField: NSTextField!
23     
24     @objc dynamic var condition: Int = 1 {
25         
26         didSet {
27             guard let cond = Condition(rawValue: condition) else { return }
28             
29             conditionBox.fillColor = conditionColor(cond)
30             conditionBox.borderColor = borderColor(cond)
31         }
32     }
33     
34     @objc dynamic var slotId: NSNumber? {
35         
36         didSet { planNameVew.slotItemID = slotId }
37     }
38     
39     @objc dynamic var maxCount: Int = 0 {
40         
41         didSet { needSupplyField.isHidden = !needSupply() }
42     }
43     
44     @objc dynamic var count: Int = 0 {
45         
46         didSet { needSupplyField.isHidden = !needSupply() }
47     }
48     
49     private func conditionColor(_ cond: Condition) -> NSColor {
50         
51         switch cond {
52         case .normal: return .clear
53         case .tired: return #colorLiteral(red: 1, green: 0.7233425379, blue: 0.1258574128, alpha: 0.8239436619)
54         case .bad: return #colorLiteral(red: 0.7320367694, green: 0.07731548697, blue: 0.06799335033, alpha: 1)
55         }
56     }
57     
58     private func borderColor(_ cond: Condition) -> NSColor {
59         
60         switch cond {
61         case .normal: return .clear
62         case .tired: return #colorLiteral(red: 0.458858192, green: 0.3335277438, blue: 0.07979661971, alpha: 1)
63         case .bad: return #colorLiteral(red: 0.5462518334, green: 0.04599834234, blue: 0.04913448542, alpha: 1)
64         }
65     }
66     
67     private func needSupply() -> Bool {
68         
69         return (maxCount - count != 0)
70     }
71     
72     required init?(coder: NSCoder) {
73         
74         super.init(coder: coder)
75     }
76     
77     deinit {
78         
79         unbind(NSBindingName(#keyPath(AirPlanInfoView.condition)))
80         unbind(NSBindingName(#keyPath(AirPlanInfoView.slotId)))
81         unbind(NSBindingName(#keyPath(AirPlanInfoView.maxCount)))
82         unbind(NSBindingName(#keyPath(AirPlanInfoView.count)))
83     }
84     
85     override func awakeFromNib() {
86         
87         bind(NSBindingName(#keyPath(AirPlanInfoView.condition)),
88              to: self,
89              withKeyPath: "objectValue.cond")
90         bind(NSBindingName(#keyPath(AirPlanInfoView.slotId)),
91              to: self,
92              withKeyPath: "objectValue.slotid")
93         bind(NSBindingName(#keyPath(AirPlanInfoView.maxCount)),
94              to: self,
95              withKeyPath: "objectValue.max_count")
96         bind(NSBindingName(#keyPath(AirPlanInfoView.count)),
97              to: self,
98              withKeyPath: "objectValue.count")
99     }
100 }