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 class ShipLabels
\r
26 private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];
\r
27 private readonly ShipLabel[][] _combinedLabels = new ShipLabel[ShipInfo.MemberCount * 2][];
\r
28 private readonly ShipLabel[] _akashiTimers = new ShipLabel[ShipInfo.MemberCount];
\r
29 private readonly ShipLabel[][] _damagedShipList = new ShipLabel[16][];
\r
30 private Control _panelDamagedShipList;
\r
31 private readonly ShipLabel[][] _ndockLabels = new ShipLabel[DockInfo.DockCount][];
\r
32 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
34 public void CreateLabels(Control parent, EventHandler onClick)
\r
36 parent.SuspendLayout();
\r
37 const int top = 3, height = 12, lh = 16;
\r
38 ShipLabel[] headings;
\r
39 parent.Controls.AddRange(headings = new[]
\r
41 new ShipLabel {Location = new Point(109, top), Text = "HP", AutoSize = true},
\r
42 new ShipLabel {Location = new Point(128, top), Text = "cond", AutoSize = true},
\r
43 new ShipLabel {Location = new Point(163, top), Text = "Lv", AutoSize = true},
\r
44 new ShipLabel {Location = new Point(194, top), Text = "Exp", AutoSize = true},
\r
45 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
47 foreach (var label in headings)
\r
50 label.BackColor = ColumnColors[1];
\r
52 for (var i = 0; i < _labels.Length; i++)
\r
54 var y = top + lh * (i + 1);
\r
55 parent.Controls.AddRange(_labels[i] = new[]
\r
57 new ShipLabel {Location = new Point(129, y), AutoSize = true, AnchorRight = true},
\r
60 Location = new Point(132, y),
\r
61 Size = new Size(23, height),
\r
62 TextAlign = ContentAlignment.MiddleRight
\r
66 Location = new Point(157, y),
\r
67 Size = new Size(23, height),
\r
68 TextAlign = ContentAlignment.MiddleRight
\r
72 Location = new Point(177, y),
\r
73 Size = new Size(41, height),
\r
74 TextAlign = ContentAlignment.MiddleRight
\r
76 new ShipLabel {Location = new Point(2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
77 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, lh - 1)}
\r
79 foreach (var label in _labels[i])
\r
82 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
84 label.Click += onClick;
\r
87 parent.ResumeLayout();
\r
90 public void SetShipInfo(ShipStatus[] statuses)
\r
92 for (var i = 0; i < _labels.Length; i++)
\r
94 var labels = _labels[i];
\r
95 if (i < statuses.Length)
\r
97 var s = statuses[i];
\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 labels[0].Text = labels[1].Text = labels[2].Text = labels[3].Text = "";
\r
107 labels[4].SetName("");
\r
108 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
113 public void CreateCombinedShipLabels(Control parent, EventHandler onClick)
\r
115 parent.SuspendLayout();
\r
116 const int top = 3, height = 12, lh = 16;
\r
117 ShipLabel[] headings;
\r
118 parent.Controls.AddRange(headings = new[]
\r
120 new ShipLabel {Location = new Point(68, top), Text = "HP", AutoSize = true},
\r
121 new ShipLabel {Location = new Point(86, top), Text = "cnd", AutoSize = true},
\r
122 new ShipLabel {Location = new Point(177, top), Text = "HP", AutoSize = true},
\r
123 new ShipLabel {Location = new Point(195, top), Text = "cnd", AutoSize = true},
\r
124 new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
126 foreach (var label in headings)
\r
129 label.BackColor = ColumnColors[1];
\r
131 for (var i = 0; i < _combinedLabels.Length; i++)
\r
133 var x = (parent.Width / 2) * (i / ShipInfo.MemberCount);
\r
134 var y = top + lh * ((i % ShipInfo.MemberCount) + 1);
\r
135 parent.Controls.AddRange(_combinedLabels[i] = new[]
\r
137 new ShipLabel {Location = new Point(x + 88, y), AutoSize = true, AnchorRight = true},
\r
140 Location = new Point(x + 86, y),
\r
141 Size = new Size(23, height),
\r
142 TextAlign = ContentAlignment.MiddleRight
\r
144 new ShipLabel {Location = new Point(x + 2, y), AutoSize = true}, // 名前のZ-orderを下に
\r
145 new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parent.Width / 2, lh - 1)}
\r
147 foreach (var label in _combinedLabels[i])
\r
150 label.PresetColor = label.BackColor = ColumnColors[i % 2];
\r
152 label.Click += onClick;
\r
155 parent.ResumeLayout();
\r
158 public void SetCombinedShipInfo(ShipStatus[] first, ShipStatus[] second)
\r
160 for (var i = 0; i < _combinedLabels.Length; i++)
\r
162 var idx = i % ShipInfo.MemberCount;
\r
163 var statuses = i < ShipInfo.MemberCount ? first : second;
\r
164 var labels = _combinedLabels[i];
\r
165 if (idx < statuses.Length)
\r
167 var s = statuses[idx];
\r
168 labels[0].SetHp(s);
\r
169 labels[1].SetCond(s);
\r
170 labels[2].SetName(s);
\r
174 labels[0].Text = labels[1].Text = "";
\r
175 labels[2].SetName("");
\r
176 labels[0].BackColor = labels[1].BackColor = labels[0].PresetColor;
\r
181 public void CreateAkashiTimers(Control parent)
\r
183 parent.SuspendLayout();
\r
184 for (var i = 0; i < _akashiTimers.Length; i++)
\r
187 var y = 3 + 16 * (i + 1);
\r
189 parent.Controls.Add(
\r
190 label = _akashiTimers[i] =
\r
193 Location = new Point(x, y),
\r
194 Size = new Size(34, 12),
\r
195 TextAlign = ContentAlignment.TopRight
\r
197 label.BackColor = ColumnColors[i % 2];
\r
199 foreach (var label in _akashiTimers)
\r
201 parent.ResumeLayout();
\r
204 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
207 for (var i = 0; i < timers.Length; i++)
\r
209 if (timers[i].Span <= TimeSpan.Zero)
\r
211 if (shortest == -1 || timers[i].Span < timers[shortest].Span)
\r
214 for (var i = 0; i < _akashiTimers.Length; i++)
\r
216 var label = _akashiTimers[i];
\r
217 var labelHp = _labels[i][0];
\r
218 var labelName = _labels[i][4];
\r
219 if (i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
221 label.Visible = false;
\r
222 labelHp.ForeColor = Control.DefaultForeColor;
\r
225 var timer = timers[i];
\r
226 var stat = statuses[i];
\r
227 label.Visible = true;
\r
228 label.Text = timer.Span.ToString(@"mm\:ss");
\r
229 label.ForeColor = Control.DefaultForeColor;
\r
230 labelName.SetShortName(stat);
\r
231 if (timer.Diff == 0)
\r
233 labelHp.ForeColor = Control.DefaultForeColor;
\r
237 label.ForeColor = Color.Red;
\r
238 labelHp.ForeColor = Color.DimGray;
\r
239 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
243 public void CreateDamagedShipList(Control parent, EventHandler onClick)
\r
245 parent.SuspendLayout();
\r
246 for (var i = 0; i < _damagedShipList.Length; i++)
\r
248 var y = 3 + i * 16;
\r
249 const int height = 12;
\r
250 parent.Controls.AddRange(_damagedShipList[i] = new[]
\r
252 new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},
\r
253 new ShipLabel {Location = new Point(119, y), Size = new Size(5, height - 1)},
\r
254 new ShipLabel {Location = new Point(75, y), AutoSize = true},
\r
255 new ShipLabel {Location = new Point(9, y), AutoSize = true},
\r
256 new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}
\r
258 foreach (var label in _damagedShipList[i])
\r
261 label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];
\r
262 label.Click += onClick;
\r
265 _panelDamagedShipList = parent;
\r
266 parent.ResumeLayout();
\r
269 public void SetDamagedShipList(ShipStatus[] list)
\r
271 const int fleet = 0, name = 3, time = 2, damage = 1;
\r
272 var parent = _panelDamagedShipList;
\r
273 var num = Min(list.Length, _damagedShipList.Length);
\r
276 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));
\r
277 var labels = _damagedShipList[0];
\r
278 labels[fleet].Text = "";
\r
279 labels[name].SetName("なし");
\r
280 labels[time].Text = "";
\r
281 labels[damage].BackColor = labels[damage].PresetColor;
\r
284 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * (num * 16 + 3)));
\r
285 for (var i = 0; i < num; i++)
\r
288 var labels = _damagedShipList[i];
\r
289 labels[fleet].SetFleet(s);
\r
290 labels[name].SetName(s);
\r
291 labels[time].SetRepairTime(s);
\r
292 labels[damage].BackColor = ShipLabel.DamageColor(s, labels[damage].PresetColor);
\r
296 public void CreateNDockLabels(Control parent, EventHandler onClick)
\r
298 for (var i = 0; i < _ndockLabels.Length; i++)
\r
300 var y = 3 + i * 15;
\r
301 parent.Controls.AddRange(
\r
302 _ndockLabels[i] = new[]
\r
306 Location = new Point(138, y),
\r
310 new ShipLabel {Location = new Point(29, y), AutoSize = true} // 名前のZ-orderを下に
\r
312 foreach (var label in _ndockLabels[i])
\r
315 label.Click += onClick;
\r
320 public void SetNDockLabels(NameAndTimer[] ndock)
\r
322 for (var i = 0; i < _ndockLabels.Length; i++)
\r
323 _ndockLabels[i][1].SetName(ndock[i].Name);
\r
326 public void SetNDockTimer(int dock, RingTimer timer, bool finishTime)
\r
328 var label = _ndockLabels[dock][0];
\r
329 label.ForeColor = timer.IsFinished ? Color.Red : Color.Black;
\r
330 label.Text = timer.ToString(finishTime);
\r
334 [System.ComponentModel.DesignerCategory("Code")]
\r
335 public class ShipLabel : Label
\r
337 public static SizeF ScaleFactor { get; set; }
\r
338 public Color PresetColor { get; set; }
\r
339 public bool AnchorRight { get; set; }
\r
340 private int _right = int.MinValue;
\r
343 public void SetShortName(ShipStatus status)
\r
345 SetName(status, true);
\r
348 public void SetName(ShipStatus status, bool shortName = false)
\r
350 var empty = status.Id != -1 && status.Slot.All(e => e.Id == -1) ? "▫" : "";
\r
351 var dc = status.PreparedDamageControl;
\r
352 var dcname = dc == 42 ? "[ダ]" : dc == 43 ? "[メ]" : "";
\r
353 var name = shortName ? status.Spec.ShortName : status.Name;
\r
354 SetName((status.Escaped ? "[避]" : dcname) + name + empty);
\r
357 public void SetName(string name)
\r
359 var lu = name != null && new Regex(@"^(?:\[.\])?\p{Lu}").IsMatch(name);
\r
360 var shift = (int)Round(ScaleFactor.Height);
\r
361 if (lu && Font.Equals(Parent.Font))
\r
363 Location += new Size(0, -shift);
\r
364 Font = new Font("Tahoma", 8f);
\r
366 else if (!lu && !Font.Equals(Parent.Font))
\r
368 Location += new Size(0, shift);
\r
369 Font = Parent.Font;
\r
374 public void SetHp(ShipStatus status)
\r
376 Text = $"{status.NowHp:D}/{status.MaxHp:D}";
\r
377 BackColor = DamageColor(status, PresetColor);
\r
380 public void SetHp(int now, int max)
\r
382 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
385 public static Color DamageColor(ShipStatus status, Color backcolor)
\r
387 switch (status.DamageLevel)
\r
389 case ShipStatus.Damage.Badly:
\r
391 case ShipStatus.Damage.Half:
\r
392 return Color.Orange;
\r
393 case ShipStatus.Damage.Small:
\r
394 return Color.FromArgb(240, 240, 0);
\r
400 public void SetCond(ShipStatus status)
\r
402 var cond = status.Cond;
\r
403 Text = cond.ToString("D");
\r
404 BackColor = cond >= 50
\r
408 : cond >= 20 ? Color.Orange : Color.Red;
\r
411 public void SetLevel(ShipStatus status)
\r
413 Text = status.Level.ToString("D");
\r
416 public void SetExpToNext(ShipStatus status)
\r
418 Text = status.ExpToNext.ToString("D");
\r
421 public void SetRepairTime(ShipStatus status)
\r
423 SetRepairTime(status.RepairTime);
\r
426 public void SetRepairTime(TimeSpan span)
\r
428 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
431 public void SetFleet(ShipStatus status)
\r
433 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
436 protected override void OnLayout(LayoutEventArgs levent)
\r
438 base.OnLayout(levent);
\r
441 if (_right == int.MinValue || _left != Left)
\r
447 if (_right == Right)
\r
449 _left -= Right - _right;
\r
450 Location = new Point(_left, Top);
\r
453 public void Scale()
\r
455 Scale(ScaleFactor);
\r