OSDN Git Service

5ea150979122569f0c768267b1e77b71efb58762
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / FleetPanel.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;\r
17 using System.Collections.Generic;\r
18 using System.Drawing;\r
19 using System.Linq;\r
20 using System.Windows.Forms;\r
21 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class FleetPanel : Panel\r
25     {\r
26         private const int LineHeight = 14;\r
27         private const int LabelHeight = 12;\r
28         private Record[] _table;\r
29         private readonly List<FleetLabels> _labelList = new List<FleetLabels>();\r
30         private readonly List<Panel> _panelList = new List<Panel>();\r
31         private readonly ResizableToolTip _toolTip = new ResizableToolTip {ShowAlways = true, AutoPopDelay = 10000};\r
32 \r
33         private class Record\r
34         {\r
35             public string Fleet { get; set; }\r
36             public string Fleet2 { get; set; }\r
37             public string Ship { get; set; }\r
38             public string Ship2 { get; set; }\r
39             public int Id { get; set; }\r
40             public string Equip { get; set; }\r
41             public Color Color { get; set; }\r
42             public string Spec { get; set; }\r
43             public string Spec2 { get; set; }\r
44             public string AircraftSpec { get; set; }\r
45 \r
46             public Record()\r
47             {\r
48                 Fleet = Ship = Equip = AircraftSpec = "";\r
49                 Color = DefaultBackColor;\r
50             }\r
51         }\r
52 \r
53         public void Update(Sniffer sniffer)\r
54         {\r
55             CreateTable(sniffer);\r
56             SuspendLayout();\r
57             CreateLabels();\r
58             SetRecords();\r
59             ResumeLayout();\r
60         }\r
61 \r
62         private class Total\r
63         {\r
64             public int Drum;\r
65             public int DrumShips;\r
66             public int Level;\r
67             public int FirePower;\r
68             public int AntiSubmarine;\r
69             public int AntiAir;\r
70             public int LoS;\r
71             public int Fuel;\r
72             public int Bull;\r
73 \r
74             public void Add(ShipStatus s)\r
75             {\r
76                 var drum = s.Slot.Count(item => item.Spec.Name == "ドラム缶(輸送用)");\r
77                 DrumShips += drum != 0 ? 1 : 0;\r
78                 Drum += drum;\r
79                 Level += s.Level;\r
80                 FirePower += s.Firepower;\r
81                 AntiSubmarine += s.MissionAntiSubmarine;\r
82                 AntiAir += s.AntiAir;\r
83                 LoS += s.LoS;\r
84                 Fuel += s.EffectiveFuelMax;\r
85                 Bull += s.EffectiveBullMax;\r
86             }\r
87         }\r
88 \r
89         private void CreateTable(Sniffer sniffer)\r
90         {\r
91             var list = new List<Record>();\r
92             var fn = new[] {"第一", "第二", "第三", "第四"};\r
93             foreach (var fleet in sniffer.Fleets)\r
94             {\r
95                 var total = new Total();\r
96                 var ships = new List<Record>();\r
97                 foreach (var s in fleet.Ships)\r
98                 {\r
99                     var equips = new List<Record>();\r
100                     for (var i = 0; i < s.Slot.Length; i++)\r
101                     {\r
102                         var item = s.Slot[i];\r
103                         var onslot = s.OnSlot[i];\r
104                         var max = s.Spec.MaxEq[i];\r
105                         if (item.Id == -1)\r
106                             continue;\r
107                         var airspec = "";\r
108                         if (item.Spec.IsDiveBomber) // 爆撃\r
109                         {\r
110                             airspec = "航空戦 " +\r
111                                       (25 + (int)((item.Spec.Bomber + item.BomberLevelBonus) * Math.Sqrt(onslot)));\r
112                         }\r
113                         else if (item.Spec.IsTorpedoBomber)\r
114                         {\r
115                             var normal = 25 + item.Spec.Torpedo * Math.Sqrt(onslot);\r
116                             airspec = "航空戦 " + (int)(normal * 0.8) + "/" + (int)(normal * 1.5);\r
117                         }\r
118                         equips.Add(new Record\r
119                         {\r
120                             Equip = GenEquipString(item),\r
121                             Spec = item.Spec.IsAircraft ? $"+{item.Alv} {onslot}/{max}" : "",\r
122                             AircraftSpec = airspec,\r
123                             Color = item.Spec.Color\r
124                         });\r
125                     }\r
126                     if (s.SlotEx.Id > 0)\r
127                     {\r
128                         var item = s.SlotEx;\r
129                         equips.Add(new Record {Equip = GenEquipString(item), Color = item.Spec.Color});\r
130                     }\r
131                     total.Add(s);\r
132                     var fire = s.EffectiveFirepower;\r
133                     var subm = s.EffectiveAntiSubmarine;\r
134                     var torp = s.EffectiveTorpedo;\r
135                     var night = s.NightBattlePower;\r
136                     var oasa = s.CanOpeningAntiSubmarineAttack ? "*" : "";\r
137                     var ship = new Record\r
138                     {\r
139                         Ship = (s.Escaped ? "[避]" : "") + s.Name + " Lv" + s.Level,\r
140                         Ship2 = $"燃{s.EffectiveFuelMax} 弾{s.EffectiveBullMax}",\r
141                         Id = s.Id,\r
142                         Spec = HideIfZero("砲", fire) + HideIfZero(" 潜", subm) + oasa,\r
143                         Spec2 = (HideIfZero("雷", torp) + HideIfZero(" 夜", night)).TrimStart(' ')\r
144                     };\r
145                     if (ship.Spec == "")\r
146                     {\r
147                         ship.Spec = ship.Spec2;\r
148                         ship.Spec2 = "";\r
149                     }\r
150                     ships.Add(ship);\r
151                     ships.AddRange(equips);\r
152                 }\r
153                 var daihatsu = fleet.DaihatsuBonus;\r
154                 var tp = fleet.TransportPoint;\r
155                 if (sniffer.IsCombinedFleet && fleet.Number == 0)\r
156                     tp += sniffer.Fleets[1].TransportPoint;\r
157                 list.Add(new Record\r
158                 {\r
159                     Fleet = fn[fleet.Number] + HideIfZero(" Lv", total.Level) +\r
160                             HideIfZero(" ドラム缶", total.Drum) + HideIfZero("(", total.DrumShips, "隻)") +\r
161                             HideIfZero(" 大発", daihatsu * 100, "%"),\r
162                     Fleet2 = "計:" +\r
163                              "火" + CutOverFlow(total.FirePower) +\r
164                              " 空" + CutOverFlow(total.AntiAir) +\r
165                              " 潜" + CutOverFlow(total.AntiSubmarine) +\r
166                              " 索" + CutOverFlow(total.LoS) + "\r\n" +\r
167                              $"戦闘:燃{total.Fuel / 5}弾{total.Bull / 5} 支援:燃{total.Fuel / 2}弾{(int)(total.Bull * 0.8)}" +\r
168                              (sniffer.IsCombinedFleet && fleet.Number == 1 ? "" : $"\r\nTP:S{(int)tp} A{(int)(tp * 0.7)}")\r
169                 });\r
170                 list.AddRange(ships);\r
171             }\r
172             if (sniffer.BaseAirCorps != null)\r
173             {\r
174                 var name = new[] {"第一", "第二", "第三"};\r
175                 foreach (var baseInfo in sniffer.BaseAirCorps)\r
176                 {\r
177                     list.Add(new Record {Fleet = baseInfo.AreaName + " 基地航空隊"});\r
178                     var i = 0;\r
179                     foreach (var airCorps in baseInfo.AirCorps)\r
180                     {\r
181                         if (i >= name.Length)\r
182                             break;\r
183                         var corpsFp = airCorps.FighterPower;\r
184                         string spec;\r
185                         string spec2;\r
186                         if (airCorps.Action == 2)\r
187                         {\r
188                             spec = "制空:" + RangeString(corpsFp.Interception);\r
189                             spec2 = corpsFp.IsInterceptor ? "制空(出撃):" + RangeString(corpsFp.AirCombat) : "";\r
190                         }\r
191                         else\r
192                         {\r
193                             spec = "制空:" + RangeString(corpsFp.AirCombat);\r
194                             spec2 = corpsFp.IsInterceptor ? "制空(防空):" + RangeString(corpsFp.Interception) : "";\r
195                         }\r
196                         var cost = airCorps.CostForSortie;\r
197                         list.Add(new Record\r
198                         {\r
199                             Ship = name[i++] + " " + airCorps.ActionName,\r
200                             Ship2 = $"出撃コスト:燃{cost[0]}弾{cost[1]}",\r
201                             Spec = spec + " 距離:" + airCorps.Distance,\r
202                             Spec2 = spec2\r
203                         });\r
204                         list.AddRange(airCorps.Planes.Select(plane =>\r
205                         {\r
206                             var planeFp = plane.FighterPower;\r
207                             return new Record\r
208                             {\r
209                                 Equip = plane.State != 1 ? plane.StateName : GenEquipString(plane.Slot),\r
210                                 Spec = plane.State != 1 ? "" : $"+{plane.Slot.Alv} {plane.Count}/{plane.MaxCount}",\r
211                                 AircraftSpec =\r
212                                     $"距離:{plane.Slot.Spec.Distance} 制空:{RangeString(planeFp.AirCombat)}" +\r
213                                     (planeFp.IsInterceptor ? $" 防空:{RangeString(planeFp.Interception)}" : ""),\r
214                                 Color = plane.Slot.Spec.Color\r
215                             };\r
216                         }));\r
217                     }\r
218                 }\r
219             }\r
220             _table = list.ToArray();\r
221         }\r
222 \r
223         private string RangeString(int[] fp) => fp[0] == fp[1] ? fp[0].ToString() : $"{fp[0]}~{fp[1]}";\r
224 \r
225         private int CutOverFlow(int value) => value > 999 ? 999 : value;\r
226 \r
227         private string HideIfZero(string name, double value, string suffix = "")\r
228         {\r
229             return value > 0 ? name + value.ToString("f1") + suffix : "";\r
230         }\r
231 \r
232         private string HideIfZero(string name, int value, string suffix = "")\r
233         {\r
234             return value > 0 ? name + value + suffix : "";\r
235         }\r
236 \r
237         private string GenEquipString(ItemStatus item)\r
238         {\r
239             var name = item.Spec.Name;\r
240             var attr = item.Level == 0 ? "" : "★" + item.Level;\r
241             var proposed = new Size(int.MaxValue, int.MaxValue);\r
242             var maxWidth = item.Spec.IsAircraft ? 132 : 180;\r
243             var result = name + attr;\r
244             if (TextRenderer.MeasureText(result, Font, proposed).Width <= maxWidth)\r
245                 return result;\r
246             var truncated = "";\r
247             foreach (var ch in name)\r
248             {\r
249                 var tmp = truncated + ch;\r
250                 if (TextRenderer.MeasureText(tmp + attr, Font, proposed).Width > maxWidth)\r
251                     break;\r
252                 truncated = tmp;\r
253             }\r
254             return truncated + attr;\r
255         }\r
256 \r
257         private void CreateLabels()\r
258         {\r
259             for (var i = _labelList.Count; i < _table.Length; i++)\r
260                 CreateLabels(i);\r
261         }\r
262 \r
263         private class FleetLabels : IEnumerable<ShipLabel>\r
264         {\r
265             public ShipLabel Fleet { get; set; }\r
266             public ShipLabel Name { get; set; }\r
267             public ShipLabel Equip { get; set; }\r
268             public ShipLabel EquipColor { get; set; }\r
269             public ShipLabel Spec { get; set; }\r
270 \r
271             public IEnumerator<ShipLabel> GetEnumerator() =>\r
272                 ((IEnumerable<ShipLabel>)new[] {Fleet, Name, Equip, EquipColor, Spec}).GetEnumerator();\r
273 \r
274             IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();\r
275         }\r
276 \r
277         private void CreateLabels(int i)\r
278         {\r
279             var y = 1 + LineHeight * i;\r
280             var lbp = new Panel\r
281             {\r
282                 Location = new Point(0, y),\r
283                 Size = new Size(ListForm.PanelWidth, LineHeight),\r
284                 BackColor = ShipLabel.ColumnColors[(i + 1) % 2],\r
285                 Visible = false\r
286             };\r
287             lbp.Scale(ShipLabel.ScaleFactor);\r
288             lbp.Tag = lbp.Location.Y;\r
289             var labels = new FleetLabels\r
290             {\r
291                 Fleet = new ShipLabel {Location = new Point(1, 2), AutoSize = true},\r
292                 Name = new ShipLabel {Location = new Point(10, 2), AutoSize = true},\r
293                 Equip = new ShipLabel {Location = new Point(38, 2), AutoSize = true},\r
294                 EquipColor = new ShipLabel {Location = new Point(35, 2), Size = new Size(4, LabelHeight - 2)},\r
295                 Spec = new ShipLabel {Location = new Point(217, 2), AutoSize = true, AnchorRight = true}\r
296             };\r
297             _labelList.Add(labels);\r
298             _panelList.Add(lbp);\r
299             lbp.Controls.AddRange(labels.Cast<Control>().ToArray());\r
300             Controls.Add(lbp);\r
301             foreach (var label in labels)\r
302             {\r
303                 label.Scale();\r
304                 label.PresetColor =\r
305                     label.BackColor = ShipLabel.ColumnColors[(i + 1) % 2];\r
306             }\r
307         }\r
308 \r
309         private void SetRecords()\r
310         {\r
311             for (var i = 0; i < _table.Length; i++)\r
312                 SetRecord(i);\r
313             for (var i = _table.Length; i < _labelList.Count; i++)\r
314                 _panelList[i].Visible = false;\r
315         }\r
316 \r
317         private void SetRecord(int i)\r
318         {\r
319             var lbp = _panelList[i];\r
320             if (!lbp.Visible)\r
321                 lbp.Location = new Point(lbp.Left, (int)lbp.Tag + AutoScrollPosition.Y);\r
322             var e = _table[i];\r
323             var labels = _labelList[i];\r
324             labels.Fleet.Text = e.Fleet;\r
325             labels.Name.SetName(e.Ship);\r
326             if (e.Ship2 != "")\r
327                 _toolTip.SetToolTip(labels.Name, e.Ship2);\r
328             labels.Equip.Text = e.Equip;\r
329             labels.EquipColor.Visible = e.Equip != "";\r
330             labels.EquipColor.BackColor = e.Color;\r
331             labels.Spec.Text = e.Spec;\r
332             if (e.Fleet != "" && e.Fleet2 != "")\r
333                 _toolTip.SetToolTip(labels.Fleet, e.Fleet2);\r
334             _toolTip.SetToolTip(labels.Equip, e.AircraftSpec != "" ? e.AircraftSpec : "");\r
335             _toolTip.SetToolTip(labels.Spec, e.Spec2 != "" ? e.Spec2 : "");\r
336             lbp.Visible = true;\r
337         }\r
338 \r
339         public void ShowShip(int id)\r
340         {\r
341             var i = Array.FindIndex(_table, e => e.Id == id);\r
342             if (i == -1)\r
343                 return;\r
344             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
345             AutoScrollPosition = new Point(0, y);\r
346         }\r
347 \r
348         public void ShowFleet(string fn)\r
349         {\r
350             var i = Array.FindIndex(_table, e => e.Fleet.StartsWith(fn));\r
351             if (i == -1)\r
352                 return;\r
353             var y = (int)Math.Round(ShipLabel.ScaleFactor.Height * LineHeight * i);\r
354             AutoScrollPosition = new Point(0, y);\r
355         }\r
356 \r
357         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)\r
358         {\r
359             base.ScaleControl(factor, specified);\r
360             if (factor.Height > 1)\r
361                 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);\r
362         }\r
363     }\r
364 }