1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>
\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
23 namespace KancolleSniffer
\r
25 public class ShipInfoLabels
\r
27 private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];
\r
28 private readonly Label[] _akashiTimers = new Label[ShipInfo.MemberCount];
\r
29 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
31 public ShipInfoLabels(Control parent, EventHandler onClick)
\r
33 CreateLabels(parent, onClick);
\r
34 CreateAkashiTimers(parent);
\r
37 private void CreateLabels(Control parent, EventHandler onClick)
\r
39 parent.SuspendLayout();
\r
40 const int top = 3, height = 12, lh = 16;
\r
41 Control[] headings =
\r
43 new Label {Location = new Point(109, top), Text = "HP", AutoSize = true},
\r
44 new Label {Location = new Point(128, top), Text = "cond", AutoSize = true},
\r
45 new Label {Location = new Point(163, top), Text = "Lv", AutoSize = true},
\r
46 new Label {Location = new Point(194, top), Text = "Exp", AutoSize = true},
\r
47 new Label {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}
\r
49 foreach (var label in headings)
\r
51 label.Scale(ShipLabel.ScaleFactor);
\r
52 label.BackColor = ColumnColors[1];
\r
54 parent.Controls.AddRange(headings);
\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
84 label.Scale(ShipLabel.ScaleFactor);
\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 private void CreateAkashiTimers(Control parent)
\r
110 parent.SuspendLayout();
\r
111 for (var i = 0; i < _akashiTimers.Length; i++)
\r
114 var y = 20 + 16 * i;
\r
116 parent.Controls.Add(
\r
117 label = _akashiTimers[i] = new Label {Location = new Point(x, y), AutoSize = true, Visible = false});
\r
118 label.BackColor = ColumnColors[i % 2];
\r
120 foreach (var label in _akashiTimers)
\r
121 label.Scale(ShipLabel.ScaleFactor);
\r
122 parent.ResumeLayout();
\r
125 public void SetAkashiTimer(ShipStatus[] statuses, AkashiTimer.RepairSpan[] timers)
\r
127 for (var i = 0; i < _akashiTimers.Length; i++)
\r
129 var label = _akashiTimers[i];
\r
130 var labelHp = _labels[i][0];
\r
131 if (timers == null || i >= timers.Length || timers[i].Span == TimeSpan.MinValue)
\r
133 label.Visible = false;
\r
134 labelHp.ForeColor = Control.DefaultForeColor;
\r
137 var timer = timers[i];
\r
138 var stat = statuses[i];
\r
139 label.Visible = true;
\r
140 label.BringToFront();
\r
141 label.Text = timer.Span.ToString(@"mm\:ss");
\r
142 if (timer.Diff == 0)
\r
144 labelHp.ForeColor = Control.DefaultForeColor;
\r
147 labelHp.ForeColor = Color.DimGray;
\r
148 labelHp.SetHp(stat.NowHp + timer.Diff, stat.MaxHp);
\r
153 [System.ComponentModel.DesignerCategory("Code")]
\r
154 public class ShipLabel : Label
\r
156 public static SizeF ScaleFactor { get; set; }
\r
157 public Color PresetColor { get; set; }
\r
158 public bool AnchorRight { get; set; }
\r
159 private int _right = int.MinValue;
\r
162 public void SetName(ShipStatus status)
\r
164 SetName(status.Name);
\r
167 public void SetName(string name)
\r
169 var lu = name != null && new Regex(@"^\p{Lu}").IsMatch(name);
\r
170 var shift = (int)Math.Round(ScaleFactor.Height);
\r
171 if (lu && Font.Equals(Parent.Font))
\r
173 Location += new Size(0, -shift);
\r
174 Font = new Font("Tahoma", 8f);
\r
176 else if (!lu && !Font.Equals(Parent.Font))
\r
178 Location += new Size(0, shift);
\r
179 Font = Parent.Font;
\r
184 public void SetHp(ShipStatus status)
\r
186 SetHp(status.NowHp, status.MaxHp);
\r
189 public void SetHp(int now, int max)
\r
191 var colors = new[] {PresetColor, Color.FromArgb(255, 240, 240, 100), Color.Orange, Color.Red};
\r
192 Text = string.Format("{0:D}/{1:D}", now, max);
\r
193 BackColor = colors[(int)ShipStatus.CalcDamage(now, max)];
\r
196 public void SetCond(ShipStatus status)
\r
198 if (status.Level == 0)
\r
201 BackColor = PresetColor;
\r
204 var cond = status.Cond;
\r
205 Text = cond.ToString("D");
\r
206 BackColor = cond >= 50
\r
210 : cond >= 20 ? Color.Orange : Color.Red;
\r
213 public void SetLevel(ShipStatus status)
\r
215 Text = status.Level.ToString("D");
\r
218 public void SetExpToNext(ShipStatus status)
\r
220 Text = status.ExpToNext.ToString("D");
\r
223 public void SetRepairTime(ShipStatus status)
\r
225 var t = status.RepairTime;
\r
226 Text = string.Format(@"{0:d2}:{1:mm\:ss}", (int)t.TotalHours, t);
\r
229 public void SetFleet(ShipStatus status)
\r
231 Text = new[] {"", "1", "2", "3", "4"}[status.Fleet + 1];
\r
234 protected override void OnLayout(LayoutEventArgs levent)
\r
236 base.OnLayout(levent);
\r
239 if (_right == int.MinValue || _left != Left)
\r
245 if (_right == Right)
\r
247 _left -= Right - _right;
\r
248 Location = new Point(_left, Top);
\r