Jypeli  5
The simple game programming library
Window.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using System.ComponentModel;
7 using Microsoft.Xna.Framework.Graphics;
8 using System.Windows.Forms;
9 
10 namespace Jypeli
11 {
12  public class JypeliWindow
13  {
14  class SizeParams
15  {
16  public int Width;
17  public int Height;
18  public bool Fullscreen;
19 
20  public SizeParams(int w, int h, bool f)
21  {
22  this.Width = w;
23  this.Height = h;
24  this.Fullscreen = f;
25  }
26  }
27 
31  public delegate void MoveEvent( int oldX, int oldY, int newX, int newY );
32 
36  public delegate void ResizeEvent(int oldWidth, int oldHeight, int newWidth, int newHeight);
37 
38 #if WINDOWS
39  System.Windows.Forms.Form form;
40 #endif
41 
42  GameWindow gwin;
43  GraphicsDeviceManager gdm;
44 
45  Point? lastPosition;
46  Point? desiredPosition = null;
47 
48  SizeParams lastSize = null;
49  SizeParams desiredSize = null;
50 
51  #region Events for XNA compatibility
52 
56  [EditorBrowsable( EditorBrowsableState.Never )]
57  public event EventHandler<EventArgs> ClientSizeChanged;
58 
62  [EditorBrowsable( EditorBrowsableState.Never )]
63  public event EventHandler<EventArgs> OrientationChanged;
64 
68  [EditorBrowsable( EditorBrowsableState.Never )]
69  public event EventHandler<EventArgs> ScreenDeviceNameChanged;
70 
77  protected void OnXnaMethod( EventHandler<EventArgs> method, object sender, EventArgs args )
78  {
79  if ( method != null )
80  method( sender, args );
81  }
82 
83  #endregion
84 
88  public event Action EnterFullscreen;
89 
93  public event Action ExitFullscreen;
94 
98  public event MoveEvent Moved;
99 
103  public event ResizeEvent Resizing;
104 
108  public event ResizeEvent Resized;
109 
113  public bool AllowUserResizing
114  {
115  get { return gwin.AllowUserResizing; }
116  set { gwin.AllowUserResizing = value; }
117  }
118 
123  {
124  get { return gwin.CurrentOrientation; }
125  }
126 
130  public string Title
131  {
132  get { return gwin.Title; }
133  set { gwin.Title = value; }
134  }
135 
139  public int Width
140  {
141  get { return desiredSize != null ? desiredSize.Width : gwin.ClientBounds.Width; }
142  set
143  {
144  MakeDesiredParams();
145  desiredSize.Width = Math.Max( 1, value );
146  OnResizing( desiredSize );
147  }
148  }
149 
153  public int Height
154  {
155  get { return desiredSize != null ? desiredSize.Height : gwin.ClientBounds.Height; }
156  set
157  {
158  MakeDesiredParams();
159  desiredSize.Height = Math.Max( 1, value );
160  OnResizing( desiredSize );
161  }
162  }
163 
167  public bool Fullscreen
168  {
169  get { return desiredSize != null ? desiredSize.Fullscreen : gdm.IsFullScreen; }
170  set
171  {
172  MakeDesiredParams();
173  desiredSize.Fullscreen = value;
174  }
175  }
176 
180  public IntPtr Handle
181  {
182  get { return gwin.Handle; }
183  }
184 
188  public string ScreenDeviceName
189  {
190  get { return gwin.ScreenDeviceName; }
191  }
192 
196  public int Left
197  {
198  get
199  {
200 #if WINDOWS
201  return desiredPosition.HasValue ? desiredPosition.Value.X : form.Left;
202 #else
203  return 0;
204 #endif
205  }
206  set
207  {
208  desiredPosition = new Point( value, Top );
209  }
210  }
211 
216  public int Top
217  {
218  get
219  {
220 #if WINDOWS
221  return desiredPosition.HasValue ? desiredPosition.Value.Y : form.Top;
222 #else
223  return 0;
224 #endif
225  }
226  set
227  {
228  desiredPosition = new Point( Left, value );
229  }
230  }
231 
235  public int Right
236  {
237  get { return Left + Width; }
238  set { Left = value - Width; }
239  }
240 
245  public int Bottom
246  {
247  get { return Top + Height; }
248  set { Top = value - Height; }
249  }
250 
254  public int X
255  {
256  get { return Left + Width / 2; }
257  set { Left = value - Width / 2; }
258  }
259 
264  public int Y
265  {
266  get { return Top + Height / 2; }
267  set { Top = value - Height / 2; }
268  }
269 
275  internal JypeliWindow( GameWindow gameWindow, GraphicsDeviceManager graphicsDeviceManager )
276  {
277  this.gwin = gameWindow;
278  this.gdm = graphicsDeviceManager;
279 
280 
281 
282  gameWindow.ClientSizeChanged += ( o, a ) => OnXnaMethod( this.ClientSizeChanged, o, a );
283  gameWindow.OrientationChanged += ( o, a ) => OnXnaMethod( this.OrientationChanged, o, a );
284  gameWindow.ScreenDeviceNameChanged += ( o, a ) => OnXnaMethod( this.ScreenDeviceNameChanged, o, a );
285  gameWindow.ClientSizeChanged += ( o, a ) => OnResized();
286 
287 #if WINDOWS
288  form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle( gameWindow.Handle );
289  form.Move += (o, a) => OnMove();
290 #endif
291  }
292 
293  private void MakeDesiredParams()
294  {
295  if ( desiredSize == null )
296  desiredSize = new SizeParams( gwin.ClientBounds.Width, gwin.ClientBounds.Height, gdm.IsFullScreen );
297  }
298 
299  private void OnMove( SizeParams newParams = null )
300  {
301  if ( Moved == null || !lastPosition.HasValue )
302  return;
303 
304 #if WINDOWS
305  Moved( lastPosition.Value.X + Width / 2, lastPosition.Value.Y - Height / 2, form.Left + form.Width / 2, form.Top - form.Height / 2 );
306 #endif
307  }
308 
309  private void OnResizing( SizeParams newParams )
310  {
311  if ( Resizing == null )
312  return;
313 
314  if ( newParams.Width != gwin.ClientBounds.Width || newParams.Height != gwin.ClientBounds.Height )
315  Resizing( gwin.ClientBounds.Width, gwin.ClientBounds.Height, newParams.Width, newParams.Height );
316  }
317 
318  private void OnResized( SizeParams newParams = null )
319  {
320  if ( Resized == null )
321  return;
322 
323  int newWidth = newParams != null ? newParams.Width : gwin.ClientBounds.Width;
324  int newHeight = newParams != null ? newParams.Height : gwin.ClientBounds.Height;
325 
326  if ( newWidth != lastSize.Width || newHeight != lastSize.Height )
327  Resized( lastSize.Width, lastSize.Height, newWidth, newHeight );
328  }
329 
330  private void OnFullscreen( SizeParams newParams = null )
331  {
332  bool newFullscreen = newParams != null ? newParams.Fullscreen : gdm.IsFullScreen;
333 
334  if ( EnterFullscreen != null && !lastSize.Fullscreen && newFullscreen )
335  EnterFullscreen();
336 
337  else if ( ExitFullscreen != null && lastSize.Fullscreen && !newFullscreen )
338  ExitFullscreen();
339  }
340 
341  #region Methods for XNA compatibility
342 
346  public Microsoft.Xna.Framework.Rectangle ClientBounds
347  {
348  get { return gwin.ClientBounds; }
349  }
350 
355  [EditorBrowsable( EditorBrowsableState.Never )]
356  public void BeginScreenDeviceChange( bool willBeFullScreen )
357  {
358  gwin.BeginScreenDeviceChange( willBeFullScreen );
359  }
360 
367  [EditorBrowsable( EditorBrowsableState.Never )]
368  public void EndScreenDeviceChange( string screenDeviceName, int clientWidth, int clientHeight )
369  {
370  gwin.EndScreenDeviceChange( screenDeviceName, clientWidth, clientHeight );
371  }
372 
373  #endregion
374 
378  public void Update()
379  {
380  if ( lastSize == null )
381  lastSize = new SizeParams( gwin.ClientBounds.Width, gwin.ClientBounds.Height, gdm.IsFullScreen );
382  else
383  {
384  lastSize.Width = gwin.ClientBounds.Width;
385  lastSize.Height = gwin.ClientBounds.Height;
386  lastSize.Fullscreen = gdm.IsFullScreen;
387  }
388 
389 #if WINDOWS
390  lastPosition = new Point( form.Left, form.Top );
391 #endif
392 
393  if ( desiredSize != null )
394  {
395  xnaSetWindowSize( desiredSize );
396  OnResized( desiredSize );
397  OnFullscreen( desiredSize );
398  desiredSize = null;
399  }
400 
401 #if WINDOWS
402  if (desiredPosition.HasValue)
403  {
404  form.Left = this.Left;
405  form.Top = this.Top;
406  desiredPosition = null;
407  }
408 #endif
409 
410  }
411 
412  private void xnaSetWindowSize( SizeParams newParams )
413  {
414  gdm.PreferredBackBufferWidth = newParams.Width;
415  gdm.PreferredBackBufferHeight = newParams.Height;
416  gdm.IsFullScreen = newParams.Fullscreen;
417  gdm.ApplyChanges();
418  }
419 
420 #if WINDOWS
421  public void CenterOnScreen()
425  {
426  int x = (Screen.PrimaryScreen.Bounds.Width - gwin.ClientBounds.Width) / 2;
427  int y = (Screen.PrimaryScreen.Bounds.Height - gwin.ClientBounds.Height) / 2;
428 
429  Control formControl = Control.FromHandle(Handle);
430 
431  if (formControl == null)
432  return;
433 
434  Form form = (Form)formControl;
435  form.DesktopLocation = new System.Drawing.Point(x, y);
436  }
437 #endif
438  }
439 }
void OnXnaMethod(EventHandler< EventArgs > method, object sender, EventArgs args)
Generic XNA event handler.
Definition: Window.cs:77
void Update()
Kutsutaan Jypelin päivityssilmukasta.
Definition: Window.cs:378
bool AllowUserResizing
Voiko käyttäjä muuttaa ikkunan kokoa ikkunan reunasta.
Definition: Window.cs:114
int Height
Ikkunan korkeus.
Definition: Window.cs:154
Action ExitFullscreen
Tapahtuu kun poistutaan koko ruudun tilasta
Definition: Window.cs:93
void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
Definition: Window.cs:368
DisplayOrientation
Puhelimen näytön asemointi.
Definition: Phone.cs:17
int X
Ikkunan keskikohdan x-koordinaatti.
Definition: Window.cs:255
int Width
Ikkunan leveys.
Definition: Window.cs:140
delegate void ResizeEvent(int oldWidth, int oldHeight, int newWidth, int newHeight)
Tapahtumatyyppi ikkunan koon muutokselle.
string ScreenDeviceName
Näyttölaitteen nimi.
Definition: Window.cs:189
delegate void MoveEvent(int oldX, int oldY, int newX, int newY)
Tapahtumatyyppi ikkunan paikan muutokselle.
int Y
Ikkunan keskikohdan y-koordinaatti. Huom. y kasvaa alaspäin!
Definition: Window.cs:265
EventHandler< EventArgs > ClientSizeChanged
Definition: Window.cs:57
ResizeEvent Resizing
Tapahtuu kun ikkunan kokoa ollaan muuttamassa.
Definition: Window.cs:103
string Title
Ikkunan otsikko.
Definition: Window.cs:131
int Bottom
Ikkunan alareunan y-koordinaatti. Huom. y kasvaa alaspäin!
Definition: Window.cs:246
void BeginScreenDeviceChange(bool willBeFullScreen)
Definition: Window.cs:356
int Right
Ikkunan oikean reunan x-koordinaatti.
Definition: Window.cs:236
DisplayOrientation CurrentOrientation
Ikkunan asento puhelimessa (pysty tai vaaka)
Definition: Window.cs:123
EventHandler< EventArgs > ScreenDeviceNameChanged
Definition: Window.cs:69
int Top
Ikkunan yläreunan y-koordinaatti. Huom. y kasvaa alaspäin!
Definition: Window.cs:217
bool Fullscreen
Onko ikkuna koko näytön kokoinen ilman reunoja.
Definition: Window.cs:168
IntPtr Handle
API-kahva ikkunaan.
Definition: Window.cs:181
Action EnterFullscreen
Tapahtuu kun mennään koko ruudun tilaan.
Definition: Window.cs:88
Microsoft.Xna.Framework.Rectangle ClientBounds
Ikkunan koko ja paikka xna-neliönä.
Definition: Window.cs:347
EventHandler< EventArgs > OrientationChanged
Definition: Window.cs:63
int Left
Ikkunan vasemman reunan x-koordinaatti.
Definition: Window.cs:197
MoveEvent Moved
Tapahtuu kun ikkunaa siirretään.
Definition: Window.cs:98
ResizeEvent Resized
Tapahtuu kun ikkunan kokoa on muutettu.
Definition: Window.cs:108