-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrmOptions.cs
360 lines (298 loc) · 12.4 KB
/
FrmOptions.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bomberman
{
public partial class FrmOptions : Form
{
private readonly Form caller;
private readonly KeyBinding keyBinding;
private Bitmap saveNorm;
private Bitmap saveFocus;
private Bitmap discardNorm;
private Bitmap discardFocus;
private Bitmap restoreDefaultsNorm;
private Bitmap restoreDefaultsFocus;
private KeyBinding tempKeyBinding;
private int selectedKeyOption;
private Label[] lblKeyLabel;
private Label[] lblKey;
private readonly SoundCollection sounds;
public FrmOptions(Form caller, KeyBinding keyBinding, SoundCollection sounds)
{
this.caller = caller;
this.keyBinding = keyBinding;
this.sounds = sounds;
InitializeComponent();
}
private static string KeyToString(Keys key)
{
switch (key)
{
case Keys.Add: return "+";
case Keys.Alt: return "Alt";
case Keys.Back: return "Backspace";
case Keys.ControlKey: return "Control";
case Keys.Divide: return "/";
case Keys.Down: return "Seta para baixo";
case Keys.Left: return "Seta para esquerda";
case Keys.Multiply: return "*";
case Keys.NumPad0: return "NP 0";
case Keys.NumPad1: return "NP 1";
case Keys.NumPad2: return "NP 2";
case Keys.NumPad3: return "NP 3";
case Keys.NumPad4: return "NP 4";
case Keys.NumPad5: return "NP 5";
case Keys.NumPad6: return "NP 6";
case Keys.NumPad7: return "NP 7";
case Keys.NumPad8: return "NP 8";
case Keys.NumPad9: return "NP 9";
case Keys.PageDown: return "Page Down";
case Keys.PageUp: return "Page Up";
case Keys.Return: return "Enter";
case Keys.Right: return "Seta para direita";
case Keys.ShiftKey: return "Shift";
case Keys.Space: return "Espaço";
case Keys.Subtract: return "-";
case Keys.Up: return "Seta para cima";
}
return key.ToString();
}
private static string KeyOptionToString(KeyBinding keyBinding, int optionIndex)
{
switch (optionIndex)
{
case 0: return KeyToString(keyBinding.Left);
case 1: return KeyToString(keyBinding.Up);
case 2: return KeyToString(keyBinding.Right);
case 3: return KeyToString(keyBinding.Down);
case 4: return KeyToString(keyBinding.DropBomb);
case 5: return KeyToString(keyBinding.Kick);
case 6: return KeyToString(keyBinding.Detonate);
case 7: return KeyToString(keyBinding.Pause);
}
return "Unknow";
}
private static void SetKey(KeyBinding keyBinding, int optionIndex, Keys key)
{
switch (optionIndex)
{
case 0:
keyBinding.Left = key;
break;
case 1:
keyBinding.Up = key;
break;
case 2:
keyBinding.Right = key;
break;
case 3:
keyBinding.Down = key;
break;
case 4:
keyBinding.DropBomb = key;
break;
case 5:
keyBinding.Kick = key;
break;
case 6:
keyBinding.Detonate = key;
break;
case 7:
keyBinding.Pause = key;
break;
}
}
private void FrmOptions_Load(object sender, EventArgs e)
{
tempKeyBinding = new KeyBinding(keyBinding);
selectedKeyOption = -1;
lblKeyLabel = new Label[8];
lblKey = new Label[8];
lblKeyLabel[0] = lblLeftLabel;
lblKeyLabel[1] = lblUpLabel;
lblKeyLabel[2] = lblRightLabel;
lblKeyLabel[3] = lblDownLabel;
lblKeyLabel[4] = lblDropBombLabel;
lblKeyLabel[5] = lblKickLabel;
lblKeyLabel[6] = lblDetonateLabel;
lblKeyLabel[7] = lblPauseLabel;
lblKey[0] = lblLeftKey;
lblKey[1] = lblUpKey;
lblKey[2] = lblRightKey;
lblKey[3] = lblDownKey;
lblKey[4] = lblDropBombKey;
lblKey[5] = lblKickKey;
lblKey[6] = lblDetonateKey;
lblKey[7] = lblPauseKey;
saveNorm = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.salvarnorm.png"));
saveFocus = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.salvarfocus.png"));
discardNorm = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.descartnorm.png"));
discardFocus = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.descartfocus.png"));
restoreDefaultsNorm = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.restnorm.png"));
restoreDefaultsFocus = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("Bomberman.Resources.Textures.Options.restfocus.png"));
}
private void FrmOptions_FormClosed(object sender, FormClosedEventArgs e)
{
if (saveNorm != null)
{
saveNorm.Dispose();
saveNorm = null;
}
if (saveFocus != null)
{
saveFocus.Dispose();
saveFocus = null;
}
if (discardNorm != null)
{
discardNorm.Dispose();
discardNorm = null;
}
if (discardFocus != null)
{
discardFocus.Dispose();
discardFocus = null;
}
if (restoreDefaultsNorm != null)
{
restoreDefaultsNorm.Dispose();
restoreDefaultsNorm = null;
}
if (restoreDefaultsFocus != null)
{
restoreDefaultsFocus.Dispose();
restoreDefaultsFocus = null;
}
caller?.Show();
}
private void UpdateLabels()
{
lblLeftKey.Text = KeyToString(tempKeyBinding.Left);
lblUpKey.Text = KeyToString(tempKeyBinding.Up);
lblRightKey.Text = KeyToString(tempKeyBinding.Right);
lblDownKey.Text = KeyToString(tempKeyBinding.Down);
lblDropBombKey.Text = KeyToString(tempKeyBinding.DropBomb);
lblKickKey.Text = KeyToString(tempKeyBinding.Kick);
lblDetonateKey.Text = KeyToString(tempKeyBinding.Detonate);
lblPauseKey.Text = KeyToString(tempKeyBinding.Pause);
}
private void SaveToDisk()
{
using (var stream = new FileStream(@"keys.bin", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
keyBinding.WriteToStream(stream);
}
}
private void FrmOptions_Shown(object sender, EventArgs e) => UpdateLabels();
private void EditKey(int optionIndex)
{
if (selectedKeyOption != -1)
CancelEditKey();
selectedKeyOption = optionIndex;
lblKeyLabel[optionIndex].ForeColor = Color.Turquoise;
lblKeyLabel[optionIndex].Font = new Font(lblKeyLabel[optionIndex].Font, FontStyle.Bold);
lblKey[optionIndex].Text = "";
sounds?.Play("confirm");
}
private void CancelEditKey()
{
if (selectedKeyOption == -1)
return;
lblKeyLabel[selectedKeyOption].ForeColor = Color.White;
lblKeyLabel[selectedKeyOption].Font = new Font(lblKeyLabel[selectedKeyOption].Font, FontStyle.Regular);
lblKey[selectedKeyOption].Text = KeyOptionToString(tempKeyBinding, selectedKeyOption);
selectedKeyOption = -1;
sounds?.Play("reset");
}
private void UpdateKey(Keys key)
{
if (selectedKeyOption == -1)
return;
lblKeyLabel[selectedKeyOption].ForeColor = Color.White;
lblKeyLabel[selectedKeyOption].Font = new Font(lblKeyLabel[selectedKeyOption].Font, FontStyle.Regular);
SetKey(tempKeyBinding, selectedKeyOption, key);
lblKey[selectedKeyOption].Text = KeyOptionToString(tempKeyBinding, selectedKeyOption);
selectedKeyOption = -1;
sounds?.Play("confirm");
}
private void lblLeftKey_DoubleClick(object sender, EventArgs e) => EditKey(0);
private void lblUpKey_DoubleClick(object sender, EventArgs e) => EditKey(1);
private void lblRightKey_DoubleClick(object sender, EventArgs e) => EditKey(2);
private void lblDownKey_DoubleClick(object sender, EventArgs e) => EditKey(3);
private void lblDropBombKey_DoubleClick(object sender, EventArgs e) => EditKey(4);
private void lblKickKey_DoubleClick(object sender, EventArgs e) => EditKey(5);
private void lblDetonateKey_DoubleClick(object sender, EventArgs e) => EditKey(6);
private void lblPauseKey_DoubleClick(object sender, EventArgs e) => EditKey(7);
private void FrmOptions_KeyDown(object sender, KeyEventArgs e)
{
if (selectedKeyOption == -1)
return;
Keys key = e.KeyCode;
if (key == Keys.Escape)
CancelEditKey();
else
UpdateKey(key);
}
private bool IsEditing(Label label) => selectedKeyOption != -1 && (label == lblKeyLabel[selectedKeyOption] || label == lblKey[selectedKeyOption]);
private void Action_MouseEnter(object sender, EventArgs e)
{
sounds?.Play("select");
var label = (Label)sender;
if (!IsEditing(label))
label.ForeColor = Color.YellowGreen;
label.Font = new Font(label.Font, FontStyle.Bold);
}
private void Action_MouseLeave(object sender, EventArgs e)
{
var label = (Label)sender;
if (!IsEditing(label))
label.ForeColor = Color.White;
label.Font = new Font(label.Font, FontStyle.Regular);
}
private void picsingle_MouseEnter(object sender, EventArgs e)
{
sounds?.Play("select");
picSave.Image = saveFocus;
}
private void picSave_MouseLeave(object sender, EventArgs e) => picSave.Image = saveNorm;
private void picDiscard_MouseEnter(object sender, EventArgs e)
{
sounds?.Play("select");
picDiscard.Image = discardFocus;
}
private void picDiscard_MouseLeave(object sender, EventArgs e) => picDiscard.Image = discardNorm;
private void picDiscard_Click(object sender, EventArgs e)
{
sounds?.Play("confirm");
Close();
}
private void picSave_Click(object sender, EventArgs e)
{
sounds?.Play("confirm");
keyBinding.UpdateFrom(tempKeyBinding);
SaveToDisk();
Close();
}
private void picRestoreDefaults_Click(object sender, EventArgs e)
{
sounds?.Play("reset");
tempKeyBinding = new KeyBinding();
UpdateLabels();
}
private void picRestoreDefaults_MouseEnter(object sender, EventArgs e)
{
sounds?.Play("select");
picRestoreDefaults.Image = restoreDefaultsFocus;
}
private void picRestoreDefaults_MouseLeave(object sender, EventArgs e) => picRestoreDefaults.Image = restoreDefaultsNorm;
}
}