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)
\r
69 protected override void OnMouseWheel(MouseEventArgs e)
\r
71 ScrollBar.Value = Max(ScrollBar.Minimum, Min(ScrollBar.Maximum - ScrollBar.LargeChange + 1,
\r
72 ScrollBar.Value - e.Delta * SystemInformation.MouseWheelScrollLines / 120));
\r
75 public void Update(Sniffer sniffer, string mode, ListForm.SortOrder sortOrder, bool byShipType)
\r
78 CreateShipList(sniffer, sortOrder, byShipType);
\r
85 [DllImport("user32.dll")]
\r
86 public static extern int SendMessage(IntPtr hWnd, int wMsg, bool wParam, IntPtr lParam);
\r
88 private void SuspendDrawing()
\r
90 SendMessage(Handle, 11, false, IntPtr.Zero); // WM_SETREDRAW = 11
\r
94 public void ResumeDrawing()
\r
97 SendMessage(Handle, 11, true, IntPtr.Zero);
\r
101 void CreateShipList(Sniffer sniffer, ListForm.SortOrder sortOrder, bool byShipType)
\r
103 var ships = _mode == "修復" ? sniffer.RepairList : FilterByGroup(sniffer.ShipList, _mode).ToArray();
\r
104 var order = _mode == "修復" ? ListForm.SortOrder.Repair : sortOrder;
\r
107 _shipList = ships.OrderBy(s => s, new CompareShip(false, order)).ToArray();
\r
110 var types = ships.Select(s => new {Id = s.Spec.ShipType, Name = s.Spec.ShipTypeName})
\r
115 Spec = new ShipSpec {Name = stype.Name, ShipType = stype.Id},
\r
120 _shipList = ships.Concat(types).OrderBy(s => s, new CompareShip(true, order)).ToArray();
\r
123 private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)
\r
125 var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);
\r
128 return from s in ships where GroupSettings[g].Contains(s.Id) select s;
\r
131 public IEnumerable<ShipStatus> CurrentShipList => _shipList.Where(ship => ship.Level != 1000);
\r
133 private class CompareShip : IComparer<ShipStatus>
\r
135 private readonly bool _shipType;
\r
136 private readonly ListForm.SortOrder _order;
\r
138 public CompareShip(bool type, ListForm.SortOrder order)
\r
144 public int Compare(ShipStatus a, ShipStatus b)
\r
146 if (a == null || b == null)
\r
147 throw new ArgumentNullException();
\r
148 if (_shipType && a.Spec.ShipType != b.Spec.ShipType)
\r
149 return a.Spec.ShipType - b.Spec.ShipType;
\r
152 case ListForm.SortOrder.None:
\r
153 case ListForm.SortOrder.ExpToNext:
\r
155 case ListForm.SortOrder.Cond:
\r
156 if (a.Cond != b.Cond)
\r
157 return a.Cond - b.Cond;
\r
159 case ListForm.SortOrder.Repair:
\r
160 if (a.RepairTime != b.RepairTime)
\r
161 return (int)(b.RepairTime - a.RepairTime).TotalSeconds;
\r
164 if ((!_shipType || _order == ListForm.SortOrder.ExpToNext) && a.Level != b.Level)
\r
165 return b.Level - a.Level;
\r
166 if (_order == ListForm.SortOrder.ExpToNext && a.ExpToNext != b.ExpToNext)
\r
167 return a.ExpToNext - b.ExpToNext;
\r
168 if (a.Spec.SortNo != b.Spec.SortNo)
\r
169 return a.Spec.SortNo - b.Spec.SortNo;
\r
170 return a.Id - b.Id;
\r
174 private void SetupLabels()
\r
176 for (var i = _labelList.Count; i * LineHeight < Height; i++)
\r
178 CreateGroupingComponents(i);
\r
179 CreateRepairLabels(i);
\r
180 CreateShipLabels(i);
\r
182 for (var i = 0; i * LineHeight < Height; i++)
\r
184 _labelPanelList[i].Visible = InShipStatus(_mode);
\r
185 _groupingPanelList[i].Visible = _mode == "分類";
\r
186 _repairPanelList[i].Visible = _mode == "修復";
\r
191 private void SetupScrollBar()
\r
193 var needBar = _shipList.Length * LineHeight * ShipLabel.ScaleFactor.Height > Height;
\r
196 ScrollBar.Visible = false;
\r
197 ScrollBar.Value = 0;
\r
200 ScrollBar.Visible = true;
\r
201 ScrollBar.Minimum = 0;
\r
202 var lines = Max(1, Height / (int)Round(LineHeight * ShipLabel.ScaleFactor.Height));
\r
203 var max = _shipList.Length - lines;
\r
204 ScrollBar.LargeChange = Min(lines, max);
\r
205 ScrollBar.Maximum =
\r
206 Max(0, max + ScrollBar.LargeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない
\r
207 ScrollBar.Value = Min(ScrollBar.Value, max);
\r
210 private void CreateGroupingComponents(int i)
\r
212 var y = 3 + LineHeight * i;
\r
213 var cfgp = new Panel
\r
215 Location = new Point(0, y - 2),
\r
216 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
217 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],
\r
219 cfgp.Scale(ShipLabel.ScaleFactor);
\r
220 cfgp.Tag = cfgp.Location.Y;
\r
225 Location = new Point(91, 2),
\r
226 Size = new Size(23, LabelHeight),
\r
227 TextAlign = ContentAlignment.MiddleRight
\r
229 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
230 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
233 var cb = new CheckBox[GroupCount];
\r
234 for (var j = 0; j < cb.Length; j++)
\r
236 cb[j] = new CheckBox
\r
238 Location = new Point(125 + j * 24, 2),
\r
239 FlatStyle = FlatStyle.Flat,
\r
240 Size = new Size(12, 11),
\r
243 cb[j].Scale(ShipLabel.ScaleFactor);
\r
244 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;
\r
246 _groupingLabelList.Add(cfgl);
\r
247 _checkBoxesList.Add(cb);
\r
248 _groupingPanelList.Add(cfgp);
\r
249 // ReSharper disable CoVariantArrayConversion
\r
250 cfgp.Controls.AddRange(cfgl);
\r
251 cfgp.Controls.AddRange(cb);
\r
252 // ReSharper restore CoVariantArrayConversion
\r
253 Controls.Add(cfgp);
\r
254 foreach (var label in cfgl)
\r
257 label.PresetColor =
\r
258 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
262 private void checkboxGroup_CheckedChanged(object sender, EventArgs e)
\r
264 var cb = (CheckBox)sender;
\r
265 var group = (int)cb.Tag % 10;
\r
266 var idx = (int)cb.Tag / 10;
\r
269 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);
\r
273 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);
\r
277 private void CreateRepairLabels(int i)
\r
279 var y = 3 + LineHeight * i;
\r
280 const int height = LabelHeight;
\r
281 var rpp = new Panel
\r
283 Location = new Point(0, y - 2),
\r
284 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
285 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],
\r
287 rpp.Scale(ShipLabel.ScaleFactor);
\r
288 rpp.Tag = rpp.Location.Y;
\r
291 new ShipLabel {Location = new Point(118, 2), AutoSize = true, AnchorRight = true},
\r
294 Location = new Point(117, 2),
\r
295 Size = new Size(23, height),
\r
296 TextAlign = ContentAlignment.MiddleRight
\r
298 new ShipLabel {Location = new Point(141, 2), AutoSize = true},
\r
299 new ShipLabel {Location = new Point(186, 2), AutoSize = true},
\r
300 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
301 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
303 _repairLabelList.Add(rpl);
\r
304 _repairPanelList.Add(rpp);
\r
305 // ReSharper disable once CoVariantArrayConversion
\r
306 rpp.Controls.AddRange(rpl);
\r
308 foreach (var label in rpl)
\r
311 label.PresetColor =
\r
312 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
316 private void CreateShipLabels(int i)
\r
318 var y = 3 + LineHeight * i;
\r
319 const int height = LabelHeight;
\r
320 var lbp = new Panel
\r
322 Location = new Point(0, y - 2),
\r
323 Size = new Size(ListForm.PanelWidth, LineHeight - 1),
\r
324 BackColor = ShipLabels.ColumnColors[(i + 1) % 2]
\r
326 lbp.Scale(ShipLabel.ScaleFactor);
\r
329 new ShipLabel {Location = new Point(126, 2), AutoSize = true, AnchorRight = true},
\r
332 Location = new Point(129, 2),
\r
333 Size = new Size(23, height),
\r
334 TextAlign = ContentAlignment.MiddleRight
\r
338 Location = new Point(155, 2),
\r
339 Size = new Size(23, height),
\r
340 TextAlign = ContentAlignment.MiddleRight
\r
344 Location = new Point(176, 2),
\r
345 Size = new Size(41, height),
\r
346 TextAlign = ContentAlignment.MiddleRight
\r
348 new ShipLabel {Location = new Point(10, 2), AutoSize = true},
\r
349 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
351 _labelList.Add(labels);
\r
352 _labelPanelList.Add(lbp);
\r
353 // ReSharper disable once CoVariantArrayConversion
\r
354 lbp.Controls.AddRange(labels);
\r
356 foreach (var label in labels)
\r
359 label.PresetColor =
\r
360 label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];
\r
364 private void SetShipLabels()
\r
366 for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)
\r
368 if (InShipStatus(_mode))
\r
377 private bool InShipStatus(string mode) => Array.Exists(new[] {"全員", "A", "B", "C", "D"}, x => mode == x);
\r
379 private void SetShipStatus(int i)
\r
381 var panel = _labelPanelList[i];
\r
382 if (i + ScrollBar.Value >= _shipList.Length)
\r
384 panel.Visible = false;
\r
387 var s = _shipList[i + ScrollBar.Value];
\r
388 var labels = _labelList[i];
\r
389 if (s.Level == 1000) // 艦種の表示
\r
394 labels[0].SetHp(s);
\r
395 labels[1].SetCond(s);
\r
396 labels[2].SetLevel(s);
\r
397 labels[3].SetExpToNext(s);
\r
398 labels[4].SetName(s, ShipNameWidth.ShipList);
\r
399 labels[5].SetFleet(s);
\r
400 panel.Visible = true;
\r
403 private void SetShipType(int i)
\r
405 var s = _shipList[i + ScrollBar.Value];
\r
406 var labels = _labelList[i];
\r
407 for (var c = 0; c < 4; c++)
\r
409 labels[c].Text = "";
\r
410 labels[c].BackColor = labels[c].PresetColor;
\r
412 labels[4].SetName("");
\r
413 labels[5].Text = s.Name;
\r
414 _labelPanelList[i].Visible = true;
\r
417 private void SetGrouping(int i)
\r
419 var panel = _groupingPanelList[i];
\r
420 if (i + ScrollBar.Value >= _shipList.Length)
\r
422 panel.Visible = false;
\r
423 _labelPanelList[i].Visible = false;
\r
426 var s = _shipList[i + ScrollBar.Value];
\r
427 var labels = _groupingLabelList[i];
\r
428 if (s.Level == 1000)
\r
430 panel.Visible = false;
\r
434 labels[0].SetLevel(s);
\r
435 labels[1].SetName(s, ShipNameWidth.GroupConfig);
\r
436 labels[2].SetFleet(s);
\r
437 var cb = _checkBoxesList[i];
\r
438 for (var j = 0; j < cb.Length; j++)
\r
439 cb[j].Checked = GroupSettings[j].Contains(s.Id);
\r
440 panel.Visible = true;
\r
443 private void SetRepairList(int i)
\r
445 var panel = _repairPanelList[i];
\r
446 if (i + ScrollBar.Value >= _shipList.Length)
\r
448 panel.Visible = false;
\r
449 _labelPanelList[i].Visible = false;
\r
452 var s = _shipList[i + ScrollBar.Value];
\r
453 if (s.Level == 1000)
\r
455 panel.Visible = false;
\r
459 var rpl = _repairLabelList[i];
\r
461 rpl[1].SetLevel(s);
\r
462 rpl[2].SetRepairTime(s);
\r
463 rpl[3].Text = TimeSpan.FromSeconds(s.RepairSecPerHp).ToString(@"mm\:ss");
\r
464 rpl[4].SetName(s, ShipNameWidth.RepairListFull);
\r
465 rpl[5].SetFleet(s);
\r
466 panel.Visible = true;
\r
469 public void ShowShip(int id)
\r
471 var i = Array.FindIndex(_shipList, s => s.Id == id);
\r
474 ScrollBar.Value = Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);
\r