OSDN Git Service

Doutakuを導入
[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     static let conditionBindingName = NSBindingName(#keyPath(AirPlanInfoView.condition))
21     static let slotIDBindingName = NSBindingName(#keyPath(AirPlanInfoView.slotId))
22     static let maxCountBindingName = NSBindingName(#keyPath(AirPlanInfoView.maxCount))
23     static let countBindingName = NSBindingName(#keyPath(AirPlanInfoView.count))
24     
25     @IBOutlet private var planNameVew: SlotItemLevelView!
26     @IBOutlet private var conditionBox: NSBox!
27     @IBOutlet private var needSupplyField: NSTextField!
28     
29     @objc dynamic var condition: Int = 1 {
30         
31         didSet {
32             guard let cond = Condition(rawValue: condition) else { return }
33             
34             conditionBox.fillColor = conditionColor(cond)
35             conditionBox.borderColor = borderColor(cond)
36         }
37     }
38     
39     @objc dynamic var slotId: NSNumber? {
40         
41         didSet { planNameVew.slotItemID = slotId }
42     }
43     
44     @objc dynamic var maxCount: Int = 0 {
45         
46         didSet { needSupplyField.isHidden = !needSupply() }
47     }
48     
49     @objc dynamic var count: Int = 0 {
50         
51         didSet { needSupplyField.isHidden = !needSupply() }
52     }
53     
54     private func conditionColor(_ cond: Condition) -> NSColor {
55         
56         switch cond {
57         case .normal: return .clear
58         case .tired: return #colorLiteral(red: 1, green: 0.7233425379, blue: 0.1258574128, alpha: 0.8239436619)
59         case .bad: return #colorLiteral(red: 0.7320367694, green: 0.07731548697, blue: 0.06799335033, alpha: 1)
60         }
61     }
62     
63     private func borderColor(_ cond: Condition) -> NSColor {
64         
65         switch cond {
66         case .normal: return .clear
67         case .tired: return #colorLiteral(red: 0.458858192, green: 0.3335277438, blue: 0.07979661971, alpha: 1)
68         case .bad: return #colorLiteral(red: 0.5462518334, green: 0.04599834234, blue: 0.04913448542, alpha: 1)
69         }
70     }
71     
72     private func needSupply() -> Bool {
73         
74         return (maxCount - count != 0)
75     }
76     
77     required init?(coder: NSCoder) {
78         
79         super.init(coder: coder)
80     }
81     
82     deinit {
83         
84         unbind(AirPlanInfoView.conditionBindingName)
85         unbind(AirPlanInfoView.slotIDBindingName)
86         unbind(AirPlanInfoView.maxCountBindingName)
87         unbind(AirPlanInfoView.countBindingName)
88     }
89     
90     override func awakeFromNib() {
91         
92         bind(AirPlanInfoView.conditionBindingName, to: self, withKeyPath: "objectValue.cond")
93         bind(AirPlanInfoView.slotIDBindingName, to: self, withKeyPath: "objectValue.slotid")
94         bind(AirPlanInfoView.maxCountBindingName, to: self, withKeyPath: "objectValue.max_count")
95         bind(AirPlanInfoView.countBindingName, to: self, withKeyPath: "objectValue.count")
96     }
97 }