OSDN Git Service

IUpdatableをIUpdateContextとIUpdateTimerに分離する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / NDockPanel.cs
1 // Copyright (C) 2019 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.Drawing;\r
17 using System.Linq;\r
18 using System.Windows.Forms;\r
19 using KancolleSniffer.Model;\r
20 \r
21 namespace KancolleSniffer.View\r
22 {\r
23     public class NDockPanel : Panel, IUpdateTimers\r
24     {\r
25         private const int TopMargin = 3;\r
26         private const int LeftMargin = 2;\r
27         private const int LineHeight = 15;\r
28         private readonly NDockLabels[] _labels = new NDockLabels[DockInfo.DockCount];\r
29 \r
30         private class NDockLabels\r
31         {\r
32             public Label Number { get; set; }\r
33             public ShipLabel.Name Name { get; set; }\r
34             public Label Timer { get; set; }\r
35         }\r
36 \r
37         public UpdateContext Context { private get; set; }\r
38 \r
39         public NDockPanel()\r
40         {\r
41             BorderStyle = BorderStyle.FixedSingle;\r
42             for (var i = 0; i < _labels.Length; i++)\r
43             {\r
44                 var y = TopMargin + i * LineHeight;\r
45                 _labels[i] = new NDockLabels\r
46                 {\r
47                     Number = new Label\r
48                     {\r
49                         Location = new Point(LeftMargin, y),\r
50                         AutoSize = true,\r
51                         Text = "第" + new[] {"一", "二", "三", "四"}[i]\r
52                     },\r
53                     Name = new ShipLabel.Name(new Point(LeftMargin + 27, y), ShipNameWidth.NDock),\r
54                     Timer = new GrowLeftLabel\r
55                     {\r
56                         Location = new Point(LeftMargin + 136, y - 1),\r
57                         GrowLeft = true,\r
58                         MinimumSize = new Size(0, LineHeight),\r
59                         TextAlign = ContentAlignment.MiddleLeft,\r
60                         Cursor = Cursors.Hand\r
61                     }\r
62                 };\r
63             }\r
64             Controls.AddRange(_labels.SelectMany(l => new Control[] {l.Number, l.Name, l.Timer}).ToArray());\r
65             SetCursor();\r
66         }\r
67 \r
68         private void SetCursor()\r
69         {\r
70             Cursor = Cursors.Hand;\r
71             foreach (Control control in Controls)\r
72                 control.Cursor = Cursors.Hand;\r
73         }\r
74 \r
75         public void SetClickHandler(EventHandler onClick)\r
76         {\r
77             Click += onClick;\r
78             foreach (Control control in Controls)\r
79                 control.Click += onClick;\r
80         }\r
81 \r
82         public new void Update()\r
83         {\r
84             for (var i = 0; i < _labels.Length; i++)\r
85                 _labels[i].Name.SetName(Context.Sniffer.NDock[i].Name);\r
86         }\r
87 \r
88         public void UpdateTimers()\r
89         {\r
90             var now = Context.GetNow();\r
91             var showEndTime = (Context.Config.ShowEndTime & TimerKind.NDock) != 0;\r
92             foreach (var entry in _labels.Zip(Context.Sniffer.NDock,\r
93                 (label, ndock) => new {label = label.Timer, timer = ndock.Timer}))\r
94             {\r
95                 entry.label.ForeColor = entry.timer.IsFinished(now) ? CUDColors.Red : Color.Black;\r
96                 entry.label.Text = entry.timer.ToString(now, showEndTime);\r
97             }\r
98         }\r
99     }\r
100 }