OSDN Git Service

連合艦隊を組んだときに連合艦隊表示にならないをの直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / MainWindow / ShipInfoPanel.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Linq;\r
4 using System.Windows.Forms;\r
5 using KancolleSniffer.Model;\r
6 \r
7 namespace KancolleSniffer.View.MainWindow\r
8 {\r
9     public class ShipInfoPanel : PanelWithToolTip, IUpdateTimers\r
10     {\r
11         private readonly BattleInfoPanel _battleInfo = new BattleInfoPanel\r
12         {\r
13             Location = new Point(59, 116),\r
14             Size = new Size(157, 14),\r
15             Visible = false\r
16         };\r
17 \r
18         private readonly FighterPower _fighterPower;\r
19 \r
20         private readonly Label _presetAkashiTimer = new Label\r
21         {\r
22             Location = new Point(2, 3),\r
23             Size = new Size(32, 12),\r
24             BackColor = CustomColors.ColumnColors.Bright\r
25         };\r
26 \r
27         public LinkLabel Guide { get; } = new LinkLabel\r
28         {\r
29             AutoSize = true,\r
30             Font = new Font("MS UI Gothic", 9.75F, FontStyle.Regular, GraphicsUnit.Point, 128),\r
31             LinkArea = new LinkArea(0, 0),\r
32             Location = new Point(31, 51),\r
33             Text = "右クリックでメニューが出ます。"\r
34         };\r
35 \r
36         private readonly Label _lineOfSight = new Label\r
37         {\r
38             Location = new Point(85, 117),\r
39             Size = new Size(38, 12),\r
40             Text = "0.0",\r
41             TextAlign = ContentAlignment.MiddleRight\r
42         };\r
43 \r
44         private readonly Label _lineOfSightCaption = new Label\r
45         {\r
46             AutoSize = true,\r
47             Location = new Point(59, 117),\r
48             Text = "索敵"\r
49         };\r
50 \r
51         private readonly Label _condTimerCaption = new Label\r
52         {\r
53             Location = new Point(128, 117),\r
54             Size = new Size(60, 12)\r
55         };\r
56 \r
57         private readonly Label _condTimer = new Label\r
58         {\r
59             AutoSize = true,\r
60             Location = new Point(186, 117)\r
61         };\r
62 \r
63         private readonly ShipListPanels _shipPanels;\r
64 \r
65         private UpdateContext _context;\r
66 \r
67         public UpdateContext Context\r
68         {\r
69             get => _context;\r
70             set => _battleInfo.Context = _fighterPower.Context = _context = value;\r
71         }\r
72 \r
73         public int CurrentFleet { get; set; }\r
74 \r
75         public bool CombinedFleet { get; set; }\r
76 \r
77         public Action<int> ShowShipOnList { get; set; }\r
78 \r
79         public ShipInfoPanel()\r
80         {\r
81             Controls.AddRange(new Control[] {Guide, _presetAkashiTimer});\r
82             BorderStyle = BorderStyle.FixedSingle;\r
83             _shipPanels = new ShipListPanels(this, ShipClickHandler);\r
84             Controls.AddRange(new Control[]\r
85             {\r
86                 _battleInfo,\r
87                 _lineOfSight, _lineOfSightCaption, _condTimer, _condTimerCaption\r
88             });\r
89             _fighterPower = new FighterPower(this);\r
90         }\r
91 \r
92         private void ShipClickHandler(object sender, EventArgs e)\r
93         {\r
94             var idx = (int)((Control)sender).Tag;\r
95             var ship = (CombinedFleet\r
96                 ? Context.Sniffer.Fleets[0].Ships.Concat(Context.Sniffer.Fleets[1].Ships)\r
97                 : Context.Sniffer.Fleets[CurrentFleet].Ships).ElementAt(idx);\r
98             if (!ship.Empty)\r
99                 ShowShipOnList(ship.Id);\r
100         }\r
101 \r
102         public void ToggleHpPercent()\r
103         {\r
104             _shipPanels.ToggleHpPercent();\r
105         }\r
106 \r
107         public bool ShowHpInPercent => _shipPanels.ShowHpInPercent;\r
108 \r
109         private bool _inSortie;\r
110         private bool _prevCombined;\r
111 \r
112         public void ChangeCurrentFleet()\r
113         {\r
114             var inSortie = Context.Sniffer.InSortie;\r
115             if (_inSortie)\r
116             {\r
117                 _inSortie = inSortie != -1;\r
118                 return;\r
119             }\r
120             if (inSortie == -1)\r
121             {\r
122                 _inSortie = false;\r
123                 if (Context.Sniffer.IsCombinedFleet && !_prevCombined)\r
124                 {\r
125                     CombinedFleet = true;\r
126                     CurrentFleet = 0;\r
127                 }\r
128                 _prevCombined = Context.Sniffer.IsCombinedFleet;\r
129             }\r
130             else\r
131             {\r
132                 _inSortie = true;\r
133                 if (inSortie == 10)\r
134                 {\r
135                     CombinedFleet = true;\r
136                     CurrentFleet = 0;\r
137                 }\r
138                 else\r
139                 {\r
140                     CombinedFleet = false;\r
141                     CurrentFleet = inSortie;\r
142                 }\r
143             }\r
144         }\r
145 \r
146         public new void Update()\r
147         {\r
148             var ships = Context.Sniffer.Fleets[CurrentFleet].ActualShips;\r
149             _shipPanels.SetShipLabels(ships);\r
150             ShowCombinedFleet();\r
151             _presetAkashiTimer.Visible = Context.Config.UsePresetAkashi;\r
152             UpdateAkashiTimer();\r
153             _fighterPower.UpdateFighterPower();\r
154             UpdateLoS();\r
155             UpdateCondTimers();\r
156         }\r
157 \r
158         private void ShowCombinedFleet()\r
159         {\r
160             if (!Context.Sniffer.IsCombinedFleet)\r
161                 CombinedFleet = false;\r
162             if (CombinedFleet)\r
163             {\r
164                 _shipPanels.SetCombinedShipLabels(Context.Sniffer.Fleets[0].ActualShips,\r
165                     Context.Sniffer.Fleets[1].ActualShips);\r
166             }\r
167         }\r
168 \r
169         private void UpdateLoS()\r
170         {\r
171             var fleet = Context.Sniffer.Fleets[CurrentFleet];\r
172             _lineOfSight.Text = RoundDown(fleet.GetLineOfSights(1)).ToString("F1");\r
173             var text = $"係数2: {RoundDown(fleet.GetLineOfSights(2)):F1}\r\n" +\r
174                        $"係数3: {RoundDown(fleet.GetLineOfSights(3)):F1}\r\n" +\r
175                        $"係数4: {RoundDown(fleet.GetLineOfSights(4)):F1}";\r
176             ToolTip.SetToolTip(_lineOfSight, text);\r
177             ToolTip.SetToolTip(_lineOfSightCaption, text);\r
178         }\r
179 \r
180         private double RoundDown(double number)\r
181         {\r
182             return Math.Floor(number * 10) / 10.0;\r
183         }\r
184 \r
185         public void UpdateBattleInfo()\r
186         {\r
187             ResetBattleInfo();\r
188             if (Context.Sniffer.Battle.BattleState == BattleState.None)\r
189                 return;\r
190             _battleInfo.Update();\r
191             _fighterPower.UpdateBattleFighterPower();\r
192         }\r
193 \r
194         private void ResetBattleInfo()\r
195         {\r
196             _battleInfo.Reset();\r
197             _fighterPower.Reset();\r
198         }\r
199 \r
200         private void UpdateCondTimers()\r
201         {\r
202             DateTime timer;\r
203             if (CombinedFleet)\r
204             {\r
205                 var timer1 = Context.Sniffer.GetConditionTimer(0);\r
206                 var timer2 = Context.Sniffer.GetConditionTimer(1);\r
207                 timer = timer2 > timer1 ? timer2 : timer1;\r
208             }\r
209             else\r
210             {\r
211                 timer = Context.Sniffer.GetConditionTimer(CurrentFleet);\r
212             }\r
213             if (timer == DateTime.MinValue)\r
214             {\r
215                 _condTimerCaption.Text = "";\r
216                 _condTimer.Text = "";\r
217                 return;\r
218             }\r
219             var span = TimeSpan.FromSeconds(Math.Ceiling((timer - Context.GetStep().Now).TotalSeconds));\r
220             if (span >= TimeSpan.FromMinutes(9) && Context.Config.NotifyConditions.Contains(40))\r
221             {\r
222                 _condTimerCaption.Text = "cond40まで";\r
223                 _condTimer.Text = (span - TimeSpan.FromMinutes(9)).ToString(@"mm\:ss");\r
224                 _condTimer.ForeColor = DefaultForeColor;\r
225             }\r
226             else\r
227             {\r
228                 _condTimerCaption.Text = "cond49まで";\r
229                 _condTimer.Text = (span >= TimeSpan.Zero ? span : TimeSpan.Zero).ToString(@"mm\:ss");\r
230                 _condTimer.ForeColor = span <= TimeSpan.Zero ? CUDColors.Red : DefaultForeColor;\r
231             }\r
232         }\r
233 \r
234         public Label AkashiRepairTimer { get; set; }\r
235 \r
236         public void UpdateTimers()\r
237         {\r
238             UpdateCondTimers();\r
239             UpdateAkashiTimer();\r
240             UpdatePresetAkashiTimer();\r
241         }\r
242 \r
243         private void UpdateAkashiTimer()\r
244         {\r
245             if (Context.Config.UsePresetAkashi)\r
246                 UpdatePresetAkashiTimer();\r
247             _shipPanels.SetAkashiTimer(Context.Sniffer.Fleets[CurrentFleet].ActualShips,\r
248                 Context.Sniffer.AkashiTimer.GetTimers(CurrentFleet, Context.GetStep().Now));\r
249         }\r
250 \r
251         private void UpdatePresetAkashiTimer()\r
252         {\r
253             var now = Context.GetStep().Now;\r
254             var akashi = Context.Sniffer.AkashiTimer;\r
255             var span = akashi.GetPresetDeckTimer(now);\r
256             var color = span == TimeSpan.Zero && akashi.CheckPresetRepairing() ? CUDColors.Red : DefaultForeColor;\r
257             var text = span == TimeSpan.MinValue ? "" : span.ToString(@"mm\:ss");\r
258             AkashiRepairTimer.ForeColor = color;\r
259             AkashiRepairTimer.Text = text;\r
260             if (akashi.CheckPresetRepairing() && !akashi.CheckRepairing(CurrentFleet, now))\r
261             {\r
262                 _presetAkashiTimer.ForeColor = color;\r
263                 _presetAkashiTimer.Text = text;\r
264             }\r
265             else\r
266             {\r
267                 _presetAkashiTimer.ForeColor = DefaultForeColor;\r
268                 _presetAkashiTimer.Text = "";\r
269             }\r
270         }\r
271     }\r
272 }