OSDN Git Service

e72c138effeab2f91241933e2e4e37434a9e5e30
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / MainWindow / 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.MainWindow\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         private Label _caption;\r
30 \r
31         private class NDockLabels\r
32         {\r
33             public Label Number { get; set; }\r
34             public ShipLabel.Name Name { get; set; }\r
35             public Label Timer { get; set; }\r
36         }\r
37 \r
38         public UpdateContext Context { private get; set; }\r
39 \r
40         public NDockPanel()\r
41         {\r
42             BorderStyle = BorderStyle.FixedSingle;\r
43             for (var i = 0; i < _labels.Length; i++)\r
44             {\r
45                 var y = TopMargin + i * LineHeight;\r
46                 _labels[i] = new NDockLabels\r
47                 {\r
48                     Number = new Label\r
49                     {\r
50                         Location = new Point(LeftMargin, y),\r
51                         AutoSize = true,\r
52                         Text = "第" + new[] {"一", "二", "三", "四"}[i]\r
53                     },\r
54                     Name = new ShipLabel.Name(new Point(LeftMargin + 27, y), ShipNameWidth.NDock),\r
55                     Timer = new GrowLeftLabel\r
56                     {\r
57                         Location = new Point(LeftMargin + 136, y - 1),\r
58                         GrowLeft = true,\r
59                         MinimumSize = new Size(0, LineHeight),\r
60                         TextAlign = ContentAlignment.MiddleLeft,\r
61                         Cursor = Cursors.Hand\r
62                     }\r
63                 };\r
64             }\r
65             Controls.AddRange(_labels.SelectMany(l => new Control[] {l.Number, l.Name, l.Timer}).ToArray());\r
66             SetCursor();\r
67             SetClickHandler();\r
68         }\r
69 \r
70         public void SetClickHandler(Label caption)\r
71         {\r
72             caption.Click += ClickHandler;\r
73             _caption = caption;\r
74         }\r
75 \r
76         private void SetCursor()\r
77         {\r
78             Cursor = Cursors.Hand;\r
79             foreach (Control control in Controls)\r
80                 control.Cursor = Cursors.Hand;\r
81         }\r
82 \r
83         private void SetClickHandler()\r
84         {\r
85             Click += ClickHandler;\r
86             foreach (Control control in Controls)\r
87                 control.Click += ClickHandler;\r
88         }\r
89 \r
90         private void ClickHandler(object sender, EventArgs e)\r
91         {\r
92             Context.Config.ShowEndTime ^= TimerKind.NDock;\r
93             SetCaption();\r
94             UpdateTimers();\r
95         }\r
96 \r
97         public new void Update()\r
98         {\r
99             for (var i = 0; i < _labels.Length; i++)\r
100                 _labels[i].Name.SetName(Context.Sniffer.NDock[i].Name);\r
101             SetCaption();\r
102         }\r
103 \r
104         private void SetCaption()\r
105         {\r
106             _caption.Text = (Context.Config.ShowEndTime & TimerKind.NDock) != 0 ? "入渠終了" : "入渠";\r
107         }\r
108 \r
109         public void UpdateTimers()\r
110         {\r
111             var now = Context.GetStep().Now;\r
112             var showEndTime = (Context.Config.ShowEndTime & TimerKind.NDock) != 0;\r
113             foreach (var entry in _labels.Zip(Context.Sniffer.NDock,\r
114                 (label, ndock) => new {label = label.Timer, timer = ndock.Timer}))\r
115             {\r
116                 entry.label.ForeColor = entry.timer.IsFinished(now) ? CUDColors.Red : Color.Black;\r
117                 entry.label.Text = entry.timer.ToString(now, showEndTime);\r
118             }\r
119         }\r
120     }\r
121 }