OSDN Git Service

タイマーの時刻を保持するTimeStepクラスを導入する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Notification / ConfigDialog.cs
1 // Copyright (C) 2017 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.Linq;\r
19 using System.Windows.Forms;\r
20 using KancolleSniffer.View;\r
21 \r
22 namespace KancolleSniffer.Notification\r
23 {\r
24     public partial class ConfigDialog : Form\r
25     {\r
26         private readonly Dictionary<string, NotificationSpec> _notifications;\r
27         private readonly Dictionary<NotificationType, CheckBox> _configCheckBoxes;\r
28         private readonly ResizableToolTip _toolTip = new ResizableToolTip();\r
29 \r
30         public ConfigDialog(Dictionary<string, NotificationSpec> notifications,\r
31             Dictionary<NotificationType, CheckBox> checkBoxes)\r
32         {\r
33             InitializeComponent();\r
34             _notifications = notifications;\r
35             _configCheckBoxes = checkBoxes;\r
36 \r
37             checkBoxFlashWindow.Tag = NotificationType.FlashWindow;\r
38             checkBoxShowBalloonTip.Tag = NotificationType.ShowBaloonTip;\r
39             checkBoxPlaySound.Tag = NotificationType.PlaySound;\r
40             checkBoxPush.Tag = NotificationType.Push;\r
41             checkBoxRepeat.Tag = NotificationType.Repeat;\r
42             checkBoxCont.Tag = NotificationType.Cont;\r
43             checkBoxPreliminary.Tag = NotificationType.Preliminary;\r
44 \r
45             // ReSharper disable once CoVariantArrayConversion\r
46             listBoxNotifications.Items.AddRange(Config.NotificationNames);\r
47         }\r
48 \r
49         private void listBoxNotifications_SelectedIndexChanged(object sender, EventArgs e)\r
50         {\r
51             if (listBoxNotifications.SelectedItem == null)\r
52                 return;\r
53             var notification = _notifications[(string)listBoxNotifications.SelectedItem];\r
54             switch (notification.Name)\r
55             {\r
56                 case "艦娘数超過":\r
57                 case "装備数超過":\r
58                     textBoxPreliminary.Visible = labelPreliminary.Visible = checkBoxPreliminary.Visible =\r
59                         textBoxRepeat.Visible = labelRepeat.Visible = checkBoxRepeat.Visible =\r
60                             checkBoxCont.Visible = false;\r
61                     break;\r
62                 default:\r
63                     textBoxRepeat.Visible = labelRepeat.Visible = checkBoxRepeat.Visible = true;\r
64                     checkBoxRepeat.Enabled = _configCheckBoxes[NotificationType.Repeat].Checked;\r
65                     textBoxRepeat.Text = notification.RepeatInterval.ToString();\r
66                     checkBoxCont.Visible = IsContAvailable;\r
67                     textBoxPreliminary.Visible =\r
68                         labelPreliminary.Visible = checkBoxPreliminary.Visible = IsPreliminaryAvailable;\r
69                     textBoxPreliminary.Text = notification.PreliminaryPeriod.ToString();\r
70                     break;\r
71             }\r
72             checkBoxFlashWindow.Checked = (notification.Flags & NotificationType.FlashWindow) != 0;\r
73             checkBoxShowBalloonTip.Checked = (notification.Flags & NotificationType.ShowBaloonTip) != 0;\r
74             checkBoxPlaySound.Checked = (notification.Flags & NotificationType.PlaySound) != 0;\r
75             checkBoxPush.Checked = (notification.Flags & NotificationType.Push) != 0;\r
76             checkBoxRepeat.Checked = (notification.Flags & NotificationType.Repeat) != 0;\r
77             _toolTip.SetToolTip(checkBoxCont,\r
78                 !IsContAvailable ? "" :\r
79                 notification.Name == "遠征終了" ? "再度遠征に出すまでリピートする。" : "再度入渠させるまでリピートする。");\r
80             checkBoxCont.Checked = (notification.Flags & NotificationType.Cont) != 0;\r
81             checkBoxPreliminary.Checked = (notification.Flags & NotificationType.Preliminary) != 0;\r
82         }\r
83 \r
84         private void checkBox_CheckedChanged(object sender, EventArgs e)\r
85         {\r
86             var checkBox = (CheckBox)sender;\r
87             var type = (NotificationType)checkBox.Tag;\r
88             var spec = _notifications[(string)listBoxNotifications.SelectedItem];\r
89             spec.Flags = checkBox.Checked ? spec.Flags | type : spec.Flags & ~type;\r
90             if (type == NotificationType.Repeat)\r
91             {\r
92                 textBoxRepeat.Enabled = labelRepeat.Enabled = checkBoxCont.Enabled =\r
93                     _configCheckBoxes[NotificationType.Repeat].Checked && checkBox.Checked;\r
94             }\r
95             if (type == NotificationType.Preliminary)\r
96                 textBoxPreliminary.Enabled = labelPreliminary.Enabled = checkBox.Checked;\r
97         }\r
98 \r
99         private bool IsContAvailable =>\r
100             new[] {"遠征終了", "入渠終了"}.Contains((string)listBoxNotifications.SelectedItem);\r
101 \r
102         private bool IsPreliminaryAvailable =>\r
103             new[] {"遠征終了", "入渠終了", "建造完了", "泊地修理20分経過", "疲労回復"}.Contains((string)listBoxNotifications.SelectedItem);\r
104 \r
105         private void textBoxRepeat_TextChanged(object sender, EventArgs e)\r
106         {\r
107             _notifications[(string)listBoxNotifications.SelectedItem].RepeatInterval =\r
108                 int.TryParse(textBoxRepeat.Text, out var interval) && interval > 0 ? interval : 0;\r
109         }\r
110 \r
111         private void NotificationConfigDialog_Load(object sender, EventArgs e)\r
112         {\r
113             checkBoxFlashWindow.Enabled = _configCheckBoxes[NotificationType.FlashWindow].Checked;\r
114             checkBoxShowBalloonTip.Enabled = _configCheckBoxes[NotificationType.ShowBaloonTip].Checked;\r
115             checkBoxPlaySound.Enabled = _configCheckBoxes[NotificationType.PlaySound].Checked;\r
116             checkBoxRepeat.Enabled = _configCheckBoxes[NotificationType.Repeat].Checked;\r
117             textBoxRepeat.Enabled = labelRepeat.Enabled = checkBoxCont.Enabled =\r
118                 checkBoxRepeat.Enabled && checkBoxRepeat.Checked;\r
119             textBoxPreliminary.Enabled = checkBoxPreliminary.Checked;\r
120 \r
121             var selected = listBoxNotifications.SelectedIndex;\r
122             listBoxNotifications.SelectedIndex = -1;\r
123             listBoxNotifications.SelectedIndex = selected == -1 ? 0 : selected;\r
124         }\r
125 \r
126         private void textBoxPreliminary_TextChanged(object sender, EventArgs e)\r
127         {\r
128             _notifications[(string)listBoxNotifications.SelectedItem].PreliminaryPeriod =\r
129                 int.TryParse(textBoxPreliminary.Text, out var preliminary) && preliminary > 0 ? preliminary : 0;\r
130         }\r
131 \r
132         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)\r
133         {\r
134             base.ScaleControl(factor, specified);\r
135             if (factor.Height > 1)\r
136                 _toolTip.Font = new Font(_toolTip.Font.FontFamily, _toolTip.Font.Size * factor.Height);\r
137         }\r
138     }\r
139 }