OSDN Git Service

補強増設スロットが空かどうかわかるようにする
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ShipLabels.cs
index d833dac..34fed82 100644 (file)
@@ -13,7 +13,6 @@
 // limitations under the License.\r
 \r
 using System;\r
-using System.Collections.Generic;\r
 using System.Drawing;\r
 using System.Linq;\r
 using System.Text.RegularExpressions;\r
@@ -22,17 +21,30 @@ using static System.Math;
 \r
 namespace KancolleSniffer\r
 {\r
+    public enum ShipNameWidth\r
+    {\r
+        MainPanel = 93,\r
+        AkashiTimer = 53,\r
+        NDock = 69,\r
+        RepairList = NDock,\r
+        RepairListFull = 75,\r
+        ShipList = 82,\r
+        GroupConfig = 82,\r
+        Combined = 54,\r
+        Max = int.MaxValue\r
+    }\r
+\r
     public class ShipLabels\r
     {\r
         private readonly ShipLabel[][] _labels = new ShipLabel[ShipInfo.MemberCount][];\r
         private readonly ShipLabel[][] _combinedLabels = new ShipLabel[ShipInfo.MemberCount * 2][];\r
         private readonly ShipLabel[] _akashiTimers = new ShipLabel[ShipInfo.MemberCount];\r
-        private readonly ShipLabel[][] _damagedShipList = new ShipLabel[16][];\r
-        private Control _panelDamagedShipList;\r
+        private readonly ShipLabel[][] _repairList = new ShipLabel[16][];\r
+        private Control _panelRepairList;\r
         private readonly ShipLabel[][] _ndockLabels = new ShipLabel[DockInfo.DockCount][];\r
         public static Color[] ColumnColors = {SystemColors.Control, Color.FromArgb(255, 250, 250, 250)};\r
 \r
-        public void CreateLabels(Control parent, EventHandler onClick)\r
+        public void CreateShipLabels(Control parent, EventHandler onClick)\r
         {\r
             parent.SuspendLayout();\r
             const int top = 3, height = 12, lh = 16;\r
@@ -88,7 +100,7 @@ namespace KancolleSniffer
             parent.ResumeLayout();\r
         }\r
 \r
-        public void SetShipInfo(ShipStatus[] statuses)\r
+        public void SetShipLabels(ShipStatus[] statuses)\r
         {\r
             for (var i = 0; i < _labels.Length; i++)\r
             {\r
@@ -100,7 +112,7 @@ namespace KancolleSniffer
                     labels[1].SetCond(s);\r
                     labels[2].SetLevel(s);\r
                     labels[3].SetExpToNext(s);\r
-                    labels[4].SetName(s);\r
+                    labels[4].SetName(s, ShipNameWidth.MainPanel);\r
                 }\r
                 else\r
                 {\r
@@ -115,6 +127,7 @@ namespace KancolleSniffer
         {\r
             parent.SuspendLayout();\r
             const int top = 3, height = 12, lh = 16;\r
+            const int parentWidth = 220; // parent.Widthを使うとDPIスケーリング時に計算がくるうので\r
             ShipLabel[] headings;\r
             parent.Controls.AddRange(headings = new[]\r
             {\r
@@ -122,7 +135,7 @@ namespace KancolleSniffer
                 new ShipLabel {Location = new Point(86, top), Text = "cnd", AutoSize = true},\r
                 new ShipLabel {Location = new Point(177, top), Text = "HP", AutoSize = true},\r
                 new ShipLabel {Location = new Point(195, top), Text = "cnd", AutoSize = true},\r
-                new ShipLabel {Location = new Point(0, 1), Size = new Size(parent.Width, lh - 1)}\r
+                new ShipLabel {Location = new Point(0, 1), Size = new Size(parentWidth, lh - 1)}\r
             });\r
             foreach (var label in headings)\r
             {\r
@@ -131,8 +144,8 @@ namespace KancolleSniffer
             }\r
             for (var i = 0; i < _combinedLabels.Length; i++)\r
             {\r
-                var x = (parent.Width / 2) * (i / ShipInfo.MemberCount);\r
-                var y = top + lh * ((i % ShipInfo.MemberCount) + 1);\r
+                var x = parentWidth / 2 * (i / ShipInfo.MemberCount);\r
+                var y = top + lh * (i % ShipInfo.MemberCount + 1);\r
                 parent.Controls.AddRange(_combinedLabels[i] = new[]\r
                 {\r
                     new ShipLabel {Location = new Point(x + 88, y), AutoSize = true, AnchorRight = true},\r
@@ -143,7 +156,7 @@ namespace KancolleSniffer
                         TextAlign = ContentAlignment.MiddleRight\r
                     },\r
                     new ShipLabel {Location = new Point(x + 2, y), AutoSize = true}, // 名前のZ-orderを下に\r
-                    new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parent.Width / 2, lh - 1)}\r
+                    new ShipLabel {Location = new Point(x, y - 2), Size = new Size(parentWidth / 2, lh - 1)}\r
                 });\r
                 foreach (var label in _combinedLabels[i])\r
                 {\r
@@ -156,7 +169,7 @@ namespace KancolleSniffer
             parent.ResumeLayout();\r
         }\r
 \r
-        public void SetCombinedShipInfo(ShipStatus[] first, ShipStatus[] second)\r
+        public void SetCombinedShipLabels(ShipStatus[] first, ShipStatus[] second)\r
         {\r
             for (var i = 0; i < _combinedLabels.Length; i++)\r
             {\r
@@ -168,7 +181,7 @@ namespace KancolleSniffer
                     var s = statuses[idx];\r
                     labels[0].SetHp(s);\r
                     labels[1].SetCond(s);\r
-                    labels[2].SetName(s);\r
+                    labels[2].SetName(s, ShipNameWidth.Combined);\r
                 }\r
                 else\r
                 {\r
@@ -228,7 +241,7 @@ namespace KancolleSniffer
                 label.Visible = true;\r
                 label.Text = timer.Span.ToString(@"mm\:ss");\r
                 label.ForeColor = Control.DefaultForeColor;\r
-                labelName.SetName(stat, _shortNameDict);\r
+                labelName.SetName(stat, ShipNameWidth.AkashiTimer);\r
                 if (timer.Diff == 0)\r
                 {\r
                     labelHp.ForeColor = Control.DefaultForeColor;\r
@@ -241,34 +254,14 @@ namespace KancolleSniffer
             }\r
         }\r
 \r
-        private readonly Dictionary<string, string> _shortNameDict = new Dictionary<string, string>\r
-        {\r
-            {"千代田航改", "千代田航"},\r
-            {"千代田航改二", "千代田航"},\r
-            {"千歳航改二", "千歳航改"},\r
-            {"五十鈴改二", "五十鈴改"},\r
-            {"あきつ丸改", "あきつ丸"},\r
-            {"Bismarck改", "Bismarck"},\r
-            {"Bismarck twei", "Bismarck"},\r
-            {"Bismarck drei", "Bismarck"},\r
-            {"Prinz Eugen", "Prinz Eug"},\r
-            {"Prinz Eugen改", "Prinz Eug"},\r
-            {"Graf Zeppelin", "Graf Zep"},\r
-            {"Graf Zeppelin改", "Graf Zep"},\r
-            {"Libeccio改", "Libeccio"},\r
-            {"阿武隈改二", "阿武隈改"},\r
-            {"瑞鶴改二甲", "瑞鶴改二"},\r
-            {"翔鶴改二甲", "翔鶴改二"},\r
-        };\r
-\r
-        public void CreateDamagedShipList(Control parent, EventHandler onClick)\r
+        public void CreateRepairList(Control parent, EventHandler onClick)\r
         {\r
             parent.SuspendLayout();\r
-            for (var i = 0; i < _damagedShipList.Length; i++)\r
+            for (var i = 0; i < _repairList.Length; i++)\r
             {\r
                 var y = 3 + i * 16;\r
                 const int height = 12;\r
-                parent.Controls.AddRange(_damagedShipList[i] = new[]\r
+                parent.Controls.AddRange(_repairList[i] = new[]\r
                 {\r
                     new ShipLabel {Location = new Point(0, y), Size = new Size(11, height)},\r
                     new ShipLabel {Location = new Point(119, y), Size = new Size(5, height - 1)},\r
@@ -276,26 +269,26 @@ namespace KancolleSniffer
                     new ShipLabel {Location = new Point(9, y), AutoSize = true},\r
                     new ShipLabel {Location = new Point(0, y - 2), Size = new Size(parent.Width, height + 3)}\r
                 });\r
-                foreach (var label in _damagedShipList[i])\r
+                foreach (var label in _repairList[i])\r
                 {\r
                     label.Scale();\r
                     label.PresetColor = label.BackColor = ColumnColors[(i + 1) % 2];\r
                     label.Click += onClick;\r
                 }\r
             }\r
-            _panelDamagedShipList = parent;\r
+            _panelRepairList = parent;\r
             parent.ResumeLayout();\r
         }\r
 \r
-        public void SetDamagedShipList(ShipStatus[] list)\r
+        public void SetRepairList(ShipStatus[] list)\r
         {\r
             const int fleet = 0, name = 3, time = 2, damage = 1;\r
-            var parent = _panelDamagedShipList;\r
-            var num = Min(list.Length, _damagedShipList.Length);\r
+            var parent = _panelRepairList;\r
+            var num = Min(list.Length, _repairList.Length);\r
             if (num == 0)\r
             {\r
                 parent.Size = new Size(parent.Width, (int)Round(ShipLabel.ScaleFactor.Height * 19));\r
-                var labels = _damagedShipList[0];\r
+                var labels = _repairList[0];\r
                 labels[fleet].Text = "";\r
                 labels[name].SetName("なし");\r
                 labels[time].Text = "";\r
@@ -306,13 +299,9 @@ namespace KancolleSniffer
             for (var i = 0; i < num; i++)\r
             {\r
                 var s = list[i];\r
-                var labels = _damagedShipList[i];\r
+                var labels = _repairList[i];\r
                 labels[fleet].SetFleet(s);\r
-                labels[name].SetName(s, new Dictionary<string, string>\r
-                {\r
-                    {"Graf Zeppelin", "Graf Zeppeli"},\r
-                    {"Graf Zeppelin改", "Graf Zeppeli"},\r
-                });\r
+                labels[name].SetName(s, ShipNameWidth.RepairList);\r
                 labels[time].SetRepairTime(s);\r
                 labels[damage].BackColor = ShipLabel.DamageColor(s, labels[damage].PresetColor);\r
             }\r
@@ -344,16 +333,8 @@ namespace KancolleSniffer
 \r
         public void SetNDockLabels(NameAndTimer[] ndock)\r
         {\r
-            var dict = new Dictionary<string, string>\r
-            {\r
-                {"Graf Zeppelin", "Graf Zeppeli"},\r
-                {"Graf Zeppelin改", "Graf Zeppeli"},\r
-            };\r
             for (var i = 0; i < _ndockLabels.Length; i++)\r
-            {\r
-                string name;\r
-                _ndockLabels[i][1].SetName(dict.TryGetValue(ndock[i].Name, out name) ? name : ndock[i].Name);\r
-            }\r
+                _ndockLabels[i][1].SetName(ndock[i].Name, ShipNameWidth.NDock);\r
         }\r
 \r
         public void SetNDockTimer(int dock, RingTimer timer, bool finishTime)\r
@@ -368,37 +349,86 @@ namespace KancolleSniffer
     public class ShipLabel : Label\r
     {\r
         public static SizeF ScaleFactor { get; set; }\r
+        public static Font LatinFont { get; set; } = new Font("Tahoma", 8f);\r
         public Color PresetColor { get; set; }\r
         public bool AnchorRight { get; set; }\r
         private int _right = int.MinValue;\r
         private int _left;\r
+        private SlotStatus _slotStatus;\r
 \r
-        public void SetName(ShipStatus status, Dictionary<string, string> convDict = null)\r
+        [Flags]\r
+        private enum SlotStatus\r
         {\r
-            string name;\r
-            if (convDict == null || !convDict.TryGetValue(status.Name, out name))\r
-                name = status.Name;\r
-            var empty = status.Id != -1 && status.Slot.All(e => e.Id == -1) ? "▫" : "";\r
+            Equipped = 0,\r
+            NormalEmpty = 1,\r
+            ExtraEmpty = 2\r
+        }\r
+\r
+        public ShipLabel()\r
+        {\r
+            UseMnemonic = false;\r
+        }\r
+\r
+        public void SetName(ShipStatus status, ShipNameWidth width = ShipNameWidth.Max)\r
+        {\r
+            SlotStatus empty = SlotStatus.Equipped;\r
+            if (status.Id != -1)\r
+            {\r
+                if (status.Slot.All(item => item.Id == -1))\r
+                    empty |= SlotStatus.NormalEmpty;\r
+                if (status.SlotEx.Id == -1)\r
+                    empty |= SlotStatus.ExtraEmpty;\r
+            }\r
             var dc = status.PreparedDamageControl;\r
             var dcname = dc == 42 ? "[ダ]" : dc == 43 ? "[メ]" : "";\r
-            SetName((status.Escaped ? "[避]" : dcname) + name + empty);\r
+            SetName((status.Escaped ? "[避]" : dcname), status.Name, empty, width);\r
         }\r
 \r
         public void SetName(string name)\r
         {\r
-            var lu = name != null && new Regex(@"^(?:\[.\])?\p{Lu}").IsMatch(name);\r
+            SetName("", name, SlotStatus.Equipped);\r
+        }\r
+\r
+        public void SetName(string name, ShipNameWidth width)\r
+        {\r
+            SetName("", name, SlotStatus.Equipped, width);\r
+        }\r
+\r
+        private void SetName(string prefix, string name, SlotStatus slotStatus, ShipNameWidth width = ShipNameWidth.Max)\r
+        {\r
+            if (name == null)\r
+                return;\r
+            _slotStatus = slotStatus;\r
+            var lu = new Regex(@"^\p{Lu}").IsMatch(name);\r
             var shift = (int)Round(ScaleFactor.Height);\r
             if (lu && Font.Equals(Parent.Font))\r
             {\r
                 Location += new Size(0, -shift);\r
-                Font = new Font("Tahoma", 8f);\r
+                Font = LatinFont;\r
             }\r
             else if (!lu && !Font.Equals(Parent.Font))\r
             {\r
                 Location += new Size(0, shift);\r
                 Font = Parent.Font;\r
             }\r
-            Text = name;\r
+            var result = prefix + name;\r
+            var measured = TextRenderer.MeasureText(result, Font).Width;\r
+            if (measured <= (int)width)\r
+            {\r
+                Text = result;\r
+                Invalidate(); // 必ずOnPaintを実行させるため\r
+                return;\r
+            }\r
+            var truncated = "";\r
+            foreach (var ch in name)\r
+            {\r
+                var tmp = truncated + ch;\r
+                if (TextRenderer.MeasureText(tmp, Font).Width > (int)width)\r
+                    break;\r
+                truncated = tmp;\r
+            }\r
+            Text = prefix + truncated.TrimEnd(' ');\r
+            Invalidate();\r
         }\r
 \r
         public void SetHp(ShipStatus status)\r
@@ -480,6 +510,25 @@ namespace KancolleSniffer
             Location = new Point(_left, Top);\r
         }\r
 \r
+        protected override void OnPaint(PaintEventArgs e)\r
+        {\r
+            base.OnPaint(e);\r
+            if ((_slotStatus & SlotStatus.NormalEmpty) != 0)\r
+            {\r
+                e.Graphics.DrawRectangle(\r
+                    Pens.Black,\r
+                    ClientSize.Width - 3 * ScaleFactor.Width, 1 * ScaleFactor.Height,\r
+                    2 * ScaleFactor.Width, 4 * ScaleFactor.Height);\r
+            }\r
+            if ((_slotStatus & SlotStatus.ExtraEmpty) != 0)\r
+            {\r
+                e.Graphics.DrawRectangle(\r
+                    Pens.Black,\r
+                    ClientSize.Width - 3 * ScaleFactor.Width, 7 * ScaleFactor.Height,\r
+                    2 * ScaleFactor.Width, 3 * ScaleFactor.Height);\r
+            }\r
+        }\r
+\r
         public void Scale()\r
         {\r
             Scale(ScaleFactor);\r