-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerForm.cs
285 lines (228 loc) · 8.06 KB
/
TimerForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;
using System.IO;
namespace Separation_Timer
{
public partial class TimerForm : Form
{
// Move window init
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
// Timer init
int timerOneTicks = 0;
int timerTwoTicks = 0;
int defaultTimeOne = 120;
int defaultTimeTwo = 180;
SoundPlayer alertSound1 = new(Separation_Timer.Properties.Resources.beep);
SoundPlayer alertSound2 = new(Separation_Timer.Properties.Resources.beep);
// Form Init
public TimerForm()
{
InitializeComponent();
loadSettings();
}
private void TimerForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void buttonExit_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void buttonMinimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void loadSettings()
{
try
{
string[] lines = File.ReadAllLines("config.cfg");
for (int i = 0; i < lines.Length; i++)
{
// config.cfg format:
// var=value
String[] args = lines[i].Split('=');
if(args[0] == "one")
{
defaultTimeOne = Int32.Parse(args[1]);
} else if(args[0] == "two"){
defaultTimeTwo = Int32.Parse(args[1]);
}
}
}
catch (FileNotFoundException)
{
// Fallback on default
defaultTimeOne = 120;
defaultTimeTwo = 180;
}
// Set the number on the buttons
buttonTimerOne1.Text = (defaultTimeOne >= 60 ? defaultTimeOne / 60 : defaultTimeOne).ToString();
buttonTimerTwo1.Text = (defaultTimeOne >= 60 ? defaultTimeOne / 60 : defaultTimeOne).ToString();
buttonTimerOne2.Text = (defaultTimeTwo >= 60 ? defaultTimeTwo / 60 : defaultTimeTwo).ToString();
buttonTimerTwo2.Text = (defaultTimeTwo >= 60 ? defaultTimeTwo / 60 : defaultTimeTwo).ToString();
}
private string secondsToString(int seconds)
{
return ((seconds / 60 % 60) >= 10 ? (seconds / 60 % 60).ToString() : "0" + seconds / 60 % 60) + ":"
+ ((seconds % 60) >= 10 ? (seconds % 60).ToString() : "0" + seconds % 60);
}
// =======================
// Section One
// =======================
private void toggleButtonsOne(bool show)
{
if (show)
{
buttonTimerOne1.Visible = true;
buttonTimerOne2.Visible = true;
labelTimeOne.Visible = false;
}
else
{
buttonTimerOne1.Visible = false;
buttonTimerOne2.Visible = false;
labelTimeOne.Visible = true;
}
alertSound1.Stop();
}
private void buttonTimerOne1_Click(object sender, EventArgs e)
{
timerOne.Enabled = true;
timerOneTicks = defaultTimeOne;
labelTimeOne.Text = secondsToString(defaultTimeOne);
toggleButtonsOne(false);
}
private void buttonTimerOne2_Click(object sender, EventArgs e)
{
timerOne.Enabled = true;
timerOneTicks = defaultTimeTwo;
labelTimeOne.Text = secondsToString(defaultTimeTwo);
toggleButtonsOne(false);
}
private void labelTimeOne_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
timerOne.Enabled = false;
labelTimeOne.Text = "00:00";
toggleButtonsOne(true);
}
}
private void timerOne_Tick(object sender, EventArgs e)
{
// Tick
timerOneTicks--;
// Play timer sound
if (timerOneTicks == 0)
{
alertSound1.Play();
}
// Color label when five seconds left
if (timerOneTicks < 6)
{
labelTimeOne.ForeColor = Color.Orange;
}
else
{
labelTimeOne.ForeColor = Color.White;
}
// Stop timer if finished or format current time
if (timerOneTicks < 0)
{
timerOne.Enabled = false;
labelTimeOne.Text = "00:00";
}
else
{
labelTimeOne.Text =
((timerOneTicks / 60 % 60) >= 10 ? (timerOneTicks / 60 % 60).ToString() : "0" + timerOneTicks / 60 % 60) + ":"
+ ((timerOneTicks % 60) >= 10 ? (timerOneTicks % 60).ToString() : "0" + timerOneTicks % 60);
}
}
// =======================
// Section Two
// =======================
private void toggleButtonsTwo(bool show)
{
if (show)
{
buttonTimerTwo1.Visible = true;
buttonTimerTwo2.Visible = true;
labelTimeTwo.Visible = false;
}
else
{
buttonTimerTwo1.Visible = false;
buttonTimerTwo2.Visible = false;
labelTimeTwo.Visible = true;
}
alertSound2.Stop();
}
private void buttonTimerTwo1_Click(object sender, EventArgs e)
{
timerTwo.Enabled = true;
timerTwoTicks = defaultTimeOne;
labelTimeTwo.Text = secondsToString(defaultTimeOne);
toggleButtonsTwo(false);
}
private void buttonTimerTwo2_Click(object sender, EventArgs e)
{
timerTwo.Enabled = true;
timerTwoTicks = defaultTimeTwo;
labelTimeTwo.Text = secondsToString(defaultTimeTwo);
toggleButtonsTwo(false);
}
private void labelTimeTwo_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
timerTwo.Enabled = false;
labelTimeTwo.Text = "00:00";
toggleButtonsTwo(true);
}
}
private void timerTwo_Tick(object sender, EventArgs e)
{
// Tick
timerTwoTicks--;
// Play timer sound
if (timerTwoTicks == 0)
{
alertSound2.Play();
}
// Color label when five seconds left
if (timerTwoTicks < 6)
{
labelTimeTwo.ForeColor = Color.Orange;
}
else
{
labelTimeTwo.ForeColor = Color.White;
}
// Stop timer if finished or format current time
if (timerTwoTicks < 0)
{
timerTwo.Enabled = false;
labelTimeTwo.Text = "00:00";
}
else
{
labelTimeTwo.Text =
((timerTwoTicks / 60 % 60) >= 10 ? (timerTwoTicks / 60 % 60).ToString() : "0" + timerTwoTicks / 60 % 60) + ":"
+ ((timerTwoTicks % 60) >= 10 ? (timerTwoTicks % 60).ToString() : "0" + timerTwoTicks % 60);
}
}
}
}