1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>
\r
3 // This program is part of KancolleSniffer.
\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify
\r
6 // it under the terms of the GNU General Public License as published by
\r
7 // the Free Software Foundation, either version 3 of the License, or
\r
8 // (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.
\r
19 using System.Collections.Generic;
\r
20 using System.Drawing;
\r
22 using System.Windows.Forms;
\r
23 using ContentAlignment = System.Drawing.ContentAlignment;
\r
25 namespace KancolleSniffer
\r
27 public partial class ShipListForm : Form
\r
29 private readonly Sniffer _sniffer;
\r
30 private readonly Config _config;
\r
31 private const int LabelHeight = 12;
\r
32 private const int LineHeight = 16;
\r
33 private const int PanelWidth = 217;
\r
34 private ShipStatus[] _shipList;
\r
35 private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();
\r
36 private readonly List<Panel> _labelPanelList = new List<Panel>();
\r
37 private readonly List<CheckBox[]> _checkBoxesList = new List<CheckBox[]>();
\r
38 private readonly List<ShipLabel[]> _configLabelList = new List<ShipLabel[]>();
\r
39 private readonly List<Panel> _checkBoxPanelList = new List<Panel>();
\r
40 private readonly List<ShipLabel[]> _repairLabelList = new List<ShipLabel[]>();
\r
41 private readonly List<Panel> _repairPanelList = new List<Panel>();
\r
42 public const int GroupCount = 4;
\r
43 private readonly HashSet<int>[] _groupSettings = new HashSet<int>[GroupCount];
\r
44 private TreeNode _itemTreeNode;
\r
45 private ItemStatus[] _prevItemList;
\r
47 public ShipListForm(Sniffer sniffer, Config config)
\r
49 InitializeComponent();
\r
54 public void UpdateList()
\r
56 panelItemHeader.Visible = InItemList();
\r
57 treeViewItem.Visible = InItemList();
\r
61 if (CreateItemList())
\r
72 private void CreateShipList()
\r
74 var ships = InRepairList() ? _sniffer.DamagedShipList : FilterByGroup(_sniffer.ShipList).ToArray();
\r
75 if (!_config.ShipList.ShipType)
\r
77 _shipList = ships.OrderBy(s => s, new CompareShip(false, InRepairList())).ToArray();
\r
80 var types = ships.Select(s => new {Id = s.Spec.ShipType, Name = s.Spec.ShipTypeName}).Distinct().
\r
84 Spec = new ShipSpec {Name = stype.Name, ShipType = stype.Id},
\r
88 _shipList = ships.Concat(types).OrderBy(s => s, new CompareShip(true, InRepairList())).ToArray();
\r
91 private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships)
\r
93 var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == comboBoxGroup.Text);
\r
96 return from s in ships where _groupSettings[g].Contains(s.Id) select s;
\r
99 private class CompareShip : IComparer<ShipStatus>
\r
101 private readonly bool _type;
\r
102 private readonly bool _repair;
\r
104 public CompareShip(bool type, bool repair)
\r
110 public int Compare(ShipStatus a, ShipStatus b)
\r
112 if (_type && a.Spec.ShipType != b.Spec.ShipType)
\r
113 return a.Spec.ShipType - b.Spec.ShipType;
\r
114 if (_repair && a.RepairTime != b.RepairTime)
\r
115 return (int)(b.RepairTime - a.RepairTime).TotalSeconds;
\r
116 if (a.Level != b.Level)
\r
117 return b.Level - a.Level;
\r
118 if (a.ExpToNext != b.ExpToNext)
\r
119 return a.ExpToNext - b.ExpToNext;
\r
120 return a.Spec.Id - b.Spec.Id;
\r
124 private bool CreateItemList()
\r
126 var itemList = _sniffer.ItemList;
\r
127 if (_prevItemList != null && _prevItemList.SequenceEqual(itemList, new ItemStatusComparer()))
\r
129 _prevItemList = itemList.Select(CloneItemStatus).ToArray();
\r
130 var grouped = from byId in
\r
131 (from item in itemList where item.Spec.Id != -1
\r
132 orderby item.Spec.Type, item.Spec.Id, item.Level descending, item.Ship.Spec.Id
\r
133 group item by new {item.Spec.Id, item.Level})
\r
134 group byId by byId.First().Spec.Type;
\r
135 _itemTreeNode = new TreeNode();
\r
136 foreach (var byType in grouped)
\r
138 var typeName = byType.First().First().Spec.TypeName;
\r
139 var typeNode = new TreeNode();
\r
140 typeNode.Name = typeNode.Text = typeName;
\r
141 _itemTreeNode.Nodes.Add(typeNode);
\r
142 foreach (var byItem in byType)
\r
144 var item = byItem.First();
\r
145 var itemNode = new TreeNode();
\r
146 itemNode.Name = itemNode.Text = item.Spec.Name + (item.Level == 0 ? "" : "★" + item.Level);
\r
147 typeNode.Nodes.Add(itemNode);
\r
149 var shipGroup = (from i in byItem group i.Ship by i.Ship.Id).ToArray();
\r
150 foreach (var name in
\r
151 from grp in shipGroup where grp.Key != -1
\r
152 let ship = grp.First()
\r
153 select (ship.Fleet != -1 ? ship.Fleet + 1 + " " : "") +
\r
154 ship.Name + "Lv" + ship.Level + "×" + grp.Count())
\r
156 itemNode.Nodes.Add(name, name);
\r
158 foreach (var name in (from grp in shipGroup where grp.Key == -1 select "未装備×" + grp.Count()))
\r
160 itemNode.Nodes.Add(name, name);
\r
167 private ItemStatus CloneItemStatus(ItemStatus org)
\r
169 return new ItemStatus
\r
173 Ship = new ShipStatus {Id = org.Ship.Id, Fleet = org.Ship.Fleet}
\r
177 private class ItemStatusComparer : IEqualityComparer<ItemStatus>
\r
179 public bool Equals(ItemStatus x, ItemStatus y)
\r
181 return x.Level == y.Level && x.Spec == y.Spec && x.Ship.Id == y.Ship.Id && x.Ship.Fleet == y.Ship.Fleet;
\r
184 public int GetHashCode(ItemStatus obj)
\r
186 return obj.Level + obj.Spec.GetHashCode() + obj.Ship.GetHashCode();
\r
190 private void CreateListLabels()
\r
192 panelShipList.SuspendLayout();
\r
193 for (var i = _labelList.Count; i < _shipList.Length; i++)
\r
195 CreateConfigComponents(i);
\r
196 CreateRepairLabels(i);
\r
197 CreateShipLabels(i);
\r
199 panelShipList.ResumeLayout();
\r
202 private void CreateConfigComponents(int i)
\r
204 var y = 3 + LineHeight * i;
\r
205 var cfgp = new Panel
\r
207 Location = new Point(0, y - 2),
\r
208 Size = new Size(PanelWidth, LineHeight - 1),
\r
209 BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2],
\r
212 cfgp.Scale(ShipLabel.ScaleFactor);
\r
213 cfgp.Tag = cfgp.Location.Y;
\r
218 Location = new Point(91, 2),
\r
219 Size = new Size(23, LabelHeight),
\r
220 TextAlign = ContentAlignment.MiddleRight,
\r
222 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
223 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
226 var cb = new CheckBox[GroupCount];
\r
227 for (var j = 0; j < cb.Length; j++)
\r
229 cb[j] = new CheckBox
\r
231 Location = new Point(125 + j * 24, 2),
\r
232 FlatStyle = FlatStyle.Flat,
\r
233 Size = new Size(12, 11),
\r
236 cb[j].Scale(ShipLabel.ScaleFactor);
\r
237 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
239 _configLabelList.Add(cfgl);
\r
240 _checkBoxesList.Add(cb);
\r
241 _checkBoxPanelList.Add(cfgp);
\r
242 // ReSharper disable CoVariantArrayConversion
\r
243 cfgp.Controls.AddRange(cfgl);
\r
244 cfgp.Controls.AddRange(cb);
\r
245 // ReSharper restore CoVariantArrayConversion
\r
246 panelShipList.Controls.Add(cfgp);
\r
247 foreach (var label in cfgl)
\r
249 label.Scale(ShipLabel.ScaleFactor);
\r
250 label.PresetColor =
\r
251 label.BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2];
\r
255 private void CreateRepairLabels(int i)
\r
257 var y = 3 + LineHeight * i;
\r
258 const int height = LabelHeight;
\r
259 var rpp = new Panel
\r
261 Location = new Point(0, y - 2),
\r
262 Size = new Size(PanelWidth, LineHeight - 1),
\r
263 BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2],
\r
266 rpp.Scale(ShipLabel.ScaleFactor);
\r
267 rpp.Tag = rpp.Location.Y;
\r
270 new ShipLabel {Location = new Point(118, 2), AutoSize = true, AnchorRight = true},
\r
273 Location = new Point(117, 2),
\r
274 Size = new Size(23, height),
\r
275 TextAlign = ContentAlignment.MiddleRight
\r
277 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
278 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
279 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
280 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
282 _repairLabelList.Add(rpl);
\r
283 _repairPanelList.Add(rpp);
\r
284 // ReSharper disable once CoVariantArrayConversion
\r
285 rpp.Controls.AddRange(rpl);
\r
286 panelShipList.Controls.Add(rpp);
\r
287 foreach (var label in rpl)
\r
289 label.Scale(ShipLabel.ScaleFactor);
\r
290 label.PresetColor =
\r
291 label.BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2];
\r
295 private void CreateShipLabels(int i)
\r
297 var y = 3 + LineHeight * i;
\r
298 const int height = LabelHeight;
\r
299 var lbp = new Panel
\r
301 Location = new Point(0, y - 2),
\r
302 Size = new Size(PanelWidth, LineHeight - 1),
\r
303 BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2],
\r
306 lbp.Scale(ShipLabel.ScaleFactor);
\r
307 lbp.Tag = lbp.Location.Y;
\r
310 new ShipLabel {Location = new Point(126, 2), AutoSize = true, AnchorRight = true},
\r
313 Location = new Point(129, 2),
\r
314 Size = new Size(23, height),
\r
315 TextAlign = ContentAlignment.MiddleRight
\r
319 Location = new Point(155, 2),
\r
320 Size = new Size(23, height),
\r
321 TextAlign = ContentAlignment.MiddleRight
\r
325 Location = new Point(176, 2),
\r
326 Size = new Size(41, height),
\r
327 TextAlign = ContentAlignment.MiddleRight
\r
329 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
330 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
332 _labelList.Add(labels);
\r
333 _labelPanelList.Add(lbp);
\r
334 // ReSharper disable once CoVariantArrayConversion
\r
335 lbp.Controls.AddRange(labels);
\r
336 panelShipList.Controls.Add(lbp);
\r
337 foreach (var label in labels)
\r
339 label.Scale(ShipLabel.ScaleFactor);
\r
340 label.PresetColor =
\r
341 label.BackColor = ShipInfoLabels.ColumnColors[(i + 1) % 2];
\r
345 private void SetShipLabels()
\r
347 panelGroupHeader.Visible = InGroupConfig();
\r
348 panelRepairHeader.Visible = InRepairList();
\r
349 panelItemHeader.Visible = InItemList();
\r
350 treeViewItem.Visible = InItemList();
\r
351 panelShipList.SuspendLayout();
\r
352 for (var i = 0; i < _shipList.Length; i++)
\r
354 var lbp = _labelPanelList[i];
\r
355 if (lbp.Visible == false && !InItemList())
\r
357 lbp.Location = new Point(lbp.Left, (int)lbp.Tag + panelShipList.AutoScrollPosition.Y);
\r
358 lbp.Visible = true;
\r
360 var cbp = _checkBoxPanelList[i];
\r
361 if (cbp.Visible == false && InGroupConfig())
\r
363 cbp.Location = new Point(cbp.Left, (int)cbp.Tag + panelShipList.AutoScrollPosition.Y);
\r
365 var rpp = _repairPanelList[i];
\r
366 if (rpp.Visible == false && InRepairList())
\r
368 rpp.Location = new Point(rpp.Left, (int)rpp.Tag + panelShipList.AutoScrollPosition.Y);
\r
370 var s = _shipList[i];
\r
371 var labels = _labelList[i];
\r
372 if (s.Level == 1000)
\r
374 cbp.Visible = false;
\r
375 rpp.Visible = false;
\r
376 for (var c = 0; c < 6; c++)
\r
378 labels[c].Text = "";
\r
379 labels[c].BackColor = labels[c].PresetColor;
\r
381 labels[4].SetName("");
\r
382 labels[5].Text = s.Name;
\r
385 cbp.Visible = InGroupConfig();
\r
386 rpp.Visible = InRepairList();
\r
387 if (InGroupConfig())
\r
389 var cfgl = _configLabelList[i];
\r
390 cfgl[0].SetLevel(s);
\r
391 cfgl[1].SetName(s);
\r
392 cfgl[2].SetFleet(s);
\r
393 var cb = _checkBoxesList[i];
\r
394 for (var j = 0; j < cb.Length; j++)
\r
395 cb[j].Checked = _groupSettings[j].Contains(s.Id);
\r
397 else if (InRepairList())
\r
399 var rpl = _repairLabelList[i];
\r
401 rpl[1].SetLevel(s);
\r
402 rpl[2].SetRepairTime(s);
\r
403 rpl[3].Text = TimeSpan.FromSeconds(s.RepairSecPerHp).ToString(@"mm\:ss");
\r
405 rpl[5].SetFleet(s);
\r
409 labels[0].SetHp(s);
\r
410 labels[1].SetCond(s);
\r
411 labels[2].SetLevel(s);
\r
412 labels[3].SetExpToNext(s);
\r
414 labels[4].SetName(s);
\r
415 labels[5].SetFleet(s);
\r
417 for (var i = _shipList.Length; i < _labelPanelList.Count; i++)
\r
419 _labelPanelList[i].Visible = false;
\r
420 _checkBoxPanelList[i].Visible = false;
\r
421 _repairPanelList[i].Visible = false;
\r
423 panelShipList.ResumeLayout();
\r
426 private void HideShipLabels()
\r
428 panelShipList.SuspendLayout();
\r
429 for (var i = 0; i < _shipList.Length; i++)
\r
430 _labelPanelList[i].Visible = _checkBoxPanelList[i].Visible = _repairPanelList[i].Visible = false;
\r
431 panelShipList.ResumeLayout();
\r
434 private void SetTreeViewItem()
\r
436 treeViewItem.BeginUpdate();
\r
437 var save = SaveTreeViewState(treeViewItem.Nodes);
\r
438 treeViewItem.Nodes.Clear();
\r
439 foreach (TreeNode child in _itemTreeNode.Nodes)
\r
440 treeViewItem.Nodes.Add(child);
\r
441 RestoreTreeViewState(treeViewItem.Nodes, save.Nodes);
\r
442 treeViewItem.EndUpdate();
\r
445 private TreeNode SaveTreeViewState(TreeNodeCollection nodes)
\r
447 var result = new TreeNode();
\r
448 foreach (TreeNode child in nodes)
\r
450 var copy = SaveTreeViewState(child.Nodes);
\r
451 if (child.IsExpanded)
\r
453 copy.Name = child.Name;
\r
454 result.Nodes.Add(copy);
\r
459 private void RestoreTreeViewState(TreeNodeCollection dst, TreeNodeCollection src)
\r
461 foreach (TreeNode d in dst)
\r
463 var s = src[d.Name];
\r
468 RestoreTreeViewState(d.Nodes, s.Nodes);
\r
472 private bool InGroupConfig()
\r
474 return comboBoxGroup.Text == "分類";
\r
477 private bool InRepairList()
\r
479 return comboBoxGroup.Text == "修復";
\r
482 private bool InItemList()
\r
484 return comboBoxGroup.Text == "装備";
\r
487 private void ShipListForm_Load(object sender, EventArgs e)
\r
489 panelShipList.Width = (int)Math.Round(PanelWidth * ShipLabel.ScaleFactor.Width) + 3 +
\r
490 SystemInformation.VerticalScrollBarWidth;
\r
491 Width = panelShipList.Width + 12 + (Width - ClientSize.Width);
\r
492 MinimumSize = new Size(Width, 0);
\r
493 MaximumSize = new Size(Width, int.MaxValue);
\r
494 var config = _config.ShipList;
\r
495 checkBoxShipType.Checked = config.ShipType;
\r
496 ActiveControl = panelShipList;
\r
497 for (var i = 0; i < GroupCount; i++)
\r
498 _groupSettings[i] = new HashSet<int>(config.ShipGroup[i]);
\r
499 comboBoxGroup.SelectedIndex = 0;
\r
500 if (config.Location.X == int.MinValue)
\r
502 var bounds = new Rectangle(config.Location, config.Size);
\r
503 if (MainForm.IsVisibleOnAnyScreen(bounds))
\r
504 Location = bounds.Location;
\r
505 Height = bounds.Height;
\r
508 private void ShipListForm_FormClosing(object sender, FormClosingEventArgs e)
\r
510 var config = _config.ShipList;
\r
511 var all = _sniffer.ShipList.Select(s => s.Id).ToArray();
\r
512 for (var i = 0; i < GroupCount; i++)
\r
514 if (_groupSettings[i] == null)
\r
516 if (all.Count() > 0)
\r
517 _groupSettings[i].IntersectWith(all);
\r
518 config.ShipGroup[i] = _groupSettings[i].ToList();
\r
523 var bounds = WindowState == FormWindowState.Normal ? Bounds : RestoreBounds;
\r
524 config.Location = bounds.Location;
\r
525 config.Size = bounds.Size;
\r
529 public void ShowShip(int id)
\r
531 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
534 var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * 16 * i);
\r
535 panelShipList.AutoScrollPosition = new Point(0, y);
\r
538 private void checkBoxShipType_CheckedChanged(object sender, EventArgs e)
\r
540 _config.ShipList.ShipType = checkBoxShipType.Checked;
\r
542 SetActiveControl();
\r
545 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
547 var cb = (CheckBox)sender;
\r
548 var group = (int)cb.Tag % 10;
\r
549 var idx = (int)cb.Tag / 10;
\r
551 _groupSettings[group].Add(_shipList[idx].Id);
\r
553 _groupSettings[group].Remove(_shipList[idx].Id);
\r
556 private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
\r
561 private void comboBoxGroup_DropDownClosed(object sender, EventArgs e)
\r
563 SetActiveControl();
\r
566 private void ShipListForm_KeyPress(object sender, KeyPressEventArgs e)
\r
568 var g = Array.FindIndex(new[] {'Z', 'A', 'B', 'C', 'D', 'G', 'R', 'W'}, x => x == char.ToUpper(e.KeyChar));
\r
571 comboBoxGroup.SelectedIndex = g;
\r
572 SetActiveControl();
\r
576 // マウスホイールでスクロールするためにコントロールにフォーカスを合わせる。
\r
577 private void SetActiveControl()
\r
579 ActiveControl = InItemList() ? (Control)treeViewItem : panelShipList;
\r