Jypeli  5
The simple game programming library
InputWindow.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
28  */
29 
30 
31 using System;
32 using System.Collections.Generic;
33 using System.Linq;
34 using System.Text;
35 using System.ComponentModel;
36 
37 #if WINDOWS_PHONE
38 using Microsoft.Xna.Framework;
39 using Microsoft.Xna.Framework.GamerServices;
40 #endif
41 
42 namespace Jypeli.Widgets
43 {
48  public class InputWindow : CustomQueryWindow<InputBox>
49  {
50  internal override bool OkButtonOnPhone { get { return true; } }
51 
55  public int MaxCharacters
56  {
57  get { return InputBox.MaxCharacters; }
58  set { InputBox.MaxCharacters = value; }
59  }
60 
64  public InputBox InputBox
65  {
66  get { return QueryWidget; }
67  }
68 
73  public bool ShowWindowOnPhone { get; set; }
74 
78  public delegate void InputWindowHandler( InputWindow sender );
79 
83  public event InputWindowHandler TextEntered;
84 
85  private void OnTextEntered()
86  {
87  if ( TextEntered != null )
88  TextEntered(this);
89  }
90 
95  public InputWindow( string question )
96  : base( question )
97  {
98  Closed += new WindowHandler( InputWindow_Closed );
99  AddedToGame += AddListeners;
100  }
101 
108  public InputWindow( double width, double height, string question )
109  : base( width, height, question )
110  {
111  Closed += new WindowHandler( InputWindow_Closed );
112  }
113 
114  protected override InputBox CreateQueryWidget()
115  {
116  //double widthInChars = Width / Font.Default.CharacterWidth;
117  //return new InputBox( (int)widthInChars - 1 );
118  return new InputBox( 40 );
119  }
120 
121  static void InputWindow_Closed( Window sender )
122  {
123  ((InputWindow)sender).OnTextEntered();
124  }
125 
126  private void Cancel()
127  {
128  InputBox.Text = "";
129  Close();
130  }
131 
132  private void AddListeners()
133  {
134 #if WINDOWS_PHONE
135  if ( !ShowWindowOnPhone )
136  {
137  if ( !Guide.IsVisible )
138  Guide.BeginShowKeyboardInput( PlayerIndex.One, "", Message.Text, InputBox.Text, TouchTextEntered, this );
139  }
140 #endif
141 
142  Game.Instance.Keyboard.Listen( Key.Enter, ButtonState.Pressed, Close, null ).InContext( this );
143  Game.Instance.Keyboard.Listen( Key.Escape, ButtonState.Pressed, Cancel, null ).InContext( this );
144  Game.Instance.PhoneBackButton.Listen( Cancel, null ).InContext( this );
145  }
146 
147 #if WINDOWS_PHONE
148  void TouchTextEntered( IAsyncResult result )
149  {
150  string typedText = Guide.EndShowKeyboardInput( result );
151  InputBox.Text = typedText != null ? typedText : "";
152  Close();
153  }
154 #endif
155  }
156 }
PhoneBackButton PhoneBackButton
Definition: Game.cs:260
InputWindow(double width, double height, string question)
Alustaa uuden tekstinkyselyikkunan.
Definition: InputWindow.cs:108
int MaxCharacters
Suurin määrä merkkejä joita tekstilaatikkoon voi kirjoittaa.
Definition: InputBox.cs:67
Laatikko, johon käyttäjä voi syöttää tekstiä.
Definition: InputBox.cs:47
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
static Game Instance
Definition: Game.cs:149
override InputBox CreateQueryWidget()
Definition: InputWindow.cs:114
Ikkuna, joka sisältää käyttäjän määrittelemän kysymyksen, tekstinsyöttökentän ja OK-painikkeen. Ikkunan koko määräytyy automaattisesti tekstin ja ruudun koon mukaan.
Definition: InputWindow.cs:48
override string Text
Definition: InputBox.cs:94
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Keyboard Keyboard
Näppäimistö.
Definition: Game.cs:248
InputWindow(string question)
Alustaa uuden tekstinkyselyikkunan.
Definition: InputWindow.cs:95
Key
Näppäimistön näppäin.
Definition: Key.cs:37
InputWindowHandler TextEntered
Tapahtuu kun käyttäjä on syöttänyt tekstin ja painanut OK / sulkenut ikkunan.
Definition: InputWindow.cs:83