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         
21         willSet {
22             
23             willChangeValue(forKey: #keyPath(TPValue))
24             willChangeValue(forKey: #keyPath(BRankTPValue))
25         }
26         didSet {
27             
28             didChangeValue(forKey: #keyPath(TPValue))
29             didChangeValue(forKey: #keyPath(BRankTPValue))
30         }
31     }
32     @objc dynamic var fleet2TPValue: Int = 0 {
33         
34         willSet {
35             
36             willChangeValue(forKey: #keyPath(TPValue))
37             willChangeValue(forKey: #keyPath(BRankTPValue))
38         }
39         didSet {
40             
41             didChangeValue(forKey: #keyPath(TPValue))
42             didChangeValue(forKey: #keyPath(BRankTPValue))
43         }
44     }
45     @objc dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
46     @objc dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
47     
48     @objc dynamic var fleet1Seiku: Int = 0 {
49         
50         willSet {
51             
52             willChangeValue(forKey: #keyPath(seiku))
53         }
54         didSet {
55             
56             didChangeValue(forKey: #keyPath(seiku))
57         }
58     }
59     @objc dynamic var fleet2Seiku: Int = 0 {
60         
61         willSet {
62             
63             willChangeValue(forKey: #keyPath(seiku))
64         }
65         didSet {
66             
67             didChangeValue(forKey: #keyPath(seiku))
68         }
69     }
70     @objc dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
71     
72     @objc dynamic var fleet1CalculatedSeiku: Int = 0 {
73         
74         willSet {
75             
76             willChangeValue(forKey: #keyPath(seiku))
77         }
78         didSet {
79             
80             didChangeValue(forKey: #keyPath(seiku))
81         }
82     }
83     @objc dynamic var fleet2CalculatedSeiku: Int = 0 {
84         
85         willSet {
86             
87             willChangeValue(forKey: #keyPath(calculatedSeiku))
88         }
89         didSet {
90             
91             didChangeValue(forKey: #keyPath(calculatedSeiku))
92         }
93     }
94     @objc dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
95     
96     var combineType: CombineType? {
97         
98         willSet { willChangeValue(forKey: #keyPath(combineTypeName)) }
99         didSet { didChangeValue(forKey: #keyPath(combineTypeName)) }
100     }
101     @objc dynamic var combineTypeName: String? { return combineType?.displayName() }
102     
103     override var nibName: NSNib.Name {
104         
105         return .nibName(instanceOf: self)
106     }
107     
108     override func viewDidLoad() {
109         
110         super.viewDidLoad()
111         
112         let placeholders: [NSView] = [placeholder1, placeholder2]
113         let fleets = [fleet1, fleet2]
114         zip(placeholders, fleets).forEach { placeholder, fleet in
115             
116             fleet.view.frame = placeholder.frame
117             fleet.view.autoresizingMask = placeholder.autoresizingMask
118             placeholder.superview?.replaceSubview(placeholder, with: fleet.view)
119         }
120         fleets.enumerated().forEach { offset, fleet in
121             
122             fleet.fleetNumber = offset + 1
123         }
124         
125         bind(NSBindingName(#keyPath(fleet1TPValue)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalTPValue))
126         bind(NSBindingName(#keyPath(fleet2TPValue)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalTPValue))
127         
128         bind(NSBindingName(#keyPath(fleet1Seiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalSeiku))
129         bind(NSBindingName(#keyPath(fleet2Seiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalSeiku))
130         
131         bind(NSBindingName(#keyPath(fleet1CalculatedSeiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
132         bind(NSBindingName(#keyPath(fleet2CalculatedSeiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
133         
134         NotificationCenter.default
135             .addObserver(forName: .CombinedDidCange, object: nil, queue: .main) { notification in
136                 
137                 let type = notification.userInfo?[CombinedCommand.userInfoKey] as? CombineType
138                 self.combineType = type
139         }
140     }
141     
142     deinit {
143         
144         unbind(NSBindingName(#keyPath(fleet1TPValue)))
145         unbind(NSBindingName(#keyPath(fleet2TPValue)))
146         
147         unbind(NSBindingName(#keyPath(fleet1Seiku)))
148         unbind(NSBindingName(#keyPath(fleet2Seiku)))
149         
150         unbind(NSBindingName(#keyPath(fleet1CalculatedSeiku)))
151         unbind(NSBindingName(#keyPath(fleet2CalculatedSeiku)))
152     }
153 }