OSDN Git Service

41b6bf322d24e68a71d95ad2699cc13bb3f6395b
[kcd/KCD.git] / KCD / ShipDetailViewController.swift
1 //
2 //  ShipDetailViewController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2016/12/25.
6 //  Copyright © 2016年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 enum ShipDetailViewType {
12     case full
13     case medium
14     case minimum
15 }
16
17 private func nibNameFor(_ type: ShipDetailViewType) -> String {
18     switch type {
19     case .full:
20         return "ShipDetailViewController"
21     case .medium:
22         return "MediumShipViewController"
23     case .minimum:
24         return "MediumShipViewController"
25     }
26 }
27
28 class ShipDetailViewController: NSViewController {
29     let type: ShipDetailViewType
30     let managedObjectContext = ServerDataStore.default.managedObjectContext
31     
32     init?(type: ShipDetailViewType) {
33         self.type = type
34         super.init(nibName: nibNameFor(type), bundle: nil)
35         
36         NotificationCenter
37             .default
38             .addObserver(forName: .DidUpdateGuardEscape,
39                          object: nil,
40                          queue: nil) { [unowned self] _ in
41                             self.guardEscaped = self.ship?.guardEscaped ?? false
42         }
43     }
44     required init?(coder: NSCoder) {
45         fatalError("not implemented")
46     }
47     deinit {
48         NotificationCenter.default.removeObserver(self)
49         damageView.unbind("damageType")
50         supply.unbind("shipStatus")
51         [slot00Field, slot01Field, slot02Field, slot03Field]
52             .forEach { $0?.unbind("slotItemID") }
53     }
54     
55     
56     @IBOutlet weak var supply: SuppliesView!
57     @IBOutlet weak var guardEscapedView: GuardEscapedView!
58     @IBOutlet weak var damageView: DamageView!
59     @IBOutlet weak var slot00Field: NSTextField!
60     @IBOutlet weak var slot01Field: NSTextField!
61     @IBOutlet weak var slot02Field: NSTextField!
62     @IBOutlet weak var slot03Field: NSTextField!
63     @IBOutlet var shipController: NSObjectController!
64     
65     dynamic var guardEscaped: Bool = false {
66         didSet {
67             guardEscapedView.isHidden = !guardEscaped
68         }
69     }
70     dynamic var ship: KCShipObject? {
71         get {
72             return shipController.content as? KCShipObject
73         }
74         set {
75             shipController.fetchPredicate = NSPredicate(format: "id = %ld", newValue?.id ?? 0)
76         }
77     }
78     
79     override func viewDidLoad() {
80         super.viewDidLoad()
81         
82         damageView.setFrameOrigin(.zero)
83         view.addSubview(damageView)
84         damageView.bind("damageType", to: shipController, withKeyPath: "selection.status", options: nil)
85         
86         supply.bind("shipStatus", to: shipController, withKeyPath: "selection.self", options: nil)
87         
88         guardEscapedView.setFrameOrigin(.zero)
89         view.addSubview(guardEscapedView)
90         switch type {
91         case .medium, .minimum:
92             guardEscapedView.controlSize = .mini
93         default: break
94         }
95         
96         let fields = [slot00Field, slot01Field, slot02Field, slot03Field]
97         let keypath = ["selection.slot_0", "selection.slot_1", "selection.slot_2", "selection.slot_3"]
98         zip(fields, keypath).forEach {
99             $0.0?.bind("slotItemID", to: shipController, withKeyPath: $0.1, options: nil)
100         }
101     }
102 }