OSDN Git Service

任務一覧の空欄に前の任務のツールチップが出るのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / View / QuestPanel.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.Windows.Forms;\r
18 using KancolleSniffer.Model;\r
19 // ReSharper disable CoVariantArrayConversion\r
20 \r
21 namespace KancolleSniffer.View\r
22 {\r
23     public class QuestLabels\r
24     {\r
25         public ShipLabel Color { get; set; }\r
26         public ShipLabel Name { get; set; }\r
27         public ShipLabel Count { get; set; }\r
28         public ShipLabel Progress { get; set; }\r
29 \r
30         public ShipLabel[] Labels => new[] {Color, Count, Progress, Name};\r
31     }\r
32 \r
33     public class QuestPanel : PanelWithToolTip\r
34     {\r
35         private const int TopMargin = 5;\r
36         private const int LeftMargin = 2;\r
37         private const int LabelHeight = 12;\r
38         public const int LineHeight = 14;\r
39         private QuestLabels[] _labels;\r
40         private QuestStatus[] _questList = new QuestStatus[0];\r
41         private ListScroller _listScroller;\r
42         private int _lines;\r
43 \r
44         public void CreateLabels(int lines, EventHandler onDoubleClick)\r
45         {\r
46             _lines = LimitLines(lines);\r
47             _labels = new QuestLabels[_lines];\r
48             SuspendLayout();\r
49             Height = (int)Math.Round((TopMargin * 2 + LineHeight * lines) * ShipLabel.ScaleFactor.Height);\r
50             for (var i = 0; i < _lines; i++)\r
51             {\r
52                 var y = TopMargin + i * LineHeight;\r
53                 _labels[i] = new QuestLabels\r
54                 {\r
55                     Color = new ShipLabel\r
56                     {\r
57                         Location = new Point(LeftMargin, y + 1),\r
58                         Size = new Size(4, LabelHeight - 1)\r
59                     },\r
60                     Name = new ShipLabel\r
61                     {\r
62                         Location = new Point(LeftMargin + 4, y),\r
63                         Size = new Size(193, LabelHeight)\r
64                     },\r
65                     Count = new ShipLabel\r
66                     {\r
67                         Location = new Point(LeftMargin + 189, y),\r
68                         AutoSize = true,\r
69                         AnchorRight = true\r
70                     },\r
71                     Progress = new ShipLabel\r
72                     {\r
73                         Location = new Point(LeftMargin + 186, y),\r
74                         Size = new Size(29, LabelHeight),\r
75                         TextAlign = ContentAlignment.MiddleRight\r
76                     }\r
77                 };\r
78                 _labels[i].Name.DoubleClick += onDoubleClick;\r
79                 Controls.AddRange(_labels[i].Labels);\r
80                 foreach (var label in _labels[i].Labels)\r
81                     label.Scale();\r
82             }\r
83             ResumeLayout();\r
84             SetupListScroller();\r
85         }\r
86 \r
87         private static int LimitLines(int lines)\r
88         {\r
89             const int min = 4;\r
90             const int max = 7;\r
91             return Math.Min(Math.Max(lines, min), max);\r
92         }\r
93 \r
94         private void SetupListScroller()\r
95         {\r
96             _listScroller = new ListScroller(this, _labels[0].Labels, _labels[_lines - 1].Labels)\r
97             {\r
98                 Lines = _lines,\r
99                 Padding = TopMargin\r
100             };\r
101             _listScroller.Update += ShowQuestList;\r
102         }\r
103 \r
104         public void Update(QuestStatus[] quests)\r
105         {\r
106             _questList = quests;\r
107             _listScroller.DataCount = quests.Length;\r
108             if (quests.Length <= _lines)\r
109                 _listScroller.Position = 0;\r
110             ShowQuestList();\r
111         }\r
112 \r
113         private void ShowQuestList()\r
114         {\r
115             SuspendLayout();\r
116             for (var i = 0; i < _lines; i++)\r
117             {\r
118                 var labels = _labels[i];\r
119                 if (i >= _questList.Length)\r
120                 {\r
121                     ClearQuest(labels);\r
122                     ClearCount(labels.Count);\r
123                     continue;\r
124                 }\r
125                 var quest = _questList[i + _listScroller.Position];\r
126                 SetQuest(labels, quest);\r
127                 if (quest.Count.Id == 0)\r
128                 {\r
129                     ClearCount(labels.Count);\r
130                     continue;\r
131                 }\r
132                 SetCount(labels.Count, quest.Count);\r
133             }\r
134             ResumeLayout(true);\r
135             _listScroller.DrawMark();\r
136         }\r
137 \r
138         private void ClearQuest(QuestLabels labels)\r
139         {\r
140             labels.Color.BackColor = DefaultBackColor;\r
141             labels.Name.Text = labels.Progress.Text = "";\r
142             ToolTip.SetToolTip(labels.Name, "");\r
143         }\r
144 \r
145         private void ClearCount(Label label)\r
146         {\r
147             label.Text = "";\r
148             label.ForeColor = Color.Black;\r
149             ToolTip.SetToolTip(label, "");\r
150         }\r
151 \r
152         private void SetQuest(QuestLabels labels, QuestStatus quest)\r
153         {\r
154             labels.Color.BackColor = quest.Color;\r
155             labels.Name.Text = quest.Name;\r
156             labels.Progress.Text = $"{quest.Progress:D}%";\r
157             ToolTip.SetToolTip(labels.Name, quest.ToToolTip());\r
158         }\r
159 \r
160         private void SetCount(Label label, QuestCount count)\r
161         {\r
162             label.Text = " " + count;\r
163             label.ForeColor = count.Cleared ? CUDColors.Red : Color.Black;\r
164             ToolTip.SetToolTip(label, count.ToToolTip());\r
165         }\r
166     }\r
167 }