OSDN Git Service

文字列を表示幅で切る処理をStringTruncatorクラスに分離する
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 10 Jul 2019 12:52:09 +0000 (21:52 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 10 Jul 2019 12:54:02 +0000 (21:54 +0900)
KancolleSniffer.Test/FleetSpecTest.cs
KancolleSniffer.sln.DotSettings
KancolleSniffer/KancolleSniffer.csproj
KancolleSniffer/View/FleetSpec.cs
KancolleSniffer/View/ShipLabel.cs
KancolleSniffer/View/StringTruncator.cs [new file with mode: 0644]

index c50eab3..5e10ab9 100644 (file)
@@ -14,6 +14,7 @@
 \r
 using System.Drawing;\r
 using ExpressionToCodeLib;\r
+using KancolleSniffer.Model;\r
 using KancolleSniffer.View;\r
 using Microsoft.VisualStudio.TestTools.UnitTesting;\r
 \r
@@ -31,7 +32,7 @@ namespace KancolleSniffer.Test
         public void ExchangeFleetMember()\r
         {\r
             var sniffer = new Sniffer();\r
-            var expected = new FleetSpec.Record()\r
+            var expected = new FleetSpec.Record\r
             {\r
                 AircraftSpec = "",\r
                 Color = SystemColors.Control,\r
@@ -78,5 +79,32 @@ namespace KancolleSniffer.Test
             var table = FleetSpec.Create(sniffer);\r
             PAssert.That(() => table[0].Fleet == "第一 高速+" && table[37].Fleet == "第二 高速");\r
         }\r
+\r
+        [TestMethod]\r
+        public void ItemString()\r
+        {\r
+            var item = new ItemStatus\r
+            {\r
+                Spec = new ItemSpec\r
+                {\r
+                    Name = "大発動艇(八九式中戦車&陸戦隊)"\r
+                }\r
+            };\r
+            Assert.AreEqual(item.Spec.Name, FleetSpec.Record.CreateItemRecord(item, 7, 7).Equip);\r
+            item.Level = 10;\r
+            Assert.AreEqual("大発動艇(八九式中戦車&陸戦★10", FleetSpec.Record.CreateItemRecord(item, 7, 7).Equip);\r
+\r
+            var aircraft = new ItemStatus\r
+            {\r
+                Spec = new ItemSpec\r
+                {\r
+                    Name = "零式艦戦53型(岩本隊)",\r
+                    Type = 6\r
+                }\r
+            };\r
+            Assert.AreEqual(aircraft.Spec.Name, FleetSpec.Record.CreateItemRecord(aircraft, 7, 7).Equip);\r
+            aircraft.Level = 10;\r
+            Assert.AreEqual("零式艦戦53型(岩本★10", FleetSpec.Record.CreateItemRecord(aircraft, 7, 7).Equip);\r
+        }\r
     }\r
 }
\ No newline at end of file
index 9e5d074..74122b7 100644 (file)
@@ -49,4 +49,5 @@
        <s:Boolean x:Key="/Default/UserDictionary/Words/=Scroller/@EntryIndexedValue">True</s:Boolean>
        <s:Boolean x:Key="/Default/UserDictionary/Words/=Scrollify/@EntryIndexedValue">True</s:Boolean>
        <s:Boolean x:Key="/Default/UserDictionary/Words/=Seiran/@EntryIndexedValue">True</s:Boolean>
-       <s:Boolean x:Key="/Default/UserDictionary/Words/=taiha/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
+       <s:Boolean x:Key="/Default/UserDictionary/Words/=taiha/@EntryIndexedValue">True</s:Boolean>
+       <s:Boolean x:Key="/Default/UserDictionary/Words/=Truncator/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
index 120a909..5f5941f 100644 (file)
     <Compile Include="Model\ItemInfo.cs" />\r
     <Compile Include="Model\Status.cs" />\r
     <Compile Include="View\ShipNameWidth.cs" />\r
+    <Compile Include="View\StringTruncator.cs" />\r
     <Compile Include="View\SwipeScrollify.cs" />\r
     <Compile Include="Net\SystemProxy.cs" />\r
     <Compile Include="TextGenerator.cs" />\r
index e918487..f2ce39c 100644 (file)
@@ -23,8 +23,6 @@ namespace KancolleSniffer.View
 {\r
     public class FleetSpec\r
     {\r
-        private static readonly Font Font = new Control().Font;\r
-\r
         public static Record[] Create(Sniffer sniffer)\r
         {\r
             var list = new List<Record>();\r
@@ -146,22 +144,8 @@ namespace KancolleSniffer.View
 \r
             private static string GenEquipString(ItemStatus item)\r
             {\r
-                var name = item.Spec.Name;\r
-                var attr = item.Level == 0 ? "" : "★" + item.Level;\r
-                var proposed = new Size(int.MaxValue, int.MaxValue);\r
-                var maxWidth = item.Spec.IsAircraft ? 132 : 180;\r
-                var result = name + attr;\r
-                if (TextRenderer.MeasureText(result, Font, proposed).Width <= maxWidth)\r
-                    return result;\r
-                var truncated = "";\r
-                foreach (var ch in name)\r
-                {\r
-                    var tmp = truncated + ch;\r
-                    if (TextRenderer.MeasureText(tmp + attr, Font, proposed).Width > maxWidth)\r
-                        break;\r
-                    truncated = tmp;\r
-                }\r
-                return truncated + attr;\r
+                var level = item.Level == 0 ? "" : "★" + item.Level;\r
+                return StringTruncator.Truncate(item.Spec.Name, level, item.Spec.IsAircraft ? 132 : 180) + level;\r
             }\r
 \r
             public static Record CreateFleetRecord(IReadOnlyList<Fleet> fleets, int number)\r
index c9e3477..43f58e8 100644 (file)
@@ -143,7 +143,7 @@ namespace KancolleSniffer.View
             {\r
                 _slotStatus = slotStatus;\r
                 ChangeFont(name);\r
-                Text = prefix + TruncateString(name, width);\r
+                Text = prefix + StringTruncator.Truncate(name, "", Scaler.ScaleWidth((int)width), Font);\r
                 Invalidate(); // 必ずOnPaintを実行させるため\r
             }\r
 \r
@@ -163,21 +163,6 @@ namespace KancolleSniffer.View
                 }\r
             }\r
 \r
-            private string TruncateString(string name, ShipNameWidth width)\r
-            {\r
-                if (TextRenderer.MeasureText(name, Font).Width <= (int)width)\r
-                    return name;\r
-                var truncated = "";\r
-                foreach (var ch in name)\r
-                {\r
-                    var tmp = truncated + ch;\r
-                    if (TextRenderer.MeasureText(tmp, Font).Width > Scaler.ScaleWidth((float)width))\r
-                        break;\r
-                    truncated = tmp;\r
-                }\r
-                return truncated.TrimEnd(' ');\r
-            }\r
-\r
             protected override void OnPaint(PaintEventArgs e)\r
             {\r
                 base.OnPaint(e);\r
diff --git a/KancolleSniffer/View/StringTruncator.cs b/KancolleSniffer/View/StringTruncator.cs
new file mode 100644 (file)
index 0000000..a53691d
--- /dev/null
@@ -0,0 +1,44 @@
+// Copyright (C) 2019 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//    http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+using System.Drawing;\r
+using System.Windows.Forms;\r
+\r
+namespace KancolleSniffer.View\r
+{\r
+    public static class StringTruncator\r
+    {\r
+        public static string Truncate(string target, string postfix, int maxWidth, Font font = null)\r
+        {\r
+            if (StringWidth(target + postfix, font) <= maxWidth)\r
+                return target;\r
+            var truncated = "";\r
+            foreach (var ch in target)\r
+            {\r
+                var tmp = truncated + ch;\r
+                if (StringWidth(tmp + postfix, font) > maxWidth)\r
+                    break;\r
+                truncated = tmp;\r
+            }\r
+            return truncated.TrimEnd(' ');\r
+        }\r
+\r
+        private static readonly Font Font = new Control().Font;\r
+\r
+        private static int StringWidth(string str, Font font)\r
+        {\r
+            return TextRenderer.MeasureText(str, font ?? Font, new Size(int.MaxValue, int.MaxValue)).Width;\r
+        }\r
+    }\r
+}
\ No newline at end of file