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 | |
---|
44 | /// <summary> |
---|
45 | /// Viesti tai kysymys. |
---|
46 | /// </summary> |
---|
47 | public Label Message { get; private set; } |
---|
48 | |
---|
49 | /// <summary> |
---|
50 | /// Kysymyskomponentti. |
---|
51 | /// </summary> |
---|
52 | public W QueryWidget { get; private set; } |
---|
53 | |
---|
54 | /// <summary> |
---|
55 | /// OK-painike |
---|
56 | /// </summary> |
---|
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 | |
---|
76 | /// <summary> |
---|
77 | /// Alustaa uuden kyselyikkunan. |
---|
78 | /// </summary> |
---|
79 | /// <param name="message">Viesti tai kysymys</param> |
---|
80 | public CustomQueryWindow( string message ) |
---|
81 | { |
---|
82 | Game.AssertInitialized<string>( Initialize, message ); |
---|
83 | } |
---|
84 | |
---|
85 | /// <summary> |
---|
86 | /// Alustaa uuden kyselyikkunan kiinteän kokoiseksi. |
---|
87 | /// </summary> |
---|
88 | /// <param name="width">Ikkunan leveys</param> |
---|
89 | /// <param name="height">Ikkunan korkeus</param> |
---|
90 | /// <param name="message">Viesti tai kysymys</param> |
---|
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 | } |
---|