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 static System.Math;
\r
23 namespace KancolleSniffer
\r
25 public class ShipListPanel : Panel
\r
27 private const int LabelHeight = 12;
\r
28 public const int LineHeight = 16;
\r
29 private ShipStatus[] _shipList;
\r
30 private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();
\r
31 private readonly List<Panel> _labelPanelList = new List<Panel>();
\r
32 private readonly List<CheckBox[]> _checkBoxesList = new List<CheckBox[]>();
\r
33 private readonly List<ShipLabel[]> _groupingLabelList = new List<ShipLabel[]>();
\r
34 private readonly List<Panel> _groupingPanelList = new List<Panel>();
\r
35 private readonly List<ShipLabel[]> _repairLabelList = new List<ShipLabel[]>();
\r
36 private readonly List<Panel> _repairPanelList = new List<Panel>();
\r
37 private string _mode;
\r
39 public const int GroupCount = 4;
\r
40 public HashSet<int>[] GroupSettings { get; } = new HashSet<int>[GroupCount];
\r
42 public ScrollBar ScrollBar { get; }
\r
44 public ShipListPanel()
\r
46 ScrollBar = new VScrollBar {Dock = DockStyle.Right, Visible = false};
\r
47 ScrollBar.ValueChanged += ScrollBarOnValueChanged;
\r
48 Controls.Add(ScrollBar);
\r
51 private void ScrollBarOnValueChanged(object sender, EventArgs eventArgs)
\r
58 protected override void OnResize(EventArgs ev)
\r
61 if (_shipList == null || _shipList.Length == 0 || !Visible)
\r
69 protected override void OnMouseWheel(MouseEventArgs e)
\r
71 if (!ScrollBar.Visible)
\r
73 ScrollBar.Value = Max(ScrollBar.Minimum, Min(ScrollBar.Maximum - ScrollBar.LargeChange + 1,
\r
74 ScrollBar.Value - e.Delta * SystemInformation.MouseWheelScrollLines / 120));
\r
77 public void Update(Sniffer sniffer, string mode, ListForm.SortOrder sortOrder, bool byShipType)
\r
80 CreateShipList(sniffer, sortOrder, byShipType);
\r
87 [DllImport("user32.dll")]
\r
88 public static extern int SendMessage(IntPtr hWnd, int wMsg, bool wParam, IntPtr lParam);
\r
90 private void SuspendDrawing()
\r
92 SendMessage(Handle, 11, false, IntPtr.Zero); // WM_SETREDRAW = 11
\r
96 public void ResumeDrawing()
\r
99 SendMessage(Handle, 11, true, IntPtr.Zero);
\r
103 void CreateShipList(Sniffer sniffer, ListForm.SortOrder sortOrder, bool byShipType)
\r
105 var ships = _mode == "修復" ? sniffer.RepairList : FilterByGroup(sniffer.ShipList, _mode).ToArray();
\r
106 var order = _mode == "修復" ? ListForm.SortOrder.Repair : sortOrder;
\r
109 _shipList = ships.OrderBy(s => s, new CompareShip(false, order)).ToArray();
\r
112 var types = ships.Select(s => new {Id = s.Spec.ShipType, Name = s.Spec.ShipTypeName})
\r
117 Spec = new ShipSpec {Name = stype.Name, ShipType = stype.Id},
\r
122 _shipList = ships.Concat(types).OrderBy(s => s, new CompareShip(true, order)).ToArray();
\r
125 private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)
\r
127 var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);
\r
130 return from s in ships where GroupSettings[g].Contains(s.Id) select s;
\r
133 public IEnumerable<ShipStatus> CurrentShipList => _shipList.Where(ship => ship.Level != 1000);
\r
135 private class CompareShip : IComparer<ShipStatus>
\r
137 private readonly bool _shipType;
\r
138 private readonly ListForm.SortOrder _order;
\r
140 public CompareShip(bool type, ListForm.SortOrder order)
\r
146 public int Compare(ShipStatus a, ShipStatus b)
\r
148 if (a == null || b == null)
\r
149 throw new ArgumentNullException();
\r
152 if (a.Spec.ShipType != b.Spec.ShipType)
\r
153 return a.Spec.ShipType - b.Spec.ShipType;
\r
154 if (a.Level != b.Level)
\r
156 if (a.Level == 1000)
\r
158 if (b.Level == 1000)
\r
162 if (_order == ListForm.SortOrder.Repair && a.RepairTime != b.RepairTime)
\r
163 return (int)(b.RepairTime - a.RepairTime).TotalSeconds;
\r
164 if (a.Cond != b.Cond)
\r
166 if (_order == ListForm.SortOrder.CondAscend)
\r
167 return a.Cond - b.Cond;
\r
168 if (_order == ListForm.SortOrder.CondDescend)
\r
169 return b.Cond - a.Cond;
\r
171 if (a.Level != b.Level)
\r
173 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
174 return b.Level - a.Level;
\r
175 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
176 return a.Level - b.Level;
\r
177 if (!_shipType) // Condが同じかSortOrder.Noneで艦種なし
\r
178 return b.Level - a.Level;
\r
180 if (a.ExpToNext != b.ExpToNext)
\r
182 if (_order == ListForm.SortOrder.ExpToNextAscend)
\r
183 return a.ExpToNext - b.ExpToNext;
\r
184 if (_order == ListForm.SortOrder.ExpToNextDescend)
\r
185 return b.ExpToNext - a.ExpToNext;
\r
187 if (a.Spec.SortNo != b.Spec.SortNo)
\r
188 return a.Spec.SortNo - b.Spec.SortNo;
\r
189 return a.Id - b.Id;
\r
193 private void SetupLabels()
\r
195 for (var i = _labelList.Count; i * LineHeight < Height; i++)
\r
197 CreateGroupingComponents(i);
\r
198 CreateRepairLabels(i);
\r
199 CreateShipLabels(i);
\r
201 for (var i = 0; i * LineHeight < Height; i++)
\r
203 _labelPanelList[i].Visible = InShipStatus(_mode);
\r
204 _groupingPanelList[i].Visible = _mode == "分類";
\r
205 _repairPanelList[i].Visible = _mode == "修復";
\r
210 private void SetupScrollBar()
\r
212 var needBar = _shipList.Length * LineHeight * ShipLabel.ScaleFactor.Height > Height;
\r
215 ScrollBar.Visible = false;
\r
216 ScrollBar.Value = 0;
\r
219 ScrollBar.Visible = true;
\r
220 ScrollBar.Minimum = 0;
\r
221 var lines = Max(1, Height / (int)Round(LineHeight * ShipLabel.ScaleFactor.Height));
\r
222 var max = _shipList.Length - lines;
\r
223 ScrollBar.LargeChange = Min(lines, max);
\r
224 ScrollBar.Maximum =
\r
225 Max(0, max + ScrollBar.LargeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない
\r
226 ScrollBar.Value = Min(ScrollBar.Value, max);
\r
229 private void CreateGroupingComponents(int i)
\r
231 var y = 3 + LineHeight * i;
\r
232 var cfgp = new Panel
\r
234 Location = new Point(0, y - 2),
\r
235 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
236 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],
\r
238 cfgp.Scale(ShipLabel.ScaleFactor);
\r
239 cfgp.Tag = cfgp.Location.Y;
\r
244 Location = new Point(90, 2),
\r
245 Size = new Size(24, LabelHeight),
\r
246 TextAlign = ContentAlignment.MiddleRight
\r
248 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
249 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
252 var cb = new CheckBox[GroupCount];
\r
253 for (var j = 0; j < cb.Length; j++)
\r
255 cb[j] = new CheckBox
\r
257 Location = new Point(125 + j * 24, 2),
\r
258 FlatStyle = FlatStyle.Flat,
\r
259 Size = new Size(12, 11),
\r
262 cb[j].Scale(ShipLabel.ScaleFactor);
\r
263 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
265 _groupingLabelList.Add(cfgl);
\r
266 _checkBoxesList.Add(cb);
\r
267 _groupingPanelList.Add(cfgp);
\r
268 // ReSharper disable CoVariantArrayConversion
\r
269 cfgp.Controls.AddRange(cfgl);
\r
270 cfgp.Controls.AddRange(cb);
\r
271 // ReSharper restore CoVariantArrayConversion
\r
272 Controls.Add(cfgp);
\r
273 foreach (var label in cfgl)
\r
276 label.PresetColor =
\r
277 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
281 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
283 var cb = (CheckBox)sender;
\r
284 var group = (int)cb.Tag % 10;
\r
285 var idx = (int)cb.Tag / 10;
\r
288 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);
\r
292 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);
\r
296 private void CreateRepairLabels(int i)
\r
298 var y = 3 + LineHeight * i;
\r
299 const int height = LabelHeight;
\r
300 var rpp = new Panel
\r
302 Location = new Point(0, y - 2),
\r
303 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
304 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],
\r
306 rpp.Scale(ShipLabel.ScaleFactor);
\r
307 rpp.Tag = rpp.Location.Y;
\r
310 new ShipLabel {Location = new Point(118, 2), AutoSize = true, AnchorRight = true},
\r
313 Location = new Point(116, 2),
\r
314 Size = new Size(24, height),
\r
315 TextAlign = ContentAlignment.MiddleRight
\r
317 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
318 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
319 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
320 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
322 _repairLabelList.Add(rpl);
\r
323 _repairPanelList.Add(rpp);
\r
324 // ReSharper disable once CoVariantArrayConversion
\r
325 rpp.Controls.AddRange(rpl);
\r
327 foreach (var label in rpl)
\r
330 label.PresetColor =
\r
331 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
335 private void CreateShipLabels(int i)
\r
337 var y = 3 + LineHeight * i;
\r
338 const int height = LabelHeight;
\r
339 var lbp = new Panel
\r
341 Location = new Point(0, y - 2),
\r
342 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
343 BackColor = ShipLabels.ColumnColors[(i + 1) % 2]
\r
345 lbp.Scale(ShipLabel.ScaleFactor);
\r
348 new ShipLabel {Location = new Point(126, 2), AutoSize = true, AnchorRight = true},
\r
351 Location = new Point(128, 2),
\r
352 Size = new Size(24, height),
\r
353 TextAlign = ContentAlignment.MiddleRight
\r
357 Location = new Point(154, 2),
\r
358 Size = new Size(24, height),
\r
359 TextAlign = ContentAlignment.MiddleRight
\r
363 Location = new Point(175, 2),
\r
364 Size = new Size(42, height),
\r
365 TextAlign = ContentAlignment.MiddleRight
\r
367 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
368 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
370 _labelList.Add(labels);
\r
371 _labelPanelList.Add(lbp);
\r
372 // ReSharper disable once CoVariantArrayConversion
\r
373 lbp.Controls.AddRange(labels);
\r
375 foreach (var label in labels)
\r
378 label.PresetColor =
\r
379 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
383 private void SetShipLabels()
\r
385 for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)
\r
387 if (InShipStatus(_mode))
\r
396 private bool InShipStatus(string mode) => Array.Exists(new[] {"全員", "A", "B", "C", "D"}, x => mode == x);
\r
398 private void SetShipStatus(int i)
\r
400 var panel = _labelPanelList[i];
\r
401 if (i + ScrollBar.Value >= _shipList.Length)
\r
403 panel.Visible = false;
\r
406 var s = _shipList[i + ScrollBar.Value];
\r
407 var labels = _labelList[i];
\r
408 if (s.Level == 1000) // 艦種の表示
\r
413 labels[0].SetHp(s);
\r
414 labels[1].SetCond(s);
\r
415 labels[2].SetLevel(s);
\r
416 labels[3].SetExpToNext(s);
\r
417 labels[4].SetName(s, ShipNameWidth.ShipList);
\r
418 labels[5].SetFleet(s);
\r
419 panel.Visible = true;
\r
422 private void SetShipType(int i)
\r
424 var s = _shipList[i + ScrollBar.Value];
\r
425 var labels = _labelList[i];
\r
426 for (var c = 0; c < 4; c++)
\r
428 labels[c].Text = "";
\r
429 labels[c].BackColor = labels[c].PresetColor;
\r
431 labels[4].SetName("");
\r
432 labels[5].Text = s.Name;
\r
433 _labelPanelList[i].Visible = true;
\r
436 private void SetGrouping(int i)
\r
438 var panel = _groupingPanelList[i];
\r
439 if (i + ScrollBar.Value >= _shipList.Length)
\r
441 panel.Visible = false;
\r
442 _labelPanelList[i].Visible = false;
\r
445 var s = _shipList[i + ScrollBar.Value];
\r
446 var labels = _groupingLabelList[i];
\r
447 if (s.Level == 1000)
\r
449 panel.Visible = false;
\r
453 labels[0].SetLevel(s);
\r
454 labels[1].SetName(s, ShipNameWidth.GroupConfig);
\r
455 labels[2].SetFleet(s);
\r
456 var cb = _checkBoxesList[i];
\r
457 for (var j = 0; j < cb.Length; j++)
\r
458 cb[j].Checked = GroupSettings[j].Contains(s.Id);
\r
459 panel.Visible = true;
\r
462 private void SetRepairList(int i)
\r
464 var panel = _repairPanelList[i];
\r
465 if (i + ScrollBar.Value >= _shipList.Length)
\r
467 panel.Visible = false;
\r
468 _labelPanelList[i].Visible = false;
\r
471 var s = _shipList[i + ScrollBar.Value];
\r
472 if (s.Level == 1000)
\r
474 panel.Visible = false;
\r
478 var rpl = _repairLabelList[i];
\r
480 rpl[1].SetLevel(s);
\r
481 rpl[2].SetRepairTime(s);
\r
482 rpl[3].Text = s.RepairTimePerHp.ToString(@"mm\:ss");
\r
483 rpl[4].SetName(s, ShipNameWidth.RepairListFull);
\r
484 rpl[5].SetFleet(s);
\r
485 panel.Visible = true;
\r
488 public void ShowShip(int id)
\r
490 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
493 ScrollBar.Value = Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);
\r