OSDN Git Service

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