2 // CombileViewController.swift
5 // Created by Hori,Masaki on 2016/12/25.
6 // Copyright © 2016年 Hori,Masaki. All rights reserved.
11 final class CombileViewController: NSViewController {
13 let fleet1 = FleetViewController(viewType: .miniVierticalType)!
14 let fleet2 = FleetViewController(viewType: .miniVierticalType)!
16 @IBOutlet private weak var placeholder1: NSView!
17 @IBOutlet private weak var placeholder2: NSView!
19 @objc dynamic var fleet1TPValue: Int = 0 {
23 willChangeValue(forKey: #keyPath(TPValue))
24 willChangeValue(forKey: #keyPath(BRankTPValue))
28 didChangeValue(forKey: #keyPath(TPValue))
29 didChangeValue(forKey: #keyPath(BRankTPValue))
32 @objc dynamic var fleet2TPValue: Int = 0 {
36 willChangeValue(forKey: #keyPath(TPValue))
37 willChangeValue(forKey: #keyPath(BRankTPValue))
41 didChangeValue(forKey: #keyPath(TPValue))
42 didChangeValue(forKey: #keyPath(BRankTPValue))
45 @objc dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
46 @objc dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
48 @objc dynamic var fleet1Seiku: Int = 0 {
52 willChangeValue(forKey: #keyPath(seiku))
56 didChangeValue(forKey: #keyPath(seiku))
59 @objc dynamic var fleet2Seiku: Int = 0 {
63 willChangeValue(forKey: #keyPath(seiku))
67 didChangeValue(forKey: #keyPath(seiku))
70 @objc dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
72 @objc dynamic var fleet1CalculatedSeiku: Int = 0 {
76 willChangeValue(forKey: #keyPath(seiku))
80 didChangeValue(forKey: #keyPath(seiku))
83 @objc dynamic var fleet2CalculatedSeiku: Int = 0 {
87 willChangeValue(forKey: #keyPath(calculatedSeiku))
91 didChangeValue(forKey: #keyPath(calculatedSeiku))
94 @objc dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
96 var combineType: CombineType? {
98 willSet { willChangeValue(forKey: #keyPath(combineTypeName)) }
99 didSet { didChangeValue(forKey: #keyPath(combineTypeName)) }
101 @objc dynamic var combineTypeName: String? { return combineType?.displayName() }
103 override var nibName: NSNib.Name {
105 return .nibName(instanceOf: self)
108 override func viewDidLoad() {
112 let placeholders: [NSView] = [placeholder1, placeholder2]
113 let fleets = [fleet1, fleet2]
114 zip(placeholders, fleets).forEach { placeholder, fleet in
116 fleet.view.frame = placeholder.frame
117 fleet.view.autoresizingMask = placeholder.autoresizingMask
118 placeholder.superview?.replaceSubview(placeholder, with: fleet.view)
120 fleets.enumerated().forEach { offset, fleet in
122 fleet.fleetNumber = offset + 1
125 bind(NSBindingName(#keyPath(fleet1TPValue)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalTPValue))
126 bind(NSBindingName(#keyPath(fleet2TPValue)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalTPValue))
128 bind(NSBindingName(#keyPath(fleet1Seiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalSeiku))
129 bind(NSBindingName(#keyPath(fleet2Seiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalSeiku))
131 bind(NSBindingName(#keyPath(fleet1CalculatedSeiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
132 bind(NSBindingName(#keyPath(fleet2CalculatedSeiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
134 NotificationCenter.default
135 .addObserver(forName: .CombinedDidCange, object: nil, queue: .main) { notification in
137 let type = notification.userInfo?[CombinedCommand.userInfoKey] as? CombineType
138 self.combineType = type
144 unbind(NSBindingName(#keyPath(fleet1TPValue)))
145 unbind(NSBindingName(#keyPath(fleet2TPValue)))
147 unbind(NSBindingName(#keyPath(fleet1Seiku)))
148 unbind(NSBindingName(#keyPath(fleet2Seiku)))
150 unbind(NSBindingName(#keyPath(fleet1CalculatedSeiku)))
151 unbind(NSBindingName(#keyPath(fleet2CalculatedSeiku)))