From 305f3b759aa5a6164ad0dde590e68819c58bf17f Mon Sep 17 00:00:00 2001 From: masakih Date: Sun, 27 Aug 2017 11:56:18 +0900 Subject: [PATCH] =?utf8?q?=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= =?utf8?q?=E3=83=AA=E3=83=B3=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 艦載機数の処理はShipDetailViewControllerしか使用しないので移動 --- KCD/Base.lproj/ShipDetailViewController.xib | 97 ++++++++-------- KCD/Ship.swift | 117 +------------------ KCD/ShipDetailViewController.swift | 167 ++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+), 162 deletions(-) diff --git a/KCD/Base.lproj/ShipDetailViewController.xib b/KCD/Base.lproj/ShipDetailViewController.xib index faac13d2..3e437626 100644 --- a/KCD/Base.lproj/ShipDetailViewController.xib +++ b/KCD/Base.lproj/ShipDetailViewController.xib @@ -1,7 +1,8 @@ - + - + + @@ -33,7 +34,7 @@ - + @@ -49,7 +50,7 @@ - + @@ -68,7 +69,7 @@ - + @@ -77,7 +78,7 @@ - + @@ -95,7 +96,7 @@ - + @@ -113,7 +114,7 @@ - + @@ -139,7 +140,7 @@ - + @@ -148,7 +149,7 @@ - + @@ -166,7 +167,7 @@ - + @@ -175,7 +176,7 @@ - + @@ -193,7 +194,7 @@ - + @@ -202,7 +203,7 @@ - + @@ -220,7 +221,7 @@ - + @@ -229,7 +230,7 @@ - + @@ -247,7 +248,7 @@ - + @@ -256,7 +257,7 @@ - + @@ -274,7 +275,7 @@ - + @@ -283,7 +284,7 @@ - + @@ -301,7 +302,7 @@ - + @@ -310,7 +311,7 @@ - + @@ -328,7 +329,7 @@ - + @@ -337,7 +338,7 @@ - + @@ -354,7 +355,7 @@ - + @@ -363,7 +364,7 @@ - + @@ -380,7 +381,7 @@ - + @@ -389,7 +390,7 @@ - + @@ -407,7 +408,7 @@ - + @@ -423,7 +424,7 @@ - + @@ -440,7 +441,7 @@ - + @@ -457,7 +458,7 @@ - + @@ -474,7 +475,7 @@ - + @@ -491,7 +492,7 @@ - + @@ -501,17 +502,17 @@ - + + IA - - + @@ -521,17 +522,17 @@ IA - + + IA - - + @@ -541,17 +542,17 @@ IA - + + IA - - + @@ -561,14 +562,14 @@ IA - + + IA - @@ -576,7 +577,7 @@ IA - + @@ -601,7 +602,7 @@ IA - + @@ -632,7 +633,7 @@ IA - + diff --git a/KCD/Ship.swift b/KCD/Ship.swift index 336ab283..c771837d 100644 --- a/KCD/Ship.swift +++ b/KCD/Ship.swift @@ -289,8 +289,6 @@ extension Ship { } } -fileprivate let allPlaneTypes: [Int] = [6, 7, 8, 9, 10, 11, 25, 26, 41, 45, 56, 57, 58, 59] - extension Ship { func setItem(_ id: Int, for slot: Int) { @@ -329,7 +327,7 @@ extension Ship { } } - private func slotItemMax(_ index: Int) -> Int { + func slotItemMax(_ index: Int) -> Int { switch index { case 0: return master_ship.maxeq_0 @@ -341,7 +339,7 @@ extension Ship { } } - private func slotItem(_ index: Int) -> SlotItem? { + func slotItem(_ index: Int) -> SlotItem? { return ServerDataStore.default.slotItem(by: slotItemId(index)) } @@ -361,11 +359,7 @@ extension Ship { return SeikuCalclator(ship: self).seiku } - class func keyPathsForValuesAffectingTotalSeiku() -> Set { - - return ["seiku"] - } - dynamic var totalSeiku: Int { + var totalSeiku: Int { return SeikuCalclator(ship: self).totalSeiku } @@ -375,109 +369,4 @@ extension Ship { return (0...4).flatMap(slotItem).filter { $0.slotitem_id == 75 }.count } - // MARK: - Plane count strings - private enum PlaneState { - - case cannotEquip - case notEquip(Int) - case equiped(Int, Int) - } - - private func planState(_ index: Int) -> PlaneState { - - let itemId = slotItemId(index) - let maxCount = slotItemMax(index) - - if maxCount == 0 { return .cannotEquip } - if itemId == -1 { return .notEquip(maxCount) } - - if let item = slotItem(index), - allPlaneTypes.contains(item.master_slotItem.type_2) { - - return .equiped(slotItemCount(index), maxCount) - } - - return .notEquip(maxCount) - } - - private func planeString(_ index: Int) -> String? { - - switch planState(index) { - case .cannotEquip: - return nil - case .notEquip(let max): - return "\(max)" - case .equiped(let count, let max): - return "\(count)/\(max)" - } - } - - class func keyPathsForValuesAffectingPlaneString0() -> Set { - - return ["onslot_0", "master_ship.maxeq_0", "equippedItem"] - } - dynamic var planeString0: String? { return planeString(0) } - - class func keyPathsForValuesAffectingPlaneString1() -> Set { - - return ["onslot_1", "master_ship.maxeq_1", "equippedItem"] - } - dynamic var planeString1: String? { return planeString(1) } - - class func keyPathsForValuesAffectingPlaneString2() -> Set { - - return ["onslot_2", "master_ship.maxeq_2", "equippedItem"] - } - dynamic var planeString2: String? { return planeString(2) } - - class func keyPathsForValuesAffectingPlaneString3() -> Set { - - return ["onslot_3", "master_ship.maxeq_3", "equippedItem"] - } - dynamic var planeString3: String? { return planeString(3) } - - class func keyPathsForValuesAffectingPlaneString4() -> Set { - - return ["onslot_4", "master_ship.maxeq_4", "equippedItem"] - } - dynamic var planeString4: String? { return planeString(4) } - - // MARK: - Plane count string color - private func planeStringColor(_ index: Int) -> NSColor { - switch planState(index) { - case .cannotEquip: return NSColor.controlTextColor - case .notEquip: return NSColor.disabledControlTextColor - case .equiped: return NSColor.controlTextColor - } - } - - class func keyPathsForValuesAffectingPlaneString0Color() -> Set { - - return ["onslot_0", "master_ship.maxeq_0", "equippedItem"] - } - dynamic var planeString0Color: NSColor { return planeStringColor(0) } - - class func keyPathsForValuesAffectingPlaneString1Color() -> Set { - - return ["onslot_1", "master_ship.maxeq_1", "equippedItem"] - } - dynamic var planeString1Color: NSColor { return planeStringColor(1) } - - class func keyPathsForValuesAffectingPlaneString2Color() -> Set { - - return ["onslot_2", "master_ship.maxeq_2", "equippedItem"] - } - dynamic var planeString2Color: NSColor { return planeStringColor(2) } - - class func keyPathsForValuesAffectingPlaneString3Color() -> Set { - - return ["onslot_3", "master_ship.maxeq_3", "equippedItem"] - } - dynamic var planeString3Color: NSColor { return planeStringColor(3) } - - class func keyPathsForValuesAffectingPlaneString4Color() -> Set { - - return ["onslot_4", "master_ship.maxeq_4", "equippedItem"] - } - dynamic var planeString4Color: NSColor { return planeStringColor(4) } } diff --git a/KCD/ShipDetailViewController.swift b/KCD/ShipDetailViewController.swift index f9178e3e..5a04c8a0 100644 --- a/KCD/ShipDetailViewController.swift +++ b/KCD/ShipDetailViewController.swift @@ -24,6 +24,13 @@ private func nibNameFor(_ type: ShipDetailViewType) -> String { } } +fileprivate var shipContext: Int = 0 +fileprivate var equippedItem0Context: Int = 0 +fileprivate var equippedItem1Context: Int = 0 +fileprivate var equippedItem2Context: Int = 0 +fileprivate var equippedItem3Context: Int = 0 +fileprivate var equippedItem4Context: Int = 0 + final class ShipDetailViewController: NSViewController { let type: ShipDetailViewType @@ -57,6 +64,8 @@ final class ShipDetailViewController: NSViewController { supply.unbind(#keyPath(SuppliesView.shipStatus)) [slot00Field, slot01Field, slot02Field, slot03Field] .forEach { $0?.unbind(#keyPath(SlotItemLevelView.slotItemID)) } + + shipController.removeObject(self) } @@ -80,6 +89,7 @@ final class ShipDetailViewController: NSViewController { get { return shipController.content as? Ship } set { + shipController.fetchPredicate = NSPredicate(format: "id = %ld", newValue?.id ?? 0) } } @@ -113,5 +123,162 @@ final class ShipDetailViewController: NSViewController { $0.0?.bind(#keyPath(SlotItemLevelView.slotItemID), to: shipController, withKeyPath: $0.1, options: nil) } + + // observe slotitems count + shipController.addObserver(self, forKeyPath: "selection", context: &shipContext) + + shipController.addObserver(self, forKeyPath: "selection.slot_0", context: &equippedItem0Context) + shipController.addObserver(self, forKeyPath: "selection.onslot_0", context: &equippedItem0Context) + shipController.addObserver(self, forKeyPath: "selection.master_ship.maxeq_0", context: &equippedItem0Context) + + shipController.addObserver(self, forKeyPath: "selection.slot_1", context: &equippedItem1Context) + shipController.addObserver(self, forKeyPath: "selection.onslot_1", context: &equippedItem1Context) + shipController.addObserver(self, forKeyPath: "selection.master_ship.maxeq_1", context: &equippedItem1Context) + + shipController.addObserver(self, forKeyPath: "selection.slot_2", context: &equippedItem2Context) + shipController.addObserver(self, forKeyPath: "selection.onslot_2", context: &equippedItem2Context) + shipController.addObserver(self, forKeyPath: "selection.master_ship.maxeq_2", context: &equippedItem2Context) + + shipController.addObserver(self, forKeyPath: "selection.slot_3", context: &equippedItem3Context) + shipController.addObserver(self, forKeyPath: "selection.onslot_3", context: &equippedItem3Context) + shipController.addObserver(self, forKeyPath: "selection.master_ship.maxeq_3", context: &equippedItem3Context) + + shipController.addObserver(self, forKeyPath: "selection.slot_4", context: &equippedItem4Context) + shipController.addObserver(self, forKeyPath: "selection.onslot_4", context: &equippedItem4Context) + shipController.addObserver(self, forKeyPath: "selection.master_ship.maxeq_4", context: &equippedItem4Context) + + } + + override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { + + if context == &shipContext { + + notifyChangeValue(forKey: "planeString0") + notifyChangeValue(forKey: "planeString0Color") + notifyChangeValue(forKey: "planeString1") + notifyChangeValue(forKey: "planeString1Color") + notifyChangeValue(forKey: "planeString2") + notifyChangeValue(forKey: "planeString2Color") + notifyChangeValue(forKey: "planeString3") + notifyChangeValue(forKey: "planeString3Color") + notifyChangeValue(forKey: "planeString4") + notifyChangeValue(forKey: "planeString4Color") + + return + } + if context == &equippedItem0Context { + + notifyChangeValue(forKey: "planeString0") + notifyChangeValue(forKey: "planeString0Color") + + return + } + if context == &equippedItem1Context { + + notifyChangeValue(forKey: "planeString1") + notifyChangeValue(forKey: "planeString1Color") + + return + } + if context == &equippedItem2Context { + + notifyChangeValue(forKey: "planeString2") + notifyChangeValue(forKey: "planeString2Color") + + return + } + if context == &equippedItem3Context { + + notifyChangeValue(forKey: "planeString3") + notifyChangeValue(forKey: "planeString3Color") + + return + } + if context == &equippedItem4Context { + + notifyChangeValue(forKey: "planeString4") + notifyChangeValue(forKey: "planeString4Color") + + return + } + + super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) } } + + +fileprivate let allPlaneTypes: [Int] = [6, 7, 8, 9, 10, 11, 25, 26, 41, 45, 56, 57, 58, 59] + + +extension ShipDetailViewController { + + // MARK: - Plane count strings + private enum PlaneState { + + case cannotEquip + case notEquip(Int) + case equiped(Int, Int) + } + + private func planState(_ index: Int) -> PlaneState { + + guard let ship = ship + else { return .cannotEquip } + + let itemId = ship.slotItemId(index) + let maxCount = ship.slotItemMax(index) + + if maxCount == 0 { return .cannotEquip } + if itemId == -1 { return .notEquip(maxCount) } + + if let item = ship.slotItem(index), + allPlaneTypes.contains(item.master_slotItem.type_2) { + + return .equiped(ship.slotItemCount(index), maxCount) + } + + return .notEquip(maxCount) + } + + private func planeString(_ index: Int) -> String? { + + switch planState(index) { + case .cannotEquip: + return nil + case .notEquip(let max): + return "\(max)" + case .equiped(let count, let max): + return "\(count)/\(max)" + } + } + + dynamic var planeString0: String? { return planeString(0) } + + dynamic var planeString1: String? { return planeString(1) } + + dynamic var planeString2: String? { return planeString(2) } + + dynamic var planeString3: String? { return planeString(3) } + + dynamic var planeString4: String? { return planeString(4) } + + // MARK: - Plane count string color + private func planeStringColor(_ index: Int) -> NSColor { + + switch planState(index) { + case .cannotEquip: return NSColor.controlTextColor + case .notEquip: return NSColor.disabledControlTextColor + case .equiped: return NSColor.controlTextColor + } + } + + dynamic var planeString0Color: NSColor { return planeStringColor(0) } + + dynamic var planeString1Color: NSColor { return planeStringColor(1) } + + dynamic var planeString2Color: NSColor { return planeStringColor(2) } + + dynamic var planeString3Color: NSColor { return planeStringColor(3) } + + dynamic var planeString4Color: NSColor { return planeStringColor(4) } +} -- 2.11.0