Jypeli  5
The simple game programming library
Phone.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using Microsoft.Xna.Framework;
3 
4 #if WINDOWS_PHONE
5 using Microsoft.Phone.Shell;
6 using System.IO.IsolatedStorage;
7 using System.Xml.Serialization;
8 using System.Reflection;
9 #endif
10 
11 
12 namespace Jypeli.WP7
13 {
17  public enum DisplayOrientation
18  {
22  Landscape,
23 
28 
33 
37  Portrait,
38  }
39 
43  public enum DisplayResolution
44  {
48  Small,
49 
53  Large,
54  }
55 
60  public class Phone
61  {
62  private DisplayOrientation _displayOrientation = DisplayOrientation.Landscape;
63  private DisplayResolution _displayResolution = DisplayResolution.Small;
64 
68  public event Action Activated;
69 
73  public event Action Deactivated;
74 
75 #if WINDOWS_PHONE
76  private void OnActivated( object sender, ActivatedEventArgs args )
77  {
78  if ( Activated != null )
79  Activated();
80  }
81 
82  private void OnDeactivated( object sender, DeactivatedEventArgs args )
83  {
84  if ( Deactivated != null )
85  Deactivated();
86  }
87 #endif
88 
93  public void Vibrate(int milliSeconds)
94  {
95 #if WINDOWS_PHONE
96  Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromMilliseconds(milliSeconds));
97 #endif
98  }
99 
103  public void StopVibrating()
104  {
105 #if WINDOWS_PHONE
106  Microsoft.Devices.VibrateController.Default.Stop();
107 #endif
108  }
109 
110  private static void GetScreenSize(DisplayResolution resolution, out int width, out int height)
111  {
112  switch (resolution)
113  {
114  case DisplayResolution.Small:
115  width = 240;
116  height = 400;
117  break;
118  default:
119  width = 480;
120  height = 800;
121  break;
122  }
123  }
124 
129  {
130  get { return _displayResolution; }
131  set
132  {
133  if (_displayResolution != value)
134  {
135  _displayResolution = value;
136  ResetScreen();
137  }
138  }
139  }
140 
145  {
146  get { return _displayOrientation; }
147  set
148  {
149  if (_displayOrientation != value)
150  {
151  _displayOrientation = value;
152  Game.Instance.Accelerometer.DisplayOrientation = value;
153  ResetScreen();
154  }
155  }
156  }
157 
158  private bool _tombstoning = false;
159 
163  public bool Tombstoning
164  {
165  get { return _tombstoning; }
166  set
167  {
168 #if WINDOWS_PHONE
169  if ( value && !_tombstoning )
170  {
171  PhoneApplicationService.Current.Deactivated += SaveTombstone;
172  _tombstoning = true;
173  }
174  else if ( !value && _tombstoning )
175  {
176  PhoneApplicationService.Current.Deactivated -= SaveTombstone;
177  _tombstoning = false;
178  }
179 #endif
180  }
181  }
182 
183 #if WINDOWS_PHONE
184  internal Phone()
185  {
186  PhoneApplicationService.Current.Launching += NewGame;
187  PhoneApplicationService.Current.Activated += LoadTombstone;
188  PhoneApplicationService.Current.Activated += OnActivated;
189  PhoneApplicationService.Current.Deactivated += OnDeactivated;
190  }
191 
192  void NewGame( object sender, LaunchingEventArgs e )
193  {
194  Game.Instance.CallBegin();
195  }
196 
197  internal void LoadTombstone( object sender, ActivatedEventArgs e )
198  {
199  Type gameType = Game.Instance.GetType();
200  bool hasContinue = true; // gameType.GetMethod("Continue", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) != null;
201 
202  if ( hasContinue && e.IsApplicationInstancePreserved )
204  else
205  Game.Instance.CallBegin();
206 
207  Game.Instance.LoadGame( "tombstone" );
208  }
209 
210  internal void SaveTombstone( object sender, DeactivatedEventArgs e )
211  {
212  Game.Instance.SaveGame( "tombstone" );
213  }
214 #endif
215 
216 
217  internal void ResetScreen()
218  {
219 #if WINDOWS_PHONE
220  int screenWidth, screenHeight;
221  GraphicsDeviceManager graphics = Game.GraphicsDeviceManager;
222  GetScreenSize(_displayResolution, out screenWidth, out screenHeight);
223 
224  switch (_displayOrientation)
225  {
226  case DisplayOrientation.Landscape:
227  graphics.SupportedOrientations = Microsoft.Xna.Framework.DisplayOrientation.LandscapeLeft | Microsoft.Xna.Framework.DisplayOrientation.LandscapeRight;
228  Game.Instance.SetWindowSize(screenHeight, screenWidth);
229  break;
230  case DisplayOrientation.LandscapeLeft:
231  graphics.SupportedOrientations = Microsoft.Xna.Framework.DisplayOrientation.LandscapeLeft;
232  Game.Instance.SetWindowSize(screenHeight, screenWidth);
233  break;
234  case DisplayOrientation.LandscapeRight:
235  graphics.SupportedOrientations = Microsoft.Xna.Framework.DisplayOrientation.LandscapeRight;
236  Game.Instance.SetWindowSize(screenHeight, screenWidth);
237  break;
238  case DisplayOrientation.Portrait:
239  graphics.SupportedOrientations = Microsoft.Xna.Framework.DisplayOrientation.Portrait;
240  Game.Instance.SetWindowSize(screenWidth, screenHeight);
241  break;
242  default:
243  break;
244  }
245 
246  graphics.ApplyChanges();
247 #endif
248  }
249  }
250 }
void LoadGame(string tagName)
Lataa pelin.
Definition: Game.cs:1508
void SaveGame(string tagName)
Tallentaa pelin.
Definition: Game.cs:1474
Accelerometer Accelerometer
Kiihtyvyysanturi. Vain kännykässä.
Definition: Game.cs:285
Action Activated
Tapahtuu kun puhelin palaa tombstone-tilasta
Definition: Phone.cs:68
Vaakasuuntainen. näyttö kääntyy automaattisesti, jos puhelin käännetään toisinpäin.
DisplayOrientation
Puhelimen näytön asemointi.
Definition: Phone.cs:17
virtual void Continue()
Tässä alustetaan peli tombstoning-tilasta. Jos metodia ei ole määritelty, kutsutaan Begin...
Definition: Game.cs:1279
static Game Instance
Definition: Game.cs:149
Aliohjelmia ja ominaisuuksia, jotka toimivat vain puhelimessa. Voidaan kutsua myös PC:lle käännettäessä, ...
Definition: Phone.cs:60
Action Deactivated
Tapahtuu kun puhelin siirtyy tombstone-tilaan
Definition: Phone.cs:73
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
DisplayResolution
Puhelimen näytön tarkkuus.
Definition: Phone.cs:43
Vaakasuuntainen, oikealle käännetty.
void Vibrate(int milliSeconds)
Värisyttää puhelinta.
Definition: Phone.cs:93
void StopVibrating()
Lopettaa puhelimen värinän.
Definition: Phone.cs:103
bool SetWindowSize(int width, int height, bool fullscreen)
Asettaa ikkunan koon.
Definition: Game.cs:1386
Vaakasuuntainen, vasemmalle käännetty.
Pieni tarkkuus. Paremi suorituskyky ja pienempi akun kulutus.