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
77 SetName(status.Escaped ? "[避]" : dcName, status.Name, empty, width);
\r
80 public void SetName(string name)
\r
82 SetName("", name, SlotStatus.Equipped);
\r
85 public void SetName(string name, ShipNameWidth width)
\r
87 SetName("", name, SlotStatus.Equipped, width);
\r
90 private void SetName(string prefix, string name, SlotStatus slotStatus, ShipNameWidth width = ShipNameWidth.Max)
\r
94 _slotStatus = slotStatus;
\r
95 var lu = new Regex(@"^\p{Lu}").IsMatch(name);
\r
96 var shift = (int)Round(ScaleFactor.Height);
\r
97 if (lu && Font.Equals(Parent.Font))
\r
99 Location += new Size(0, -shift);
\r
102 else if (!lu && !Font.Equals(Parent.Font))
\r
104 Location += new Size(0, shift);
\r
105 Font = Parent.Font;
\r
107 var result = prefix + name;
\r
108 var measured = TextRenderer.MeasureText(result, Font).Width;
\r
109 if (measured <= (int)width)
\r
112 Invalidate(); // 必ずOnPaintを実行させるため
\r
115 var truncated = "";
\r
116 foreach (var ch in name)
\r
118 var tmp = truncated + ch;
\r
119 if (TextRenderer.MeasureText(tmp, Font).Width > (int)width * ScaleFactor.Width)
\r
123 Text = prefix + truncated.TrimEnd(' ');
\r
127 public void SetHp(ShipStatus status)
\r
130 if (status == null)
\r
133 BackColor = PresetColor;
\r
137 ? $"{(int)Floor(status.NowHp * 100.0 / status.MaxHp):D}%"
\r
138 : $"{status.NowHp:D}/{status.MaxHp:D}";
\r
139 BackColor = DamageColor(status, PresetColor);
\r
142 public void ToggleHpPercent()
\r
144 _hpPercent = !_hpPercent;
\r
148 public void SetHp(int now, int max)
\r
150 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
153 public static Color DamageColor(ShipStatus status, Color backColor)
\r
155 switch (status.DamageLevel)
\r
157 case ShipStatus.Damage.Sunk:
\r
158 return Color.CornflowerBlue;
\r
159 case ShipStatus.Damage.Badly:
\r
160 return CUDColors.Red;
\r
161 case ShipStatus.Damage.Half:
\r
162 return CUDColors.Orange;
\r
163 case ShipStatus.Damage.Small:
\r
164 return Color.FromArgb(240, 240, 0);
\r
170 public void SetCond(ShipStatus status)
\r
172 if (status == null)
\r
175 BackColor = PresetColor;
\r
178 var cond = status.Cond;
\r
179 Text = cond.ToString("D");
\r
180 BackColor = cond >= 50
\r
189 public void SetLevel(ShipStatus status)
\r
191 Text = status?.Level.ToString("D");
\r
194 public void SetExpToNext(ShipStatus status)
\r
196 Text = status?.ExpToNext.ToString("D");
\r
199 public void SetRepairTime(ShipStatus status)
\r
201 if (status == null)
\r
206 SetRepairTime(status.RepairTime);
\r
209 public void SetRepairTime(TimeSpan span)
\r
211 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
214 public void SetFleet(ShipStatus status)
\r
216 Text = status?.Fleet == null ? "" : new[] {"1", "2", "3", "4"}[status.Fleet.Number];
\r
219 protected override void OnLayout(LayoutEventArgs args)
\r
221 base.OnLayout(args);
\r
224 if (_right == int.MinValue || _left != Left)
\r
230 if (_right == Right)
\r
232 _left -= Right - _right;
\r
233 Location = new Point(_left, Top);
\r
236 protected override void OnPaint(PaintEventArgs e)
\r
239 if ((_slotStatus & SlotStatus.NormalEmpty) != 0)
\r
241 e.Graphics.DrawRectangle(
\r
243 ClientSize.Width - 3 * ScaleFactor.Width, 1 * ScaleFactor.Height,
\r
244 2 * ScaleFactor.Width, 4 * ScaleFactor.Height);
\r
246 if ((_slotStatus & SlotStatus.ExtraEmpty) != 0)
\r
248 e.Graphics.DrawRectangle(
\r
250 ClientSize.Width - 3 * ScaleFactor.Width, 7 * ScaleFactor.Height,
\r
251 2 * ScaleFactor.Width, 3 * ScaleFactor.Height);
\r
255 public void Scale()
\r
257 Scale(ScaleFactor);
\r