OSDN Git Service

ApplicationDirecroriesの中のアプリケーションに依存する部分を分離した
[kcd/KCD.git] / KCD / CombileViewController.swift
index c555aed..7172d4e 100644 (file)
@@ -13,71 +13,96 @@ final class CombileViewController: NSViewController {
     let fleet1 = FleetViewController(viewType: .miniVierticalType)!
     let fleet2 = FleetViewController(viewType: .miniVierticalType)!
     
-    @IBOutlet weak var placeholder1: NSView!
-    @IBOutlet weak var placeholder2: NSView!
+    @IBOutlet private weak var placeholder1: NSView!
+    @IBOutlet private weak var placeholder2: NSView!
     
-    dynamic var fleet1TPValue: Int = 0 {
+    @objc dynamic var fleet1TPValue: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(TPValue))
             willChangeValue(forKey: #keyPath(BRankTPValue))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(TPValue))
             didChangeValue(forKey: #keyPath(BRankTPValue))
         }
     }
-    dynamic var fleet2TPValue: Int = 0 {
+    @objc dynamic var fleet2TPValue: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(TPValue))
             willChangeValue(forKey: #keyPath(BRankTPValue))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(TPValue))
             didChangeValue(forKey: #keyPath(BRankTPValue))
         }
     }
-    dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
-    dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
+    @objc dynamic var TPValue: Int { return fleet1TPValue + fleet2TPValue }
+    @objc dynamic var BRankTPValue: Int { return Int(floor(Double(TPValue) * 0.7)) }
     
-    dynamic var fleet1Seiku: Int = 0 {
+    @objc dynamic var fleet1Seiku: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(seiku))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(seiku))
         }
     }
-    dynamic var fleet2Seiku: Int = 0 {
+    @objc dynamic var fleet2Seiku: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(seiku))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(seiku))
         }
     }
-    dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
+    @objc dynamic var seiku: Int { return fleet1Seiku + fleet2Seiku }
     
-    dynamic var fleet1CalculatedSeiku: Int = 0 {
+    @objc dynamic var fleet1CalculatedSeiku: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(seiku))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(seiku))
         }
     }
-    dynamic var fleet2CalculatedSeiku: Int = 0 {
+    @objc dynamic var fleet2CalculatedSeiku: Int = 0 {
+        
         willSet {
+            
             willChangeValue(forKey: #keyPath(calculatedSeiku))
         }
         didSet {
+            
             didChangeValue(forKey: #keyPath(calculatedSeiku))
         }
     }
-    dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
+    @objc dynamic var calculatedSeiku: Int { return fleet1CalculatedSeiku + fleet2CalculatedSeiku }
+    
+    var combineType: CombineType? {
+        
+        willSet { willChangeValue(forKey: #keyPath(combineTypeName)) }
+        didSet { didChangeValue(forKey: #keyPath(combineTypeName)) }
+    }
+    @objc dynamic var combineTypeName: String? { return combineType?.displayName() }
     
-    override var nibName: String! {
+    override var nibName: NSNib.Name {
         
-        return "CombileViewController"
+        return .nibName(instanceOf: self)
     }
     
     override func viewDidLoad() {
@@ -86,33 +111,43 @@ final class CombileViewController: NSViewController {
         
         let placeholders: [NSView] = [placeholder1, placeholder2]
         let fleets = [fleet1, fleet2]
-        zip(placeholders, fleets).enumerated().forEach {
+        zip(placeholders, fleets).forEach { placeholder, fleet in
+            
+            fleet.view.frame = placeholder.frame
+            fleet.view.autoresizingMask = placeholder.autoresizingMask
+            placeholder.superview?.replaceSubview(placeholder, with: fleet.view)
+        }
+        fleets.enumerated().forEach { offset, fleet in
             
-            $0.element.1.view.frame = $0.element.0.frame
-            $0.element.1.view.autoresizingMask = $0.element.0.autoresizingMask
-            $0.element.0.superview?.replaceSubview($0.element.0, with: $0.element.1.view)
-            $0.element.1.fleetNumber = $0.offset + 1
+            fleet.fleetNumber = offset + 1
         }
         
-        bind(#keyPath(fleet1TPValue), to: fleet1, withKeyPath: "totalTPValue")
-        bind(#keyPath(fleet2TPValue), to: fleet2, withKeyPath: "totalTPValue")
+        bind(NSBindingName(#keyPath(fleet1TPValue)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalTPValue))
+        bind(NSBindingName(#keyPath(fleet2TPValue)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalTPValue))
+        
+        bind(NSBindingName(#keyPath(fleet1Seiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalSeiku))
+        bind(NSBindingName(#keyPath(fleet2Seiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalSeiku))
         
-        bind(#keyPath(fleet1Seiku), to: fleet1, withKeyPath: "totalSeiku")
-        bind(#keyPath(fleet2Seiku), to: fleet2, withKeyPath: "totalSeiku")
+        bind(NSBindingName(#keyPath(fleet1CalculatedSeiku)), to: fleet1, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
+        bind(NSBindingName(#keyPath(fleet2CalculatedSeiku)), to: fleet2, withKeyPath: #keyPath(FleetViewController.totalCalclatedSeiku))
         
-        bind(#keyPath(fleet1CalculatedSeiku), to: fleet1, withKeyPath: "totalCalclatedSeiku")
-        bind(#keyPath(fleet2CalculatedSeiku), to: fleet2, withKeyPath: "totalCalclatedSeiku")
+        NotificationCenter.default
+            .addObserver(forName: .CombinedDidCange, object: nil, queue: .main) { notification in
+                
+                let type = notification.userInfo?[CombinedCommand.userInfoKey] as? CombineType
+                self.combineType = type
+        }
     }
     
     deinit {
         
-        unbind(#keyPath(fleet1TPValue))
-        unbind(#keyPath(fleet2TPValue))
+        unbind(NSBindingName(#keyPath(fleet1TPValue)))
+        unbind(NSBindingName(#keyPath(fleet2TPValue)))
         
-        unbind(#keyPath(fleet1Seiku))
-        unbind(#keyPath(fleet2Seiku))
+        unbind(NSBindingName(#keyPath(fleet1Seiku)))
+        unbind(NSBindingName(#keyPath(fleet2Seiku)))
         
-        unbind(#keyPath(fleet1CalculatedSeiku))
-        unbind(#keyPath(fleet2CalculatedSeiku))
+        unbind(NSBindingName(#keyPath(fleet1CalculatedSeiku)))
+        unbind(NSBindingName(#keyPath(fleet2CalculatedSeiku)))
     }
 }