Jypeli  9
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 
33 namespace Jypeli
34 {
35  public abstract class CustomQueryWindow<W> : Window where W : Widget
36  {
37  internal virtual bool OkButtonOnPhone { get { return false; } }
38 
42  public Label Message { get; private set; }
43 
47  public W QueryWidget { get; private set; }
48 
52  public PushButton OKButton { get; private set; }
53 
54  public override Color Color
55  {
56  get { return base.Color; }
57  set
58  {
59  QueryWidget.Color = value;
60 
61 #if WINDOWS_PHONE || ANDROID
62  if ( OkButtonOnPhone )
63 #endif
64  {
65  OKButton.Color = Color.Darker( value, 40 );
66  }
67  base.Color = value;
68  }
69  }
70 
75  public CustomQueryWindow( string message )
76  {
77  Game.AssertInitialized<string>( Initialize, message );
78  }
79 
86  public CustomQueryWindow( double width, double height, string message )
87  : base( width, height )
88  {
89  Game.AssertInitialized<string>( Initialize, message );
90  }
91 
92  private void Initialize( string message )
93  {
94  Layout = new VerticalLayout { Spacing = 20, LeftPadding = 15, RightPadding = 15, TopPadding = 15, BottomPadding = 15 };
95 
96 //#if ANDROID
97  int maxWidth = (int)Game.Screen.Width - 30;
98  Message = new Label(Math.Min(maxWidth, Font.Default.MeasureSize(message).X), 100, message)
99  { SizeMode = TextSizeMode.Wrapped, VerticalSizing = Sizing.Expanding };
100 //#else
101 // Wrapped text and layouts don't work that well together... :/
102 // A simple workaround:
103 // Message = new Label( 600, 100, message );
104 // Message.SizeMode = TextSizeMode.Wrapped;
105 // Message.HorizontalAlignment = HorizontalAlignment.Left;
106 // Message.VerticalAlignment = VerticalAlignment.Top;
107 //#endif
108  Add(Message);
109 
111  Add( QueryWidget );
112 
113 #if WINDOWS_PHONE || ANDROID
114  if ( OkButtonOnPhone )
115 #endif
116  {
117  // By adding some space, we make it harder to accidently hit the OK button, especially on the phone.
118  // Buttonrow is created in order to move the button to the right edge, for the same reason.
119  Add( new VerticalSpacer { PreferredSize = new Vector( 1, 20 ), VerticalSizing = Sizing.FixedSize } );
120  Add( CreateButtonRow() );
121  }
122 
123  AddedToGame += AddListeners;
124  }
125 
127  {
128  // Button row with only one button :)
129  Widget buttonRow = new Widget( new HorizontalLayout() ) { Color = Color.Transparent };
130  buttonRow.Add( new HorizontalSpacer() );
131 
132  OKButton = new PushButton( "OK" );
133 #if WINDOWS_PHONE
134  if ( Game.Instance.Phone.DisplayResolution == DisplayResolution.Large )
135  OKButton.TextScale = new Vector(2, 2);
136  else if ( Game.Instance.Phone.DisplayResolution == DisplayResolution.HD720 )
137  OKButton.TextScale = new Vector( 3, 3 );
138 #endif
139  OKButton.Clicked += new Action(Close);
140  buttonRow.Add( OKButton );
141 
142  return buttonRow;
143  }
144 
145  protected abstract W CreateQueryWidget();
146 
147  private void AddListeners()
148  {
149 #if WINDOWS_PHONE || ANDROID
150  if ( !OkButtonOnPhone )
151  var l = Game.Instance.TouchPanel.Listen( ButtonState.Pressed, delegate { Close(); }, null ).InContext( this );
152 #else
153  var l = Game.Instance.Keyboard.Listen( Key.Enter, ButtonState.Pressed, OKButton.Click, null ).InContext( this );
154 #endif
155  associatedListeners.Add(l);
156  }
157  }
158 }
Jypeli.CustomQueryWindow.AddListeners
void AddListeners()
Definition: CustomQueryWindow.cs:147
Jypeli.CustomQueryWindow.Message
Label Message
Viesti tai kysymys.
Definition: CustomQueryWindow.cs:42
Jypeli.HorizontalSpacer
Definition: ILayout.cs:53
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli
Definition: Automobile.cs:5
Jypeli.TextSizeMode
TextSizeMode
Definition: Label.cs:38
Jypeli.Font.Default
static readonly Font Default
Oletusfontti.
Definition: Font.cs:30
Jypeli.Window.Close
void Close()
Sulkee ikkunan.
Definition: Window.cs:213
Jypeli.Label
Tekstikenttä.
Definition: Label.cs:66
Jypeli.Label.TextScale
Vector TextScale
Tekstin skaalaus. Oletus (1,1) ; isompi suurempi.
Definition: Label.cs:117
Jypeli.Game.Phone
Device Phone
Phone-olio esim. puhelimen tärisyttämiseen.
Definition: Game.cs:109
Jypeli.ScreenView.Width
double Width
Näytön leveys x-suunnassa.
Definition: View.cs:222
Jypeli.Game.AssertInitialized
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: DelayedActions.cs:53
Jypeli.TouchPanel.Listen
Listener Listen(ButtonState state, TouchHandler handler, string helpText)
Kuuntelee kosketusnäyttöä.
Definition: TouchPanel.cs:296
Jypeli.CustomQueryWindow.QueryWidget
W QueryWidget
Kysymyskomponentti.
Definition: CustomQueryWindow.cs:47
Jypeli.CustomQueryWindow.OkButtonOnPhone
virtual bool OkButtonOnPhone
Definition: CustomQueryWindow.cs:37
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.DisplayResolution.HD720
static readonly DisplayResolution HD720
HD720-tarkkuus (720p, 1280 x 720). Ei toimi kaikilla puhelimilla.
Definition: DisplayResolution.cs:23
Jypeli.Widget
Käyttöliittymän komponentti.
Definition: Appearance.cs:6
Jypeli.Color.Transparent
static readonly Color Transparent
Läpinäkyvä väri.
Definition: Color.cs:878
Jypeli.PushButton.Click
void Click()
Definition: PushButton.cs:332
Jypeli.Font.MeasureSize
Vector MeasureSize(string str)
Laskee tekstin koon fontilla.
Definition: Font.cs:282
Jypeli.VerticalLayout
Asettelee widgetit päällekäin, järjestyksessä ylhäältä alas.
Definition: VerticalLayout.cs:39
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.CustomQueryWindow
Definition: CustomQueryWindow.cs:36
Jypeli.PushButton
Painonappi.
Definition: PushButton.cs:40
Jypeli.DisplayResolution.Large
static readonly DisplayResolution Large
Suuri tarkkuus (WVGA, 800 x 480). Oletus WP8:lla.
Definition: DisplayResolution.cs:17
Jypeli.CustomQueryWindow.Initialize
void Initialize(string message)
Definition: CustomQueryWindow.cs:92
Jypeli.PushButton.Color
override Color Color
Definition: PushButton.cs:127
Jypeli.CustomQueryWindow.CreateQueryWidget
abstract W CreateQueryWidget()
Jypeli.Color
Väri.
Definition: Color.cs:13
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.ButtonState
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Widget.associatedListeners
List< Listener > associatedListeners
Tähän listaan lisätyt kuuntelijat tuhotaan automaattisesti kun Widget poistetaan pelistä.
Definition: Control.cs:14
Jypeli.Color.Darker
static Color Darker(Color c, int howMuch)
Antaa tummemman värin. Vähentaa jokaista kolmea osaväriä arvon howMuch verran.
Definition: Color.cs:417
Jypeli.Game.TouchPanel
TouchPanel TouchPanel
Kosketusnäyttö
Definition: Controls.cs:62
Jypeli.Window
Ikkuna.
Definition: Window.cs:37
Jypeli.HorizontalLayout
Asettelee widgetit riviin vaakasuunnassa.
Definition: HorizontalLayout.cs:39
Jypeli.Font
Fontti.
Definition: Font.cs:23
Jypeli.CustomQueryWindow.CreateButtonRow
Widget CreateButtonRow()
Definition: CustomQueryWindow.cs:126
Jypeli.PushButton.Clicked
Action Clicked
Tapahtuu kun nappia on painettu.
Definition: PushButton.cs:144
Jypeli.CustomQueryWindow.CustomQueryWindow
CustomQueryWindow(string message)
Alustaa uuden kyselyikkunan.
Definition: CustomQueryWindow.cs:75
Jypeli.Game
Definition: Content.cs:46
Jypeli.Color.Color
Color(XnaColor c)
Definition: Color.cs:38
Jypeli.Key
Key
Näppäimistön näppäin.
Definition: Key.cs:38
Jypeli.DisplayResolution
Definition: DisplayResolution.cs:6
Jypeli.VerticalSpacer
Definition: ILayout.cs:64
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.CustomQueryWindow.CustomQueryWindow
CustomQueryWindow(double width, double height, string message)
Alustaa uuden kyselyikkunan kiinteän kokoiseksi.
Definition: CustomQueryWindow.cs:86
Jypeli.CustomQueryWindow.OKButton
PushButton OKButton
OK-painike
Definition: CustomQueryWindow.cs:52