OSDN Git Service

一覧の艦隊のリサイズとスケールがおかしいのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / ListWindow / FleetDataPanel.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System;\r
16 using System.Collections.Generic;\r
17 using System.Drawing;\r
18 using System.Windows.Forms;\r
19 using Clipboard = KancolleSniffer.Util.Clipboard;\r
20 using KancolleSniffer.Forms;\r
21 \r
22 namespace KancolleSniffer.View.ListWindow\r
23 {\r
24     public class FleetDataPanel : PanelWithToolTip, IPanelResize\r
25     {\r
26         private const int LineHeight = 14;\r
27         private const int LabelHeight = 12;\r
28         private FleetData.Record[] _data = new FleetData.Record[0];\r
29         private readonly List<FleetLabels> _labelList = new List<FleetLabels>();\r
30 \r
31         public FleetDataPanel()\r
32         {\r
33             ToolTip.AutoPopDelay = 10000;\r
34         }\r
35 \r
36         public void Update(Sniffer sniffer)\r
37         {\r
38             _data = FleetData.Create(sniffer);\r
39             SuspendLayout();\r
40             CreateLabels();\r
41             ResizeLabels();\r
42             SetRecords();\r
43             ResumeLayout();\r
44         }\r
45 \r
46         private void CreateLabels()\r
47         {\r
48             for (var i = _labelList.Count; i < _data.Length; i++)\r
49                 CreateLabels(i);\r
50         }\r
51 \r
52         private class FleetLabels : ControlsArranger\r
53         {\r
54             public Label Fleet { get; set; }\r
55             public ShipLabel.Name Name { get; set; }\r
56             public Label Equip { get; set; }\r
57             public Label EquipColor { get; set; }\r
58             public GrowLeftLabel Spec { get; set; }\r
59 \r
60             public override Control[] Controls => new Control[] {Fleet, Name, Equip, EquipColor, Spec};\r
61         }\r
62 \r
63         private void CreateLabels(int i)\r
64         {\r
65             var y = 1 + LineHeight * i;\r
66             var labels = new FleetLabels\r
67             {\r
68                 Fleet = new Label {Location = new Point(1, 2), AutoSize = true},\r
69                 Name = new ShipLabel.Name(new Point(10, 2), ShipNameWidth.Max),\r
70                 Equip = new Label {Location = new Point(38, 2), AutoSize = true, Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top},\r
71                 EquipColor = new Label {Location = new Point(35, 2), Size = new Size(4, LabelHeight - 2)},\r
72                 Spec = new GrowLeftLabel {Location = new Point(217, 2), GrowLeft = true, Anchor = AnchorStyles.Right | AnchorStyles.Top},\r
73                 BackPanel = new Panel\r
74                 {\r
75                     Location = new Point(0, y),\r
76                     Size = new Size(ListForm.PanelWidth, LineHeight),\r
77                     BackColor = CustomColors.ColumnColors.BrightFirst(i)\r
78                 }\r
79             };\r
80             _labelList.Add(labels);\r
81             labels.Fleet.DoubleClick += (obj, ev) => { Clipboard.SetText((string)labels.Fleet.Tag); };\r
82             labels.Arrange(this, CustomColors.ColumnColors.BrightFirst(i));\r
83             labels.Scale();\r
84             labels.Move(AutoScrollPosition);\r
85         }\r
86 \r
87         public void ApplyResize()\r
88         {\r
89             SuspendLayout();\r
90             ResizeLabels();\r
91             SetRecords();\r
92             ResumeLayout();\r
93         }\r
94 \r
95         private void ResizeLabels()\r
96         {\r
97             var width = Width - SystemInformation.VerticalScrollBarWidth - 2;\r
98             foreach (var labels in _labelList)\r
99             {\r
100                 labels.BackPanel.Width = width;\r
101                 labels.Spec.AdjustLocation();\r
102             }\r
103         }\r
104 \r
105         private void SetRecords()\r
106         {\r
107             for (var i = 0; i < _data.Length; i++)\r
108                 SetRecord(i);\r
109             for (var i = _data.Length; i < _labelList.Count; i++)\r
110                 _labelList[i].BackPanel.Visible = false;\r
111         }\r
112 \r
113         private void SetRecord(int i)\r
114         {\r
115             var e = _data[i];\r
116             var labels = _labelList[i];\r
117             labels.Fleet.Text = e.Fleet;\r
118             labels.Fleet.Tag = "";\r
119             labels.Name.SetName(e.Ship);\r
120             if (e.Ship2 != "")\r
121                 ToolTip.SetToolTip(labels.Name, e.Ship2);\r
122             labels.Equip.Text = e.Equip.ToString(Scaler.DownWidth(labels.BackPanel.Width) - ListForm.PanelWidth);\r
123             labels.EquipColor.Visible = e.Equip.ToString() != "";\r
124             labels.EquipColor.BackColor = e.Color;\r
125             labels.Spec.Text = e.Spec;\r
126             if (e.Fleet != "" && e.Fleet2 != "")\r
127             {\r
128                 ToolTip.SetToolTip(labels.Fleet, e.Fleet2);\r
129                 labels.Fleet.Tag = e.Fleet2;\r
130             }\r
131             ToolTip.SetToolTip(labels.Equip, e.AircraftSpec != "" ? e.AircraftSpec : "");\r
132             ToolTip.SetToolTip(labels.Spec, e.Spec2 != "" ? e.Spec2 : "");\r
133             labels.BackPanel.Visible = true;\r
134         }\r
135 \r
136         public void ShowShip(int id)\r
137         {\r
138             var i = Array.FindIndex(_data, e => e.Id == id);\r
139             if (i == -1)\r
140                 return;\r
141             var y = Scaler.ScaleHeight(LineHeight * i);\r
142             AutoScrollPosition = new Point(0, y);\r
143         }\r
144 \r
145         public void ShowFleet(string fn)\r
146         {\r
147             var i = Array.FindIndex(_data, e => e.Fleet.Contains(fn));\r
148             if (i == -1)\r
149                 return;\r
150             var y = fn == "基地" ? int.MaxValue : Scaler.ScaleHeight(LineHeight * i);\r
151             AutoScrollPosition = new Point(0, y);\r
152         }\r
153     }\r
154 }