Jypeli  5
The simple game programming library
CustomQueryWindow.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 using Jypeli.GameObjects;
37 
38 namespace Jypeli.Widgets
39 {
40  public abstract class CustomQueryWindow<W> : Window where W : Widget
41  {
42  internal virtual bool OkButtonOnPhone { get { return false; } }
43 
47  public Label Message { get; private set; }
48 
52  public W QueryWidget { get; private set; }
53 
57  public PushButton OKButton { get; private set; }
58 
59  public override Color Color
60  {
61  get { return base.Color; }
62  set
63  {
64  QueryWidget.Color = value;
65 
66 #if WINDOWS_PHONE
67  if ( OkButtonOnPhone )
68 #endif
69  {
70  OKButton.Color = Color.Darker( value, 40 );
71  }
72  base.Color = value;
73  }
74  }
75 
80  public CustomQueryWindow( string message )
81  {
82  Game.AssertInitialized<string>( Initialize, message );
83  }
84 
91  public CustomQueryWindow( double width, double height, string message )
92  : base( width, height )
93  {
94  Game.AssertInitialized<string>( Initialize, message );
95  }
96 
97  private void Initialize( string message )
98  {
99  Layout = new VerticalLayout { Spacing = 20, LeftPadding = 15, RightPadding = 15, TopPadding = 15, BottomPadding = 15 };
100 
101  // Wrapped text and layouts don't work that well together... :/
102  // A simple workaround:
103 #if WINDOWS_PHONE
104  Message = new Label( message );
105 #else
106  Message = new Label( 600, 100, message );
107 #endif
108 
109  Message.SizeMode = TextSizeMode.Wrapped;
110  Message.HorizontalAlignment = HorizontalAlignment.Left;
111  Message.VerticalAlignment = VerticalAlignment.Top;
112  Add( Message );
113 
114  QueryWidget = CreateQueryWidget();
115  Add( QueryWidget );
116 
117 #if WINDOWS_PHONE
118  if ( OkButtonOnPhone )
119 #endif
120  {
121  // By adding some space, we make it harder to accidently hit the OK button, especially on the phone.
122  // Buttonrow is created in order to move the button to the right edge, for the same reason.
123  Add( new VerticalSpacer { PreferredSize = new Vector( 1, 20 ), VerticalSizing = Sizing.FixedSize } );
124  Add( CreateButtonRow() );
125  }
126 
127  AddedToGame += AddListeners;
128  }
129 
130  private Widget CreateButtonRow()
131  {
132  // Button row with only one button :)
133  Widget buttonRow = new Widget( new HorizontalLayout() ) { Color = Color.Transparent };
134  buttonRow.Add( new HorizontalSpacer() );
135 
136  OKButton = new PushButton( "OK" );
137 #if WINDOWS_PHONE
138  if ( Game.Instance.Phone.DisplayResolution == WP7.DisplayResolution.Large )
139  OKButton.TextScale = new Vector(2, 2);
140 #endif
141  OKButton.Clicked += new Action(Close);
142  buttonRow.Add( OKButton );
143 
144  return buttonRow;
145  }
146 
147  protected abstract W CreateQueryWidget();
148 
149  private void AddListeners()
150  {
151 #if WINDOWS_PHONE
152  if ( !OkButtonOnPhone )
153  Game.Instance.TouchPanel.Listen( ButtonState.Pressed, delegate { Close(); }, null ).InContext( this );
154 #else
155  Game.Instance.Keyboard.Listen( Key.Enter, ButtonState.Pressed, OKButton.Click, null ).InContext( this );
156 #endif
157  }
158  }
159 }
HorizontalAlignment
Asemointi vaakasuunnassa.
Definition: View.cs:177
CustomQueryWindow(double width, double height, string message)
Alustaa uuden kyselyikkunan kiinteän kokoiseksi.
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:407
Sizing
Olion koon asettaminen asettelijan sisällä.
Definition: ILayout.cs:38
Tekstikenttä.
Definition: Label.cs:65
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Asettelee widgetit riviin vaakasuunnassa.
static Game Instance
Definition: Game.cs:149
Käyttöliittymän komponentti.
Definition: Appearance.cs:9
CustomQueryWindow(string message)
Alustaa uuden kyselyikkunan.
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:869
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
Phone Phone
Phone-olio esim. puhelimen tärisyttämiseen.
Definition: Game.cs:292
Keyboard Keyboard
Näppäimistö.
Definition: Game.cs:248
TouchPanel TouchPanel
Kosketusnäyttö. Vain kännykässä.
Definition: Game.cs:258
Väri.
Definition: Color.cs:13
VerticalAlignment
Asemointi pystysuunnassa.
Definition: View.cs:198
void Add(IGameObject childObject)
Lisää annetun peliolion tämän olion lapseksi. Lapsiolio liikkuu tämän olion mukana, ja sen paikka ja koko ilmaistaan suhteessa tähän olioon.
Key
Näppäimistön näppäin.
Definition: Key.cs:37
2D-vektori.
Definition: Vector.cs:56
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: Game.cs:630