OSDN Git Service

入渠と遠征のタイマー以外の部分のクリックを無効にする
[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.Collections;\r
17 using System.Collections.Generic;\r
18 using System.Drawing;\r
19 using System.Linq;\r
20 using System.Windows.Forms;\r
21 using KancolleSniffer.Model;\r
22 \r
23 namespace KancolleSniffer.View.MainWindow\r
24 {\r
25     public class NDockPanel : Panel, IUpdateTimers\r
26     {\r
27         private const int TopMargin = 3;\r
28         private const int LeftMargin = 2;\r
29         private const int LineHeight = 15;\r
30         private readonly NDockLabels[] _labels = new NDockLabels[DockInfo.DockCount];\r
31         private Label _caption;\r
32 \r
33         private class NDockLabels\r
34         {\r
35             public Label Number { get; set; }\r
36             public ShipLabel.Name Name { get; set; }\r
37             public Label Timer { get; set; }\r
38         }\r
39 \r
40         public UpdateContext Context { private get; set; }\r
41 \r
42         public NDockPanel()\r
43         {\r
44             BorderStyle = BorderStyle.FixedSingle;\r
45             for (var i = 0; i < _labels.Length; i++)\r
46             {\r
47                 var y = TopMargin + i * LineHeight;\r
48                 _labels[i] = new NDockLabels\r
49                 {\r
50                     Number = new Label\r
51                     {\r
52                         Location = new Point(LeftMargin, y),\r
53                         AutoSize = true,\r
54                         Text = "第" + new[] {"一", "二", "三", "四"}[i]\r
55                     },\r
56                     Name = new ShipLabel.Name(new Point(LeftMargin + 27, y), ShipNameWidth.NDock),\r
57                     Timer = new GrowLeftLabel\r
58                     {\r
59                         Location = new Point(LeftMargin + 136, y - 1),\r
60                         GrowLeft = true,\r
61                         MinimumSize = new Size(0, LineHeight),\r
62                         TextAlign = ContentAlignment.MiddleLeft,\r
63                         Cursor = Cursors.Hand\r
64                     }\r
65                 };\r
66             }\r
67             Controls.AddRange(_labels.SelectMany(l => new Control[] {l.Number, l.Name, l.Timer}).ToArray());\r
68             var timers = _labels.Select(l => l.Timer).ToArray();\r
69             SetCursor(timers);\r
70             SetClickHandler(timers);\r
71         }\r
72 \r
73         public void SetClickHandler(Label caption)\r
74         {\r
75             caption.Click += ClickHandler;\r
76             _caption = caption;\r
77         }\r
78 \r
79         private void SetCursor(IEnumerable<Control> controls)\r
80         {\r
81             foreach (var control in controls)\r
82                 control.Cursor = Cursors.Hand;\r
83         }\r
84 \r
85         private void SetClickHandler(IEnumerable<Control> controls)\r
86         {\r
87             foreach (var control in controls)\r
88                 control.Click += ClickHandler;\r
89         }\r
90 \r
91         private void ClickHandler(object sender, EventArgs e)\r
92         {\r
93             Context.Config.ShowEndTime ^= TimerKind.NDock;\r
94             SetCaption();\r
95             UpdateTimers();\r
96         }\r
97 \r
98         public new void Update()\r
99         {\r
100             for (var i = 0; i < _labels.Length; i++)\r
101                 _labels[i].Name.SetName(Context.Sniffer.NDock[i].Name);\r
102             SetCaption();\r
103         }\r
104 \r
105         private void SetCaption()\r
106         {\r
107             _caption.Text = (Context.Config.ShowEndTime & TimerKind.NDock) != 0 ? "入渠終了" : "入渠";\r
108         }\r
109 \r
110         public void UpdateTimers()\r
111         {\r
112             var now = Context.GetStep().Now;\r
113             var showEndTime = (Context.Config.ShowEndTime & TimerKind.NDock) != 0;\r
114             foreach (var entry in _labels.Zip(Context.Sniffer.NDock,\r
115                 (label, ndock) => new {label = label.Timer, timer = ndock.Timer}))\r
116             {\r
117                 entry.label.ForeColor = entry.timer.IsFinished(now) ? CUDColors.Red : Color.Black;\r
118                 entry.label.Text = entry.timer.ToString(now, showEndTime);\r
119             }\r
120         }\r
121     }\r
122 }