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.White};
\r
30 public static Font LatinFont { get; set; } = new Font("Tahoma", 8f);
\r
31 public bool AnchorRight { get; set; }
\r
32 private Color _initialBackColor;
\r
33 private int _right = Int32.MinValue;
\r
35 private SlotStatus _slotStatus;
\r
36 private ShipStatus _status;
\r
37 private bool _hpPercent;
\r
38 private Font _strongFont;
\r
39 private ShipLabel _hpStrongLabel;
\r
41 private Font BaseFont => Parent.Font;
\r
43 private Font StrongFont => _strongFont ?? (_strongFont = new Font("Leelawadee", BaseFont.Size));
\r
45 public override Color BackColor
\r
47 get => base.BackColor;
\r
50 if (_initialBackColor == Color.Empty)
\r
51 _initialBackColor = value;
\r
52 base.BackColor = value;
\r
57 private enum SlotStatus
\r
67 UseMnemonic = false;
\r
70 public void SetName(ShipStatus status, ShipNameWidth width = ShipNameWidth.Max)
\r
77 var empty = SlotStatus.Equipped;
\r
80 var slots = status.Slot.Take(status.Spec.SlotNum).ToArray();
\r
81 if (slots.Any(item => item.Empty))
\r
82 empty |= slots.All(item => item.Empty) ? SlotStatus.NormalEmpty : SlotStatus.SemiEquipped;
\r
83 if (status.SlotEx.Empty)
\r
84 empty |= SlotStatus.ExtraEmpty;
\r
86 var dc = status.PreparedDamageControl;
\r
87 var dcName = dc == 42 ? "[ダ]" :
\r
88 dc == 43 ? "[メ]" : "";
\r
90 switch (status.SpecialAttack)
\r
92 case ShipStatus.Attack.Fire:
\r
95 case ShipStatus.Attack.Fired:
\r
99 SetName((status.Escaped ? "[避]" : dcName) + sp, status.Name, empty, width);
\r
102 public void SetName(string name)
\r
104 SetName("", name, SlotStatus.Equipped);
\r
107 public void SetName(string name, ShipNameWidth width)
\r
109 SetName("", name, SlotStatus.Equipped, width);
\r
112 private void SetName(string prefix, string name, SlotStatus slotStatus, ShipNameWidth width = ShipNameWidth.Max)
\r
116 _slotStatus = slotStatus;
\r
117 var lu = new Regex(@"^\p{Lu}").IsMatch(name);
\r
118 var shift = Scaler.ScaleHeight(1);
\r
119 if (lu && Font.Equals(BaseFont))
\r
121 Location += new Size(0, -shift);
\r
124 else if (!lu && !Font.Equals(BaseFont))
\r
126 Location += new Size(0, shift);
\r
129 var result = prefix + name;
\r
130 var measured = TextRenderer.MeasureText(result, Font).Width;
\r
131 if (measured <= (int)width)
\r
134 Invalidate(); // 必ずOnPaintを実行させるため
\r
137 var truncated = "";
\r
138 foreach (var ch in name)
\r
140 var tmp = truncated + ch;
\r
141 if (TextRenderer.MeasureText(tmp, Font).Width > Scaler.ScaleWidth((float)width))
\r
145 Text = prefix + truncated.TrimEnd(' ');
\r
149 public void SetHp(ShipStatus status)
\r
152 if (_hpStrongLabel != null)
\r
153 _hpStrongLabel.Text = "";
\r
155 if (status == null)
\r
158 BackColor = _initialBackColor;
\r
163 var percent = $"{(int)Floor(status.NowHp * 100.0 / status.MaxHp):D}";
\r
164 if (status.DamageLevel == ShipStatus.Damage.Badly)
\r
167 if (_hpStrongLabel == null)
\r
168 CreateHpStrongLabel();
\r
169 _hpStrongLabel.Text = percent;
\r
173 Text = percent + "%";
\r
178 Text = $"{status.NowHp:D}/{status.MaxHp:D}";
\r
179 if (status.DamageLevel == ShipStatus.Damage.Badly)
\r
182 BackColor = DamageColor(status);
\r
185 private void CreateHpStrongLabel()
\r
187 _hpStrongLabel = new ShipLabel
\r
190 BackColor = CUDColors.Red,
\r
191 Location = Scaler.Move(Left, Top, 4, 0),
\r
193 MinimumSize = new Size(0, Height),
\r
194 AnchorRight = true,
\r
195 TextAlign = ContentAlignment.MiddleLeft,
\r
196 Cursor = Cursors.Hand
\r
198 _hpStrongLabel.DoubleClick += (sender, e) => { OnDoubleClick(e); };
\r
199 Parent.Controls.Add(_hpStrongLabel);
\r
200 var index = Parent.Controls.GetChildIndex(this);
\r
201 Parent.Controls.SetChildIndex(_hpStrongLabel, index + 1);
\r
205 public void ToggleHpPercent()
\r
207 _hpPercent = !_hpPercent;
\r
211 public void SetHp(int now, int max)
\r
213 SetHp(new ShipStatus {NowHp = now, MaxHp = max});
\r
216 public Color DamageColor(ShipStatus status)
\r
218 switch (status.DamageLevel)
\r
220 case ShipStatus.Damage.Sunk:
\r
221 return Color.CornflowerBlue;
\r
222 case ShipStatus.Damage.Badly:
\r
223 return CUDColors.Red;
\r
224 case ShipStatus.Damage.Half:
\r
225 return CUDColors.Orange;
\r
226 case ShipStatus.Damage.Small:
\r
227 return Color.FromArgb(240, 240, 0);
\r
229 return _initialBackColor;
\r
233 public void SetCond(ShipStatus status)
\r
235 if (status == null)
\r
238 BackColor = _initialBackColor;
\r
241 var cond = status.Cond;
\r
242 Text = cond.ToString("D");
\r
243 BackColor = cond >= 50
\r
246 ? _initialBackColor
\r
252 public void SetLevel(ShipStatus status)
\r
254 Text = status?.Level.ToString("D");
\r
257 public void SetExpToNext(ShipStatus status)
\r
259 Text = status?.ExpToNext.ToString("D");
\r
262 public void SetRepairTime(ShipStatus status)
\r
264 if (status == null)
\r
269 SetRepairTime(status.RepairTime);
\r
272 public void SetRepairTime(TimeSpan span)
\r
274 Text = $@"{(int)span.TotalHours:d2}:{span:mm\:ss}";
\r
277 public void SetFleet(ShipStatus status)
\r
279 Text = status?.Fleet == null ? "" : new[] {"1", "2", "3", "4"}[status.Fleet.Number];
\r
282 protected override void OnSizeChanged(EventArgs args)
\r
284 base.OnSizeChanged(args);
\r
288 protected override void OnLayout(LayoutEventArgs args)
\r
290 base.OnLayout(args);
\r
294 private void KeepAnchorRight()
\r
298 if (_right == int.MinValue || _left != Left)
\r
304 if (_right == Right)
\r
306 _left -= Right - _right;
\r
307 Location = new Point(_left, Top);
\r
310 protected override void OnPaint(PaintEventArgs e)
\r
313 if ((_slotStatus & SlotStatus.NormalEmpty) != 0)
\r
315 e.Graphics.DrawRectangle(Pens.Black,
\r
316 new Rectangle(Scaler.Move(ClientSize.Width, 0, -3, 0), Scaler.Scale(2, 5)));
\r
318 else if ((_slotStatus & SlotStatus.SemiEquipped) != 0)
\r
320 e.Graphics.DrawLine(Pens.Black,
\r
321 Scaler.Move(ClientSize.Width, 0, -1, 0),
\r
322 Scaler.Move(ClientSize.Width, 0, -1, 5));
\r
324 if ((_slotStatus & SlotStatus.ExtraEmpty) != 0)
\r
326 e.Graphics.DrawRectangle(Pens.Black,
\r
327 new Rectangle(Scaler.Move(ClientSize.Width, 0, -3, 8), Scaler.Scale(2, 3)));
\r