-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
196 lines (170 loc) · 5.72 KB
/
FormMain.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
using System;
using System.Windows.Forms;
using memory_game.Properties;
namespace memory_game
{
public partial class FormMain : Form
{
public MemoryGame Game;
public CustomUtils Utils;
public AppConfig Config;
public int Hours = 0;
public int Seconds;
private const bool Debug = Program.Debug;
private bool _isMinimized;
// SystemMenu object
private SystemMenu _mSystemMenu;
// ID constants
private const int MAboutId = 0x100;
private const int MHallFameId = 0x300;
private const int MDebugId = 0x200;
public FormMain()
{
InitializeComponent();
Game = new MemoryGame(this, ContainerBlocks, btnStatus, lblTimerTotal, lblTimer);
Utils = new CustomUtils();
Config = new AppConfig();
Game.SoundEnabled = Config.GetCFG("SoundEnabled", Config.Defaults.SoundEnabled);
btnSound.Image = Game.SoundEnabled ? Resources.sound_on : Resources.sound_off;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
AutoSize = true;
}
public sealed override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
private void frmMain_Load(object sender, EventArgs e)
{
Text = Program.AppTitle;
toolTipSound.SetToolTip(btnSound, "Sound on/off");
lblTimer.Visible = false;
lblTimerTotal.Visible = false;
_isMinimized = false;
Game.DrawBoard(6);
Utils.CenterForm(this);
// System menu
try
{
_mSystemMenu = SystemMenu.FromForm(this);
_mSystemMenu.AppendSeparator();
_mSystemMenu.AppendMenu(MHallFameId, "Hall of Fame");
_mSystemMenu.AppendMenu(MAboutId, "About");
// debuging!
if (Debug)
{
_mSystemMenu.AppendMenu(MDebugId, "Debug");
}
}
catch (NoSystemMenuException ex)
{
Console.WriteLine(ex.ToString());
}
}
private void btnSound_Click(object sender, EventArgs e)
{
var b = (Button)sender;
if (Game.SoundEnabled)
{
b.Image = Resources.sound_off;
Game.SoundEnabled = false;
}
else {
b.Image = Resources.sound_on;
Game.SoundEnabled = true;
}
Config.SaveCfg("SoundEnabled", Game.SoundEnabled);
}
private void btnNewGame_Click(object sender, EventArgs e)
{
if (Game.state == MemoryGame.GameState.GameStarted || Game.state == MemoryGame.GameState.GameIntro)
{
if (MessageBox.Show(this, "Start new game?", Program.AppTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
}
var dlg = new FormNewGame();
if (dlg.ShowDialog() == DialogResult.OK)
{
Hide();
Game.NewGame(dlg.GetRows());
Seconds = 3;
lblTimer.Visible = true;
lblTimer.Text = "Starting in " + Seconds;
lblTimerTotal.Visible = false;
timerNewGame.Enabled = true;
Utils.CenterForm(this);
Refresh();
Show();
}
}
private void timerNewGame_Tick(object sender, EventArgs e)
{
Seconds--;
if (Seconds <= 0)
{
timerNewGame.Enabled = false;
lblTimer.Visible = false;
lblTimer.Text = "00:00";
Seconds = 0;
Game.HideIcons();
Game.StartGame();
}
else
{
var s = Seconds.ToString();
lblTimer.Text = "Starting in " + s;
}
}
private void frmMain_ResizeEnd(object sender, EventArgs e)
{
Utils.CenterControl(this, lblTimer, CenterMethod.CenterHorizontal);
}
public void AboutBoxShow()
{
var dlg = new FormAbout();
Game.GamePaused = true;
dlg.ShowDialog();
Game.GamePaused = false;
}
public void HallFameShow()
{
var dlg = new FormHallFame(this);
Game.GamePaused = true;
dlg.ShowDialog();
Game.GamePaused = false;
}
protected override void WndProc ( ref Message msg )
{
if ( msg.Msg == (int)WindowMessages.WmSysCommand )
{
switch ( msg.WParam.ToInt32() )
{
case MAboutId:
AboutBoxShow();
break;
case MHallFameId:
HallFameShow();
break;
case MDebugId:
Game.ShowIcons();
break;
}
}
// Call base class function
base.WndProc(ref msg);
}
private void frmMain_LocationChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized && !_isMinimized)
{
_isMinimized = true;
Game.GamePaused = true;
}
else if (WindowState == FormWindowState.Normal && _isMinimized)
{
_isMinimized = false;
Game.GamePaused = false;
}
}
}
}