source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/Widgets/InputWindow.cs @ 4507

Revision 4507, 5.3 KB checked in by anlakane, 6 years ago (diff)

Talletus.

Line 
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
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using System.ComponentModel;
36
37#if WINDOWS_PHONE
38using Microsoft.Xna.Framework;
39using Microsoft.Xna.Framework.GamerServices;
40#endif
41
42namespace Jypeli.Widgets
43{
44    /// <summary>
45    /// Ikkuna, joka sisältää käyttäjän määrittelemän kysymyksen, tekstinsyöttökentän ja
46    /// OK-painikkeen. Ikkunan koko määräytyy automaattisesti tekstin ja ruudun koon mukaan.
47    /// </summary>
48    public class InputWindow : CustomQueryWindow<InputBox>
49    {
50        internal override bool OkButtonOnPhone { get { return true; } }
51
52        /// <summary>
53        /// Suurin määrä merkkejä joita tekstilaatikkoon voi kirjoittaa.
54        /// </summary>
55        public int MaxCharacters
56        {
57            get { return InputBox.MaxCharacters; }
58            set { InputBox.MaxCharacters = value; }
59        }
60
61        /// <summary>
62        /// Vastauslaatikko.
63        /// </summary>
64        public InputBox InputBox
65        {
66            get { return QueryWidget; }
67        }
68
69        /// <summary>
70        /// Näytetäänkö ikkuna puhelimella. Oletuksena false, jolloin näytetään vain
71        /// puhelimen oma tekstinsyöttöikkuna.
72        /// </summary>
73        public bool ShowWindowOnPhone { get; set; }
74
75        /// <summary>
76        /// Syöttöikkunatapahtumien käsittelijä.
77        /// </summary>
78        public delegate void InputWindowHandler( InputWindow sender );
79
80        /// <summary>
81        /// Tapahtuu kun käyttäjä on syöttänyt tekstin ja painanut OK / sulkenut ikkunan.
82        /// </summary>
83        public event InputWindowHandler TextEntered;
84
85        private void OnTextEntered()
86        {
87            if ( TextEntered != null )
88                TextEntered(this);
89        }
90
91        /// <summary>
92        /// Alustaa uuden tekstinkyselyikkunan.
93        /// </summary>
94        /// <param name="question">Kysymys</param>
95        public InputWindow( string question )
96            : base( question )
97        {
98            Closed += new WindowHandler( InputWindow_Closed );
99            AddedToGame += AddListeners;
100        }
101
102        /// <summary>
103        /// Alustaa uuden tekstinkyselyikkunan.
104        /// </summary>
105        /// <param name="width">Ikkunan leveys</param>
106        /// <param name="height">Ikkunan korkeus</param>
107        /// <param name="question">Kysymys</param>
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}
Note: See TracBrowser for help on using the repository browser.