1 // Copyright (C) 2017 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.Collections.Generic;
\r
17 using System.Drawing;
\r
19 using System.Text.RegularExpressions;
\r
20 using System.Windows.Forms;
\r
21 using KancolleSniffer.Model;
\r
22 using static System.Math;
\r
24 namespace KancolleSniffer.View
\r
26 public class BattleResultPanel : Panel
\r
28 private const int LineHeight = 16;
\r
29 private readonly List<ShipLabel[]> _friendLabels = new List<ShipLabel[]>();
\r
30 private readonly List<ShipLabel[]> _enemyLabels = new List<ShipLabel[]>();
\r
31 private readonly List<Panel> _panelList = new List<Panel>();
\r
32 private readonly List<ShipLabel> _hpLabels = new List<ShipLabel>();
\r
33 private readonly ResizableToolTip _toolTip = new ResizableToolTip {ShowAlways = true};
\r
34 private readonly BattleInfo.BattleResult[] _result = new BattleInfo.BattleResult[2];
\r
35 private Label _phaseLabel, _rankLabel, _cellLabel;
\r
36 private BattleState _prevBattleState;
\r
37 private readonly BattleResultRank[] _rank = new BattleResultRank[2];
\r
38 private readonly InformationPanel _informationPanel;
\r
39 private CellInfo _cellInfo;
\r
41 public Spoiler Spoilers { get; set; }
\r
43 public BattleResultPanel()
\r
47 _informationPanel = new InformationPanel();
\r
48 Controls.Add(_informationPanel);
\r
52 public event Action HpLabelClick;
\r
54 private void HpLabelClickHandler(object sender, EventArgs ev)
\r
56 HpLabelClick?.Invoke();
\r
59 public void ToggleHpPercent()
\r
61 foreach (var label in _hpLabels)
\r
62 label.ToggleHpPercent();
\r
65 public void Update(Sniffer sniffer)
\r
67 var state = sniffer.Battle.BattleState;
\r
68 var prev = _prevBattleState;
\r
69 _prevBattleState = state;
\r
70 _cellInfo = sniffer.CellInfo;
\r
71 if (prev == BattleState.None && state != BattleState.None)
\r
72 _result[0] = _result[1] = null;
\r
73 if (prev != BattleState.None && state == BattleState.None &&
\r
74 !(_result[0] == null && _result[1] == null))
\r
78 UpdateCellInfo(_cellInfo);
\r
81 if (state != BattleState.Day && state != BattleState.Night)
\r
83 if ((Spoilers & Spoiler.BattleResult) != 0)
\r
85 ShowResult(sniffer.Battle.Result);
\r
86 ShowResultRank(sniffer.Battle.ResultRank);
\r
87 if (state == BattleState.Day)
\r
89 _result[0] = sniffer.Battle.Result;
\r
90 _rank[0] = sniffer.Battle.ResultRank;
\r
93 else if (state == BattleState.Night)
\r
95 _result[1] = sniffer.Battle.Result;
\r
96 _rank[1] = sniffer.Battle.ResultRank;
\r
99 _informationPanel.Visible = true;
\r
105 if (state == BattleState.Day)
\r
107 _result[0] = sniffer.Battle.Result;
\r
108 _rank[0] = sniffer.Battle.ResultRank;
\r
110 else if (state == BattleState.Night)
\r
112 _result[1] = sniffer.Battle.Result;
\r
113 _rank[1] = sniffer.Battle.ResultRank;
\r
116 _informationPanel.SetInformation(sniffer.Battle);
\r
119 private void PhaseLabelClick(object sender, EventArgs ev)
\r
121 switch (_phaseLabel.Text)
\r
124 if (_result[0] != null)
\r
126 ShowResult(_result[0]);
\r
127 ShowResultRank(_rank[0]);
\r
130 else if (_result[1] != null)
\r
132 ShowResult(_result[1]);
\r
133 ShowResultRank(_rank[1]);
\r
138 if (_result[1] != null)
\r
140 ShowResult(_result[1]);
\r
141 ShowResultRank(_rank[1]);
\r
146 if (_result[0] != null)
\r
148 ShowResult(_result[0]);
\r
149 ShowResultRank(_rank[0]);
\r
156 private void SetPhase(string phase)
\r
158 _phaseLabel.Text = phase;
\r
159 if (phase == "結果" || _result[0] != null && _result[1] != null)
\r
161 _phaseLabel.BorderStyle = BorderStyle.FixedSingle;
\r
162 _phaseLabel.Cursor = Cursors.Hand;
\r
166 _phaseLabel.BorderStyle = BorderStyle.None;
\r
167 _phaseLabel.Cursor = Cursors.Default;
\r
171 private Point _scrollPosition;
\r
173 private void ClearResult()
\r
175 _scrollPosition = AutoScrollPosition;
\r
176 foreach (var panel in _panelList)
\r
177 panel.Visible = false;
\r
178 _informationPanel.Visible = false;
\r
179 _rankLabel.Text = "";
\r
182 private void ShowResult(BattleInfo.BattleResult result)
\r
185 AutoScrollPosition = _scrollPosition;
\r
186 var friend = result.Friend;
\r
187 var enemy = result.Enemy;
\r
188 for (var i = 0; i < friend.Main.Length; i++)
\r
190 var labels = _friendLabels[i];
\r
191 var ship = friend.Main[i];
\r
192 labels[0].SetHp(ship);
\r
193 labels[1].SetName(ship, ShipNameWidth.BattleResult);
\r
194 _toolTip.SetToolTip(labels[1], GetEquipString(ship));
\r
196 if (friend.Guard.Length > 0)
\r
198 _friendLabels[friend.Main.Length][1].Text = "護衛";
\r
199 _friendLabels[friend.Main.Length][0].SetHp(null);
\r
200 for (var i = 0; i < friend.Guard.Length; i++)
\r
202 var labels = _friendLabels[friend.Main.Length + 1 + i];
\r
203 var ship = friend.Guard[i];
\r
204 labels[0].SetHp(ship);
\r
205 labels[1].SetName(ship, ShipNameWidth.BattleResult);
\r
206 _toolTip.SetToolTip(labels[1], GetEquipString(ship));
\r
209 var friendLines = friend.Main.Length + (friend.Guard.Length > 0 ? friend.Guard.Length + 1 : 0);
\r
210 for (var i = friendLines; i < _friendLabels.Count; i++)
\r
212 _friendLabels[i][0].SetHp(null);
\r
213 _friendLabels[i][1].SetName("");
\r
215 for (var i = 0; i < enemy.Main.Length; i++)
\r
217 var labels = _enemyLabels[i];
\r
218 var ship = enemy.Main[i];
\r
219 labels[0].SetHp(ship);
\r
220 labels[1].SetName(ShortenName(ship.Name));
\r
221 _toolTip.SetToolTip(labels[1], GetEquipString(ship));
\r
223 if (enemy.Guard.Length > 0)
\r
225 _enemyLabels[enemy.Main.Length][1].Text = "護衛";
\r
226 _enemyLabels[enemy.Main.Length][0].SetHp(null);
\r
227 for (var i = 0; i < enemy.Guard.Length; i++)
\r
229 var labels = _enemyLabels[enemy.Main.Length + 1 + i];
\r
230 var ship = enemy.Guard[i];
\r
231 labels[0].SetHp(ship);
\r
232 labels[1].SetName(ShortenName(ship.Name));
\r
233 _toolTip.SetToolTip(labels[1], GetEquipString(ship));
\r
236 var enemyLines = enemy.Main.Length + (enemy.Guard.Length > 0 ? enemy.Guard.Length + 1 : 0);
\r
237 for (var i = enemyLines; i < _enemyLabels.Count; i++)
\r
239 _enemyLabels[i][0].SetHp(null);
\r
240 _enemyLabels[i][1].SetName("");
\r
242 var lines = Max(friendLines, enemyLines);
\r
243 for (var i = 0; i < lines; i++)
\r
245 var panel = _panelList[i];
\r
248 panel.Location = new Point(AutoScrollPosition.X,
\r
249 (int)Round((int)panel.Tag * ShipLabel.ScaleFactor.Height) + AutoScrollPosition.Y);
\r
250 panel.Visible = true;
\r
252 for (var i = lines; i < _panelList.Count; i++)
\r
253 _panelList[i].Visible = false;
\r
254 ResumeLayout(); // スクロールバーの有無を決定する
\r
255 var panelWidth = Max(ClientSize.Width, // スクロールバーの有無を反映した横幅
\r
256 _enemyLabels[0][1].Location.X + _enemyLabels.Max(labels => labels[1].Size.Width) - 1); // 敵の名前の右端
\r
257 for (var i = 0; i < lines; i++)
\r
258 _panelList[i].Width = panelWidth;
\r
259 _informationPanel.Location = new Point(
\r
260 (int)Round(0 * ShipLabel.ScaleFactor.Width) + AutoScrollPosition.X,
\r
261 (int)Round(20 * ShipLabel.ScaleFactor.Height) + AutoScrollPosition.Y);
\r
262 _informationPanel.Visible = true;
\r
263 UpdateCellInfo(_cellInfo);
\r
266 private string GetEquipString(ShipStatus ship)
\r
269 (from i in Enumerable.Range(0, ship.Slot.Count)
\r
270 let item = ship.Slot[i]
\r
272 select item.Spec.Name + (item.Spec.IsAircraft && ship.OnSlot.Length > 0 && ship.Spec.MaxEq.Length > 0
\r
273 ? $"{ship.OnSlot[i]}/{ship.Spec.MaxEq[i]}"
\r
275 if (ship.SlotEx.Id > 0)
\r
276 result = result.Concat(new[] {ship.SlotEx.Spec.Name});
\r
277 return string.Join("\r\n", result);
\r
280 private string ShortenName(string name)
\r
282 return new Regex(@"\(elite\)|\(flagship\)").Replace(name,
\r
283 match => match.Value == "(elite)" ? "(e)" : "(f)");
\r
286 private void ShowResultRank(BattleResultRank rank)
\r
288 var result = new[] {"完全S", "勝利S", "勝利A", "勝利B", "敗北C", "敗北D", "敗北E"};
\r
289 _rankLabel.Text = result[(int)rank];
\r
292 public void UpdateCellInfo(CellInfo cellInfo)
\r
294 _cellLabel.Text = (Spoilers & Spoiler.NextCell) == 0 ? cellInfo.Current : cellInfo.Next;
\r
295 _cellLabel.Location = new Point(ClientSize.Width - _cellLabel.Width - 2, 4);
\r
298 private void CreateLabels()
\r
300 _phaseLabel = new Label
\r
302 Location = new Point(4, 4),
\r
303 Size = new Size(31, 14)
\r
305 _phaseLabel.Click += PhaseLabelClick;
\r
306 Controls.Add(_phaseLabel);
\r
307 _rankLabel = new Label
\r
309 Location = new Point(37, 4),
\r
310 Size = new Size(42, 12)
\r
312 Controls.Add(_rankLabel);
\r
313 _cellLabel = new Label
\r
315 Location = new Point(0, 4),
\r
318 Controls.Add(_cellLabel);
\r
319 for (var i = 0; i < 13; i++)
\r
321 var y = LineHeight * i + 38;
\r
322 var panel = new Panel
\r
324 Location = new Point(0, y),
\r
325 Size = new Size(0, LineHeight),
\r
326 BackColor = ShipLabel.ColumnColors[i % 2],
\r
330 _panelList.Add(panel);
\r
336 Location = new Point(101, 0),
\r
338 AnchorRight = true,
\r
339 MinimumSize = new Size(0, LineHeight),
\r
340 TextAlign = ContentAlignment.MiddleLeft,
\r
341 Cursor = Cursors.Hand
\r
343 new ShipLabel {Location = new Point(1, 2), AutoSize = true}
\r
345 _friendLabels.Add(friend);
\r
346 _hpLabels.Add(friend[0]);
\r
347 friend[0].Click += HpLabelClickHandler;
\r
352 Location = new Point(119, 0),
\r
354 MinimumSize = new Size(0, LineHeight),
\r
355 TextAlign = ContentAlignment.MiddleLeft
\r
357 new ShipLabel {Location = new Point(164, 2), AutoSize = true}
\r
359 _enemyLabels.Add(enemy);
\r
360 foreach (var label in friend.Concat(enemy))
\r
362 panel.Controls.Add(label);
\r
363 label.BackColor = label.PresetColor = ShipLabel.ColumnColors[i % 2];
\r
365 Controls.Add(panel);
\r
369 protected override void OnMouseWheel(MouseEventArgs e)
\r
371 if (VScroll && (ModifierKeys & Keys.Shift) == Keys.Shift)
\r
378 base.OnMouseWheel(e);
\r
381 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
\r
383 base.ScaleControl(factor, specified);
\r
384 if (factor.Height > 1)
\r
385 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);
\r
388 private class InformationPanel : Panel
\r
390 private readonly Label[] _formation;
\r
391 private readonly Label[] _fighterPower;
\r
392 private readonly ResizableToolTip _toolTip = new ResizableToolTip {ShowAlways = true};
\r
394 public InformationPanel()
\r
397 Size = new Size(206, 16);
\r
398 Controls.AddRange(_formation = new[]
\r
402 Location = new Point(47, 2),
\r
403 Size = new Size(29, 12)
\r
407 Location = new Point(75, 2),
\r
408 Size = new Size(29, 12)
\r
412 Location = new Point(1, 2),
\r
413 Size = new Size(48, 12),
\r
414 TextAlign = ContentAlignment.MiddleCenter
\r
417 Controls.AddRange(_fighterPower = new[]
\r
421 Location = new Point(162, 2),
\r
422 Size = new Size(23, 12),
\r
423 TextAlign = ContentAlignment.MiddleRight
\r
427 Location = new Point(183, 2),
\r
428 Size = new Size(23, 12),
\r
429 TextAlign = ContentAlignment.MiddleRight
\r
433 Location = new Point(110, 2),
\r
434 Size = new Size(53, 12)
\r
437 // ReSharper disable once VirtualMemberCallInConstructor
\r
438 BackColor = ShipLabel.ColumnColors[1];
\r
441 public void SetInformation(BattleInfo battleInfo)
\r
443 _formation[0].Text = FormationName(battleInfo.Formation[0]);
\r
444 _formation[1].Text = FormationName(battleInfo.Formation[1]);
\r
445 _formation[2].Text = new[] {"同航戦", "反航戦", "T字有利", "T字不利"}[battleInfo.Formation[2] - 1];
\r
447 if (battleInfo.AirControlLevel == -1)
\r
449 if (battleInfo.BattleState == BattleState.Night)
\r
451 foreach (var label in _fighterPower)
\r
452 label.Visible = false;
\r
455 var fp = battleInfo.FighterPower;
\r
456 _fighterPower[0].Text = fp[0].ToString("D");
\r
457 _toolTip.SetToolTip(_fighterPower[0], fp[0] == fp[1] ? "" : $"{fp[0]}~{fp[1]}");
\r
458 var efp = battleInfo.EnemyFighterPower;
\r
459 _fighterPower[1].Text = efp.AirCombat + efp.UnknownMark;
\r
460 _toolTip.SetToolTip(_fighterPower[1],
\r
461 efp.AirCombat == efp.Interception ? "" : "防空:" + efp.Interception + efp.UnknownMark);
\r
462 _fighterPower[2].Text =
\r
463 new[] {"", "制空均衡", "制空確保", "航空優勢", "航空劣勢", "制空喪失"}[battleInfo.AirControlLevel + 1];
\r
464 foreach (var label in _fighterPower)
\r
465 label.Visible = true;
\r
468 private string FormationName(int formation)
\r
497 protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
\r
499 base.ScaleControl(factor, specified);
\r
500 if (factor.Height > 1)
\r
501 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);
\r