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.ComponentModel;
\r
17 using System.Drawing;
\r
19 using System.Text.RegularExpressions;
\r
20 using System.Windows.Forms;
\r
21 using KancolleSniffer.Model;
\r
22 using static System.Math;
\r
24 namespace KancolleSniffer.View
\r
26 [DesignerCategory("Code")]
\r
27 public class ShipLabel : Label
\r
29 public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};
\r
30 public static SizeF ScaleFactor { get; set; }
\r
31 public static Font LatinFont { get; set; } = new Font("Tahoma", 8f);
\r
32 public Color PresetColor { get; set; }
\r
33 public bool AnchorRight { get; set; }
\r
34 private int _right = Int32.MinValue;
\r
36 private SlotStatus _slotStatus;
\r
37 private ShipStatus _status;
\r
38 private bool _hpPercent;
\r
40 public override Color BackColor
\r
42 get => base.BackColor;
\r
43 set => base.BackColor = value == DefaultBackColor ? PresetColor : value;
\r
47 private enum SlotStatus
\r
56 UseMnemonic = false;
\r
59 public void SetName(ShipStatus status, ShipNameWidth width = ShipNameWidth.Max)
\r
66 var empty = SlotStatus.Equipped;
\r
69 if (status.Slot.All(item => item.Empty))
\r
70 empty |= SlotStatus.NormalEmpty;
\r
71 if (status.SlotEx.Empty)
\r
72 empty |= SlotStatus.ExtraEmpty;
\r
74 var dc = status.PreparedDamageControl;
\r
75 var dcName = dc == 42 ? "[ダ]" :
\r
76 dc == 43 ? "[メ]" : "";
\r
78 switch (status.SpecialAttack)
\r
80 case ShipStatus.Attack.Fire:
\r
83 case ShipStatus.Attack.Fired:
\r
87 SetName((status.Escaped ? "[避]" : dcName) + sp, status.Name, empty, width);
\r
90 public void SetName(string name)
\r
92 SetName("", name, SlotStatus.Equipped);
\r
95 public void SetName(string name, ShipNameWidth width)
\r
97 SetName("", name, SlotStatus.Equipped, width);
\r
100 private void SetName(string prefix, string name, SlotStatus slotStatus, ShipNameWidth width = ShipNameWidth.Max)
\r
104 _slotStatus = slotStatus;
\r
105 var lu = new Regex(@"^\p{Lu}").IsMatch(name);
\r
106 var shift = (int)Round(ScaleFactor.Height);
\r
107 if (lu && Font.Equals(Parent.Font))
\r
109 Location += new Size(0, -shift);
\r
112 else if (!lu && !Font.Equals(Parent.Font))
\r
114 Location += new Size(0, shift);
\r
115 Font = Parent.Font;
\r
117 var result = prefix + name;
\r
118 var measured = TextRenderer.MeasureText(result, Font).Width;
\r
119 if (measured <= (int)width)
\r
122 Invalidate(); // 必ずOnPaintを実行させるため
\r
125 var truncated = "";
\r
126 foreach (var ch in name)
\r
128 var tmp = truncated + ch;
\r
129 if (TextRenderer.MeasureText(tmp, Font).Width > (int)width * ScaleFactor.Width)
\r
133 Text = prefix + truncated.TrimEnd(' ');
\r
137 public void SetHp(ShipStatus status)
\r
140 if (status == null)
\r
143 BackColor = PresetColor;
\r
147 ? $"{(int)Floor(status.NowHp * 100.0 / status.MaxHp):D}%"
\r
148 : $"{status.NowHp:D}/{status.MaxHp:D}";
\r
149 BackColor = DamageColor(status, PresetColor);
\r
152 public void ToggleHpPercent()
\r
154 _hpPercent = !_hpPercent;
\r
158 public void SetHp(int now, int max)
\r
160 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
163 public static Color DamageColor(ShipStatus status, Color backColor)
\r
165 switch (status.DamageLevel)
\r
167 case ShipStatus.Damage.Sunk:
\r
168 return Color.CornflowerBlue;
\r
169 case ShipStatus.Damage.Badly:
\r
170 return CUDColors.Red;
\r
171 case ShipStatus.Damage.Half:
\r
172 return CUDColors.Orange;
\r
173 case ShipStatus.Damage.Small:
\r
174 return Color.FromArgb(240, 240, 0);
\r
180 public void SetCond(ShipStatus status)
\r
182 if (status == null)
\r
185 BackColor = PresetColor;
\r
188 var cond = status.Cond;
\r
189 Text = cond.ToString("D");
\r
190 BackColor = cond >= 50
\r
199 public void SetLevel(ShipStatus status)
\r
201 Text = status?.Level.ToString("D");
\r
204 public void SetExpToNext(ShipStatus status)
\r
206 Text = status?.ExpToNext.ToString("D");
\r
209 public void SetRepairTime(ShipStatus status)
\r
211 if (status == null)
\r
216 SetRepairTime(status.RepairTime);
\r
219 public void SetRepairTime(TimeSpan span)
\r
221 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
224 public void SetFleet(ShipStatus status)
\r
226 Text = status?.Fleet == null ? "" : new[] {"1", "2", "3", "4"}[status.Fleet.Number];
\r
229 protected override void OnSizeChanged(EventArgs args)
\r
231 base.OnSizeChanged(args);
\r
235 protected override void OnLayout(LayoutEventArgs args)
\r
237 base.OnLayout(args);
\r
241 private void KeepAnchorRight()
\r
245 if (_right == int.MinValue || _left != Left)
\r
251 if (_right == Right)
\r
253 _left -= Right - _right;
\r
254 Location = new Point(_left, Top);
\r
257 protected override void OnPaint(PaintEventArgs e)
\r
260 if ((_slotStatus & SlotStatus.NormalEmpty) != 0)
\r
262 e.Graphics.DrawRectangle(
\r
264 ClientSize.Width - 3 * ScaleFactor.Width, 1 * ScaleFactor.Height,
\r
265 2 * ScaleFactor.Width, 4 * ScaleFactor.Height);
\r
267 if ((_slotStatus & SlotStatus.ExtraEmpty) != 0)
\r
269 e.Graphics.DrawRectangle(
\r
271 ClientSize.Width - 3 * ScaleFactor.Width, 7 * ScaleFactor.Height,
\r
272 2 * ScaleFactor.Width, 3 * ScaleFactor.Height);
\r
276 public void Scale()
\r
278 Scale(ScaleFactor);
\r