-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatanTimer.cs
executable file
·144 lines (123 loc) · 4.11 KB
/
CatanTimer.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace CatanTimer
{
public partial class CatanTimer : Form
{
// This value will be set when a button is clicked and will set our target value for the timer. This is in milliseconds
public int MillisecondsLeft = 0;
public const int SIXTY_SECONDS = 60000;
public const int THIRTY_SECONDS = 30000;
public const int MILLISECONDS_PER_SECOND = 1000;
public SoundPlayer Player;
public CatanTimer()
{
InitializeComponent();
Player = new SoundPlayer(Properties.Resources.clarinet);
}
// Override IsInputKey method to identify the Special keys
protected override bool ProcessTabKey(bool forward)
{
forward = true;
return forward;
}
private void UpdateTimerLabelText()
{
float time = (float)MillisecondsLeft / MILLISECONDS_PER_SECOND;
if (time == 0)
{
TimerLabel.Text = "Done!";
}
else
{
TimerLabel.Text = time.ToString("F2") + "s";
}
}
private void PlayTimerSound()
{
//Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CatanTimer.clarinet.wav");
Player.Play();
}
#region Click_Actions
private void ThirtySecondsButton_Click(object sender, EventArgs e)
{
MillisecondsLeft = THIRTY_SECONDS;
Timer.Enabled = true;
}
private void NextTurnButton_Click(object sender, EventArgs e)
{
MillisecondsLeft = SIXTY_SECONDS;
Timer.Enabled = true;
}
private void PlayPauseButton_Click(object sender, EventArgs e)
{
if (MillisecondsLeft > 0)
{
Timer.Enabled = !Timer.Enabled;
}
}
#endregion
private void Timer_Tick(object sender, EventArgs e)
{
// Decrement Timer
MillisecondsLeft -= Timer.Interval;
if (MillisecondsLeft <= 0)
{
MillisecondsLeft = 0;
PlayTimerSound();
Timer.Enabled = false;
}
UpdateTimerLabelText();
}
private void TimerPanel_Resize(object sender, EventArgs e)
{
// We need to do two things:
// 1. Center the timer label (left - right)
// 2. Center the Buttons panel (left - right)
Point location;
location = TimerLabel.Location;
location.X = (TimerPanel.Width / 2 ) - (TimerLabel.Width / 2);
TimerLabel.Location = location;
location = TimerButtonsPanel.Location;
location.X = (TimerPanel.Width / 2) - (TimerButtonsPanel.Width / 2);
TimerButtonsPanel.Location = location;
}
private void CatanTimer_KeyUp(object sender, KeyEventArgs e)
{
}
private void CatanTimer_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
PlayPauseButton_Click(sender, e);
e.Handled = true;
}
else if (e.KeyCode == Keys.Tab)
{
ThirtySecondsButton_Click(sender, e);
e.Handled = true;
}
else if (e.KeyCode == Keys.Enter)
{
NextTurnButton_Click(sender, e);
e.Handled = true;
}
}
private void CatanTimer_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
e.IsInputKey = true;
}
}
}
}