OSDN Git Service

艦娘一覧の処理をShipListPanelとして分離する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ListForm.cs
1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Drawing;\r
18 using System.Linq;\r
19 using System.Windows.Forms;\r
20 using static System.Math;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public partial class ListForm : Form\r
25     {\r
26         private readonly Sniffer _sniffer;\r
27         private readonly Config _config;\r
28         public const int PanelWidth = 217;\r
29 \r
30         public enum SortOrder\r
31         {\r
32             None,\r
33             Cond,\r
34             ExpToNext,\r
35             Repair\r
36         }\r
37 \r
38         public ListForm(Sniffer sniffer, Config config)\r
39         {\r
40             InitializeComponent();\r
41             _sniffer = sniffer;\r
42             _config = config;\r
43             var swipe = new SwipeScrollify();\r
44             swipe.AddPanel(shipListPanel);\r
45             swipe.AddTreeView(itemTreeView);\r
46             swipe.AddPanel(equipPanel);\r
47         }\r
48 \r
49         public void UpdateList()\r
50         {\r
51             panelItemHeader.Visible = InItemList || InEquip || InMiscText;\r
52             panelGroupHeader.Visible = InGroupConfig;\r
53             panelRepairHeader.Visible = InRepairList;\r
54             // SwipeScrollifyが誤作動するのでEnabledも切り替える\r
55             shipListPanel.Visible = shipListPanel.Enabled = InShipStatus || InGroupConfig || InRepairList;\r
56             itemTreeView.Visible = itemTreeView.Enabled = InItemList;\r
57             equipPanel.Visible = equipPanel.Enabled = InEquip;\r
58             richTextBoxMiscText.Visible = InMiscText;\r
59             if (InItemList)\r
60             {\r
61                 itemTreeView.SetNodes(_sniffer.ItemList);\r
62             }\r
63             else if (InEquip)\r
64             {\r
65                 equipPanel.UpdateEquip(_sniffer);\r
66             }\r
67             else if (InMiscText)\r
68             {\r
69                 richTextBoxMiscText.Text = _sniffer.MiscText;\r
70             }\r
71             else\r
72             {\r
73                 SetHeaderSortOrder();\r
74                 shipListPanel.Update(_sniffer, comboBoxGroup.Text, _config.ShipList.SortOrder, _config.ShipList.ShipType);\r
75             }\r
76         }\r
77 \r
78         private void SetHeaderSortOrder()\r
79         {\r
80             switch (_config.ShipList.SortOrder)\r
81             {\r
82                 case SortOrder.None:\r
83                     labelHeaderCond.Text = "cond";\r
84                     labelHeaderExp.Text = "Exp";\r
85                     break;\r
86                 case SortOrder.Cond:\r
87                     labelHeaderCond.Text = "cond▴";\r
88                     labelHeaderExp.Text = "Exp";\r
89                     break;\r
90                 case SortOrder.ExpToNext:\r
91                     labelHeaderCond.Text = "cond";\r
92                     labelHeaderExp.Text = "Exp▴";\r
93                     break;\r
94             }\r
95         }\r
96 \r
97         private bool InShipStatus => Array.Exists(new[] {"全員", "A", "B", "C", "D"}, x => comboBoxGroup.Text == x);\r
98 \r
99         private bool InGroupConfig => comboBoxGroup.Text == "分類";\r
100 \r
101         private bool InRepairList => comboBoxGroup.Text == "修復";\r
102 \r
103         private bool InItemList => comboBoxGroup.Text == "装備";\r
104 \r
105         private bool InEquip => comboBoxGroup.Text == "艦隊";\r
106 \r
107         private bool InMiscText => comboBoxGroup.Text == "情報";\r
108 \r
109         private void ShipListForm_Load(object sender, EventArgs e)\r
110         {\r
111             shipListPanel.Width = itemTreeView.Width = equipPanel.Width =\r
112                 (int)Round(PanelWidth * ShipLabel.ScaleFactor.Width) + 3 + SystemInformation.VerticalScrollBarWidth;\r
113             Width = shipListPanel.Width + 12 + (Width - ClientSize.Width);\r
114             MinimumSize = new Size(Width, 0);\r
115             MaximumSize = new Size(Width, int.MaxValue);\r
116             var config = _config.ShipList;\r
117             checkBoxShipType.Checked = config.ShipType;\r
118             ActiveControl = shipListPanel;\r
119             for (var i = 0; i < ShipListPanel.GroupCount; i++)\r
120             {\r
121                 shipListPanel.GroupSettings[i] = i < config.ShipGroup.Count\r
122                     ? new HashSet<int>(config.ShipGroup[i])\r
123                     : new HashSet<int>();\r
124             }\r
125             comboBoxGroup.SelectedIndex = 0;\r
126             if (config.Location.X == int.MinValue)\r
127                 return;\r
128             var bounds = new Rectangle(config.Location, config.Size);\r
129             if (MainForm.IsVisibleOnAnyScreen(bounds))\r
130                 Location = bounds.Location;\r
131             Height = bounds.Height;\r
132         }\r
133 \r
134         private void ShipListForm_FormClosing(object sender, FormClosingEventArgs e)\r
135         {\r
136             e.Cancel = true;\r
137             if (!Visible)\r
138                 return;\r
139             var config = _config.ShipList;\r
140             var all = _sniffer.ShipList.Select(s => s.Id).ToArray();\r
141             config.ShipGroup.Clear();\r
142             for (var i = 0; i < ShipListPanel.GroupCount; i++)\r
143             {\r
144                 if (all.Length > 0)\r
145                     shipListPanel.GroupSettings[i].IntersectWith(all);\r
146                 config.ShipGroup.Add(shipListPanel.GroupSettings[i].ToList());\r
147             }\r
148             var bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds;\r
149             config.Location = bounds.Location;\r
150             config.Size = bounds.Size;\r
151             Hide();\r
152         }\r
153 \r
154         public void ShowShip(int id)\r
155         {\r
156             if (InShipStatus)\r
157             {\r
158                 shipListPanel.ShowShip(id);\r
159             }\r
160             else if (InEquip)\r
161             {\r
162                 equipPanel.ShowShip(id);\r
163             }\r
164         }\r
165 \r
166         private void checkBoxShipType_CheckedChanged(object sender, EventArgs e)\r
167         {\r
168             _config.ShipList.ShipType = checkBoxShipType.Checked;\r
169             UpdateList();\r
170             SetActiveControl();\r
171         }\r
172 \r
173         private void comboBoxGroup_DropDownClosed(object sender, EventArgs e)\r
174         {\r
175             SetActiveControl();\r
176         }\r
177 \r
178         private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)\r
179         {\r
180             UpdateList();\r
181             SetActiveControl();\r
182             copyToolStripMenuItem.Enabled = InShipStatus | InItemList;\r
183         }\r
184 \r
185         private void ShipListForm_KeyPress(object sender, KeyPressEventArgs e)\r
186         {\r
187             var g = Array.FindIndex(new[] {'Z', 'A', 'B', 'C', 'D', 'G', 'R', 'W', 'X', 'I'},\r
188                 x => x == char.ToUpper(e.KeyChar));\r
189             if (g == -1)\r
190                 return;\r
191             comboBoxGroup.SelectedIndex = g;\r
192             e.Handled = true;\r
193         }\r
194 \r
195         // マウスホイールでスクロールするためにコントロールにフォーカスを合わせる。\r
196         private void SetActiveControl()\r
197         {\r
198             if (InItemList)\r
199             {\r
200                 ActiveControl = itemTreeView;\r
201             }\r
202             else if (InEquip)\r
203             {\r
204                 ActiveControl = equipPanel;\r
205             }\r
206             else\r
207             {\r
208                 ActiveControl = shipListPanel;\r
209             }\r
210         }\r
211 \r
212         private void copyToolStripMenuItem_Click(object sender, EventArgs e)\r
213         {\r
214             Clipboard.SetText(TextGenerator.GenerateItemList(_sniffer.ItemList));\r
215         }\r
216 \r
217         private void textToolStripMenuItem_Click(object sender, EventArgs e)\r
218         {\r
219             Clipboard.SetText(TextGenerator.GenerateFleetData(_sniffer));\r
220         }\r
221 \r
222         private void deckBuilderToolStripMenuItem_Click(object sender, EventArgs e)\r
223         {\r
224             Clipboard.SetText(TextGenerator.GenerateDeckBuilderData(_sniffer));\r
225         }\r
226 \r
227         private void labelHeaderCond_Click(object sender, EventArgs e)\r
228         {\r
229             _config.ShipList.SortOrder = _config.ShipList.SortOrder == SortOrder.Cond ? SortOrder.None : SortOrder.Cond;\r
230             UpdateList();\r
231         }\r
232 \r
233         private void labelHeaderExp_Click(object sender, EventArgs e)\r
234         {\r
235             _config.ShipList.SortOrder = _config.ShipList.SortOrder == SortOrder.ExpToNext\r
236                 ? SortOrder.None\r
237                 : SortOrder.ExpToNext;\r
238             UpdateList();\r
239         }\r
240 \r
241         private void csvToolStripMenuItem_Click(object sender, EventArgs e)\r
242         {\r
243             Clipboard.SetText(TextGenerator.GenerateShipList(shipListPanel.CurrentShipList));\r
244         }\r
245 \r
246         private void kantaiSarashiToolStripMenuItem_Click(object sender, EventArgs e)\r
247         {\r
248             Clipboard.SetText(TextGenerator.GenerateKantaiSarashiData(shipListPanel.CurrentShipList));\r
249         }\r
250     }\r
251 }