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
21 using System.Text.RegularExpressions;
\r
22 using System.Windows.Forms;
\r
23 using static System.Math;
\r
25 namespace KancolleSniffer
\r
27 public class ShipLabels
\r
29 private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];
\r
30 private readonly ShipLabel[][] _combinedLabels = new ShipLabel[ShipInfo.MemberCount * 2][];
\r
31 private readonly ShipLabel[] _akashiTimers = new ShipLabel[ShipInfo.MemberCount];
\r
32 private readonly ShipLabel[][] _damagedShipList = new ShipLabel[16][];
\r
33 private Control _panelDamagedShipList;
\r
34 private readonly ShipLabel[][] _ndockLabels = new ShipLabel[DockInfo.DockCount][];
\r
35 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
37 public void CreateLabels(Control parent, EventHandler onClick)
\r
39 parent.SuspendLayout();
\r
40 const int top = 3, height = 12, lh = 16;
\r
41 ShipLabel[] headings;
\r
42 parent.Controls.AddRange(headings = new[]
\r
44 new ShipLabel {Location = new Point(109, top), Text = "HP", AutoSize = true},
\r
45 new ShipLabel {Location = new Point(128, top), Text = "cond", AutoSize = true},
\r
46 new ShipLabel {Location = new Point(163, top), Text = "Lv", AutoSize = true},
\r
47 new ShipLabel {Location = new Point(194, top), Text = "Exp", AutoSize = true},
\r
48 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
50 foreach (var label in headings)
\r
53 label.BackColor = ColumnColors[1];
\r
55 for (var i = 0; i < _labels.Length; i++)
\r
57 var y = top + lh * (i + 1);
\r
58 parent.Controls.AddRange(_labels[i] = new[]
\r
60 new ShipLabel {Location = new Point(129, y), AutoSize = true, AnchorRight = true},
\r
63 Location = new Point(132, y),
\r
64 Size = new Size(23, height),
\r
65 TextAlign = ContentAlignment.MiddleRight
\r
69 Location = new Point(157, y),
\r
70 Size = new Size(23, height),
\r
71 TextAlign = ContentAlignment.MiddleRight
\r
75 Location = new Point(177, y),
\r
76 Size = new Size(41, height),
\r
77 TextAlign = ContentAlignment.MiddleRight
\r
79 new ShipLabel {Location = new Point(2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
80 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, lh - 1)}
\r
82 foreach (var label in _labels[i])
\r
85 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
87 label.Click += onClick;
\r
90 parent.ResumeLayout();
\r
93 public void SetShipInfo(ShipStatus[] statuses)
\r
95 var empty = new ShipStatus();
\r
96 for (var i = 0; i < _labels.Length; i++)
\r
98 var labels = _labels[i];
\r
99 var s = i < statuses.Length ? statuses[i] : empty;
\r
100 labels[0].SetHp(s);
\r
101 labels[1].SetCond(s);
\r
102 labels[2].SetLevel(s);
\r
103 labels[3].SetExpToNext(s);
\r
104 labels[4].SetName(s);
\r
108 public void CreateCombinedShipLabels(Control parent, EventHandler onClick)
\r
110 parent.SuspendLayout();
\r
111 const int top = 3, height = 12, lh = 16;
\r
112 ShipLabel[] headings;
\r
113 parent.Controls.AddRange(headings = new[]
\r
115 new ShipLabel {Location = new Point(68, top), Text = "HP", AutoSize = true},
\r
116 new ShipLabel {Location = new Point(86, top), Text = "cnd", AutoSize = true},
\r
117 new ShipLabel {Location = new Point(177, top), Text = "HP", AutoSize = true},
\r
118 new ShipLabel {Location = new Point(195, top), Text = "cnd", AutoSize = true},
\r
119 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
121 foreach (var label in headings)
\r
124 label.BackColor = ColumnColors[1];
\r
126 for (var i = 0; i < _combinedLabels.Length; i++)
\r
128 var x = (parent.Width / 2) * (i / ShipInfo.MemberCount);
\r
129 var y = top + lh * ((i % ShipInfo.MemberCount) + 1);
\r
130 parent.Controls.AddRange(_combinedLabels[i] = new[]
\r
132 new ShipLabel {Location = new Point(x + 88, y), AutoSize = true, AnchorRight = true},
\r
135 Location = new Point(x + 86, y),
\r
136 Size = new Size(23, height),
\r
137 TextAlign = ContentAlignment.MiddleRight
\r
139 new ShipLabel {Location = new Point(x + 2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
140 new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parent.Width / 2, lh - 1)}
\r
142 foreach (var label in _combinedLabels[i])
\r
145 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
147 label.Click += onClick;
\r
150 parent.ResumeLayout();
\r
153 public void SetCombinedShipInfo(ShipStatus[] first, ShipStatus[] second)
\r
155 var empty = new ShipStatus();
\r
156 for (var i = 0; i < _combinedLabels.Length; i++)
\r
158 var idx = i % ShipInfo.MemberCount;
\r
159 var statuses = i < ShipInfo.MemberCount ? first : second;
\r
160 var labels = _combinedLabels[i];
\r
161 var s = idx < statuses.Length ? statuses[idx] : empty;
\r
162 labels[0].SetHp(s);
\r
163 labels[1].SetCond(s);
\r
164 labels[2].SetName(s);
\r
168 public void CreateAkashiTimers(Control parent)
\r
170 parent.SuspendLayout();
\r
171 for (var i = 0; i < _akashiTimers.Length; i++)
\r
174 var y = 3 + 16 * (i + 1);
\r
176 parent.Controls.Add(
\r
177 label = _akashiTimers[i] =
\r
180 Location = new Point(x, y),
\r
181 Size = new Size(34, 12),
\r
182 TextAlign = ContentAlignment.TopRight
\r
184 label.BackColor = ColumnColors[i % 2];
\r
186 foreach (var label in _akashiTimers)
\r
188 parent.ResumeLayout();
\r
191 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
194 for (var i = 0; i < timers.Length; i++)
\r
196 if (timers[i].Span <= TimeSpan.Zero)
\r
198 if (shortest == -1 || timers[i].Span < timers[shortest].Span)
\r
201 for (var i = 0; i < _akashiTimers.Length; i++)
\r
203 var label = _akashiTimers[i];
\r
204 var labelHp = _labels[i][0];
\r
205 if (i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
207 label.Visible = false;
\r
208 labelHp.ForeColor = Control.DefaultForeColor;
\r
211 var timer = timers[i];
\r
212 var stat = statuses[i];
\r
213 label.Visible = true;
\r
214 label.Text = timer.Span.ToString(@"mm\:ss");
\r
215 label.ForeColor = Control.DefaultForeColor;
\r
216 if (timer.Diff == 0)
\r
218 labelHp.ForeColor = Control.DefaultForeColor;
\r
222 label.ForeColor = Color.Red;
\r
223 labelHp.ForeColor = Color.DimGray;
\r
224 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
228 public void CreateDamagedShipList(Control parent, EventHandler onClick)
\r
230 parent.SuspendLayout();
\r
231 for (var i = 0; i < _damagedShipList.Length; i++)
\r
233 var y = 3 + i * 16;
\r
234 const int height = 12;
\r
235 parent.Controls.AddRange(_damagedShipList[i] = new[]
\r
237 new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},
\r
238 new ShipLabel {Location = new Point(119, y), Size = new Size(4, height - 1)},
\r
239 new ShipLabel {Location = new Point(75, y), AutoSize = true},
\r
240 new ShipLabel {Location = new Point(9, y), AutoSize = true},
\r
241 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}
\r
243 foreach (var label in _damagedShipList[i])
\r
246 label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];
\r
247 label.Click += onClick;
\r
250 _panelDamagedShipList = parent;
\r
251 parent.ResumeLayout();
\r
254 public void SetDamagedShipList(ShipStatus[] list)
\r
256 const int fleet = 0, name = 3, time = 2, damage = 1;
\r
257 var parent = _panelDamagedShipList;
\r
258 var num = Min(list.Length, _damagedShipList.Length);
\r
261 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));
\r
262 var labels = _damagedShipList[0];
\r
263 labels[fleet].Text = "";
\r
264 labels[name].SetName("なし");
\r
265 labels[time].Text = "";
\r
266 labels[damage].BackColor = labels[damage].PresetColor;
\r
269 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * (num * 16 + 3)));
\r
270 var colors = new[] {Color.FromArgb(255, 225, 225, 21), Color.Orange, Color.Red};
\r
271 for (var i = 0; i < num; i++)
\r
274 var labels = _damagedShipList[i];
\r
275 labels[fleet].SetFleet(s);
\r
276 labels[name].SetName(s);
\r
277 labels[time].SetRepairTime(s);
\r
278 labels[damage].BackColor = (int)s.DamageLevel == 0
\r
279 ? labels[damage].PresetColor
\r
280 : colors[(int)s.DamageLevel - 1];
\r
284 public void CreateNDockLabels(Control parent, EventHandler onClick)
\r
286 for (var i = 0; i < _ndockLabels.Length; i++)
\r
288 var y = 3 + i * 15;
\r
289 parent.Controls.AddRange(
\r
290 _ndockLabels[i] = new[]
\r
294 Location = new Point(93, y),
\r
296 AnchorRight = true,
\r
299 new ShipLabel {Location = new Point(29, y), AutoSize = true} // 名前のZ-orderを下に
\r
301 foreach (var label in _ndockLabels[i])
\r
304 label.Click += onClick;
\r
307 parent.Click += onClick;
\r
310 public void SetNDockLabels(NameAndTimer[] ndock)
\r
312 for (var i = 0; i < _ndockLabels.Length; i++)
\r
313 _ndockLabels[i][1].SetName(ndock[i].Name);
\r
316 public void SetNDockTimer(int dock, RingTimer timer, bool finishTime)
\r
318 var label = _ndockLabels[dock][0];
\r
319 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
321 label.SetRepairFinishTime(timer.EndTime);
\r
323 label.SetRepairTime(timer.Rest);
\r
327 [System.ComponentModel.DesignerCategory("Code")]
\r
328 public class ShipLabel : Label
\r
330 public static SizeF ScaleFactor { get; set; }
\r
331 public Color PresetColor { get; set; }
\r
332 public bool AnchorRight { get; set; }
\r
333 private int _right = int.MinValue;
\r
336 public void SetName(ShipStatus status)
\r
338 var empty = status.Id != -1 && status.Slot.All(e => e.Id == -1) ? "▫" : "";
\r
339 var dc = status.PreparedDamageControl;
\r
340 var dcname = dc == 42 ? "[ダ]" : dc == 43 ? "[メ]" : "";
\r
341 SetName((status.Escaped ? "[避]" : dcname) + status.Name + empty);
\r
344 public void SetName(string name)
\r
346 var lu = name != null && new Regex(@"^\p{Lu}").IsMatch(name);
\r
347 var shift = (int)Round(ScaleFactor.Height);
\r
348 if (lu && Font.Equals(Parent.Font))
\r
350 Location += new Size(0, -shift);
\r
351 Font = new Font("Tahoma", 8f);
\r
353 else if (!lu && !Font.Equals(Parent.Font))
\r
355 Location += new Size(0, shift);
\r
356 Font = Parent.Font;
\r
361 public void SetHp(ShipStatus status)
\r
363 SetHp(status.NowHp, status.MaxHp);
\r
366 public void SetHp(int now, int max)
\r
368 var colors = new[] {PresetColor, Color.FromArgb(255, 240, 240, 100), Color.Orange, Color.Red};
\r
369 Text = $"{now:D}/{max:D}";
\r
370 BackColor = colors[(int)ShipStatus.CalcDamage(now, max)];
\r
373 public void SetCond(ShipStatus status)
\r
375 if (status.Level == 0)
\r
378 BackColor = PresetColor;
\r
381 var cond = status.Cond;
\r
382 Text = cond.ToString("D");
\r
383 BackColor = cond >= 50
\r
387 : cond >= 20 ? Color.Orange : Color.Red;
\r
390 public void SetLevel(ShipStatus status)
\r
392 Text = status.Level.ToString("D");
\r
395 public void SetExpToNext(ShipStatus status)
\r
397 Text = status.ExpToNext.ToString("D");
\r
400 public void SetRepairTime(ShipStatus status)
\r
402 SetRepairTime(status.RepairTime);
\r
405 public void SetRepairTime(TimeSpan span)
\r
407 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
410 public void SetRepairFinishTime(DateTime time)
\r
412 Text = time == DateTime.MinValue ? "-------" : time.ToString(@"dd\ HH\:mm");
\r
415 public void SetFleet(ShipStatus status)
\r
417 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
420 protected override void OnLayout(LayoutEventArgs levent)
\r
422 base.OnLayout(levent);
\r
425 if (_right == int.MinValue || _left != Left)
\r
431 if (_right == Right)
\r
433 _left -= Right - _right;
\r
434 Location = new Point(_left, Top);
\r
437 public void Scale()
\r
439 Scale(ScaleFactor);
\r