1 // Copyright (C) 2014, 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // This program is part of KancolleSniffer.
\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify
\r
6 // it under the terms of the GNU General Public License as published by
\r
7 // the Free Software Foundation, either version 3 of the License, or
\r
8 // (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.
\r
19 using System.Drawing;
\r
20 using System.Text.RegularExpressions;
\r
21 using System.Windows.Forms;
\r
22 using static System.Math;
\r
24 namespace KancolleSniffer
\r
26 public class ShipLabels
\r
28 private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];
\r
29 private readonly ShipLabel[] _akashiTimers = new ShipLabel[ShipInfo.MemberCount];
\r
30 private readonly ShipLabel[][] _damagedShipList = new ShipLabel[16][];
\r
31 private Control _panelDamagedShipList;
\r
32 private readonly ShipLabel[][] _ndockLabels = new ShipLabel[DockInfo.DockCount][];
\r
33 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
35 public void CreateLabels(Control parent, EventHandler onClick)
\r
37 parent.SuspendLayout();
\r
38 const int top = 3, height = 12, lh = 16;
\r
39 ShipLabel[] headings;
\r
40 parent.Controls.AddRange(headings = new[]
\r
42 new ShipLabel {Location = new Point(109, top), Text = "HP", AutoSize = true},
\r
43 new ShipLabel {Location = new Point(128, top), Text = "cond", AutoSize = true},
\r
44 new ShipLabel {Location = new Point(163, top), Text = "Lv", AutoSize = true},
\r
45 new ShipLabel {Location = new Point(194, top), Text = "Exp", AutoSize = true},
\r
46 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
48 foreach (var label in headings)
\r
51 label.BackColor = ColumnColors[1];
\r
53 for (var i = 0; i < _labels.Length; i++)
\r
55 var y = top + lh * (i + 1);
\r
56 parent.Controls.AddRange(_labels[i] = new[]
\r
58 new ShipLabel {Location = new Point(129, y), AutoSize = true, AnchorRight = true},
\r
61 Location = new Point(132, y),
\r
62 Size = new Size(23, height),
\r
63 TextAlign = ContentAlignment.MiddleRight
\r
67 Location = new Point(157, y),
\r
68 Size = new Size(23, height),
\r
69 TextAlign = ContentAlignment.MiddleRight
\r
73 Location = new Point(177, y),
\r
74 Size = new Size(41, height),
\r
75 TextAlign = ContentAlignment.MiddleRight
\r
77 new ShipLabel {Location = new Point(2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
78 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, lh - 1)}
\r
80 foreach (var label in _labels[i])
\r
83 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
85 label.Click += onClick;
\r
88 parent.ResumeLayout();
\r
91 public void SetShipInfo(ShipStatus[] statuses)
\r
93 var empty = new ShipStatus();
\r
94 for (var i = 0; i < _labels.Length; i++)
\r
96 var labels = _labels[i];
\r
97 var s = i < statuses.Length ? statuses[i] : empty;
\r
99 labels[1].SetCond(s);
\r
100 labels[2].SetLevel(s);
\r
101 labels[3].SetExpToNext(s);
\r
102 labels[4].SetName(s);
\r
106 public void CreateAkashiTimers(Control parent)
\r
108 parent.SuspendLayout();
\r
109 for (var i = 0; i < _akashiTimers.Length; i++)
\r
112 var y = 20 + 16 * i;
\r
114 parent.Controls.Add(
\r
115 label = _akashiTimers[i] = new ShipLabel {Location = new Point(x, y), AutoSize = true});
\r
116 label.BackColor = ColumnColors[i % 2];
\r
118 foreach (var label in _akashiTimers)
\r
120 parent.ResumeLayout();
\r
123 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
126 for (var i = 0; i < timers.Length; i++)
\r
128 if (timers[i].Span <= TimeSpan.Zero)
\r
130 if (shortest == -1 || timers[i].Span < timers[shortest].Span)
\r
133 for (var i = 0; i < _akashiTimers.Length; i++)
\r
135 var label = _akashiTimers[i];
\r
136 var labelHp = _labels[i][0];
\r
137 if (i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
139 label.Visible = false;
\r
140 labelHp.ForeColor = Control.DefaultForeColor;
\r
143 var timer = timers[i];
\r
144 var stat = statuses[i];
\r
145 label.Visible = true;
\r
146 label.Text = timer.Span.ToString(@"mm\:ss");
\r
147 label.ForeColor = Control.DefaultForeColor;
\r
148 if (timer.Diff == 0)
\r
150 labelHp.ForeColor = Control.DefaultForeColor;
\r
154 label.ForeColor = Color.Red;
\r
155 labelHp.ForeColor = Color.DimGray;
\r
156 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
160 public void CreateDamagedShipList(Control parent)
\r
162 parent.SuspendLayout();
\r
163 for (var i = 0; i < _damagedShipList.Length; i++)
\r
165 var y = 3 + i * 16;
\r
166 const int height = 12;
\r
167 parent.Controls.AddRange(_damagedShipList[i] = new[]
\r
169 new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},
\r
170 new ShipLabel {Location = new Point(119, y), Size = new Size(4, height - 1)},
\r
171 new ShipLabel {Location = new Point(75, y), AutoSize = true},
\r
172 new ShipLabel {Location = new Point(9, y), AutoSize = true},
\r
173 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}
\r
175 foreach (var label in _damagedShipList[i])
\r
178 label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];
\r
181 _panelDamagedShipList = parent;
\r
182 parent.ResumeLayout();
\r
185 public void SetDamagedShipList(ShipStatus[] list)
\r
187 const int fleet = 0, name = 3, time = 2, damage = 1;
\r
188 var parent = _panelDamagedShipList;
\r
189 var num = Min(list.Length, _damagedShipList.Length);
\r
192 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));
\r
193 var labels = _damagedShipList[0];
\r
194 labels[fleet].Text = "";
\r
195 labels[name].SetName("なし");
\r
196 labels[time].Text = "";
\r
197 labels[damage].BackColor = labels[damage].PresetColor;
\r
200 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * (num * 16 + 3)));
\r
201 var colors = new[] { Color.FromArgb(255, 225, 225, 21), Color.Orange, Color.Red };
\r
202 for (var i = 0; i < num; i++)
\r
205 var labels = _damagedShipList[i];
\r
206 labels[fleet].SetFleet(s);
\r
207 labels[name].SetName(s);
\r
208 labels[time].SetRepairTime(s);
\r
209 labels[damage].BackColor = (int)s.DamageLevel == 0
\r
210 ? labels[damage].PresetColor
\r
211 : colors[(int)s.DamageLevel - 1];
\r
215 public void CreateNDockLabels(Control parent)
\r
217 for (var i = 0; i < _ndockLabels.Length; i++)
\r
219 var y = 3 + i * 15;
\r
220 parent.Controls.AddRange(
\r
221 _ndockLabels[i] = new[]
\r
223 new ShipLabel {Location = new Point(93, y), AutoSize = true, Text = "00:00:00"},
\r
224 new ShipLabel {Location = new Point(29, y), AutoSize = true} // 名前のZ-orderを下に
\r
226 foreach (var label in _ndockLabels[i])
\r
231 public void SetNDockLabels(NameAndTimer[] ndock)
\r
233 for (var i = 0; i < _ndockLabels.Length; i++)
\r
234 _ndockLabels[i][1].SetName(ndock[i].Name);
\r
237 public void SetNDockTimer(int dock, RingTimer timer)
\r
239 var label = _ndockLabels[dock][0];
\r
240 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
241 label.SetRepairTime(timer.Rest);
\r
245 [System.ComponentModel.DesignerCategory("Code")]
\r
246 public class ShipLabel : Label
\r
248 public static SizeF ScaleFactor { get; set; }
\r
249 public Color PresetColor { get; set; }
\r
250 public bool AnchorRight { get; set; }
\r
251 private int _right = int.MinValue;
\r
254 public void SetName(ShipStatus status)
\r
256 SetName((status.Escaped ? "[避]" : "") + status.Name);
\r
259 public void SetName(string name)
\r
261 var lu = name != null && new Regex(@"^\p{Lu}").IsMatch(name);
\r
262 var shift = (int)Round(ScaleFactor.Height);
\r
263 if (lu && Font.Equals(Parent.Font))
\r
265 Location += new Size(0, -shift);
\r
266 Font = new Font("Tahoma", 8f);
\r
268 else if (!lu && !Font.Equals(Parent.Font))
\r
270 Location += new Size(0, shift);
\r
271 Font = Parent.Font;
\r
276 public void SetHp(ShipStatus status)
\r
278 SetHp(status.NowHp, status.MaxHp);
\r
281 public void SetHp(int now, int max)
\r
283 var colors = new[] {PresetColor, Color.FromArgb(255, 240, 240, 100), Color.Orange, Color.Red};
\r
284 Text = $"{now:D}/{max:D}";
\r
285 BackColor = colors[(int)ShipStatus.CalcDamage(now, max)];
\r
288 public void SetCond(ShipStatus status)
\r
290 if (status.Level == 0)
\r
293 BackColor = PresetColor;
\r
296 var cond = status.Cond;
\r
297 Text = cond.ToString("D");
\r
298 BackColor = cond >= 50
\r
302 : cond >= 20 ? Color.Orange : Color.Red;
\r
305 public void SetLevel(ShipStatus status)
\r
307 Text = status.Level.ToString("D");
\r
310 public void SetExpToNext(ShipStatus status)
\r
312 Text = status.ExpToNext.ToString("D");
\r
315 public void SetRepairTime(ShipStatus status)
\r
317 SetRepairTime(status.RepairTime);
\r
320 public void SetRepairTime(TimeSpan span)
\r
322 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
325 public void SetFleet(ShipStatus status)
\r
327 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
330 protected override void OnLayout(LayoutEventArgs levent)
\r
332 base.OnLayout(levent);
\r
335 if (_right == int.MinValue || _left != Left)
\r
341 if (_right == Right)
\r
343 _left -= Right - _right;
\r
344 Location = new Point(_left, Top);
\r
347 public void Scale()
\r
349 Scale(ScaleFactor);
\r