SequentialButtonTranslator.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
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using UAM.InformatiX.SPOT.Input;

namespace SPŠST.DMP.SebisGame
{
/// <summary>
/// Translates a button press into textual or numeric input assuming that the buttons enum values form a continuous sequence of numbers
/// and that the desired textual data consists of single characters.
/// </summary>
public class SequentialButtonTranslator : IButtonTranslator
{
/// <summary>
/// The first button in the sequence representing a textual input.
/// </summary>
public Button FirstCharacterButton;
/// <summary>
/// The first button in the sequence representing a numeric input.
/// </summary>
public Button FirstNumberButton; // The textual and numeric sequences can overlap (and often are the same).

/// <summary>
/// The array of strings of associated button characters.
/// Each string in the array represents different button and the character position determines the number of presses needed,
/// for example string "ABC" means 'A' for first button press, 'B' if the button was pressed twice etc.
/// The sequence of strings in the array must match the button enum values sequence.
/// The length of the array determines how many buttons starting at <see cref="FirstCharacterButton" /> are in the sequence.
/// </summary>
public string[] CharacterData;
/// <summary>
/// The array of numbers associated with buttons.
/// Each number in the array represents different button.
/// The sequence of numbers in the array musst match the button enum values sequence.
/// The length of the array determines how many buttons atarting at <see cref="FirstNumberButton" /> are in the sequence.
/// </summary>
public int[] NumberData;

/// <summary>
/// Translates a button press to a character.
/// </summary>
/// <param name="button">The button which was pressed.</param>
/// <param name="pressedCount">The number of presses.</param>
/// <param name="text">The character associated with this button and number of presses.</param>
/// <returns>true if any associated character was found and <paramref name="text"/> is valid; otherwise false.</returns>
public bool GetText(Button button, int count, out string text)
{
int index = (int)button - (int)FirstCharacterButton; // find out the button relative position (to the sequence)
if (CharacterData != null && (index >= 0 && index < CharacterData.Length)) // if there are any character data at all and the button position falled into the sequence
{
string data = CharacterData[index]; // get all letters for this button, eg. "ABC"
if (data == null || data.Length < 1) // letters data are not valid
{ // (in fact you could exploit this checking to skip some buttons in the sequence
text = null; // and make this button translator a little bit less sequential, but it won't work with numbers)
return false;
}
text = data[count % data.Length].ToString(); // extract the letter according the presses count, in cycle, like 1='A' 2='B'... 4='A' ... 7='A' ...
return true; // we found any
}
else
{
text = null; // this button does not belong to our sequence
return false;
}
}
/// <summary>
/// Translates a button press to a number.
/// </summary>
/// <param name="button">The button which was pressed.</param>
/// <param name="number">The number associated with this button.</param>
/// <returns>true if associated number was found and <paramref name="number"/> is valid; otherwise false.</returns>
public bool GetNumber(Button button, out int number)
{
number = (int)button - (int)FirstNumberButton; // find out the button relative position (to the sequence)
if (NumberData != null && (number >= 0 && number < NumberData.Length)) // if there are any number data at all and the button position falled into the sequence
{
number = NumberData[number]; // get the number for this button
return true; // and return success
}
return false; // this button does not belong to our sequence
}
}
}