OSDN Git Service

洋上補給の補強増設用のショートネームをつけた
[kcd/KCD.git] / KCD / CombileViewController.swift
1 //
2 //  CombileViewController.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 final class CombileViewController: NSViewController {
12     
13     let fleet1 = FleetViewController(viewType: .miniVierticalType)!
14     let fleet2 = FleetViewController(viewType: .miniVierticalType)!
15     
16     @IBOutlet private weak var placeholder1: NSView!
17     @IBOutlet private weak var placeholder2: NSView!
18     
19     @objc dynamic var fleet1TPValue: Int = 0 {
20         willSet {
21             willChangeValue(forKey: #keyPath(TPValue))
22             willChangeValue(forKey: #keyPath(BRankTPValue))
23         }
24         didSet {
25             didChangeValue(forKey: #keyPath(TPValue))
26             didChangeValue(forKey: #keyPath(BRankTPValue))
27         }
28     }
29     @objc dynamic var fleet2TPValue: Int = 0 {
30         willSet {
31             willChangeValue(forKey: #keyPath(TPValue))
32             willChangeValue(forKey: #keyPath(BRankTPValue))
33         }
34         didSet {
35             didChangeValue(forKey: #keyPath(TPValue))
36             didChangeValue(forKey: #keyPath(BRankTPValue))
37         }
38     }
39     @objc dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
40     @objc dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
41     
42     @objc dynamic var fleet1Seiku: Int = 0 {
43         willSet {
44             willChangeValue(forKey: #keyPath(seiku))
45         }
46         didSet {
47             didChangeValue(forKey: #keyPath(seiku))
48         }
49     }
50     @objc dynamic var fleet2Seiku: Int = 0 {
51         willSet {
52             willChangeValue(forKey: #keyPath(seiku))
53         }
54         didSet {
55             didChangeValue(forKey: #keyPath(seiku))
56         }
57     }
58     @objc dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
59     
60     @objc dynamic var fleet1CalculatedSeiku: Int = 0 {
61         willSet {
62             willChangeValue(forKey: #keyPath(seiku))
63         }
64         didSet {
65             didChangeValue(forKey: #keyPath(seiku))
66         }
67     }
68     @objc dynamic var fleet2CalculatedSeiku: Int = 0 {
69         willSet {
70             willChangeValue(forKey: #keyPath(calculatedSeiku))
71         }
72         didSet {
73             didChangeValue(forKey: #keyPath(calculatedSeiku))
74         }
75     }
76     @objc dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
77     
78     var combineType: CombineType? {
79         willSet { willChangeValue(forKey: #keyPath(combineTypeName)) }
80         didSet { didChangeValue(forKey: #keyPath(combineTypeName)) }
81     }
82     @objc dynamic var combineTypeName: String? { return combineType?.displayName() }
83     
84     override var nibName: NSNib.Name {
85         
86         return .nibName(instanceOf: self)
87     }
88     
89     override func viewDidLoad() {
90         
91         super.viewDidLoad()
92         
93         let placeholders: [NSView] = [placeholder1, placeholder2]
94         let fleets = [fleet1, fleet2]
95         zip(placeholders, fleets).forEach { placeholder, fleet in
96             
97             fleet.view.frame = placeholder.frame
98             fleet.view.autoresizingMask = placeholder.autoresizingMask
99             placeholder.superview?.replaceSubview(placeholder, with: fleet.view)
100         }
101         fleets.enumerated().forEach { offset, fleet in
102             fleet.fleetNumber = offset + 1
103         }
104         
105         bind(NSBindingName(#keyPath(fleet1TPValue)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalTPValue))
106         bind(NSBindingName(#keyPath(fleet2TPValue)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalTPValue))
107         
108         bind(NSBindingName(#keyPath(fleet1Seiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalSeiku))
109         bind(NSBindingName(#keyPath(fleet2Seiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalSeiku))
110         
111         bind(NSBindingName(#keyPath(fleet1CalculatedSeiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
112         bind(NSBindingName(#keyPath(fleet2CalculatedSeiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
113         
114         NotificationCenter.default
115             .addObserver(forName: .CombinedDidCange, object: nil, queue: .main) { notification in
116                 
117                 let type = notification.userInfo?[CombinedCommand.userInfoKey] as? CombineType
118                 self.combineType = type
119         }
120     }
121     
122     deinit {
123         
124         unbind(NSBindingName(#keyPath(fleet1TPValue)))
125         unbind(NSBindingName(#keyPath(fleet2TPValue)))
126         
127         unbind(NSBindingName(#keyPath(fleet1Seiku)))
128         unbind(NSBindingName(#keyPath(fleet2Seiku)))
129         
130         unbind(NSBindingName(#keyPath(fleet1CalculatedSeiku)))
131         unbind(NSBindingName(#keyPath(fleet2CalculatedSeiku)))
132     }
133 }