Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameWindowOpenTK.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 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP && SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
4 using System;
5 using System.Diagnostics;
6 using System.Threading;
7 using System.Windows.Forms;
8 
9 using SiliconStudio.Core.Mathematics;
10 using SiliconStudio.Paradox.Graphics;
11 
12 namespace SiliconStudio.Paradox.Games
13 {
14  /// <summary>
15  /// An abstract window.
16  /// </summary>
17  internal class GameWindowOpenTK : GameWindow
18  {
19  private bool isMouseVisible;
20 
21  private bool isMouseCurrentlyHidden;
22 
23  private OpenTK.GameWindow gameForm;
24  private WindowHandle nativeWindow;
25 
26  internal GameWindowOpenTK()
27  {
28  }
29 
30  public override WindowHandle NativeWindow
31  {
32  get
33  {
34  return nativeWindow;
35  }
36  }
37 
38  public override Int2 Position
39  {
40  get
41  {
42  if (gameForm == null)
43  return base.Position;
44 
45  return new Int2(gameForm.X, gameForm.Y);
46  }
47  set
48  {
49  if (gameForm != null)
50  {
51  gameForm.X = value.X;
52  gameForm.Y = value.Y;
53  }
54 
55  base.Position = value;
56  }
57  }
58 
59  public override void BeginScreenDeviceChange(bool willBeFullScreen)
60  {
61 
62  }
63 
64  public override void EndScreenDeviceChange(int clientWidth, int clientHeight)
65  {
66 
67  }
68 
69  protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
70  {
71  // Desktop doesn't have orientation (unless on Windows 8?)
72  }
73 
74  internal override bool CanHandle(GameContext gameContext)
75  {
76  return gameContext.ContextType == AppContextType.DesktopOpenTK;
77  }
78 
79  internal override void Initialize(GameContext gameContext)
80  {
81  this.GameContext = gameContext;
82 
83  gameForm = (OpenTK.GameWindow)gameContext.Control;
84  nativeWindow = new WindowHandle(AppContextType.DesktopOpenTK, gameForm);
85 
86  // Setup the initial size of the window
87  var width = gameContext.RequestedWidth;
88  if (width == 0)
89  {
90  width = gameForm.Width;
91  }
92 
93  var height = gameContext.RequestedHeight;
94  if (height == 0)
95  {
96  height = gameForm.Height;
97  }
98 
99  gameForm.ClientSize = new System.Drawing.Size(width, height);
100 
101  gameForm.MouseEnter += GameWindowForm_MouseEnter;
102  gameForm.MouseLeave += GameWindowForm_MouseLeave;
103 
104  gameForm.Resize += OnClientSizeChanged;
105  }
106 
107  internal override void Run()
108  {
109  Debug.Assert(InitCallback != null);
110  Debug.Assert(RunCallback != null);
111 
112  // Initialize the init callback
113  InitCallback();
114 
115  // Make the window visible
116  gameForm.Visible = true;
117 
118  // Run the rendering loop
119  try
120  {
121  while (!Exiting)
122  {
123  gameForm.ProcessEvents();
124 
125  RunCallback();
126  }
127 
128  if (gameForm != null)
129  {
130  if (OpenTK.Graphics.GraphicsContext.CurrentContext == gameForm.Context)
131  gameForm.Context.MakeCurrent(null);
132  gameForm.Close();
133  gameForm.Dispose();
134  gameForm = null;
135  }
136  }
137  finally
138  {
139  if (ExitCallback != null)
140  {
141  ExitCallback();
142  }
143  }
144  }
145 
146  private void GameWindowForm_MouseEnter(object sender, System.EventArgs e)
147  {
148  if (!isMouseVisible && !isMouseCurrentlyHidden)
149  {
150  Cursor.Hide();
151  isMouseCurrentlyHidden = true;
152  }
153  }
154 
155  private void GameWindowForm_MouseLeave(object sender, System.EventArgs e)
156  {
157  if (isMouseCurrentlyHidden)
158  {
159  Cursor.Show();
160  isMouseCurrentlyHidden = false;
161  }
162  }
163 
164  public override bool IsMouseVisible
165  {
166  get
167  {
168  return isMouseVisible;
169  }
170  set
171  {
172  if (isMouseVisible != value)
173  {
174  isMouseVisible = value;
175  if (isMouseVisible)
176  {
177  if (isMouseCurrentlyHidden)
178  {
179  Cursor.Show();
180  isMouseCurrentlyHidden = false;
181  }
182  }
183  else if (!isMouseCurrentlyHidden)
184  {
185  Cursor.Hide();
186  isMouseCurrentlyHidden = true;
187  }
188  }
189  }
190  }
191 
192  /// <summary>
193  /// Gets or sets a value indicating whether this <see cref="GameWindow" /> is visible.
194  /// </summary>
195  /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
196  public override bool Visible
197  {
198  get
199  {
200  return gameForm.Visible;
201  }
202  set
203  {
204  gameForm.Visible = value;
205  }
206  }
207 
208  protected override void SetTitle(string title)
209  {
210  gameForm.Title = title;
211  }
212 
213  internal override void Resize(int width, int height)
214  {
215  gameForm.ClientSize = new System.Drawing.Size(width, height);
216  }
217 
218  public override bool IsBorderLess
219  {
220  get
221  {
222  return true;
223  }
224  set
225  {
226  }
227  }
228 
229  public override bool AllowUserResizing
230  {
231  get
232  {
233  return true;
234  }
235  set
236  {
237  }
238  }
239 
240  public override Rectangle ClientBounds
241  {
242  get
243  {
244  return new Rectangle(0, 0, gameForm.ClientSize.Width, gameForm.ClientSize.Height);
245  }
246  }
247 
248  public override DisplayOrientation CurrentOrientation
249  {
250  get
251  {
252  return DisplayOrientation.Default;
253  }
254  }
255 
256  public override bool IsMinimized
257  {
258  get
259  {
260  return gameForm.WindowState == OpenTK.WindowState.Minimized;
261  }
262  }
263 
264  protected override void Destroy()
265  {
266  if (gameForm != null)
267  {
268  gameForm.Context.MakeCurrent(null);
269  gameForm.Close();
270  gameForm.Dispose();
271  gameForm = null;
272  }
273 
274  base.Destroy();
275  }
276  }
277 }
278 #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.
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16