OSDN Git Service

スクロールバーが大きく動くときにページ単位でスクロールさせる
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ShipListPanel.cs
1 // Copyright (C) 2016 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Drawing;\r
18 using System.Linq;\r
19 using System.Runtime.InteropServices;\r
20 using System.Windows.Forms;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class ShipListPanel : Panel\r
25     {\r
26         private const int LabelHeight = 12;\r
27         public const int LineHeight = 16;\r
28         private ShipStatus[] _shipList;\r
29         private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();\r
30         private readonly List<Panel> _labelPanelList = new List<Panel>();\r
31         private readonly List<CheckBox[]> _checkBoxesList = new List<CheckBox[]>();\r
32         private readonly List<ShipLabel[]> _groupingLabelList = new List<ShipLabel[]>();\r
33         private readonly List<Panel> _groupingPanelList = new List<Panel>();\r
34         private readonly List<ShipLabel[]> _repairLabelList = new List<ShipLabel[]>();\r
35         private readonly List<Panel> _repairPanelList = new List<Panel>();\r
36         private string _mode;\r
37 \r
38         public const int GroupCount = 4;\r
39         public HashSet<int>[] GroupSettings { get; } = new HashSet<int>[GroupCount];\r
40 \r
41         public ScrollBar ScrollBar { get; }\r
42 \r
43         public ShipListPanel()\r
44         {\r
45             ScrollBar = new VScrollBar {Dock = DockStyle.Right, Visible = false};\r
46             ScrollBar.ValueChanged += ScrollBarOnValueChanged;\r
47             Controls.Add(ScrollBar);\r
48         }\r
49 \r
50         private void ScrollBarOnValueChanged(object sender, EventArgs eventArgs)\r
51         {\r
52             SuspendDrawing();\r
53             SetShipLabels();\r
54             ResumeDrawing();\r
55         }\r
56 \r
57         protected override void OnResize(EventArgs ev)\r
58         {\r
59             base.OnResize(ev);\r
60             if (_shipList == null || _shipList.Length == 0)\r
61                 return;\r
62             SuspendDrawing();\r
63             SetupLabels();\r
64             SetShipLabels();\r
65             ResumeDrawing();\r
66         }\r
67 \r
68         protected override void OnMouseWheel(MouseEventArgs e)\r
69         {\r
70             ScrollBar.Value = Math.Max(ScrollBar.Minimum, Math.Min(ScrollBar.Maximum - ScrollBar.LargeChange + 1,\r
71                 ScrollBar.Value - e.Delta * SystemInformation.MouseWheelScrollLines / 120));\r
72         }\r
73 \r
74         public void Update(Sniffer sniffer, string mode, ListForm.SortOrder sortOrder, bool byShipType)\r
75         {\r
76             _mode = mode;\r
77             CreateShipList(sniffer, sortOrder, byShipType);\r
78             SuspendDrawing();\r
79             SetupLabels();\r
80             SetShipLabels();\r
81             ResumeDrawing();\r
82         }\r
83 \r
84         [DllImport("user32.dll")]\r
85         public static extern int SendMessage(IntPtr hWnd, int wMsg, bool wParam, IntPtr lParam);\r
86 \r
87         private void SuspendDrawing()\r
88         {\r
89             SendMessage(Handle, 11, false, IntPtr.Zero); // WM_SETREDRAW = 11\r
90             SuspendLayout();\r
91         }\r
92 \r
93         public void ResumeDrawing()\r
94         {\r
95             ResumeLayout();\r
96             SendMessage(Handle, 11, true, IntPtr.Zero);\r
97             Refresh();\r
98         }\r
99 \r
100         void CreateShipList(Sniffer sniffer, ListForm.SortOrder sortOrder, bool byShipType)\r
101         {\r
102             var ships = _mode == "修復" ? sniffer.RepairList : FilterByGroup(sniffer.ShipList, _mode).ToArray();\r
103             var order = _mode == "修復" ? ListForm.SortOrder.Repair : sortOrder;\r
104             if (!byShipType)\r
105             {\r
106                 _shipList = ships.OrderBy(s => s, new CompareShip(false, order)).ToArray();\r
107                 return;\r
108             }\r
109             var types = ships.Select(s => new {Id = s.Spec.ShipType, Name = s.Spec.ShipTypeName})\r
110                 .Distinct()\r
111                 .Select(stype =>\r
112                     new ShipStatus\r
113                     {\r
114                         Spec = new ShipSpec {Name = stype.Name, ShipType = stype.Id},\r
115                         Level = 1000,\r
116                         NowHp = -1000,\r
117                         Cond = -1000\r
118                     });\r
119             _shipList = ships.Concat(types).OrderBy(s => s, new CompareShip(true, order)).ToArray();\r
120         }\r
121 \r
122         private IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)\r
123         {\r
124             var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);\r
125             if (g == -1)\r
126                 return ships;\r
127             return from s in ships where GroupSettings[g].Contains(s.Id) select s;\r
128         }\r
129 \r
130         public IEnumerable<ShipStatus> CurrentShipList => _shipList.Where(ship => ship.Level != 1000);\r
131 \r
132         private class CompareShip : IComparer<ShipStatus>\r
133         {\r
134             private readonly bool _shipType;\r
135             private readonly ListForm.SortOrder _order;\r
136 \r
137             public CompareShip(bool type, ListForm.SortOrder order)\r
138             {\r
139                 _shipType = type;\r
140                 _order = order;\r
141             }\r
142 \r
143             public int Compare(ShipStatus a, ShipStatus b)\r
144             {\r
145                 if (a == null || b == null)\r
146                     throw new ArgumentNullException();\r
147                 if (_shipType && a.Spec.ShipType != b.Spec.ShipType)\r
148                     return a.Spec.ShipType - b.Spec.ShipType;\r
149                 switch (_order)\r
150                 {\r
151                     case ListForm.SortOrder.None:\r
152                     case ListForm.SortOrder.ExpToNext:\r
153                         break;\r
154                     case ListForm.SortOrder.Cond:\r
155                         if (a.Cond != b.Cond)\r
156                             return a.Cond - b.Cond;\r
157                         break;\r
158                     case ListForm.SortOrder.Repair:\r
159                         if (a.RepairTime != b.RepairTime)\r
160                             return (int)(b.RepairTime - a.RepairTime).TotalSeconds;\r
161                         break;\r
162                 }\r
163                 if ((!_shipType || _order == ListForm.SortOrder.ExpToNext) && a.Level != b.Level)\r
164                     return b.Level - a.Level;\r
165                 if (_order == ListForm.SortOrder.ExpToNext && a.ExpToNext != b.ExpToNext)\r
166                     return a.ExpToNext - b.ExpToNext;\r
167                 if (a.Spec.SortNo != b.Spec.SortNo)\r
168                     return a.Spec.SortNo - b.Spec.SortNo;\r
169                 return a.Id - b.Id;\r
170             }\r
171         }\r
172 \r
173         private void SetupLabels()\r
174         {\r
175             for (var i = _labelList.Count; i * LineHeight < Height; i++)\r
176             {\r
177                 CreateGroupingComponents(i);\r
178                 CreateRepairLabels(i);\r
179                 CreateShipLabels(i);\r
180             }\r
181             for (var i = 0; i * LineHeight < Height; i++)\r
182             {\r
183                 _labelPanelList[i].Visible = InShipStatus(_mode);\r
184                 _groupingPanelList[i].Visible = _mode == "分類";\r
185                 _repairPanelList[i].Visible = _mode == "修復";\r
186             }\r
187             SetupScrollBar();\r
188         }\r
189 \r
190         private void SetupScrollBar()\r
191         {\r
192             ScrollBar.Visible = _shipList.Length * LineHeight > Height;\r
193             if (!ScrollBar.Visible)\r
194             {\r
195                 ScrollBar.Value = 0;\r
196                 return;\r
197             }\r
198             ScrollBar.Minimum = 0;\r
199             var lines = Math.Max(1, Height / LineHeight);\r
200             var max = _shipList.Length - lines;\r
201             ScrollBar.LargeChange = Math.Min(lines, max);\r
202             ScrollBar.Maximum =\r
203                 Math.Max(0, max + ScrollBar.LargeChange - 1); // ScrollBarを最大まで動かしてもmaxには届かない\r
204             ScrollBar.Value = Math.Min(ScrollBar.Value, max);\r
205         }\r
206 \r
207         private void CreateGroupingComponents(int i)\r
208         {\r
209             var y = 3 + LineHeight * i;\r
210             var cfgp = new Panel\r
211             {\r
212                 Location = new Point(0, y - 2),\r
213                 Size = new Size(ListForm.PanelWidth, LineHeight - 1),\r
214                 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],\r
215             };\r
216             cfgp.Scale(ShipLabel.ScaleFactor);\r
217             cfgp.Tag = cfgp.Location.Y;\r
218             var cfgl = new[]\r
219             {\r
220                 new ShipLabel\r
221                 {\r
222                     Location = new Point(91, 2),\r
223                     Size = new Size(23, LabelHeight),\r
224                     TextAlign = ContentAlignment.MiddleRight\r
225                 },\r
226                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
227                 new ShipLabel {Location = new Point(1, 2), AutoSize = true}\r
228             };\r
229 \r
230             var cb = new CheckBox[GroupCount];\r
231             for (var j = 0; j < cb.Length; j++)\r
232             {\r
233                 cb[j] = new CheckBox\r
234                 {\r
235                     Location = new Point(125 + j * 24, 2),\r
236                     FlatStyle = FlatStyle.Flat,\r
237                     Size = new Size(12, 11),\r
238                     Tag = i * 10 + j\r
239                 };\r
240                 cb[j].Scale(ShipLabel.ScaleFactor);\r
241                 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;\r
242             }\r
243             _groupingLabelList.Add(cfgl);\r
244             _checkBoxesList.Add(cb);\r
245             _groupingPanelList.Add(cfgp);\r
246             // ReSharper disable CoVariantArrayConversion\r
247             cfgp.Controls.AddRange(cfgl);\r
248             cfgp.Controls.AddRange(cb);\r
249             // ReSharper restore CoVariantArrayConversion\r
250             Controls.Add(cfgp);\r
251             foreach (var label in cfgl)\r
252             {\r
253                 label.Scale();\r
254                 label.PresetColor =\r
255                     label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];\r
256             }\r
257         }\r
258 \r
259         private void checkboxGroup_CheckedChanged(object sender, EventArgs e)\r
260         {\r
261             var cb = (CheckBox)sender;\r
262             var group = (int)cb.Tag % 10;\r
263             var idx = (int)cb.Tag / 10;\r
264             if (cb.Checked)\r
265             {\r
266                 GroupSettings[group].Add(_shipList[idx + ScrollBar.Value].Id);\r
267             }\r
268             else\r
269             {\r
270                 GroupSettings[group].Remove(_shipList[idx + ScrollBar.Value].Id);\r
271             }\r
272         }\r
273 \r
274         private void CreateRepairLabels(int i)\r
275         {\r
276             var y = 3 + LineHeight * i;\r
277             const int height = LabelHeight;\r
278             var rpp = new Panel\r
279             {\r
280                 Location = new Point(0, y - 2),\r
281                 Size = new Size(ListForm.PanelWidth, LineHeight - 1),\r
282                 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],\r
283             };\r
284             rpp.Scale(ShipLabel.ScaleFactor);\r
285             rpp.Tag = rpp.Location.Y;\r
286             var rpl = new[]\r
287             {\r
288                 new ShipLabel {Location = new Point(118, 2), AutoSize = true, AnchorRight = true},\r
289                 new ShipLabel\r
290                 {\r
291                     Location = new Point(117, 2),\r
292                     Size = new Size(23, height),\r
293                     TextAlign = ContentAlignment.MiddleRight\r
294                 },\r
295                 new ShipLabel {Location = new Point(141, 2), AutoSize = true},\r
296                 new ShipLabel {Location = new Point(186, 2), AutoSize = true},\r
297                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
298                 new ShipLabel {Location = new Point(1, 2), AutoSize = true}\r
299             };\r
300             _repairLabelList.Add(rpl);\r
301             _repairPanelList.Add(rpp);\r
302             // ReSharper disable once CoVariantArrayConversion\r
303             rpp.Controls.AddRange(rpl);\r
304             Controls.Add(rpp);\r
305             foreach (var label in rpl)\r
306             {\r
307                 label.Scale();\r
308                 label.PresetColor =\r
309                     label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];\r
310             }\r
311         }\r
312 \r
313         private void CreateShipLabels(int i)\r
314         {\r
315             var y = 3 + LineHeight * i;\r
316             const int height = LabelHeight;\r
317             var lbp = new Panel\r
318             {\r
319                 Location = new Point(0, y - 2),\r
320                 Size = new Size(ListForm.PanelWidth, LineHeight - 1),\r
321                 BackColor = ShipLabels.ColumnColors[(i + 1) % 2]\r
322             };\r
323             lbp.Scale(ShipLabel.ScaleFactor);\r
324             var labels = new[]\r
325             {\r
326                 new ShipLabel {Location = new Point(126, 2), AutoSize = true, AnchorRight = true},\r
327                 new ShipLabel\r
328                 {\r
329                     Location = new Point(129, 2),\r
330                     Size = new Size(23, height),\r
331                     TextAlign = ContentAlignment.MiddleRight\r
332                 },\r
333                 new ShipLabel\r
334                 {\r
335                     Location = new Point(155, 2),\r
336                     Size = new Size(23, height),\r
337                     TextAlign = ContentAlignment.MiddleRight\r
338                 },\r
339                 new ShipLabel\r
340                 {\r
341                     Location = new Point(176, 2),\r
342                     Size = new Size(41, height),\r
343                     TextAlign = ContentAlignment.MiddleRight\r
344                 },\r
345                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
346                 new ShipLabel {Location = new Point(1, 2), AutoSize = true}\r
347             };\r
348             _labelList.Add(labels);\r
349             _labelPanelList.Add(lbp);\r
350             // ReSharper disable once CoVariantArrayConversion\r
351             lbp.Controls.AddRange(labels);\r
352             Controls.Add(lbp);\r
353             foreach (var label in labels)\r
354             {\r
355                 label.Scale();\r
356                 label.PresetColor =\r
357                     label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];\r
358             }\r
359         }\r
360 \r
361         private void SetShipLabels()\r
362         {\r
363             for (var i = 0; i < (Height + LineHeight - 1) / LineHeight; i++)\r
364             {\r
365                 if (InShipStatus(_mode))\r
366                     SetShipStatus(i);\r
367                 if (_mode == "分類")\r
368                     SetGrouping(i);\r
369                 if (_mode == "修復")\r
370                     SetRepairList(i);\r
371             }\r
372         }\r
373 \r
374         private bool InShipStatus(string mode) => Array.Exists(new[] {"全員", "A", "B", "C", "D"}, x => mode == x);\r
375 \r
376         private void SetShipStatus(int i)\r
377         {\r
378             var panel = _labelPanelList[i];\r
379             if (i + ScrollBar.Value >= _shipList.Length)\r
380             {\r
381                 panel.Visible = false;\r
382                 return;\r
383             }\r
384             var s = _shipList[i + ScrollBar.Value];\r
385             var labels = _labelList[i];\r
386             if (s.Level == 1000) // 艦種の表示\r
387             {\r
388                 SetShipType(i);\r
389                 return;\r
390             }\r
391             labels[0].SetHp(s);\r
392             labels[1].SetCond(s);\r
393             labels[2].SetLevel(s);\r
394             labels[3].SetExpToNext(s);\r
395             labels[4].SetName(s, ShipNameWidth.ShipList);\r
396             labels[5].SetFleet(s);\r
397             panel.Visible = true;\r
398         }\r
399 \r
400         private void SetShipType(int i)\r
401         {\r
402             var s = _shipList[i + ScrollBar.Value];\r
403             var labels = _labelList[i];\r
404             for (var c = 0; c < 4; c++)\r
405             {\r
406                 labels[c].Text = "";\r
407                 labels[c].BackColor = labels[c].PresetColor;\r
408             }\r
409             labels[4].SetName("");\r
410             labels[5].Text = s.Name;\r
411             _labelPanelList[i].Visible = true;\r
412         }\r
413 \r
414         private void SetGrouping(int i)\r
415         {\r
416             var panel = _groupingPanelList[i];\r
417             if (i + ScrollBar.Value >= _shipList.Length)\r
418             {\r
419                 panel.Visible = false;\r
420                 _labelPanelList[i].Visible = false;\r
421                 return;\r
422             }\r
423             var s = _shipList[i + ScrollBar.Value];\r
424             var labels = _groupingLabelList[i];\r
425             if (s.Level == 1000)\r
426             {\r
427                 panel.Visible = false;\r
428                 SetShipType(i);\r
429                 return;\r
430             }\r
431             labels[0].SetLevel(s);\r
432             labels[1].SetName(s, ShipNameWidth.GroupConfig);\r
433             labels[2].SetFleet(s);\r
434             var cb = _checkBoxesList[i];\r
435             for (var j = 0; j < cb.Length; j++)\r
436                 cb[j].Checked = GroupSettings[j].Contains(s.Id);\r
437             panel.Visible = true;\r
438         }\r
439 \r
440         private void SetRepairList(int i)\r
441         {\r
442             var panel = _repairPanelList[i];\r
443             if (i + ScrollBar.Value >= _shipList.Length)\r
444             {\r
445                 panel.Visible = false;\r
446                 _labelPanelList[i].Visible = false;\r
447                 return;\r
448             }\r
449             var s = _shipList[i + ScrollBar.Value];\r
450             if (s.Level == 1000)\r
451             {\r
452                 panel.Visible = false;\r
453                 SetShipType(i);\r
454                 return;\r
455             }\r
456             var rpl = _repairLabelList[i];\r
457             rpl[0].SetHp(s);\r
458             rpl[1].SetLevel(s);\r
459             rpl[2].SetRepairTime(s);\r
460             rpl[3].Text = TimeSpan.FromSeconds(s.RepairSecPerHp).ToString(@"mm\:ss");\r
461             rpl[4].SetName(s, ShipNameWidth.RepairListFull);\r
462             rpl[5].SetFleet(s);\r
463             panel.Visible = true;\r
464         }\r
465 \r
466         public void ShowShip(int id)\r
467         {\r
468             var i = Array.FindIndex(_shipList, s => s.Id == id);\r
469             if (i == -1)\r
470                 return;\r
471             ScrollBar.Value = Math.Min(i, ScrollBar.Maximum + 1 - ScrollBar.LargeChange);\r
472             SetShipLabels();\r
473         }\r
474     }\r
475 }