Jypeli  9
The simple game programming library
InputBox.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2009-2011 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tomi Karppinen, Tero Jäntti, Rami Pasanen
28  */
29 
30 
31 using System;
32 using Microsoft.Xna.Framework;
33 
34 // TODO: text input on Windows Phone
35 
36 namespace Jypeli
37 {
41  public class InputBox : Label
42  {
44 
48  public InputBox()
49  : this(15)
50  {
51  }
52 
56  public int WidthInCharacters { get; set; }
57 
61  public int MaxCharacters { get; set; }
62 
66  public Widget Cursor { get; set; }
67 
68  public override Vector PreferredSize
69  {
70  get
71  {
73  }
74  }
75 
76  public override Vector Size
77  {
78  get { return base.Size; }
79  set
80  {
81  base.Size = value;
82  if ( Cursor != null )
84  }
85  }
86 
87  public override string Text
88  {
89  get { return base.Text; }
90  set
91  {
92  base.Text = value.Length > MaxCharacters ?
93  base.Text = value.Substring (0, MaxCharacters) : value;
94 
96  }
97  }
98 
102  public event Action<string> TextChanged;
103 
104  protected void OnTextChanged()
105  {
106  if ( TextChanged != null )
107  TextChanged( Text );
108  }
109 
110 #if ANDROID
111  private bool vkSubscribed = false;
112 #endif
113 
121  public InputBox( int characters )
122  : base()
123  {
124  MaxCharacters = int.MaxValue;
125  WidthInCharacters = characters;
127  HorizontalSizing = Sizing.Expanding;
128  XMargin = 7;
129  YMargin = 2;
131  Color = new Color( 0, 255, 255, 150 );
132  BorderColor = new Color( 200, 200, 200 );
133  SizeMode = TextSizeMode.None;
135 
137  Cursor.Color = new Color(255, 0, 0, 100);
138  Add( Cursor );
139  AddedToGame += UpdateCursorPosition;
140 
141  cursorBlinkTimer = new Timer();
144 
145  AddedToGame += OnAdded;
146  Removed += onRemoved;
147  }
148 
149  private void OnAdded()
150  {
152 
153  // TODO: Should also work on android
154 #if ANDROID
155  ShowVirtualKeyboard();
156 #endif
157 
158  Game.Instance.Window.TextInput += InputText;
159  var l = Game.Instance.Keyboard.Listen(Key.Back, ButtonState.Pressed, EraseText, null).InContext(this);
160  associatedListeners.Add(l);
161  }
162 
163 #if ANDROID
164 
165  private void ShowVirtualKeyboard()
166  {
167  // For some reason OnAdded() gets called twice on Android but only once on Windows
168  // when using EasyHighScore. What makes it even more odd is that when subscribing
169  // to AddedToGame from outside, the event is only fired once.
170  // We use vkSubscribed as a work-around to avoid subscribing to the key events
171  // multiple times.
172  Game.VirtualKeyboard.Show();
173  if (!vkSubscribed)
174  {
175  Game.VirtualKeyboard.InputEntered += VirtualKeyboard_InputEntered;
176  Game.VirtualKeyboard.EnterPressed += VirtualKeyboard_EnterPressed;
177  Game.VirtualKeyboard.BackspacePressed += VirtualKeyboard_BackspacePressed;
178  }
180  vkSubscribed = true;
181  }
182 
183  private void VirtualKeyboard_BackspacePressed(object sender, EventArgs e)
184  {
185  EraseText();
186  }
187 
188  private void VirtualKeyboard_EnterPressed(object sender, EventArgs e)
189  {
190  HideVirtualKeyboard();
191  }
192 
193  private void VirtualKeyboard_InputEntered(object sender, Controls.Keyboard.VirtualKeyboardInputEventArgs e)
194  {
195  AddText(e.Text);
196  }
197 
198  private void HideVirtualKeyboard()
199  {
200  Game.VirtualKeyboard.Hide();
201  Game.VirtualKeyboard.InputEntered -= VirtualKeyboard_InputEntered;
202  Game.VirtualKeyboard.EnterPressed -= VirtualKeyboard_EnterPressed;
203  Game.VirtualKeyboard.BackspacePressed -= VirtualKeyboard_BackspacePressed;
205  Cursor.IsVisible = false;
206  vkSubscribed = false;
207  }
208 
209 #endif
210 
211  private void onRemoved()
212  {
214 #if ANDROID
215  HideVirtualKeyboard();
216 #endif
217  Game.Instance.Window.TextInput -= InputText;
218 
219  }
220 
221  private void BlinkCursor()
222  {
223  Cursor.IsVisible = !Cursor.IsVisible;
224  }
225 
227  {
228  Cursor.Left = Math.Min( -Width / 2 + XMargin + TextSize.X, Width / 2 - Font.CharacterWidth );
229  }
230 
231 
232  void InputText( object sender, TextInputEventArgs e )
233  {
234  if ( !this.ControlContext.Active ) return;
235  if ( e.Character == 0x7F || e.Character == 0x08 ) return;
236 
237  if ( !this.Font.XnaFont.Characters.Contains( e.Character ) )
238  {
239  // Unsupported character
240  return;
241  }
242 
243  AddText(e.Character.ToString());
244  }
245 
246 
247  void AddText(string text)
248  {
249  Text += text;
250  OnTextChanged();
252  }
253 
254  void EraseText()
255  {
256  if (Text.Length == 0) return;
257  Text = Text.Substring(0, Text.Length - 1);
258  OnTextChanged();
259  }
260 
261  public override void Draw( Matrix parentTransformation, Matrix transformation )
262  {
263  if ( ! IsTruncated )
264  base.Draw( parentTransformation, transformation, Text );
265  else
266  {
267  String shownText = "";
268 
269  for ( int i = Text.Length - 1; i >= 0; i-- )
270  {
271  String newText = Text[i] + shownText.ToString();
272 
273  if ( Font.XnaFont.MeasureString( newText ).X >= Width )
274  break;
275 
276  shownText = newText;
277  }
278 
279  base.Draw( parentTransformation, transformation, shownText );
280  }
281  }
282  }
283 }
Jypeli.Font.CharacterWidth
double CharacterWidth
Merkin leveys.
Definition: Font.cs:98
Jypeli.Label.TextSize
Vector TextSize
Näytettävän tekstin koko. Ei välttämättä sama kuin Size.
Definition: Label.cs:202
Jypeli.InputBox.cursorBlinkTimer
Timer cursorBlinkTimer
Definition: InputBox.cs:43
Jypeli.Color.Black
static readonly Color Black
Musta.
Definition: Color.cs:503
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.InputBox.BlinkCursor
void BlinkCursor()
Definition: InputBox.cs:221
Jypeli.Label.XMargin
double XMargin
Marginaali vasemmasta/oikeasta reunasta.
Definition: Label.cs:236
Jypeli.Timer.Timeout
Action Timeout
Tapahtuu väliajoin.
Definition: Timer.cs:44
Jypeli
Definition: Automobile.cs:5
Jypeli.TextSizeMode
TextSizeMode
Definition: Label.cs:38
Jypeli.InputBox.UpdateCursorPosition
void UpdateCursorPosition()
Definition: InputBox.cs:226
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Label
Tekstikenttä.
Definition: Label.cs:66
Jypeli.InputBox.MaxCharacters
int MaxCharacters
Suurin määrä merkkejä joita tekstilaatikkoon voi kirjoittaa.
Definition: InputBox.cs:61
Jypeli.Font.XnaFont
SpriteFont XnaFont
Definition: Font.cs:90
Jypeli.InputBox.Text
override string? Text
Definition: InputBox.cs:88
Jypeli.InputBox.WidthInCharacters
int WidthInCharacters
Tekstilaatikon pituus kirjaimissa.
Definition: InputBox.cs:56
Jypeli.InputBox.PreferredSize
override Vector PreferredSize
Definition: InputBox.cs:69
Jypeli.Label.SizeMode
TextSizeMode SizeMode
Kuinka tekstikentän koko määräytyy.
Definition: Label.cs:193
Jypeli.Font.CharacterHeight
double CharacterHeight
Merkin korkeus.
Definition: Font.cs:106
Jypeli.InputBox.InputText
void InputText(object sender, TextInputEventArgs e)
Definition: InputBox.cs:232
Jypeli.InputBox.OnTextChanged
void OnTextChanged()
Definition: InputBox.cs:104
Jypeli.InputBox
Laatikko, johon käyttäjä voi syöttää tekstiä.
Definition: InputBox.cs:42
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Widget
Käyttöliittymän komponentti.
Definition: Appearance.cs:6
Jypeli.InputBox.InputBox
InputBox()
Alustaa uuden syöttökentän.
Definition: InputBox.cs:48
Jypeli.HorizontalAlignment
HorizontalAlignment
Asemointi vaakasuunnassa.
Definition: View.cs:441
Jypeli.InputBox.InputBox
InputBox(int characters)
Alustaa uuden syöttökentän.
Definition: InputBox.cs:121
Jypeli.Timer.Interval
double Interval
Aika sekunneissa, jonka välein TimeOut tapahtuu.
Definition: Timer.cs:87
Jypeli.InputBox.AddText
void AddText(string text)
Definition: InputBox.cs:247
Jypeli.InputBox.Cursor
Widget Cursor
Kursori.
Definition: InputBox.cs:66
Jypeli.Widget.ControlContext
ListenContext ControlContext
Definition: Control.cs:16
Jypeli.InputBox.Draw
override void Draw(Matrix parentTransformation, Matrix transformation)
Definition: InputBox.cs:261
Jypeli.Label.YMargin
double YMargin
Marginaali ylä-/alareunasta.
Definition: Label.cs:245
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Timer.Stop
void Stop()
Pysäyttää ajastimen ja nollaa sen tilan.
Definition: Timer.cs:292
Jypeli.Widget.Widget
Widget(Animation animation)
Alustaa widgetin.
Definition: Widget.cs:14
Jypeli.Game.Keyboard
Keyboard Keyboard
Näppäimistö.
Definition: Controls.cs:44
Jypeli.Label.IsTruncated
bool IsTruncated
Onko tekstiä katkaistu
Definition: Label.cs:107
Jypeli.Label.TextColor
Color TextColor
Tekstin väri.
Definition: Label.cs:207
Jypeli.ButtonState
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Timer
Ajastin, joka voidaan asettaa laukaisemaan tapahtumia tietyin väliajoin.
Definition: Timer.cs:38
Jypeli.Widget.associatedListeners
List< Listener > associatedListeners
Tähän listaan lisätyt kuuntelijat tuhotaan automaattisesti kun Widget poistetaan pelistä.
Definition: Control.cs:14
Jypeli.InputBox.onRemoved
void onRemoved()
Definition: InputBox.cs:211
Jypeli.Font
Fontti.
Definition: Font.cs:23
Jypeli.Widget.BorderColor
Color BorderColor
Reunojen väri.
Definition: Appearance.cs:10
Jypeli.InputBox.EraseText
void EraseText()
Definition: InputBox.cs:254
Jypeli.Timer.Start
void Start()
Käynnistää ajastimen.
Definition: Timer.cs:257
Jypeli.InputBox.OnAdded
void OnAdded()
Definition: InputBox.cs:149
Jypeli.Game
Definition: Content.cs:46
Jypeli.Key
Key
Näppäimistön näppäin.
Definition: Key.cs:38
Jypeli.Sizing
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:39
Jypeli.Keyboard.Listen
Listener Listen(Key k, ButtonState state, Action handler, string helpText)
Kuuntelee näppäinten painalluksia.
Definition: Keyboard.cs:161
Jypeli.InputBox.TextChanged
Action< string > TextChanged
Tapahtuma tekstin muuttumiselle.
Definition: InputBox.cs:102
Jypeli.InputBox.Size
override Vector Size
Definition: InputBox.cs:77