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 for (var i = 0; i < _labels.Length; i++)
\r
97 var labels = _labels[i];
\r
98 if (i < statuses.Length)
\r
100 var s = statuses[i];
\r
101 labels[0].SetHp(s);
\r
102 labels[1].SetCond(s);
\r
103 labels[2].SetLevel(s);
\r
104 labels[3].SetExpToNext(s);
\r
105 labels[4].SetName(s);
\r
109 labels[0].Text = labels[1].Text = labels[2].Text = labels[3].Text = "";
\r
110 labels[4].SetName("");
\r
111 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
116 public void CreateCombinedShipLabels(Control parent, EventHandler onClick)
\r
118 parent.SuspendLayout();
\r
119 const int top = 3, height = 12, lh = 16;
\r
120 ShipLabel[] headings;
\r
121 parent.Controls.AddRange(headings = new[]
\r
123 new ShipLabel {Location = new Point(68, top), Text = "HP", AutoSize = true},
\r
124 new ShipLabel {Location = new Point(86, top), Text = "cnd", AutoSize = true},
\r
125 new ShipLabel {Location = new Point(177, top), Text = "HP", AutoSize = true},
\r
126 new ShipLabel {Location = new Point(195, top), Text = "cnd", AutoSize = true},
\r
127 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
129 foreach (var label in headings)
\r
132 label.BackColor = ColumnColors[1];
\r
134 for (var i = 0; i < _combinedLabels.Length; i++)
\r
136 var x = (parent.Width / 2) * (i / ShipInfo.MemberCount);
\r
137 var y = top + lh * ((i % ShipInfo.MemberCount) + 1);
\r
138 parent.Controls.AddRange(_combinedLabels[i] = new[]
\r
140 new ShipLabel {Location = new Point(x + 88, y), AutoSize = true, AnchorRight = true},
\r
143 Location = new Point(x + 86, y),
\r
144 Size = new Size(23, height),
\r
145 TextAlign = ContentAlignment.MiddleRight
\r
147 new ShipLabel {Location = new Point(x + 2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
148 new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parent.Width / 2, lh - 1)}
\r
150 foreach (var label in _combinedLabels[i])
\r
153 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
155 label.Click += onClick;
\r
158 parent.ResumeLayout();
\r
161 public void SetCombinedShipInfo(ShipStatus[] first, ShipStatus[] second)
\r
163 for (var i = 0; i < _combinedLabels.Length; i++)
\r
165 var idx = i % ShipInfo.MemberCount;
\r
166 var statuses = i < ShipInfo.MemberCount ? first : second;
\r
167 var labels = _combinedLabels[i];
\r
168 if (idx < statuses.Length)
\r
170 var s = statuses[idx];
\r
171 labels[0].SetHp(s);
\r
172 labels[1].SetCond(s);
\r
173 labels[2].SetName(s);
\r
177 labels[0].Text = labels[1].Text = "";
\r
178 labels[2].SetName("");
\r
179 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
184 public void CreateAkashiTimers(Control parent)
\r
186 parent.SuspendLayout();
\r
187 for (var i = 0; i < _akashiTimers.Length; i++)
\r
190 var y = 3 + 16 * (i + 1);
\r
192 parent.Controls.Add(
\r
193 label = _akashiTimers[i] =
\r
196 Location = new Point(x, y),
\r
197 Size = new Size(34, 12),
\r
198 TextAlign = ContentAlignment.TopRight
\r
200 label.BackColor = ColumnColors[i % 2];
\r
202 foreach (var label in _akashiTimers)
\r
204 parent.ResumeLayout();
\r
207 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
210 for (var i = 0; i < timers.Length; i++)
\r
212 if (timers[i].Span <= TimeSpan.Zero)
\r
214 if (shortest == -1 || timers[i].Span < timers[shortest].Span)
\r
217 for (var i = 0; i < _akashiTimers.Length; i++)
\r
219 var label = _akashiTimers[i];
\r
220 var labelHp = _labels[i][0];
\r
221 if (i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
223 label.Visible = false;
\r
224 labelHp.ForeColor = Control.DefaultForeColor;
\r
227 var timer = timers[i];
\r
228 var stat = statuses[i];
\r
229 label.Visible = true;
\r
230 label.Text = timer.Span.ToString(@"mm\:ss");
\r
231 label.ForeColor = Control.DefaultForeColor;
\r
232 if (timer.Diff == 0)
\r
234 labelHp.ForeColor = Control.DefaultForeColor;
\r
238 label.ForeColor = Color.Red;
\r
239 labelHp.ForeColor = Color.DimGray;
\r
240 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
244 public void CreateDamagedShipList(Control parent, EventHandler onClick)
\r
246 parent.SuspendLayout();
\r
247 for (var i = 0; i < _damagedShipList.Length; i++)
\r
249 var y = 3 + i * 16;
\r
250 const int height = 12;
\r
251 parent.Controls.AddRange(_damagedShipList[i] = new[]
\r
253 new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},
\r
254 new ShipLabel {Location = new Point(119, y), Size = new Size(4, height - 1)},
\r
255 new ShipLabel {Location = new Point(75, y), AutoSize = true},
\r
256 new ShipLabel {Location = new Point(9, y), AutoSize = true},
\r
257 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}
\r
259 foreach (var label in _damagedShipList[i])
\r
262 label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];
\r
263 label.Click += onClick;
\r
266 _panelDamagedShipList = parent;
\r
267 parent.ResumeLayout();
\r
270 public void SetDamagedShipList(ShipStatus[] list)
\r
272 const int fleet = 0, name = 3, time = 2, damage = 1;
\r
273 var parent = _panelDamagedShipList;
\r
274 var num = Min(list.Length, _damagedShipList.Length);
\r
277 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));
\r
278 var labels = _damagedShipList[0];
\r
279 labels[fleet].Text = "";
\r
280 labels[name].SetName("なし");
\r
281 labels[time].Text = "";
\r
282 labels[damage].BackColor = labels[damage].PresetColor;
\r
285 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * (num * 16 + 3)));
\r
286 for (var i = 0; i < num; i++)
\r
289 var labels = _damagedShipList[i];
\r
290 labels[fleet].SetFleet(s);
\r
291 labels[name].SetName(s);
\r
292 labels[time].SetRepairTime(s);
\r
293 labels[damage].BackColor = ShipLabel.DamageColor(s, labels[damage].PresetColor);
\r
297 public void CreateNDockLabels(Control parent, EventHandler onClick)
\r
299 for (var i = 0; i < _ndockLabels.Length; i++)
\r
301 var y = 3 + i * 15;
\r
302 parent.Controls.AddRange(
\r
303 _ndockLabels[i] = new[]
\r
307 Location = new Point(138, y),
\r
311 new ShipLabel {Location = new Point(29, y), AutoSize = true} // 名前のZ-orderを下に
\r
313 foreach (var label in _ndockLabels[i])
\r
316 label.Click += onClick;
\r
319 parent.Click += onClick;
\r
322 public void SetNDockLabels(NameAndTimer[] ndock)
\r
324 for (var i = 0; i < _ndockLabels.Length; i++)
\r
325 _ndockLabels[i][1].SetName(ndock[i].Name);
\r
328 public void SetNDockTimer(int dock, RingTimer timer, bool finishTime)
\r
330 var label = _ndockLabels[dock][0];
\r
331 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
332 if (timer.EndTime == DateTime.MinValue)
\r
339 label.Text = timer.EndTime.ToString(@"dd\ HH\:mm");
\r
341 label.SetRepairTime(timer.Rest);
\r
346 [System.ComponentModel.DesignerCategory("Code")]
\r
347 public class ShipLabel : Label
\r
349 public static SizeF ScaleFactor { get; set; }
\r
350 public Color PresetColor { get; set; }
\r
351 public bool AnchorRight { get; set; }
\r
352 private int _right = int.MinValue;
\r
355 public void SetName(ShipStatus status)
\r
357 var empty = status.Id != -1 && status.Slot.All(e => e.Id == -1) ? "▫" : "";
\r
358 var dc = status.PreparedDamageControl;
\r
359 var dcname = dc == 42 ? "[ダ]" : dc == 43 ? "[メ]" : "";
\r
360 SetName((status.Escaped ? "[避]" : dcname) + status.Name + empty);
\r
363 public void SetName(string name)
\r
365 var lu = name != null && new Regex(@"^\p{Lu}").IsMatch(name);
\r
366 var shift = (int)Round(ScaleFactor.Height);
\r
367 if (lu && Font.Equals(Parent.Font))
\r
369 Location += new Size(0, -shift);
\r
370 Font = new Font("Tahoma", 8f);
\r
372 else if (!lu && !Font.Equals(Parent.Font))
\r
374 Location += new Size(0, shift);
\r
375 Font = Parent.Font;
\r
380 public void SetHp(ShipStatus status)
\r
382 Text = $"{status.NowHp:D}/{status.MaxHp:D}";
\r
383 BackColor = DamageColor(status, PresetColor);
\r
386 public void SetHp(int now, int max)
\r
388 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
391 public static Color DamageColor(ShipStatus status, Color backcolor)
\r
393 switch (status.DamageLevel)
\r
395 case ShipStatus.Damage.Badly:
\r
397 case ShipStatus.Damage.Half:
\r
398 return Color.Orange;
\r
399 case ShipStatus.Damage.Small:
\r
400 return Color.FromArgb(225, 225, 21);
\r
406 public void SetCond(ShipStatus status)
\r
408 var cond = status.Cond;
\r
409 Text = cond.ToString("D");
\r
410 BackColor = cond >= 50
\r
414 : cond >= 20 ? Color.Orange : Color.Red;
\r
417 public void SetLevel(ShipStatus status)
\r
419 Text = status.Level.ToString("D");
\r
422 public void SetExpToNext(ShipStatus status)
\r
424 Text = status.ExpToNext.ToString("D");
\r
427 public void SetRepairTime(ShipStatus status)
\r
429 SetRepairTime(status.RepairTime);
\r
432 public void SetRepairTime(TimeSpan span)
\r
434 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
437 public void SetFleet(ShipStatus status)
\r
439 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
442 protected override void OnLayout(LayoutEventArgs levent)
\r
444 base.OnLayout(levent);
\r
447 if (_right == int.MinValue || _left != Left)
\r
453 if (_right == Right)
\r
455 _left -= Right - _right;
\r
456 Location = new Point(_left, Top);
\r
459 public void Scale()
\r
461 Scale(ScaleFactor);
\r