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
245 if (!_shipType) // Condが同じかSortOrder.Noneで艦種なし
\r
246 return b.Level - a.Level;
\r
248 if (a.ExpToNext != b.ExpToNext)
\r
250 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
251 return a.ExpToNext - b.ExpToNext;
\r
252 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
253 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 return a.Id - b.Id;
\r
261 private void SetupLabels()
\r
263 for (var i = _labelList.Count; i * LineHeight < Height; i++)
\r
265 CreateGroupingComponents(i);
\r
266 CreateRepairLabels(i);
\r
267 CreateShipLabels(i);
\r
272 private void SetupScrollBar()
\r
274 var needBar = _shipList.Length * LineHeight * ShipLabel.ScaleFactor.Height > Height;
\r
277 ScrollBar.Visible = false;
\r
278 ScrollBar.Value = 0;
\r
281 ScrollBar.Visible = true;
\r
282 ScrollBar.Minimum = 0;
\r
283 var lines = Max(1, Height / (int)Round(LineHeight * ShipLabel.ScaleFactor.Height));
\r
284 var max = _shipList.Length - lines;
\r
285 var largeChange = Min(lines, max);
\r
286 ScrollBar.LargeChange = largeChange;
\r
287 ScrollBar.Maximum = Max(0, max + largeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない
\r
288 ScrollBar.Value = Min(ScrollBar.Value, max);
\r
291 private void CreateGroupingComponents(int i)
\r
293 var y = LineHeight * i + 1;
\r
294 var panel = new Panel
\r
296 Location = new Point(0, y),
\r
297 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
298 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
300 panel.Scale(ShipLabel.ScaleFactor);
\r
301 panel.Tag = panel.Location.Y;
\r
306 Location = new Point(90, 2),
\r
307 Size = new Size(24, LabelHeight),
\r
308 TextAlign = ContentAlignment.MiddleRight
\r
310 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
311 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
314 var cb = new CheckBox[GroupCount];
\r
315 for (var j = 0; j < cb.Length; j++)
\r
317 cb[j] = new CheckBox
\r
319 Location = new Point(125 + j * 24, 2),
\r
320 FlatStyle = FlatStyle.Flat,
\r
321 Size = new Size(12, 11),
\r
324 cb[j].Scale(ShipLabel.ScaleFactor);
\r
325 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
327 _groupingLabelList.Add(labels);
\r
328 _checkBoxesList.Add(cb);
\r
329 _groupingPanelList.Add(panel);
\r
330 // ReSharper disable CoVariantArrayConversion
\r
331 panel.Controls.AddRange(labels);
\r
332 panel.Controls.AddRange(cb);
\r
333 // ReSharper restore CoVariantArrayConversion
\r
334 Controls.Add(panel);
\r
335 var unused = panel.Handle; // create handle
\r
336 foreach (var label in labels)
\r
339 label.PresetColor =
\r
340 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
344 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
346 var cb = (CheckBox)sender;
\r
347 var group = (int)cb.Tag % 10;
\r
348 var idx = (int)cb.Tag / 10;
\r
351 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);
\r
355 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);
\r
357 GroupUpdated = true;
\r
360 private void CreateRepairLabels(int i)
\r
362 var y = LineHeight * i + 1;
\r
363 const int height = LabelHeight;
\r
364 var panel = new Panel
\r
366 Location = new Point(0, y),
\r
367 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
368 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
370 panel.Scale(ShipLabel.ScaleFactor);
\r
371 panel.Tag = panel.Location.Y;
\r
376 Location = new Point(118, 0),
\r
378 AnchorRight = true,
\r
379 MinimumSize = new Size(0, LineHeight),
\r
380 TextAlign = ContentAlignment.MiddleLeft,
\r
381 Cursor = Cursors.Hand
\r
385 Location = new Point(116, 2),
\r
386 Size = new Size(24, height),
\r
387 TextAlign = ContentAlignment.MiddleRight
\r
389 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
390 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
391 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
392 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
394 _repairLabelList.Add(labels);
\r
395 _repairPanelList.Add(panel);
\r
396 // ReSharper disable once CoVariantArrayConversion
\r
397 panel.Controls.AddRange(labels);
\r
398 Controls.Add(panel);
\r
399 var unused = panel.Handle; // create handle
\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 CreateShipLabels(int i)
\r
414 var y = LineHeight * i + 1;
\r
415 const int height = LabelHeight;
\r
416 var panel = new Panel
\r
418 Location = new Point(0, y),
\r
419 Size = new Size(ListForm.PanelWidth, LineHeight),
\r
420 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]
\r
422 panel.Scale(ShipLabel.ScaleFactor);
\r
427 Location = new Point(126, 0),
\r
429 AnchorRight = true,
\r
430 MinimumSize = new Size(0, LineHeight),
\r
431 TextAlign = ContentAlignment.MiddleLeft,
\r
432 Cursor = Cursors.Hand
\r
436 Location = new Point(128, 0),
\r
437 Size = new Size(24, LineHeight),
\r
438 TextAlign = ContentAlignment.MiddleRight
\r
442 Location = new Point(154, 2),
\r
443 Size = new Size(24, height),
\r
444 TextAlign = ContentAlignment.MiddleRight
\r
448 Location = new Point(175, 2),
\r
449 Size = new Size(42, height),
\r
450 TextAlign = ContentAlignment.MiddleRight
\r
452 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
453 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
455 _labelList.Add(labels);
\r
456 _labelPanelList.Add(panel);
\r
457 // ReSharper disable once CoVariantArrayConversion
\r
458 panel.Controls.AddRange(labels);
\r
459 Controls.Add(panel);
\r
460 var unused = panel.Handle; // create handle
\r
461 foreach (var label in labels)
\r
464 label.PresetColor =
\r
465 label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];
\r
468 labels[0].ToggleHpPercent();
\r
469 _hpLabels.Add(labels[0]);
\r
470 labels[0].DoubleClick += HpLabelClickHandler;
\r
473 private void SetShipLabels()
\r
475 for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)
\r
478 if (i + ScrollBar.Value >= _shipList.Length)
\r
480 if (InShipStatus(_mode))
\r
489 private void HidePanels(int i)
\r
491 _labelPanelList[i].Visible = _groupingPanelList[i].Visible = _repairPanelList[i].Visible = false;
\r
494 private bool InShipStatus(string mode) => Array.Exists(new[] {"全艦", "A", "B", "C", "D"}, x => mode == x);
\r
496 private void SetShipStatus(int i)
\r
498 var s = _shipList[i + ScrollBar.Value];
\r
499 var labels = _labelList[i];
\r
500 if (s.Level == 1000) // 艦種の表示
\r
505 labels[0].SetHp(s);
\r
506 labels[1].SetCond(s);
\r
507 labels[2].SetLevel(s);
\r
508 labels[3].SetExpToNext(s);
\r
509 labels[4].SetName(s, ShipNameWidth.ShipList);
\r
510 labels[5].SetFleet(s);
\r
511 _labelPanelList[i].Visible = true;
\r
514 private void SetShipType(int i)
\r
516 var s = _shipList[i + ScrollBar.Value];
\r
517 var labels = _labelList[i];
\r
518 labels[0].SetHp(null);
\r
519 labels[1].SetCond(null);
\r
520 labels[2].SetLevel(null);
\r
521 labels[3].SetExpToNext(null);
\r
522 labels[4].SetName(null);
\r
523 labels[5].SetFleet(null);
\r
524 labels[5].Text = s.Name;
\r
525 _labelPanelList[i].Visible = true;
\r
528 private void SetGrouping(int i)
\r
530 var s = _shipList[i + ScrollBar.Value];
\r
531 var labels = _groupingLabelList[i];
\r
532 if (s.Level == 1000)
\r
537 labels[0].SetLevel(s);
\r
538 labels[1].SetName(s, ShipNameWidth.GroupConfig);
\r
539 labels[2].SetFleet(s);
\r
540 var cb = _checkBoxesList[i];
\r
541 for (var j = 0; j < cb.Length; j++)
\r
542 cb[j].Checked = GroupSettings[j].Contains(s.Id);
\r
543 _groupingPanelList[i].Visible = true;
\r
546 private void SetRepairList(int i)
\r
548 var s = _shipList[i + ScrollBar.Value];
\r
549 if (s.Level == 1000)
\r
554 var labels = _repairLabelList[i];
\r
555 labels[0].SetHp(s);
\r
556 labels[1].SetLevel(s);
\r
557 labels[2].SetRepairTime(s);
\r
558 labels[3].Text = s.RepairTimePerHp.ToString(@"mm\:ss");
\r
559 labels[4].SetName(s, ShipNameWidth.RepairListFull);
\r
560 labels[5].SetFleet(s);
\r
561 _repairPanelList[i].Visible = true;
\r
564 public event Action HpLabelClick;
\r
566 private void HpLabelClickHandler(object sender, EventArgs ev)
\r
568 HpLabelClick?.Invoke();
\r
571 public void ToggleHpPercent()
\r
573 _hpPercent = !_hpPercent;
\r
574 foreach (var label in _hpLabels)
\r
575 label.ToggleHpPercent();
\r
578 public void ShowShip(int id)
\r
580 if (!ScrollBar.Visible)
\r
582 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
585 ScrollBar.Value = Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);
\r