OSDN Git Service

b4332d3c6791ad8a5191655e8776efc07ddb5d47
[kcd/KCD.git] / KCD / InformationTabViewController.swift
1 //
2 //  InformationTabViewController.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2018/02/04.
6 //  Copyright © 2018年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 @objc
12 enum ShipTabType: Int {
13     
14     case all = 0
15     
16     case destroyer = 1
17     
18     case lightCruiser = 2
19     
20     case heavyCruiser = 3
21     
22     case aircraftCarrier = 4
23     
24     case battleShip = 5
25     
26     case submarine = 6
27     
28     case other = 7
29 }
30
31 private let shipTypeCategories: [[Int]] = [
32     [0],    // dummy
33     [2],    // destoryer
34     [3, 4], // leght cruiser
35     [5, 6], // heavy crusier
36     [7, 11, 16, 18],    // aircraft carrier
37     [8, 9, 10, 12], // battle ship
38     [13, 14],   // submarine
39     [1, 15, 17, 19, 20, 21, 22]
40 ]
41
42 func shipTypePredicte(for type: ShipTabType) -> NSPredicate? {
43     
44     switch type {
45         
46     case .all:
47         
48         return nil
49         
50     case .destroyer, .lightCruiser, .heavyCruiser,
51          .aircraftCarrier, .battleShip, .submarine:
52         
53         return NSPredicate(#keyPath(Ship.master_ship.stype.id), valuesIn: shipTypeCategories[type.rawValue])
54         
55     case .other:
56         let omitTypes = shipTypeCategories
57             .lazy
58             .enumerated()
59             .filter { $0.offset != 0 && $0.offset != 7 }
60             .flatMap { $0.element }
61         
62         return .not(NSPredicate(#keyPath(Ship.master_ship.stype.id), valuesIn: omitTypes))
63         
64     }
65 }
66
67 class InformationTabViewController: NSViewController {
68     
69     @objc private(set) dynamic var hasShipTypeSelector: Bool = false
70     @objc private(set) dynamic var selectedShipType: ShipTabType = .all {
71         
72         didSet {
73             guard case 0..<tabViewItemViewControllers.count = selectionIndex else {
74                 
75                 return
76             }
77             tabViewItemViewControllers[selectionIndex].selectedShipType = selectedShipType
78         }
79     }
80     @objc dynamic var selectionIndex: Int = 0 {
81         
82         willSet {
83             
84             unbind(NSBindingName("selectedShipType"))
85         }
86
87         didSet {
88             
89             guard case 0..<tabViewItemViewControllers.count = selectionIndex else {
90                 
91                 return
92             }
93             hasShipTypeSelector = tabViewItemViewControllers[selectionIndex].hasShipTypeSelector
94
95             bind(NSBindingName("selectedShipType"),
96                  to: tabViewItemViewControllers[selectionIndex],
97                  withKeyPath: #keyPath(MainTabVIewItemViewController.selectedShipType))
98             
99             selectionDidChangeHandler?()
100         }
101     }
102     
103     @IBOutlet private var informations: NSTabView!
104     
105     var selectionDidChangeHandler: (() -> Void)?
106     
107     private var tabViewItemViewControllers: [MainTabVIewItemViewController] = []
108     
109     override var nibName: NSNib.Name {
110         
111         return .nibName(instanceOf: self)
112     }
113     
114     override func viewDidLoad() {
115         
116         super.viewDidLoad()
117         
118         tabViewItemViewControllers = [
119             DocksViewController(),
120             ShipViewController(),
121             PowerUpSupportViewController(),
122             StrengthenListViewController(),
123             RepairListViewController()
124         ]
125         tabViewItemViewControllers.enumerated().forEach {
126             
127             _ = $0.element.view
128             let item = informations.tabViewItem(at: $0.offset)
129             item.viewController = $0.element
130         }
131     }
132 }