Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameWindowDesktop.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
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 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP && SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D
25 using System;
26 using System.Diagnostics;
27 using System.Drawing;
28 using System.Threading;
29 using System.Windows.Forms;
30 
31 using SiliconStudio.Core.Mathematics;
32 using SiliconStudio.Paradox.Graphics;
33 using SharpDX.Windows;
35 
36 namespace SiliconStudio.Paradox.Games
37 {
38  /// <summary>
39  /// An abstract window.
40  /// </summary>
41  internal class GameWindowDesktop : GameWindow
42  {
43  private bool isMouseVisible;
44 
45  private bool isMouseCurrentlyHidden;
46 
47  public Control Control;
48 
49  private WindowHandle windowHandle;
50 
51  private Form form;
52 
53  private bool isFullScreenMaximized;
54  private FormBorderStyle savedFormBorderStyle;
55  private bool oldVisible;
56  private bool deviceChangeChangedVisible;
57  private bool? deviceChangeWillBeFullScreen;
58 
59  private bool allowUserResizing;
60  private bool isBorderLess;
61 
62  internal GameWindowDesktop()
63  {
64  }
65 
66  public override WindowHandle NativeWindow
67  {
68  get
69  {
70  return windowHandle;
71  }
72  }
73 
74  public override void BeginScreenDeviceChange(bool willBeFullScreen)
75  {
76  if (willBeFullScreen && !isFullScreenMaximized && form != null)
77  {
78  savedFormBorderStyle = form.FormBorderStyle;
79  }
80 
81  if (willBeFullScreen != isFullScreenMaximized)
82  {
83  deviceChangeChangedVisible = true;
84  oldVisible = Visible;
85  Visible = false;
86 
87  if (form != null)
88  form.SendToBack();
89  }
90  else
91  {
92  deviceChangeChangedVisible = false;
93  }
94 
95  if (!willBeFullScreen && isFullScreenMaximized && form != null)
96  {
97  form.TopMost = false;
98  form.FormBorderStyle = savedFormBorderStyle;
99  }
100 
101  deviceChangeWillBeFullScreen = willBeFullScreen;
102  }
103 
104  public override void EndScreenDeviceChange(int clientWidth, int clientHeight)
105  {
106  if (!deviceChangeWillBeFullScreen.HasValue)
107  return;
108 
109  if (deviceChangeWillBeFullScreen.Value)
110  {
111  isFullScreenMaximized = true;
112  }
113  else if (isFullScreenMaximized)
114  {
115  if (form != null)
116  {
117  form.BringToFront();
118  }
119  isFullScreenMaximized = false;
120  }
121 
122  UpdateFormBorder();
123 
124  if (deviceChangeChangedVisible)
125  Visible = oldVisible;
126 
127  if (form != null)
128  {
129  form.ClientSize = new Size(clientWidth, clientHeight);
130  }
131 
132  // Notifies the GameForm about the fullscreen state
133  var gameForm = form as GameForm;
134  if (gameForm != null)
135  {
136  gameForm.IsFullScreen = isFullScreenMaximized;
137  }
138 
139  deviceChangeWillBeFullScreen = null;
140  }
141 
142  protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
143  {
144  // Desktop doesn't have orientation (unless on Windows 8?)
145  }
146 
147  internal override bool CanHandle(GameContext gameContext)
148  {
149  return gameContext.ContextType == AppContextType.Desktop;
150  }
151 
152  internal override void Initialize(GameContext gameContext)
153  {
154  this.GameContext = gameContext;
155 
156  Control = (Control)gameContext.Control;
157 
158  // Setup the initial size of the window
159  var width = gameContext.RequestedWidth;
160  if (width == 0)
161  {
162  width = Control is Form ? GraphicsDeviceManager.DefaultBackBufferWidth : Control.ClientSize.Width;
163  }
164 
165  var height = gameContext.RequestedHeight;
166  if (height == 0)
167  {
168  height = Control is Form ? GraphicsDeviceManager.DefaultBackBufferHeight : Control.ClientSize.Height;
169  }
170 
171  windowHandle = new WindowHandle(AppContextType.Desktop, Control);
172 
173  Control.ClientSize = new System.Drawing.Size(width, height);
174 
175  Control.MouseEnter += GameWindowForm_MouseEnter;
176  Control.MouseLeave += GameWindowForm_MouseLeave;
177 
178  form = Control as Form;
179  var gameForm = Control as GameForm;
180  if (gameForm != null)
181  {
182  //gameForm.AppActivated += OnActivated;
183  //gameForm.AppDeactivated += OnDeactivated;
184  gameForm.UserResized += OnClientSizeChanged;
185  }
186  else
187  {
188  Control.Resize += OnClientSizeChanged;
189  }
190  }
191 
192  internal override void Run()
193  {
194  Debug.Assert(InitCallback != null);
195  Debug.Assert(RunCallback != null);
196 
197  // Initialize the init callback
198  InitCallback();
199 
200  if (GameContext.IsUserManagingRun)
201  {
202  GameContext.RunCallback = RunCallback;
203  GameContext.ExitCallback = ExitCallback;
204  }
205  else
206  {
207  var runCallback = new WindowsMessageLoop.RenderCallback(RunCallback);
208  // Run the rendering loop
209  try
210  {
211  WindowsMessageLoop.Run(Control, () =>
212  {
213  if (Exiting)
214  {
215  if (Control != null)
216  {
217  Control.Dispose();
218  Control = null;
219  }
220  return;
221  }
222 
223  runCallback();
224  });
225  }
226  finally
227  {
228  if (ExitCallback != null)
229  {
230  ExitCallback();
231  }
232  }
233  }
234  }
235 
236  private void GameWindowForm_MouseEnter(object sender, System.EventArgs e)
237  {
238  if (!isMouseVisible && !isMouseCurrentlyHidden)
239  {
240  Cursor.Hide();
241  isMouseCurrentlyHidden = true;
242  }
243  }
244 
245  private void GameWindowForm_MouseLeave(object sender, System.EventArgs e)
246  {
247  if (isMouseCurrentlyHidden)
248  {
249  Cursor.Show();
250  isMouseCurrentlyHidden = false;
251  }
252  }
253 
254  public override bool IsMouseVisible
255  {
256  get
257  {
258  return isMouseVisible;
259  }
260  set
261  {
262  if (isMouseVisible != value)
263  {
264  isMouseVisible = value;
265  if (isMouseVisible)
266  {
267  if (isMouseCurrentlyHidden)
268  {
269  Cursor.Show();
270  isMouseCurrentlyHidden = false;
271  }
272  }
273  else if (!isMouseCurrentlyHidden)
274  {
275  Cursor.Hide();
276  isMouseCurrentlyHidden = true;
277  }
278  }
279  }
280  }
281 
282  /// <summary>
283  /// Gets or sets a value indicating whether this <see cref="GameWindow" /> is visible.
284  /// </summary>
285  /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
286  public override bool Visible
287  {
288  get
289  {
290  return Control.Visible;
291  }
292  set
293  {
294  Control.Visible = value;
295  }
296  }
297 
298  public override Int2 Position
299  {
300  get
301  {
302  if (Control == null)
303  return base.Position;
304 
305  return new Int2(Control.Location.X, Control.Location.Y);
306  }
307  set
308  {
309  if (Control != null)
310  Control.Location = new Point(value.X, value.Y);
311 
312  base.Position = value;
313  }
314  }
315 
316  protected override void SetTitle(string title)
317  {
318  if (form != null)
319  {
320  form.Text = title;
321  }
322  }
323 
324  internal override void Resize(int width, int height)
325  {
326  Control.ClientSize = new Size(width, height);
327  }
328 
329  public override bool AllowUserResizing
330  {
331  get
332  {
333  return allowUserResizing;
334  }
335  set
336  {
337  if (form != null)
338  {
339  allowUserResizing = value;
340  UpdateFormBorder();
341  }
342  }
343  }
344 
345  public override bool IsBorderLess
346  {
347  get
348  {
349  return isBorderLess;
350  }
351  set
352  {
353  if (isBorderLess != value)
354  {
355  isBorderLess = value;
356  UpdateFormBorder();
357  }
358  }
359  }
360 
361  private void UpdateFormBorder()
362  {
363  if (form != null)
364  {
365  form.MaximizeBox = allowUserResizing;
366  form.FormBorderStyle = isFullScreenMaximized || isBorderLess ? FormBorderStyle.None : allowUserResizing ? FormBorderStyle.Sizable : FormBorderStyle.FixedSingle;
367 
368  if (isFullScreenMaximized)
369  {
370  form.TopMost = true;
371  form.BringToFront();
372  }
373  }
374  }
375 
376  public override SiliconStudio.Core.Mathematics.Rectangle ClientBounds
377  {
378  get
379  {
380  // Ensure width and height are at least 1 to avoid divisions by 0
381  return new SiliconStudio.Core.Mathematics.Rectangle(0, 0, Math.Max(Control.ClientSize.Width, 1), Math.Max(Control.ClientSize.Height, 1));
382  }
383  }
384 
385  public override DisplayOrientation CurrentOrientation
386  {
387  get
388  {
389  return DisplayOrientation.Default;
390  }
391  }
392 
393  public override bool IsMinimized
394  {
395  get
396  {
397  if (form != null)
398  {
399  return form.WindowState == FormWindowState.Minimized;
400  }
401 
402  // Check for non-form control
403  return false;
404  }
405  }
406 
407  protected override void Destroy()
408  {
409  if (Control != null)
410  {
411  Control.Dispose();
412  Control = null;
413  }
414 
415  base.Destroy();
416  }
417  }
418 }
419 #endif
AppContextType
Type of a GameContext.
HRESULT Resize(_In_ const Image &srcImage, _In_ size_t width, _In_ size_t height, _In_ DWORD filter, _Out_ ScratchImage &image)
ComponentBase.Destroy() event.
DisplayOrientation
Describes the orientation of the display.
Structure using the same layout than System.Drawing.Rectangle
Definition: Rectangle.cs:35
System.Windows.Point Point
Definition: ColorPicker.cs:15