Jypeli  5
The simple game programming library
MultiSelectWindow.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Jypeli.Controls;
6 using Jypeli.GameObjects;
7 
8 namespace Jypeli.Widgets
9 {
13  public class MultiSelectWindow : Window
14  {
15  static readonly Key[] keys = { Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.D0 };
16 
17  private int _defaultCancel = 0;
18  private Listener _escListener = null;
19  private Listener _xboxListener = null;
20  private Listener _backListener = null;
21 
22  private int _selectedIndex = -1;
23  private Color _selectedColor = Color.Black;
24  private Color _selectionColor = Color.Cyan;
25  private bool _buttonColorSet = false;
26  private Font _font;
27 
28  private Dictionary<int, List<Action>> handlers = new Dictionary<int,List<Action>>();
29  private int clickedButton = -1;
30 
35 
39  public PushButton[] Buttons { get; private set; }
40 
44  public Font Font
45  {
46  get
47  {
48  return this._font;
49  }
50  set
51  {
52  QuestionLabel.Font = value;
53 
54  for (int i = 0; i < Buttons.Length; i++)
55  {
56  Buttons[i].Font = value;
57  }
58  }
59  }
60 
65  public int DefaultCancel
66  {
67  get { return _defaultCancel; }
68  set
69  {
70  _defaultCancel = value;
71  if (Game.Instance != null) AddDefaultControls();
72  }
73  }
74 
78  public int SelectedIndex
79  {
80  get { return _selectedIndex; }
81  set
82  {
83  if ( !IsAddedToGame ) RememberSelection = true;
84  SelectButton( value );
85  }
86  }
87 
91  public PushButton SelectedButton
92  {
93  get { return _selectedIndex >= 0 || _selectedIndex >= Buttons.Length ? Buttons[_selectedIndex] : null; }
94  }
95 
99  public override Color Color
100  {
101  get
102  {
103  return base.Color;
104  }
105  set
106  {
107  if (!_buttonColorSet) _setButtonColor(Color.Darker(value, 40));
108  base.Color = value;
109  }
110  }
111 
115  public Color SelectionColor
116  {
117  get { return _selectionColor; }
118  set
119  {
120  _selectionColor = value;
121  SelectButton(_selectedIndex);
122  }
123  }
124 
128  public bool RememberSelection { get; set; }
129 
134  public event Action<int> ItemSelected;
135 
141  public MultiSelectWindow(string question, params string[] buttonTexts)
142  : base()
143  {
144  if (buttonTexts.Length == 0) throw new InvalidOperationException("You must add at least one button");
145 
146  VerticalScrollLayout layout = new VerticalScrollLayout();
147  layout.LeftPadding = layout.RightPadding = 20;
148  layout.BottomPadding = 30;
149  layout.Spacing = 20;
150  this.Layout = layout;
151 
152  QuestionLabel = new Label(question);
153  Add(QuestionLabel);
154 
155  this.Closed += CallButtonHandlers;
156 
157  Buttons = new PushButton[buttonTexts.Length];
158  this._font = Font.Default;
159  for (int i = 0; i < buttonTexts.Length; i++)
160  {
161  PushButton button = new PushButton(buttonTexts[i]);
162  button.Tag = i;
163  button.Clicked += new Action(delegate { ButtonClicked((int)button.Tag); });
164 #if WINDOWS_PHONE
165  if ( Game.Instance.Phone.DisplayResolution == WP7.DisplayResolution.Large )
166  button.TextScale = new Vector(2, 2);
167 #endif
168  Add(button);
169  Buttons[i] = button;
170  }
171 
172  Game.AssertInitialized(delegate { AddControls(); AddDefaultControls(); });
173  AddedToGame += delegate { SelectButton( (RememberSelection && _selectedIndex >= 0) ? _selectedIndex : 0 ); };
174  }
175 
176  private void SelectButton(int p)
177  {
178 #if !WINDOWS_PHONE
179  UnselectButton();
180  if (p < 0 || p >= Buttons.Length) return;
181 
182  _selectedIndex = p;
183  _selectedColor = SelectedButton.Color;
184  SelectedButton.Color = SelectionColor;
185 #endif
186  }
187 
188  private void UnselectButton()
189  {
190  if (_selectedIndex < 0) return;
191  SelectedButton.Color = _selectedColor;
192  _selectedIndex = -1;
193  }
194 
195  public void AddItemHandler(int item, Action handler)
196  {
197  if ( !handlers.ContainsKey(item) )
198  handlers[item] = new List<Action>();
199 
200  handlers[item].Add( handler );
201  }
202 
203  public void AddItemHandler<T1>(int item, Action<T1> handler, T1 p1)
204  {
205  if ( !handlers.ContainsKey( item ) )
206  handlers[item] = new List<Action>();
207 
208  handlers[item].Add( delegate { handler( p1 ); } );
209  }
210 
211  public void AddItemHandler<T1, T2>(int item, Action<T1, T2> handler, T1 p1, T2 p2)
212  {
213  if ( !handlers.ContainsKey( item ) )
214  handlers[item] = new List<Action>();
215 
216  handlers[item].Add( delegate { handler( p1, p2 ); } );
217  }
218 
219  public void AddItemHandler<T1, T2, T3>(int item, Action<T1, T2, T3> handler, T1 p1, T2 p2, T3 p3)
220  {
221  if ( !handlers.ContainsKey( item ) )
222  handlers[item] = new List<Action>();
223 
224  handlers[item].Add( delegate { handler( p1, p2, p3 ); } );
225  }
226 
227  public void RemoveItemHandler(int item, Action handler)
228  {
229  if ( !handlers.ContainsKey( item ) )
230  return;
231 
232  handlers[item].Remove( handler );
233  }
234 
235  private void _setButtonColor(Color color)
236  {
237  for (int i = 0; i < Buttons.Length; i++)
238  {
239  Buttons[i].Color = color;
240  }
241 
242  // Re-set the color for selected item and reselect it
243  _selectedColor = color;
244  SelectButton(_selectedIndex);
245  }
246 
247  public void SetButtonColor(Color color)
248  {
249  _setButtonColor(color);
250  _buttonColorSet = true;
251  }
252 
253  public void SetButtonTextColor(Color color)
254  {
255  for (int i = 0; i < Buttons.Length; i++)
256  {
257  Buttons[i].TextColor = color;
258  }
259  }
260 
261  void AddControls()
262  {
263  for (int i = 0; i < Math.Min(Buttons.Length, keys.Length); i++)
264  {
265  Jypeli.Game.Instance.Keyboard.Listen(keys[i], ButtonState.Pressed, ButtonClicked, null, i).InContext(this);
266  }
267 
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(); };
271 
272  Jypeli.Game.Instance.Keyboard.Listen(Key.Up, ButtonState.Pressed, selectPrev, null).InContext(this);
273  Jypeli.Game.Instance.Keyboard.Listen(Key.Down, ButtonState.Pressed, selectNext, null).InContext(this);
274  Jypeli.Game.Instance.Keyboard.Listen(Key.Enter, ButtonState.Pressed, confirmSelect, null).InContext(this);
275 
276  Jypeli.Game.Instance.ControllerOne.Listen(Button.DPadUp, ButtonState.Pressed, selectPrev, null).InContext(this);
277  Jypeli.Game.Instance.ControllerOne.Listen(Button.DPadDown, ButtonState.Pressed, selectNext, null).InContext(this);
278  Jypeli.Game.Instance.ControllerOne.Listen(Button.A, ButtonState.Pressed, confirmSelect, null).InContext(this);
279  }
280 
281  void AddDefaultControls()
282  {
283  if (_backListener != null) Jypeli.Game.Instance.PhoneBackButton.Remove(_backListener);
284  if (_escListener != null) Jypeli.Game.Instance.Keyboard.Remove(_escListener);
285  if (_xboxListener != null) Jypeli.Game.Instance.Keyboard.Remove(_xboxListener);
286 
287  if (_defaultCancel >= 0 && _defaultCancel < Buttons.Length)
288  {
289  _backListener = Jypeli.Game.Instance.PhoneBackButton.Listen(Buttons[_defaultCancel].Click, null).InContext(this);
290  _escListener = Jypeli.Game.Instance.Keyboard.Listen(Key.Escape, ButtonState.Pressed, Buttons[_defaultCancel].Click, null).InContext(this);
291  _xboxListener = Jypeli.Game.Instance.ControllerOne.Listen(Button.B, ButtonState.Pressed, Buttons[_defaultCancel].Click, null).InContext(this);
292  }
293  }
294 
295  void ButtonClicked(int index)
296  {
297  clickedButton = index;
298  Close();
299  }
300 
301  void CallButtonHandlers( Window window )
302  {
303  if ( clickedButton < 0 || clickedButton >= Buttons.Length )
304  return;
305 
306  if ( handlers.ContainsKey( clickedButton ) )
307  handlers[clickedButton].ForEach( handler => handler() );
308 
309  if ( ItemSelected != null )
310  ItemSelected( clickedButton );
311  }
312  }
313 }
PhoneBackButton PhoneBackButton
Definition: Game.cs:260
Font Font
Tekstin fontti.
Definition: Label.cs:221
static readonly Color Black
Musta.
Definition: Color.cs:494
static readonly Color Cyan
Syaani.
Definition: Color.cs:534
Vector TextScale
Tekstin skaalaus. Oletus (1,1) ; isompi suurempi.
Definition: Label.cs:119
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:407
Fontti.
Definition: Font.cs:22
Listener Listen(Button button, ButtonState state, Handler handler, string helpText)
Definition: GamePad.cs:415
Action< int > ItemSelected
Tapahtuma joka tapahtuu kun nappia painetaan. Ottaa parametrikseen painonapin indeksin (alkaen nollas...
Tekstikenttä.
Definition: Label.cs:65
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
static Game Instance
Definition: Game.cs:149
void AddItemHandler(int item, Action handler)
GamePad ControllerOne
Peliohjain yksi.
Definition: Game.cs:265
void RemoveItemHandler(int item, Action handler)
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Listener Listen(Handler handler, string helpText)
Kuuntelee Windows Phonen Back-nappia.
Listener InContext(ListenContext context)
Kuuntelee tapahtumaa vain tietyssä kontekstissa.
Definition: Listeners.cs:239
Button
Definition: Button.cs:34
Phone Phone
Phone-olio esim. puhelimen tärisyttämiseen.
Definition: Game.cs:292
Keyboard Keyboard
Näppäimistö.
Definition: Game.cs:248
Listener Listen(Key k, ButtonState state, Handler handler, String helpText)
Definition: Keyboard.cs:292
static readonly Font Default
OletusFontti.
Definition: Font.cs:27
delegate void Handler()
Ohjaintapahtumankäsittelijä ilman parametreja.
Väri.
Definition: Color.cs:13
Key
Näppäimistön näppäin.
Definition: Key.cs:37
2D-vektori.
Definition: Vector.cs:56
MultiSelectWindow(string question, params string[] buttonTexts)
Luo uuden monivalintaikkunan.
Ikkuna, joka antaa käyttäjän valita yhden annetuista vaihtoehdoista.
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: Game.cs:630
object Tag
Vapaasti asetettava muuttuja.
Action Clicked
Tapahtuu kun nappia on painettu.
Definition: PushButton.cs:145