OSDN Git Service

6fa0aec07321a4ee539c721f4fa8505afb04eb54
[kcd/KCD.git] / KCD / SuppliesView.swift
1 //
2 //  SuppliesView.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 fileprivate var ShipStatusContext: Int = 0
12
13 class SuppliesView: NSControl {
14     private let observeKeys = ["fuel", "maxFuel", "bull", "maxBull"]
15     private let suppliesCell: SuppliesCell
16     
17     override init(frame: NSRect) {
18         suppliesCell = SuppliesCell()
19         super.init(frame: frame)
20         self.cell = suppliesCell
21     }
22     required init?(coder: NSCoder) {
23         suppliesCell = SuppliesCell()
24         super.init(coder: coder)
25         self.cell = suppliesCell
26     }
27     deinit {
28         observeKeys.forEach {
29             suppliesCell.shipStatus?.removeObserver(self, forKeyPath: $0)
30         }
31     }
32     
33     var shipStatus: KCShipObject? {
34         get {
35             return suppliesCell.shipStatus
36         }
37         set {
38             observeKeys.forEach {
39                 suppliesCell.shipStatus?.removeObserver(self, forKeyPath: $0)
40             }
41             suppliesCell.shipStatus = newValue
42             observeKeys.forEach {
43                 suppliesCell.shipStatus?.addObserver(self, forKeyPath: $0, context: &ShipStatusContext)
44             }
45             needsDisplay = true
46         }
47     }
48     
49     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
50         if context == &ShipStatusContext {
51             needsDisplay = true
52             return
53         }
54         super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
55     }
56 }