1 // Copyright (C) 2016 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
17 using System.Drawing;
\r
19 using System.Runtime.InteropServices;
\r
20 using System.Windows.Forms;
\r
21 using KancolleSniffer.Model;
\r
22 using static System.Math;
\r
24 namespace KancolleSniffer.View
\r
26 public class ShipListPanel : Panel
\r
28 private const int LabelHeight = 12;
\r
29 public const int LineHeight = 16;
\r
30 private ShipStatus[] _shipList;
\r
31 private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();
\r
32 private readonly List<Panel> _labelPanelList = new List<Panel>();
\r
33 private readonly List<CheckBox[]> _checkBoxesList = new List<CheckBox[]>();
\r
34 private readonly List<ShipLabel[]> _groupingLabelList = new List<ShipLabel[]>();
\r
35 private readonly List<Panel> _groupingPanelList = new List<Panel>();
\r
36 private readonly List<ShipLabel[]> _repairLabelList = new List<ShipLabel[]>();
\r
37 private readonly List<Panel> _repairPanelList = new List<Panel>();
\r
38 private readonly List<ShipLabel> _hpLabels = new List<ShipLabel>();
\r
39 private string _mode;
\r
40 private bool _hpPercent;
\r
42 public const int GroupCount = 4;
\r
43 public HashSet<int>[] GroupSettings { get; } = new HashSet<int>[GroupCount];
\r
44 public bool GroupUpdated { get; set; }
\r
46 public ScrollBar ScrollBar { get; }
\r
48 public ShipListPanel()
\r
50 ScrollBar = new VScrollBar {Dock = DockStyle.Right, Visible = false};
\r
51 ScrollBar.ValueChanged += ScrollBarOnValueChanged;
\r
52 Controls.Add(ScrollBar);
\r
55 private void ScrollBarOnValueChanged(object sender, EventArgs eventArgs)
\r
62 protected override void OnResize(EventArgs ev)
\r
65 if (_shipList == null || _shipList.Length == 0 || !Visible)
\r
73 protected override void OnMouseWheel(MouseEventArgs e)
\r
75 if (!ScrollBar.Visible)
\r
77 ScrollBar.Value = Max(ScrollBar.Minimum, Min(ScrollBar.Maximum - ScrollBar.LargeChange + 1,
\r
78 ScrollBar.Value - e.Delta * SystemInformation.MouseWheelScrollLines / 120));
\r
81 public void Update(Sniffer sniffer, string mode, ListForm.SortOrder sortOrder, bool byShipType)
\r
84 CreateShipList(sniffer, sortOrder, byShipType);
\r
91 [DllImport("user32.dll")]
\r
92 public static extern int SendMessage(IntPtr hWnd, int wMsg, bool wParam, IntPtr lParam);
\r
94 private void SuspendDrawing()
\r
96 SendMessage(Handle, 11, false, IntPtr.Zero); // WM_SETREDRAW = 11
\r
100 public void ResumeDrawing()
\r
103 SendMessage(Handle, 11, true, IntPtr.Zero);
\r
107 private void CreateShipList(Sniffer sniffer, ListForm.SortOrder sortOrder, bool byShipType)
\r
109 var ships = _mode == "修復" ? sniffer.RepairList : FilterByGroup(sniffer.ShipList, _mode).ToArray();
\r
110 var order = _mode == "修復" ? ListForm.SortOrder.Repair : sortOrder;
\r
113 _shipList = ships.OrderBy(s => s, new CompareShip(false, order)).ToArray();
\r
116 var types = ships.Select(s => new {Id = s.Spec.ShipType, Name = s.Spec.ShipTypeName})
\r
121 Spec = new ShipSpec {Name = stype.Name, ShipType = stype.Id},
\r
126 _shipList = ships.Concat(types).OrderBy(s => s, new CompareShip(true, order)).ToArray();
\r
129 private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)
\r
131 var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);
\r
134 return from s in ships where GroupSettings[g].Contains(s.Id) select s;
\r
137 public IEnumerable<ShipStatus> CurrentShipList => _shipList.Where(ship => ship.Level != 1000);
\r
139 private class CompareShip : IComparer<ShipStatus>
\r
141 private readonly bool _shipType;
\r
142 private readonly ListForm.SortOrder _order;
\r
144 public CompareShip(bool type, ListForm.SortOrder order)
\r
150 public int Compare(ShipStatus a, ShipStatus b)
\r
152 if (a == null || b == null)
\r
153 throw new ArgumentNullException();
\r
156 if (a.Spec.ShipType != b.Spec.ShipType)
\r
157 return a.Spec.ShipType - b.Spec.ShipType;
\r
158 if (a.Level != b.Level)
\r
160 if (a.Level == 1000)
\r
162 if (b.Level == 1000)
\r
166 if (_order == ListForm.SortOrder.Repair && a.RepairTime != b.RepairTime)
\r
167 return (int)(b.RepairTime - a.RepairTime).TotalSeconds;
\r
168 if (a.Cond != b.Cond)
\r
170 if (_order == ListForm.SortOrder.CondAscend)
\r
171 return a.Cond - b.Cond;
\r
172 if (_order == ListForm.SortOrder.CondDescend)
\r
173 return b.Cond - a.Cond;
\r
175 if (a.Level != b.Level)
\r
177 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
178 return b.Level - a.Level;
\r
179 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
180 return a.Level - b.Level;
\r
181 if (!_shipType) // Condが同じかSortOrder.Noneで艦種なし
\r
182 return b.Level - a.Level;
\r
184 if (a.ExpToNext != b.ExpToNext)
\r
186 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
187 return a.ExpToNext - b.ExpToNext;
\r
188 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
189 return b.ExpToNext - a.ExpToNext;
\r
191 if (a.Spec.SortNo != b.Spec.SortNo)
\r
192 return a.Spec.SortNo - b.Spec.SortNo;
\r
193 return a.Id - b.Id;
\r
197 private void SetupLabels()
\r
199 for (var i = _labelList.Count; i * LineHeight < Height; i++)
\r
201 CreateGroupingComponents(i);
\r
202 CreateRepairLabels(i);
\r
203 CreateShipLabels(i);
\r
205 for (var i = 0; i * LineHeight < Height; i++)
\r
207 _labelPanelList[i].Visible = InShipStatus(_mode);
\r
208 _groupingPanelList[i].Visible = _mode == "分類";
\r
209 _repairPanelList[i].Visible = _mode == "修復";
\r
214 private void SetupScrollBar()
\r
216 var needBar = _shipList.Length * LineHeight * ShipLabel.ScaleFactor.Height > Height;
\r
219 ScrollBar.Visible = false;
\r
220 ScrollBar.Value = 0;
\r
223 ScrollBar.Visible = true;
\r
224 ScrollBar.Minimum = 0;
\r
225 var lines = Max(1, Height / (int)Round(LineHeight * ShipLabel.ScaleFactor.Height));
\r
226 var max = _shipList.Length - lines;
\r
227 var largeChange = Min(lines, max);
\r
228 ScrollBar.LargeChange = largeChange;
\r
229 ScrollBar.Maximum = Max(0, max + largeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない
\r
230 ScrollBar.Value = Min(ScrollBar.Value, max);
\r
233 private void CreateGroupingComponents(int i)
\r
235 var y = LineHeight * i + 1;
\r
236 var cfgp = new Panel
\r
238 Location = new Point(0, y),
\r
239 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
240 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
242 cfgp.Scale(ShipLabel.ScaleFactor);
\r
243 cfgp.Tag = cfgp.Location.Y;
\r
248 Location = new Point(90, 2),
\r
249 Size = new Size(24, LabelHeight),
\r
250 TextAlign = ContentAlignment.MiddleRight
\r
252 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
253 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
256 var cb = new CheckBox[GroupCount];
\r
257 for (var j = 0; j < cb.Length; j++)
\r
259 cb[j] = new CheckBox
\r
261 Location = new Point(125 + j * 24, 2),
\r
262 FlatStyle = FlatStyle.Flat,
\r
263 Size = new Size(12, 11),
\r
266 cb[j].Scale(ShipLabel.ScaleFactor);
\r
267 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
269 _groupingLabelList.Add(cfgl);
\r
270 _checkBoxesList.Add(cb);
\r
271 _groupingPanelList.Add(cfgp);
\r
272 // ReSharper disable CoVariantArrayConversion
\r
273 cfgp.Controls.AddRange(cfgl);
\r
274 cfgp.Controls.AddRange(cb);
\r
275 // ReSharper restore CoVariantArrayConversion
\r
276 Controls.Add(cfgp);
\r
277 foreach (var label in cfgl)
\r
280 label.PresetColor =
\r
281 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
285 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
287 var cb = (CheckBox)sender;
\r
288 var group = (int)cb.Tag % 10;
\r
289 var idx = (int)cb.Tag / 10;
\r
292 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);
\r
296 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);
\r
298 GroupUpdated = true;
\r
301 private void CreateRepairLabels(int i)
\r
303 var y = LineHeight * i + 1;
\r
304 const int height = LabelHeight;
\r
305 var rpp = new Panel
\r
307 Location = new Point(0, y),
\r
308 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
309 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
311 rpp.Scale(ShipLabel.ScaleFactor);
\r
312 rpp.Tag = rpp.Location.Y;
\r
317 Location = new Point(118, 0),
\r
319 AnchorRight = true,
\r
320 MinimumSize = new Size(0, LineHeight),
\r
321 TextAlign = ContentAlignment.MiddleLeft,
\r
322 Cursor = Cursors.Hand
\r
326 Location = new Point(116, 2),
\r
327 Size = new Size(24, height),
\r
328 TextAlign = ContentAlignment.MiddleRight
\r
330 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
331 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
332 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
333 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
335 _repairLabelList.Add(rpl);
\r
336 _repairPanelList.Add(rpp);
\r
337 // ReSharper disable once CoVariantArrayConversion
\r
338 rpp.Controls.AddRange(rpl);
\r
340 foreach (var label in rpl)
\r
343 label.PresetColor =
\r
344 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
347 rpl[0].ToggleHpPercent();
\r
348 _hpLabels.Add(rpl[0]);
\r
349 rpl[0].DoubleClick += HpLabelClickHandler;
\r
352 private void CreateShipLabels(int i)
\r
354 var y = LineHeight * i + 1;
\r
355 const int height = LabelHeight;
\r
356 var lbp = new Panel
\r
358 Location = new Point(0, y),
\r
359 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
360 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
362 lbp.Scale(ShipLabel.ScaleFactor);
\r
367 Location = new Point(126, 0),
\r
369 AnchorRight = true,
\r
370 MinimumSize = new Size(0, LineHeight),
\r
371 TextAlign = ContentAlignment.MiddleLeft,
\r
372 Cursor = Cursors.Hand
\r
376 Location = new Point(128, 0),
\r
377 Size = new Size(24, LineHeight),
\r
378 TextAlign = ContentAlignment.MiddleRight
\r
382 Location = new Point(154, 2),
\r
383 Size = new Size(24, height),
\r
384 TextAlign = ContentAlignment.MiddleRight
\r
388 Location = new Point(175, 2),
\r
389 Size = new Size(42, height),
\r
390 TextAlign = ContentAlignment.MiddleRight
\r
392 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
393 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
395 _labelList.Add(labels);
\r
396 _labelPanelList.Add(lbp);
\r
397 // ReSharper disable once CoVariantArrayConversion
\r
398 lbp.Controls.AddRange(labels);
\r
400 foreach (var label in labels)
\r
403 label.PresetColor =
\r
404 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
407 labels[0].ToggleHpPercent();
\r
408 _hpLabels.Add(labels[0]);
\r
409 labels[0].DoubleClick += HpLabelClickHandler;
\r
412 private void SetShipLabels()
\r
414 for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)
\r
416 if (InShipStatus(_mode))
\r
425 private bool InShipStatus(string mode) => Array.Exists(new[] {"全艦", "A", "B", "C", "D"}, x => mode == x);
\r
427 private void SetShipStatus(int i)
\r
429 var panel = _labelPanelList[i];
\r
430 if (i + ScrollBar.Value >= _shipList.Length)
\r
432 panel.Visible = false;
\r
435 var s = _shipList[i + ScrollBar.Value];
\r
436 var labels = _labelList[i];
\r
437 if (s.Level == 1000) // 艦種の表示
\r
442 labels[0].SetHp(s);
\r
443 labels[1].SetCond(s);
\r
444 labels[2].SetLevel(s);
\r
445 labels[3].SetExpToNext(s);
\r
446 labels[4].SetName(s, ShipNameWidth.ShipList);
\r
447 labels[5].SetFleet(s);
\r
448 panel.Visible = true;
\r
451 private void SetShipType(int i)
\r
453 var s = _shipList[i + ScrollBar.Value];
\r
454 var labels = _labelList[i];
\r
455 labels[0].SetHp(null);
\r
456 labels[1].SetCond(null);
\r
457 labels[2].SetLevel(null);
\r
458 labels[3].SetExpToNext(null);
\r
459 labels[4].SetName(null);
\r
460 labels[5].SetFleet(null);
\r
461 labels[5].Text = s.Name;
\r
462 _labelPanelList[i].Visible = true;
\r
465 private void SetGrouping(int i)
\r
467 var panel = _groupingPanelList[i];
\r
468 if (i + ScrollBar.Value >= _shipList.Length)
\r
470 panel.Visible = false;
\r
471 _labelPanelList[i].Visible = false;
\r
474 var s = _shipList[i + ScrollBar.Value];
\r
475 var labels = _groupingLabelList[i];
\r
476 if (s.Level == 1000)
\r
478 panel.Visible = false;
\r
482 labels[0].SetLevel(s);
\r
483 labels[1].SetName(s, ShipNameWidth.GroupConfig);
\r
484 labels[2].SetFleet(s);
\r
485 var cb = _checkBoxesList[i];
\r
486 for (var j = 0; j < cb.Length; j++)
\r
487 cb[j].Checked = GroupSettings[j].Contains(s.Id);
\r
488 panel.Visible = true;
\r
491 private void SetRepairList(int i)
\r
493 var panel = _repairPanelList[i];
\r
494 if (i + ScrollBar.Value >= _shipList.Length)
\r
496 panel.Visible = false;
\r
497 _labelPanelList[i].Visible = false;
\r
500 var s = _shipList[i + ScrollBar.Value];
\r
501 if (s.Level == 1000)
\r
503 panel.Visible = false;
\r
507 var rpl = _repairLabelList[i];
\r
509 rpl[1].SetLevel(s);
\r
510 rpl[2].SetRepairTime(s);
\r
511 rpl[3].Text = s.RepairTimePerHp.ToString(@"mm\:ss");
\r
512 rpl[4].SetName(s, ShipNameWidth.RepairListFull);
\r
513 rpl[5].SetFleet(s);
\r
514 panel.Visible = true;
\r
517 public event Action HpLabelClick;
\r
519 private void HpLabelClickHandler(object sender, EventArgs ev)
\r
521 HpLabelClick?.Invoke();
\r
524 public void ToggleHpPercent()
\r
526 _hpPercent = !_hpPercent;
\r
527 foreach (var label in _hpLabels)
\r
528 label.ToggleHpPercent();
\r
531 public void ShowShip(int id)
\r
533 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
536 ScrollBar.Value = Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);
\r