OSDN Git Service

不要になったオペレータを削除
[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 private var pShipStatusContext: Int = 0
12
13 final class SuppliesView: NSControl {
14     
15     private let observeKeys = [ #keyPath(Ship.fuel),
16                                 #keyPath(Ship.maxFuel),
17                                 #keyPath(Ship.bull),
18                                 #keyPath(Ship.maxBull)]
19     private let suppliesCell: SuppliesCell
20     
21     override init(frame: NSRect) {
22         
23         suppliesCell = SuppliesCell()
24         
25         super.init(frame: frame)
26         
27         self.cell = suppliesCell
28     }
29     
30     required init?(coder: NSCoder) {
31         
32         suppliesCell = SuppliesCell()
33         
34         super.init(coder: coder)
35         
36         self.cell = suppliesCell
37     }
38     
39     deinit {
40         
41         observeKeys.forEach {
42             
43             suppliesCell.shipStatus?.removeObserver(self, forKeyPath: $0)
44         }
45     }
46     
47     @objc var shipStatus: Ship? {
48         
49         get { return suppliesCell.shipStatus }
50         set {
51             observeKeys.forEach {
52                 
53                 suppliesCell.shipStatus?.removeObserver(self, forKeyPath: $0)
54             }
55             suppliesCell.shipStatus = newValue
56             observeKeys.forEach {
57                 
58                 suppliesCell.shipStatus?.addObserver(self, forKeyPath: $0, context: &pShipStatusContext)
59             }
60             needsDisplay = true
61         }
62     }
63     
64     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
65         
66         if context == &pShipStatusContext {
67             
68             needsDisplay = true
69             
70             return
71         }
72         
73         super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
74     }
75 }