OSDN Git Service

艦隊情報にツールチップでTPを表示する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / EquipPanel.cs
1 // Copyright (C) 2015 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.Windows.Forms;\r
19 \r
20 namespace KancolleSniffer\r
21 {\r
22     public class EquipPanel : Panel\r
23     {\r
24         private const int LineHeight = 14;\r
25         private const int LabelHeight = 12;\r
26         private EquipColumn[] _equipList;\r
27         private readonly List<ShipLabel[]> _labelList = new List<ShipLabel[]>();\r
28         private readonly List<Panel> _panelList = new List<Panel>();\r
29         private readonly ToolTip _toolTip = new ToolTip {ShowAlways = true};\r
30 \r
31         private class EquipColumn\r
32         {\r
33             public string Fleet { get; set; }\r
34             public string Fleet2 { get; set; }\r
35             public string Ship { get; set; }\r
36             public int Id { get; set; }\r
37             public string Equip { get; set; }\r
38             public Color Color { get; set; }\r
39             public string Spec { get; set; }\r
40             public string Spec2 { get; set; }\r
41             public string AircraftSpec { get; set; }\r
42 \r
43             public EquipColumn()\r
44             {\r
45                 Fleet = Ship = Equip = AircraftSpec = "";\r
46                 Color = DefaultBackColor;\r
47             }\r
48         }\r
49 \r
50         public void UpdateEquip(Sniffer sniffer)\r
51         {\r
52             CreateEquipList(sniffer);\r
53             SuspendLayout();\r
54             CreateEquipLabels();\r
55             SetEquipLabels();\r
56             ResumeLayout();\r
57         }\r
58 \r
59         private void CreateEquipList(Sniffer sniffer)\r
60         {\r
61             var list = new List<EquipColumn>();\r
62             var fn = new[] {"第一艦隊", "第二艦隊", "第三艦隊", "第四艦隊"};\r
63             var tp = 0.0;\r
64             for (var f = 0; f < fn.Length; f++)\r
65             {\r
66                 var drumTotal = 0;\r
67                 var drumShips = 0;\r
68                 var levelTotal = 0;\r
69                 var ships = new List<EquipColumn>();\r
70                 foreach (var s in sniffer.GetShipStatuses(f))\r
71                 {\r
72                     var drum = 0;\r
73                     var equips = new List<EquipColumn>();\r
74                     for (var i = 0; i < s.Slot.Length; i++)\r
75                     {\r
76                         var item = s.Slot[i];\r
77                         var onslot = s.OnSlot[i];\r
78                         var max = s.Spec.MaxEq[i];\r
79                         if (item.Id == -1)\r
80                             continue;\r
81                         if (item.Spec.Name == "ドラム缶(輸送用)")\r
82                             drum++;\r
83                         var airspec = "";\r
84                         if (item.Spec.CanAirCombat)\r
85                         {\r
86                             if (item.Spec.Type == 7 || item.Spec.Type == 11) // 爆撃\r
87                             {\r
88                                 airspec = "航空戦 " + (25 + (int)(item.Spec.Bomber * Math.Sqrt(onslot)));\r
89                             }\r
90                             else if (item.Spec.Type == 8)\r
91                             {\r
92                                 var normal = 25 + item.Spec.Torpedo * Math.Sqrt(onslot);\r
93                                 airspec = "航空戦 " + (int)(normal * 0.8) + "/" + (int)(normal * 1.5);\r
94                             }\r
95                         }\r
96                         equips.Add(new EquipColumn\r
97                         {\r
98                             Equip = item.Spec.Name +\r
99                                     (item.Alv == 0 ? "" : "+" + item.Alv) +\r
100                                     (item.Level == 0 ? "" : "★" + item.Level) +\r
101                                     (!item.Spec.IsAircraft ? "" : " " + onslot + "/" + max),\r
102                             AircraftSpec = airspec,\r
103                             Color = item.Spec.Color\r
104                         });\r
105                     }\r
106                     if (s.SlotEx.Id > 0)\r
107                     {\r
108                         var item = s.SlotEx;\r
109                         equips.Add(new EquipColumn {Equip = item.Spec.Name, Color = item.Spec.Color});\r
110                     }\r
111                     if (drum != 0)\r
112                         drumShips++;\r
113                     drumTotal += drum;\r
114                     levelTotal += s.Level;\r
115                     if (f < 2)\r
116                         tp += s.TransportPoint;\r
117                     var fire = s.RealFirepower;\r
118                     var subm = s.RealAntiSubmarine;\r
119                     var torp = s.RealTorpedo;\r
120                     var night = s.NightBattlePower;\r
121                     var ship = new EquipColumn\r
122                     {\r
123                         Ship = (s.Escaped ? "[避]" : "") + s.Name + " Lv" + s.Level,\r
124                         Id = s.Id,\r
125                         // ReSharper disable CompareOfFloatsByEqualityOperator\r
126                         Spec = (fire == 0 ? "" : $"砲{fire:f1}") + (subm == 0 ? "" : $" 潜{subm:f1}"),\r
127                         Spec2 = (torp == 0 ? "" : $"雷{torp:f1}") + (night == 0 ? "" : $" 夜{night:f1}")\r
128                         // ReSharper restore CompareOfFloatsByEqualityOperator\r
129                     };\r
130                     if (ship.Spec == "")\r
131                     {\r
132                         ship.Spec = ship.Spec2;\r
133                         ship.Spec2 = "";\r
134                     }\r
135                     ships.Add(ship);\r
136                     ships.AddRange(equips);\r
137                 }\r
138                 list.Add(new EquipColumn\r
139                 {\r
140                     Fleet = fn[f] + (levelTotal == 0 ? "" : " 合計Lv" + levelTotal) +\r
141                             (drumTotal == 0 ? "" : " ドラム缶" + drumTotal + "(" + drumShips + "隻)")\r
142                 });\r
143                 list.AddRange(ships);\r
144             }\r
145             list[0].Fleet2 = $"TP: S{(int)tp} A{(int)(tp * 0.7)}";\r
146             _equipList = list.ToArray();\r
147         }\r
148 \r
149         private void CreateEquipLabels()\r
150         {\r
151             for (var i = _labelList.Count; i < _equipList.Length; i++)\r
152                 CreateEquipLabels(i);\r
153         }\r
154 \r
155         private void CreateEquipLabels(int i)\r
156         {\r
157             var y = 1 + LineHeight * i;\r
158             var lbp = new Panel\r
159             {\r
160                 Location = new Point(0, y),\r
161                 Size = new Size(ShipListForm.PanelWidth, LineHeight),\r
162                 BackColor = ShipLabels.ColumnColors[(i + 1) % 2],\r
163                 Visible = false\r
164             };\r
165             lbp.Scale(ShipLabel.ScaleFactor);\r
166             lbp.Tag = lbp.Location.Y;\r
167             var labels = new[]\r
168             {\r
169                 new ShipLabel {Location = new Point(1, 2), AutoSize = true},\r
170                 new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
171                 new ShipLabel {Location = new Point(40, 2), AutoSize = true},\r
172                 new ShipLabel {Location = new Point(37, 2), Size = new Size(4, LabelHeight - 2)},\r
173                 new ShipLabel {Location = new Point(217, 2), AutoSize = true, AnchorRight = true}\r
174             };\r
175             _labelList.Add(labels);\r
176             _panelList.Add(lbp);\r
177             // ReSharper disable once CoVariantArrayConversion\r
178             lbp.Controls.AddRange(labels);\r
179             Controls.Add(lbp);\r
180             foreach (var label in labels)\r
181             {\r
182                 label.Scale();\r
183                 label.PresetColor =\r
184                     label.BackColor = ShipLabels.ColumnColors[(i + 1) % 2];\r
185             }\r
186         }\r
187 \r
188         private void SetEquipLabels()\r
189         {\r
190             for (var i = 0; i < _equipList.Length; i++)\r
191                 SetEquip(i);\r
192             for (var i = _equipList.Length; i < _labelList.Count; i++)\r
193                 _panelList[i].Visible = false;\r
194         }\r
195 \r
196         private void SetEquip(int i)\r
197         {\r
198             var lbp = _panelList[i];\r
199             if (!lbp.Visible)\r
200                 lbp.Location = new Point(lbp.Left, (int)lbp.Tag + AutoScrollPosition.Y);\r
201             var e = _equipList[i];\r
202             var labels = _labelList[i];\r
203             labels[0].Text = e.Fleet;\r
204             labels[1].SetName(e.Ship);\r
205             labels[2].Text = e.Equip;\r
206             labels[3].Visible = e.Equip != "";\r
207             labels[3].BackColor = e.Color;\r
208             labels[4].Text = e.Spec;\r
209             if (e.Fleet != "" && e.Fleet2 != "")\r
210                 _toolTip.SetToolTip(labels[0], e.Fleet2);\r
211             _toolTip.SetToolTip(labels[2], e.AircraftSpec != "" ? e.AircraftSpec : "");\r
212             _toolTip.SetToolTip(labels[4], e.Spec2 != "" ? e.Spec2 : "");\r
213             lbp.Visible = true;\r
214         }\r
215 \r
216         public void ShowShip(int id)\r
217         {\r
218             var i = Array.FindIndex(_equipList, e => e.Id == id);\r
219             if (i == -1)\r
220                 return;\r
221             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
222             AutoScrollPosition = new Point(0, y);\r
223         }\r
224     }\r
225 }