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, ShipListConfig config)
\r
84 CreateShipList(sniffer, config);
\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, ShipListConfig config)
\r
109 var ships = FilterByShipTypes(
\r
110 _mode == "修復" ? sniffer.RepairList : FilterByGroup(sniffer.ShipList, _mode),
\r
111 config.ShipCategories).ToArray();
\r
112 var order = _mode == "修復" ? ListForm.SortOrder.Repair : config.SortOrder;
\r
113 if (!config.ShipType)
\r
115 _shipList = ships.OrderBy(s => s, new CompareShip(false, order)).ToArray();
\r
118 _shipList = ships.Select(ship => new {Id = ship.Spec.ShipType, Name = ship.Spec.ShipTypeName})
\r
119 .Distinct().Select(type => new ShipStatus
\r
121 Spec = new ShipSpec { Name = type.Name, ShipType = type.Id},
\r
123 }).Concat(ships).OrderBy(ship => ship, new CompareShip(true, order)).ToArray();
\r
126 private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)
\r
128 var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);
\r
131 return from s in ships where GroupSettings[g].Contains(s.Id) select s;
\r
134 private static readonly int[][] ShipTypeIds =
\r
182 private static readonly int[] ShipTypeSortIds = CreateShipTypeSortIds();
\r
184 private static int[] CreateShipTypeSortIds()
\r
186 var ids = ShipTypeIds.SelectMany(x => x).ToArray();
\r
187 var res = new int[ids.Max() + 1];
\r
188 for (var i = 0; i < ids.Length; i++)
\r
193 private IEnumerable<ShipStatus> FilterByShipTypes(IEnumerable<ShipStatus> ships, ShipCategory shipTypes)
\r
195 var ids = Enumerable.Range(0, ShipTypeIds.Length)
\r
196 .Where(type => ((int)shipTypes & (1 << type)) != 0)
\r
197 .SelectMany(type => ShipTypeIds[type]).ToArray();
\r
198 return ships.Where(ship => ids.Contains(ship.Spec.ShipType));
\r
201 public IEnumerable<ShipStatus> CurrentShipList => _shipList.Where(ship => ship.Level != 1000);
\r
203 private class CompareShip : IComparer<ShipStatus>
\r
205 private readonly bool _shipType;
\r
206 private readonly ListForm.SortOrder _order;
\r
208 public CompareShip(bool type, ListForm.SortOrder order)
\r
214 public int Compare(ShipStatus a, ShipStatus b)
\r
216 if (a == null || b == null)
\r
217 throw new ArgumentNullException();
\r
220 if (a.Spec.ShipType != b.Spec.ShipType)
\r
221 return ShipTypeSortIds[a.Spec.ShipType] - ShipTypeSortIds[b.Spec.ShipType];
\r
222 if (a.Level != b.Level)
\r
224 if (a.Level == 1000)
\r
226 if (b.Level == 1000)
\r
230 if (_order == ListForm.SortOrder.Repair && a.RepairTime != b.RepairTime)
\r
231 return (int)(b.RepairTime - a.RepairTime).TotalSeconds;
\r
232 if (a.Cond != b.Cond)
\r
234 if (_order == ListForm.SortOrder.CondAscend)
\r
235 return a.Cond - b.Cond;
\r
236 if (_order == ListForm.SortOrder.CondDescend)
\r
237 return b.Cond - a.Cond;
\r
239 if (a.Level != b.Level)
\r
241 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
242 return b.Level - a.Level;
\r
243 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
244 return a.Level - b.Level;
\r
246 if (a.ExpToNext != b.ExpToNext)
\r
248 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
249 return a.ExpToNext - b.ExpToNext;
\r
250 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
251 return b.ExpToNext - a.ExpToNext;
\r
255 if (a.Spec.SortId != b.Spec.SortId)
\r
256 return a.Spec.SortId - b.Spec.SortId;
\r
257 if (a.Level != b.Level)
\r
258 return b.Level - a.Level;
\r
262 if (a.Level != b.Level)
\r
263 return b.Level - a.Level;
\r
264 if (a.Spec.SortId != b.Spec.SortId)
\r
265 return a.Spec.SortId - b.Spec.SortId;
\r
267 return a.Id - b.Id;
\r
271 private void SetupLabels()
\r
273 for (var i = _labelList.Count; i * LineHeight < Height; i++)
\r
275 CreateGroupingComponents(i);
\r
276 CreateRepairLabels(i);
\r
277 CreateShipLabels(i);
\r
282 private void SetupScrollBar()
\r
284 var needBar = _shipList.Length * LineHeight * ShipLabel.ScaleFactor.Height > Height;
\r
287 ScrollBar.Visible = false;
\r
288 ScrollBar.Value = 0;
\r
291 ScrollBar.Visible = true;
\r
292 ScrollBar.Minimum = 0;
\r
293 var lines = Max(1, Height / (int)Round(LineHeight * ShipLabel.ScaleFactor.Height));
\r
294 var max = _shipList.Length - lines;
\r
295 var largeChange = Min(lines, max);
\r
296 ScrollBar.LargeChange = largeChange;
\r
297 ScrollBar.Maximum = Max(0, max + largeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない
\r
298 ScrollBar.Value = Min(ScrollBar.Value, max);
\r
301 private void CreateGroupingComponents(int i)
\r
303 var y = LineHeight * i + 1;
\r
304 var panel = new Panel
\r
306 Location = new Point(0, y),
\r
307 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
308 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
310 panel.Scale(ShipLabel.ScaleFactor);
\r
311 panel.Tag = panel.Location.Y;
\r
316 Location = new Point(90, 2),
\r
317 Size = new Size(24, LabelHeight),
\r
318 TextAlign = ContentAlignment.MiddleRight
\r
320 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
321 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
324 var cb = new CheckBox[GroupCount];
\r
325 for (var j = 0; j < cb.Length; j++)
\r
327 cb[j] = new CheckBox
\r
329 Location = new Point(125 + j * 24, 2),
\r
330 FlatStyle = FlatStyle.Flat,
\r
331 Size = new Size(12, 11),
\r
334 cb[j].Scale(ShipLabel.ScaleFactor);
\r
335 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
337 _groupingLabelList.Add(labels);
\r
338 _checkBoxesList.Add(cb);
\r
339 _groupingPanelList.Add(panel);
\r
340 // ReSharper disable CoVariantArrayConversion
\r
341 panel.Controls.AddRange(labels);
\r
342 panel.Controls.AddRange(cb);
\r
343 // ReSharper restore CoVariantArrayConversion
\r
344 Controls.Add(panel);
\r
345 var unused = panel.Handle; // create handle
\r
346 foreach (var label in labels)
\r
349 label.PresetColor =
\r
350 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
354 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
356 var cb = (CheckBox)sender;
\r
357 var group = (int)cb.Tag % 10;
\r
358 var idx = (int)cb.Tag / 10;
\r
361 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);
\r
365 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);
\r
367 GroupUpdated = true;
\r
370 private void CreateRepairLabels(int i)
\r
372 var y = LineHeight * i + 1;
\r
373 const int height = LabelHeight;
\r
374 var panel = new Panel
\r
376 Location = new Point(0, y),
\r
377 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
378 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
380 panel.Scale(ShipLabel.ScaleFactor);
\r
381 panel.Tag = panel.Location.Y;
\r
386 Location = new Point(118, 0),
\r
388 AnchorRight = true,
\r
389 MinimumSize = new Size(0, LineHeight),
\r
390 TextAlign = ContentAlignment.MiddleLeft,
\r
391 Cursor = Cursors.Hand
\r
395 Location = new Point(116, 2),
\r
396 Size = new Size(24, height),
\r
397 TextAlign = ContentAlignment.MiddleRight
\r
399 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
400 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
401 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
402 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
404 _repairLabelList.Add(labels);
\r
405 _repairPanelList.Add(panel);
\r
406 // ReSharper disable once CoVariantArrayConversion
\r
407 panel.Controls.AddRange(labels);
\r
408 Controls.Add(panel);
\r
409 var unused = panel.Handle; // create handle
\r
410 foreach (var label in labels)
\r
413 label.PresetColor =
\r
414 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
417 labels[0].ToggleHpPercent();
\r
418 _hpLabels.Add(labels[0]);
\r
419 labels[0].DoubleClick += HpLabelClickHandler;
\r
422 private void CreateShipLabels(int i)
\r
424 var y = LineHeight * i + 1;
\r
425 const int height = LabelHeight;
\r
426 var panel = new Panel
\r
428 Location = new Point(0, y),
\r
429 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
430 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
432 panel.Scale(ShipLabel.ScaleFactor);
\r
437 Location = new Point(126, 0),
\r
439 AnchorRight = true,
\r
440 MinimumSize = new Size(0, LineHeight),
\r
441 TextAlign = ContentAlignment.MiddleLeft,
\r
442 Cursor = Cursors.Hand
\r
446 Location = new Point(128, 0),
\r
447 Size = new Size(24, LineHeight),
\r
448 TextAlign = ContentAlignment.MiddleRight
\r
452 Location = new Point(154, 2),
\r
453 Size = new Size(24, height),
\r
454 TextAlign = ContentAlignment.MiddleRight
\r
458 Location = new Point(175, 2),
\r
459 Size = new Size(42, height),
\r
460 TextAlign = ContentAlignment.MiddleRight
\r
462 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
463 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
465 _labelList.Add(labels);
\r
466 _labelPanelList.Add(panel);
\r
467 // ReSharper disable once CoVariantArrayConversion
\r
468 panel.Controls.AddRange(labels);
\r
469 Controls.Add(panel);
\r
470 var unused = panel.Handle; // create handle
\r
471 foreach (var label in labels)
\r
474 label.PresetColor =
\r
475 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
478 labels[0].ToggleHpPercent();
\r
479 _hpLabels.Add(labels[0]);
\r
480 labels[0].DoubleClick += HpLabelClickHandler;
\r
483 private void SetShipLabels()
\r
485 for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)
\r
488 if (i + ScrollBar.Value >= _shipList.Length)
\r
490 if (InShipStatus(_mode))
\r
499 private void HidePanels(int i)
\r
501 _labelPanelList[i].Visible = _groupingPanelList[i].Visible = _repairPanelList[i].Visible = false;
\r
504 private bool InShipStatus(string mode) => Array.Exists(new[] {"全艦", "A", "B", "C", "D"}, x => mode == x);
\r
506 private void SetShipStatus(int i)
\r
508 var s = _shipList[i + ScrollBar.Value];
\r
509 var labels = _labelList[i];
\r
510 if (s.Level == 1000) // 艦種の表示
\r
515 labels[0].SetHp(s);
\r
516 labels[1].SetCond(s);
\r
517 labels[2].SetLevel(s);
\r
518 labels[3].SetExpToNext(s);
\r
519 labels[4].SetName(s, ShipNameWidth.ShipList);
\r
520 labels[5].SetFleet(s);
\r
521 _labelPanelList[i].Visible = true;
\r
524 private void SetShipType(int i)
\r
526 var s = _shipList[i + ScrollBar.Value];
\r
527 var labels = _labelList[i];
\r
528 labels[0].SetHp(null);
\r
529 labels[1].SetCond(null);
\r
530 labels[2].SetLevel(null);
\r
531 labels[3].SetExpToNext(null);
\r
532 labels[4].SetName(null);
\r
533 labels[5].SetFleet(null);
\r
534 labels[5].Text = s.Name;
\r
535 _labelPanelList[i].Visible = true;
\r
538 private void SetGrouping(int i)
\r
540 var s = _shipList[i + ScrollBar.Value];
\r
541 var labels = _groupingLabelList[i];
\r
542 if (s.Level == 1000)
\r
547 labels[0].SetLevel(s);
\r
548 labels[1].SetName(s, ShipNameWidth.GroupConfig);
\r
549 labels[2].SetFleet(s);
\r
550 var cb = _checkBoxesList[i];
\r
551 for (var j = 0; j < cb.Length; j++)
\r
552 cb[j].Checked = GroupSettings[j].Contains(s.Id);
\r
553 _groupingPanelList[i].Visible = true;
\r
556 private void SetRepairList(int i)
\r
558 var s = _shipList[i + ScrollBar.Value];
\r
559 if (s.Level == 1000)
\r
564 var labels = _repairLabelList[i];
\r
565 labels[0].SetHp(s);
\r
566 labels[1].SetLevel(s);
\r
567 labels[2].SetRepairTime(s);
\r
568 labels[3].Text = s.RepairTimePerHp.ToString(@"mm\:ss");
\r
569 labels[4].SetName(s, ShipNameWidth.RepairListFull);
\r
570 labels[5].SetFleet(s);
\r
571 _repairPanelList[i].Visible = true;
\r
574 public event Action HpLabelClick;
\r
576 private void HpLabelClickHandler(object sender, EventArgs ev)
\r
578 HpLabelClick?.Invoke();
\r
581 public void ToggleHpPercent()
\r
583 _hpPercent = !_hpPercent;
\r
584 foreach (var label in _hpLabels)
\r
585 label.ToggleHpPercent();
\r
588 public void ShowShip(int id)
\r
590 if (!ScrollBar.Visible)
\r
592 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
595 ScrollBar.Value = Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);
\r