1 // Copyright (C) 2014, 2015 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.Drawing;
\r
18 using System.Text.RegularExpressions;
\r
19 using System.Windows.Forms;
\r
20 using static System.Math;
\r
22 namespace KancolleSniffer
\r
24 public enum ShipNameWidth
\r
30 RepairListFull = 75,
\r
36 public class ShipLabels
\r
38 private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];
\r
39 private readonly ShipLabel[][] _combinedLabels = new ShipLabel[ShipInfo.MemberCount * 2][];
\r
40 private readonly ShipLabel[] _akashiTimers = new ShipLabel[ShipInfo.MemberCount];
\r
41 private readonly ShipLabel[][] _repairList = new ShipLabel[16][];
\r
42 private Control _panelRepairList;
\r
43 private readonly ShipLabel[][] _ndockLabels = new ShipLabel[DockInfo.DockCount][];
\r
44 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
46 public void CreateShipLabels(Control parent, EventHandler onClick)
\r
48 parent.SuspendLayout();
\r
49 const int top = 3, height = 12, lh = 16;
\r
50 ShipLabel[] headings;
\r
51 parent.Controls.AddRange(headings = new[]
\r
53 new ShipLabel {Location = new Point(109, top), Text = "HP", AutoSize = true},
\r
54 new ShipLabel {Location = new Point(128, top), Text = "cond", AutoSize = true},
\r
55 new ShipLabel {Location = new Point(163, top), Text = "Lv", AutoSize = true},
\r
56 new ShipLabel {Location = new Point(194, top), Text = "Exp", AutoSize = true},
\r
57 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
59 foreach (var label in headings)
\r
62 label.BackColor = ColumnColors[1];
\r
64 for (var i = 0; i < _labels.Length; i++)
\r
66 var y = top + lh * (i + 1);
\r
67 parent.Controls.AddRange(_labels[i] = new[]
\r
69 new ShipLabel {Location = new Point(129, y), AutoSize = true, AnchorRight = true},
\r
72 Location = new Point(132, y),
\r
73 Size = new Size(23, height),
\r
74 TextAlign = ContentAlignment.MiddleRight
\r
78 Location = new Point(157, y),
\r
79 Size = new Size(23, height),
\r
80 TextAlign = ContentAlignment.MiddleRight
\r
84 Location = new Point(177, y),
\r
85 Size = new Size(41, height),
\r
86 TextAlign = ContentAlignment.MiddleRight
\r
88 new ShipLabel {Location = new Point(2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
89 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, lh - 1)}
\r
91 foreach (var label in _labels[i])
\r
94 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
96 label.Click += onClick;
\r
99 parent.ResumeLayout();
\r
102 public void SetShipLabels(ShipStatus[] statuses)
\r
104 for (var i = 0; i < _labels.Length; i++)
\r
106 var labels = _labels[i];
\r
107 if (i < statuses.Length)
\r
109 var s = statuses[i];
\r
110 labels[0].SetHp(s);
\r
111 labels[1].SetCond(s);
\r
112 labels[2].SetLevel(s);
\r
113 labels[3].SetExpToNext(s);
\r
114 labels[4].SetName(s, ShipNameWidth.MainPanel);
\r
118 labels[0].Text = labels[1].Text = labels[2].Text = labels[3].Text = "";
\r
119 labels[4].SetName("");
\r
120 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
125 public void CreateCombinedShipLabels(Control parent, EventHandler onClick)
\r
127 parent.SuspendLayout();
\r
128 const int top = 3, height = 12, lh = 16;
\r
129 const int parentWidth = 220; // parent.Widthを使うとDPIスケーリング時に計算がくるうので
\r
130 ShipLabel[] headings;
\r
131 parent.Controls.AddRange(headings = new[]
\r
133 new ShipLabel {Location = new Point(68, top), Text = "HP", AutoSize = true},
\r
134 new ShipLabel {Location = new Point(86, top), Text = "cnd", AutoSize = true},
\r
135 new ShipLabel {Location = new Point(177, top), Text = "HP", AutoSize = true},
\r
136 new ShipLabel {Location = new Point(195, top), Text = "cnd", AutoSize = true},
\r
137 new ShipLabel {Location = new Point(0, 1), Size = new Size(parentWidth, lh - 1)}
\r
139 foreach (var label in headings)
\r
142 label.BackColor = ColumnColors[1];
\r
144 for (var i = 0; i < _combinedLabels.Length; i++)
\r
146 var x = parentWidth / 2 * (i / ShipInfo.MemberCount);
\r
147 var y = top + lh * ((i % ShipInfo.MemberCount) + 1);
\r
148 parent.Controls.AddRange(_combinedLabels[i] = new[]
\r
150 new ShipLabel {Location = new Point(x + 88, y), AutoSize = true, AnchorRight = true},
\r
153 Location = new Point(x + 86, y),
\r
154 Size = new Size(23, height),
\r
155 TextAlign = ContentAlignment.MiddleRight
\r
157 new ShipLabel {Location = new Point(x + 2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
158 new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parentWidth / 2, lh - 1)}
\r
160 foreach (var label in _combinedLabels[i])
\r
163 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
165 label.Click += onClick;
\r
168 parent.ResumeLayout();
\r
171 public void SetCombinedShipLabels(ShipStatus[] first, ShipStatus[] second)
\r
173 for (var i = 0; i < _combinedLabels.Length; i++)
\r
175 var idx = i % ShipInfo.MemberCount;
\r
176 var statuses = i < ShipInfo.MemberCount ? first : second;
\r
177 var labels = _combinedLabels[i];
\r
178 if (idx < statuses.Length)
\r
180 var s = statuses[idx];
\r
181 labels[0].SetHp(s);
\r
182 labels[1].SetCond(s);
\r
183 labels[2].SetName(s);
\r
187 labels[0].Text = labels[1].Text = "";
\r
188 labels[2].SetName("");
\r
189 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
194 public void CreateAkashiTimers(Control parent)
\r
196 parent.SuspendLayout();
\r
197 for (var i = 0; i < _akashiTimers.Length; i++)
\r
200 var y = 3 + 16 * (i + 1);
\r
202 parent.Controls.Add(
\r
203 label = _akashiTimers[i] =
\r
206 Location = new Point(x, y),
\r
207 Size = new Size(34, 12),
\r
208 TextAlign = ContentAlignment.TopRight
\r
210 label.BackColor = ColumnColors[i % 2];
\r
212 foreach (var label in _akashiTimers)
\r
214 parent.ResumeLayout();
\r
217 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
220 for (var i = 0; i < timers.Length; i++)
\r
222 if (timers[i].Span <= TimeSpan.Zero)
\r
224 if (shortest == -1 || timers[i].Span < timers[shortest].Span)
\r
227 for (var i = 0; i < _akashiTimers.Length; i++)
\r
229 var label = _akashiTimers[i];
\r
230 var labelHp = _labels[i][0];
\r
231 var labelName = _labels[i][4];
\r
232 if (i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
234 label.Visible = false;
\r
235 labelHp.ForeColor = Control.DefaultForeColor;
\r
238 var timer = timers[i];
\r
239 var stat = statuses[i];
\r
240 label.Visible = true;
\r
241 label.Text = timer.Span.ToString(@"mm\:ss");
\r
242 label.ForeColor = Control.DefaultForeColor;
\r
243 labelName.SetName(stat, ShipNameWidth.AkashiTimer);
\r
244 if (timer.Diff == 0)
\r
246 labelHp.ForeColor = Control.DefaultForeColor;
\r
250 label.ForeColor = Color.Red;
\r
251 labelHp.ForeColor = Color.DimGray;
\r
252 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
256 public void CreateRepairList(Control parent, EventHandler onClick)
\r
258 parent.SuspendLayout();
\r
259 for (var i = 0; i < _repairList.Length; i++)
\r
261 var y = 3 + i * 16;
\r
262 const int height = 12;
\r
263 parent.Controls.AddRange(_repairList[i] = new[]
\r
265 new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},
\r
266 new ShipLabel {Location = new Point(119, y), Size = new Size(5, height - 1)},
\r
267 new ShipLabel {Location = new Point(75, y), AutoSize = true},
\r
268 new ShipLabel {Location = new Point(9, y), AutoSize = true},
\r
269 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}
\r
271 foreach (var label in _repairList[i])
\r
274 label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];
\r
275 label.Click += onClick;
\r
278 _panelRepairList = parent;
\r
279 parent.ResumeLayout();
\r
282 public void SetRepairList(ShipStatus[] list)
\r
284 const int fleet = 0, name = 3, time = 2, damage = 1;
\r
285 var parent = _panelRepairList;
\r
286 var num = Min(list.Length, _repairList.Length);
\r
289 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));
\r
290 var labels = _repairList[0];
\r
291 labels[fleet].Text = "";
\r
292 labels[name].SetName("なし");
\r
293 labels[time].Text = "";
\r
294 labels[damage].BackColor = labels[damage].PresetColor;
\r
297 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * (num * 16 + 3)));
\r
298 for (var i = 0; i < num; i++)
\r
301 var labels = _repairList[i];
\r
302 labels[fleet].SetFleet(s);
\r
303 labels[name].SetName(s, ShipNameWidth.RepairList);
\r
304 labels[time].SetRepairTime(s);
\r
305 labels[damage].BackColor = ShipLabel.DamageColor(s, labels[damage].PresetColor);
\r
309 public void CreateNDockLabels(Control parent, EventHandler onClick)
\r
311 for (var i = 0; i < _ndockLabels.Length; i++)
\r
313 var y = 3 + i * 15;
\r
314 parent.Controls.AddRange(
\r
315 _ndockLabels[i] = new[]
\r
319 Location = new Point(138, y),
\r
323 new ShipLabel {Location = new Point(29, y), AutoSize = true} // 名前のZ-orderを下に
\r
325 foreach (var label in _ndockLabels[i])
\r
328 label.Click += onClick;
\r
333 public void SetNDockLabels(NameAndTimer[] ndock)
\r
335 for (var i = 0; i < _ndockLabels.Length; i++)
\r
336 _ndockLabels[i][1].SetName("", ndock[i].Name, "", ShipNameWidth.NDock);
\r
339 public void SetNDockTimer(int dock, RingTimer timer, bool finishTime)
\r
341 var label = _ndockLabels[dock][0];
\r
342 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
343 label.Text = timer.ToString(finishTime);
\r
347 [System.ComponentModel.DesignerCategory("Code")]
\r
348 public class ShipLabel : Label
\r
350 public static SizeF ScaleFactor { get; set; }
\r
351 public static Font LatinFont { get; set; } = new Font("Tahoma", 8f);
\r
352 public Color PresetColor { get; set; }
\r
353 public bool AnchorRight { get; set; }
\r
354 private int _right = int.MinValue;
\r
359 UseMnemonic = false;
\r
362 public void SetName(ShipStatus status, ShipNameWidth width = ShipNameWidth.Max)
\r
364 var empty = status.Id != -1 && status.Slot.All(e => e.Id == -1) ? "▫" : "";
\r
365 var dc = status.PreparedDamageControl;
\r
366 var dcname = dc == 42 ? "[ダ]" : dc == 43 ? "[メ]" : "";
\r
367 SetName((status.Escaped ? "[避]" : dcname), status.Name, empty, width);
\r
370 public void SetName(string name)
\r
372 SetName("", name, "");
\r
375 public void SetName(string prefix, string name, string suffix, ShipNameWidth width = ShipNameWidth.Max)
\r
379 var lu = new Regex(@"^\p{Lu}").IsMatch(name);
\r
380 var shift = (int)Round(ScaleFactor.Height);
\r
381 if (lu && Font.Equals(Parent.Font))
\r
383 Location += new Size(0, -shift);
\r
386 else if (!lu && !Font.Equals(Parent.Font))
\r
388 Location += new Size(0, shift);
\r
389 Font = Parent.Font;
\r
391 var result = prefix + name + suffix;
\r
392 var measured = TextRenderer.MeasureText(result, Font).Width;
\r
393 if (measured <= (int)width)
\r
398 var truncated = "";
\r
399 foreach (var ch in name)
\r
401 var tmp = truncated + ch;
\r
402 if (TextRenderer.MeasureText(tmp, Font).Width > (int)width)
\r
406 Text = prefix + truncated.TrimEnd(' ') + suffix;
\r
409 public void SetHp(ShipStatus status)
\r
411 Text = $"{status.NowHp:D}/{status.MaxHp:D}";
\r
412 BackColor = DamageColor(status, PresetColor);
\r
415 public void SetHp(int now, int max)
\r
417 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
420 public static Color DamageColor(ShipStatus status, Color backcolor)
\r
422 switch (status.DamageLevel)
\r
424 case ShipStatus.Damage.Badly:
\r
426 case ShipStatus.Damage.Half:
\r
427 return Color.Orange;
\r
428 case ShipStatus.Damage.Small:
\r
429 return Color.FromArgb(240, 240, 0);
\r
435 public void SetCond(ShipStatus status)
\r
437 var cond = status.Cond;
\r
438 Text = cond.ToString("D");
\r
439 BackColor = cond >= 50
\r
443 : cond >= 20 ? Color.Orange : Color.Red;
\r
446 public void SetLevel(ShipStatus status)
\r
448 Text = status.Level.ToString("D");
\r
451 public void SetExpToNext(ShipStatus status)
\r
453 Text = status.ExpToNext.ToString("D");
\r
456 public void SetRepairTime(ShipStatus status)
\r
458 SetRepairTime(status.RepairTime);
\r
461 public void SetRepairTime(TimeSpan span)
\r
463 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
466 public void SetFleet(ShipStatus status)
\r
468 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
471 protected override void OnLayout(LayoutEventArgs levent)
\r
473 base.OnLayout(levent);
\r
476 if (_right == int.MinValue || _left != Left)
\r
482 if (_right == Right)
\r
484 _left -= Right - _right;
\r
485 Location = new Point(_left, Top);
\r
488 public void Scale()
\r
490 Scale(ScaleFactor);
\r