2 using System.Collections.Generic;
17 private int _defaultCancel = 0;
18 private Listener _escListener = null;
19 private Listener _xboxListener = null;
20 private Listener _backListener = null;
22 private int _selectedIndex = -1;
25 private bool _buttonColorSet =
false;
28 private Dictionary<int, List<Action>> handlers =
new Dictionary<int,List<Action>>();
29 private int clickedButton = -1;
52 QuestionLabel.
Font = value;
54 for (
int i = 0; i < Buttons.Length; i++)
56 Buttons[i].Font = value;
65 public int DefaultCancel
67 get {
return _defaultCancel; }
70 _defaultCancel = value;
78 public int SelectedIndex
80 get {
return _selectedIndex; }
83 if ( !IsAddedToGame ) RememberSelection =
true;
84 SelectButton( value );
93 get {
return _selectedIndex >= 0 || _selectedIndex >= Buttons.Length ? Buttons[_selectedIndex] : null; }
107 if (!_buttonColorSet) _setButtonColor(
Color.
Darker(value, 40));
115 public Color SelectionColor
117 get {
return _selectionColor; }
120 _selectionColor = value;
121 SelectButton(_selectedIndex);
128 public bool RememberSelection {
get;
set; }
144 if (buttonTexts.Length == 0)
throw new InvalidOperationException(
"You must add at least one button");
146 VerticalScrollLayout layout =
new VerticalScrollLayout();
147 layout.LeftPadding = layout.RightPadding = 20;
148 layout.BottomPadding = 30;
150 this.Layout = layout;
152 QuestionLabel =
new Label(question);
155 this.Closed += CallButtonHandlers;
159 for (
int i = 0; i < buttonTexts.Length; i++)
163 button.
Clicked +=
new Action(delegate { ButtonClicked((
int)button.
Tag); });
173 AddedToGame += delegate { SelectButton( (RememberSelection && _selectedIndex >= 0) ? _selectedIndex : 0 ); };
176 private void SelectButton(
int p)
180 if (p < 0 || p >= Buttons.Length)
return;
183 _selectedColor = SelectedButton.Color;
184 SelectedButton.Color = SelectionColor;
188 private void UnselectButton()
190 if (_selectedIndex < 0)
return;
191 SelectedButton.Color = _selectedColor;
197 if ( !handlers.ContainsKey(item) )
198 handlers[item] =
new List<Action>();
200 handlers[item].Add( handler );
203 public void AddItemHandler<T1>(
int item, Action<T1> handler, T1 p1)
205 if ( !handlers.ContainsKey( item ) )
206 handlers[item] =
new List<Action>();
208 handlers[item].Add( delegate { handler( p1 ); } );
211 public void AddItemHandler<T1, T2>(
int item, Action<T1, T2> handler, T1 p1, T2 p2)
213 if ( !handlers.ContainsKey( item ) )
214 handlers[item] =
new List<Action>();
216 handlers[item].Add( delegate { handler( p1, p2 ); } );
219 public void AddItemHandler<T1, T2, T3>(
int item, Action<T1, T2, T3> handler, T1 p1, T2 p2, T3 p3)
221 if ( !handlers.ContainsKey( item ) )
222 handlers[item] =
new List<Action>();
224 handlers[item].Add( delegate { handler( p1, p2, p3 ); } );
229 if ( !handlers.ContainsKey( item ) )
232 handlers[item].Remove( handler );
235 private void _setButtonColor(
Color color)
237 for (
int i = 0; i < Buttons.Length; i++)
239 Buttons[i].Color = color;
243 _selectedColor = color;
244 SelectButton(_selectedIndex);
249 _setButtonColor(color);
250 _buttonColorSet =
true;
255 for (
int i = 0; i < Buttons.Length; i++)
257 Buttons[i].TextColor = color;
263 for (
int i = 0; i < Math.Min(Buttons.Length, keys.Length); i++)
268 Handler selectPrev = delegate { SelectButton(_selectedIndex > 0 ? _selectedIndex - 1 : Buttons.Length - 1); };
269 Handler selectNext = delegate { SelectButton(_selectedIndex < Buttons.Length - 1 ? _selectedIndex + 1 : 0); };
270 Handler confirmSelect = delegate { SelectedButton.Click(); };
281 void AddDefaultControls()
287 if (_defaultCancel >= 0 && _defaultCancel < Buttons.Length)
295 void ButtonClicked(
int index)
297 clickedButton = index;
301 void CallButtonHandlers(
Window window )
303 if ( clickedButton < 0 || clickedButton >= Buttons.Length )
306 if ( handlers.ContainsKey( clickedButton ) )
307 handlers[clickedButton].ForEach( handler => handler() );
309 if ( ItemSelected != null )
310 ItemSelected( clickedButton );
PhoneBackButton PhoneBackButton
static readonly Color Black
Musta.
static readonly Color Cyan
Syaani.
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Listener Listen(Button button, ButtonState state, Handler handler, string helpText)
ButtonState
Napin (minkä tahansa) asento.
GamePad ControllerOne
Peliohjain yksi.
Peliluokka reaaliaikaisille peleille.
Listener InContext(ListenContext context)
Kuuntelee tapahtumaa vain tietyssä kontekstissa.
Phone Phone
Phone-olio esim. puhelimen tärisyttämiseen.
Keyboard Keyboard
Näppäimistö.
Listener Listen(Key k, ButtonState state, Handler handler, String helpText)
static readonly Font Default
OletusFontti.
delegate void Handler()
Ohjaintapahtumankäsittelijä ilman parametreja.
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
object Tag
Vapaasti asetettava muuttuja.