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 {
21 willChangeValue(forKey: #keyPath(TPValue))
22 willChangeValue(forKey: #keyPath(BRankTPValue))
25 didChangeValue(forKey: #keyPath(TPValue))
26 didChangeValue(forKey: #keyPath(BRankTPValue))
29 @objc dynamic var fleet2TPValue: Int = 0 {
31 willChangeValue(forKey: #keyPath(TPValue))
32 willChangeValue(forKey: #keyPath(BRankTPValue))
35 didChangeValue(forKey: #keyPath(TPValue))
36 didChangeValue(forKey: #keyPath(BRankTPValue))
39 @objc dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
40 @objc dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
42 @objc dynamic var fleet1Seiku: Int = 0 {
44 willChangeValue(forKey: #keyPath(seiku))
47 didChangeValue(forKey: #keyPath(seiku))
50 @objc dynamic var fleet2Seiku: Int = 0 {
52 willChangeValue(forKey: #keyPath(seiku))
55 didChangeValue(forKey: #keyPath(seiku))
58 @objc dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
60 @objc dynamic var fleet1CalculatedSeiku: Int = 0 {
62 willChangeValue(forKey: #keyPath(seiku))
65 didChangeValue(forKey: #keyPath(seiku))
68 @objc dynamic var fleet2CalculatedSeiku: Int = 0 {
70 willChangeValue(forKey: #keyPath(calculatedSeiku))
73 didChangeValue(forKey: #keyPath(calculatedSeiku))
76 @objc dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
78 var combineType: CombineType? {
79 willSet { willChangeValue(forKey: #keyPath(combineTypeName)) }
80 didSet { didChangeValue(forKey: #keyPath(combineTypeName)) }
82 @objc dynamic var combineTypeName: String? { return combineType?.displayName() }
84 override var nibName: NSNib.Name {
86 return .nibName(instanceOf: self)
89 override func viewDidLoad() {
93 let placeholders: [NSView] = [placeholder1, placeholder2]
94 let fleets = [fleet1, fleet2]
95 zip(placeholders, fleets).forEach { placeholder, fleet in
97 fleet.view.frame = placeholder.frame
98 fleet.view.autoresizingMask = placeholder.autoresizingMask
99 placeholder.superview?.replaceSubview(placeholder, with: fleet.view)
101 fleets.enumerated().forEach { offset, fleet in
102 fleet.fleetNumber = offset + 1
105 bind(NSBindingName(#keyPath(fleet1TPValue)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalTPValue))
106 bind(NSBindingName(#keyPath(fleet2TPValue)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalTPValue))
108 bind(NSBindingName(#keyPath(fleet1Seiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalSeiku))
109 bind(NSBindingName(#keyPath(fleet2Seiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalSeiku))
111 bind(NSBindingName(#keyPath(fleet1CalculatedSeiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
112 bind(NSBindingName(#keyPath(fleet2CalculatedSeiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
114 NotificationCenter.default
115 .addObserver(forName: .CombinedDidCange, object: nil, queue: .main) { notification in
117 let type = notification.userInfo?[CombinedCommand.userInfoKey] as? CombineType
118 self.combineType = type
124 unbind(NSBindingName(#keyPath(fleet1TPValue)))
125 unbind(NSBindingName(#keyPath(fleet2TPValue)))
127 unbind(NSBindingName(#keyPath(fleet1Seiku)))
128 unbind(NSBindingName(#keyPath(fleet2Seiku)))
130 unbind(NSBindingName(#keyPath(fleet1CalculatedSeiku)))
131 unbind(NSBindingName(#keyPath(fleet2CalculatedSeiku)))