OSDN Git Service

eefd85820a2635f3f007e80b9fad18e05b52b6f0
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / ShipListPanel / GroupConfigLabels.cs
1 // Copyright (C) 2019 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.Windows.Forms;\r
20 using KancolleSniffer.Model;\r
21 \r
22 // ReSharper disable CoVariantArrayConversion\r
23 \r
24 namespace KancolleSniffer.View.ShipListPanel\r
25 {\r
26     public class GroupConfigLabels\r
27     {\r
28         private readonly ShipListPanel _shipListPanel;\r
29         private readonly List<CheckBox[]> _checkBoxesList = new List<CheckBox[]>();\r
30         private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();\r
31         private readonly List<Panel> _panelList = new List<Panel>();\r
32 \r
33         public const int GroupCount = 4;\r
34         public HashSet<int>[] GroupSettings { get; } = new HashSet<int>[GroupCount];\r
35         public bool GroupUpdated { get; set; }\r
36 \r
37         public GroupConfigLabels(ShipListPanel shipListPanel)\r
38         {\r
39             _shipListPanel = shipListPanel;\r
40         }\r
41 \r
42         public void CreateComponents(int i)\r
43         {\r
44             var y = ShipListPanel.LineHeight * i + 1;\r
45             var panel = new Panel\r
46             {\r
47                 Location = new Point(0, y),\r
48                 Size = new Size(ListForm.PanelWidth, ShipListPanel.LineHeight),\r
49                 BackColor = ShipLabel.ColumnColors[(i + 1) % 2]\r
50             };\r
51             Scaler.Scale(panel);\r
52             panel.Tag = panel.Location.Y;\r
53             var labels = new[]\r
54             {\r
55                 new ShipLabel\r
56                 {\r
57                     Location = new Point(90, 2),\r
58                     Size = new Size(24, ShipListPanel.LabelHeight),\r
59                     TextAlign = ContentAlignment.MiddleRight\r
60                 },\r
61                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
62                 new ShipLabel {Location = new Point(1, 2), AutoSize = true}\r
63             };\r
64 \r
65             var cb = new CheckBox[GroupCount];\r
66             for (var j = 0; j < cb.Length; j++)\r
67             {\r
68                 cb[j] = new CheckBox\r
69                 {\r
70                     Location = new Point(125 + j * 24, 2),\r
71                     FlatStyle = FlatStyle.Flat,\r
72                     Size = new Size(12, 11),\r
73                     Tag = i * 10 + j\r
74                 };\r
75                 Scaler.Scale(cb[j]);\r
76                 cb[j].CheckedChanged += checkboxGroup_CheckedChanged;\r
77             }\r
78             _labelList.Add(labels);\r
79             _checkBoxesList.Add(cb);\r
80             _panelList.Add(panel);\r
81             panel.Controls.AddRange(labels);\r
82             panel.Controls.AddRange(cb);\r
83             _shipListPanel.Controls.Add(panel);\r
84             var unused = panel.Handle; // create handle\r
85             foreach (var label in labels)\r
86             {\r
87                 Scaler.Scale(label);\r
88                 label.PresetColor =\r
89                     label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];\r
90             }\r
91         }\r
92 \r
93         private void checkboxGroup_CheckedChanged(object sender, EventArgs e)\r
94         {\r
95             var cb = (CheckBox)sender;\r
96             var group = (int)cb.Tag % 10;\r
97             var idx = (int)cb.Tag / 10;\r
98             if (cb.Checked)\r
99             {\r
100                 GroupSettings[group].Add(_shipListPanel.GetShip(idx).Id);\r
101             }\r
102             else\r
103             {\r
104                 GroupSettings[group].Remove(_shipListPanel.GetShip(idx).Id);\r
105             }\r
106             GroupUpdated = true;\r
107         }\r
108 \r
109         public void SetGrouping(int i)\r
110         {\r
111             var s = _shipListPanel.GetShip(i);\r
112             var labels = _labelList[i];\r
113             if (s.Level == 1000)\r
114             {\r
115                 _shipListPanel.SetShipType(i);\r
116                 return;\r
117             }\r
118             labels[0].SetLevel(s);\r
119             labels[1].SetName(s, ShipNameWidth.GroupConfig);\r
120             labels[2].SetFleet(s);\r
121             var cb = _checkBoxesList[i];\r
122             for (var j = 0; j < cb.Length; j++)\r
123                 cb[j].Checked = GroupSettings[j].Contains(s.Id);\r
124             _panelList[i].Visible = true;\r
125         }\r
126 \r
127         public void HidePanel(int i)\r
128         {\r
129             _panelList[i].Visible = false;\r
130         }\r
131 \r
132         public IEnumerable<ShipStatus> FilterByGroup(IEnumerable<ShipStatus> ships, string group)\r
133         {\r
134             var g = Array.FindIndex(new[] {"A", "B", "C", "D"}, x => x == group);\r
135             if (g == -1)\r
136                 return ships;\r
137             return from s in ships where GroupSettings[g].Contains(s.Id) select s;\r
138         }\r
139     }\r
140 }