Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameWindowAndroid.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_ANDROID
4 using System;
5 using System.Diagnostics;
6 using System.Drawing;
7 using Android.Content;
8 using Android.Views.InputMethods;
9 using SiliconStudio.Core;
10 using SiliconStudio.Paradox.Games.Android;
11 using SiliconStudio.Paradox.Graphics;
13 using OpenTK.Platform.Android;
14 
15 namespace SiliconStudio.Paradox.Games
16 {
17  /// <summary>
18  /// An abstract window.
19  /// </summary>
20  internal class GameWindowAndroid : GameWindow
21  {
22  private AndroidParadoxGameView paradoxGameForm;
23  private WindowHandle nativeWindow;
24 
25  public override WindowHandle NativeWindow
26  {
27  get
28  {
29  return nativeWindow;
30  }
31  }
32 
33  public override void BeginScreenDeviceChange(bool willBeFullScreen)
34  {
35 
36  }
37 
38  public override void EndScreenDeviceChange(int clientWidth, int clientHeight)
39  {
40 
41  }
42 
43  protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
44  {
45  // Desktop doesn't have orientation (unless on Windows 8?)
46  }
47 
48  internal override bool CanHandle(GameContext gameContext)
49  {
50  return gameContext.ContextType == AppContextType.Android;
51  }
52 
53  internal override void Initialize(GameContext gameContext)
54  {
55  GameContext = gameContext;
56 
57  paradoxGameForm = (AndroidParadoxGameView)gameContext.Control;
58  nativeWindow = new WindowHandle(AppContextType.Android, paradoxGameForm);
59 
60  paradoxGameForm.Load += gameForm_Resume;
61  paradoxGameForm.OnPause += gameForm_OnPause;
62  paradoxGameForm.Unload += gameForm_Unload;
63  paradoxGameForm.RenderFrame += gameForm_RenderFrame;
64 
65  // Setup the initial size of the window
66  var width = gameContext.RequestedWidth;
67  if (width == 0)
68  {
69  width = paradoxGameForm.Width;
70  }
71 
72  var height = gameContext.RequestedHeight;
73  if (height == 0)
74  {
75  height = paradoxGameForm.Height;
76  }
77 
78  // Transmit requested back buffer and depth stencil formats to OpenTK
79  paradoxGameForm.RequestedBackBufferFormat = gameContext.RequestedBackBufferFormat;
80  paradoxGameForm.RequestedDepthStencilFormat = gameContext.RequestedDepthStencilFormat;
81 
82  paradoxGameForm.Size = new Size(width, height);
83 
84  //paradoxGameForm.Resize += OnClientSizeChanged;
85  }
86 
87  void gameForm_Resume(object sender, EventArgs e)
88  {
89  // Call InitCallback only first time
90  if (InitCallback != null)
91  {
92  InitCallback();
93  InitCallback = null;
94  }
95  paradoxGameForm.Run();
96 
97  OnResume();
98  }
99 
100  void gameForm_OnPause(object sender, EventArgs e)
101  {
102  // Hide android soft keyboard (doesn't work anymore if done during Unload)
103  var inputMethodManager = (InputMethodManager)PlatformAndroid.Context.GetSystemService(Context.InputMethodService);
104  inputMethodManager.HideSoftInputFromWindow(GameContext.Control.RootView.WindowToken, HideSoftInputFlags.None);
105  }
106 
107  void gameForm_Unload(object sender, EventArgs e)
108  {
109  OnPause();
110  }
111 
112  void gameForm_RenderFrame(object sender, OpenTK.FrameEventArgs e)
113  {
114  RunCallback();
115  }
116 
117  internal override void Run()
118  {
119  Debug.Assert(InitCallback != null);
120  Debug.Assert(RunCallback != null);
121 
122  if (paradoxGameForm.GraphicsContext != null)
123  {
124  throw new NotImplementedException("Only supports not yet initialized AndroidGameView.");
125  }
126  }
127 
128  /// <summary>
129  /// Gets or sets a value indicating whether this <see cref="GameWindow" /> is visible.
130  /// </summary>
131  /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
132  public override bool Visible
133  {
134  get
135  {
136  return paradoxGameForm.Visible;
137  }
138  set
139  {
140  paradoxGameForm.Visible = value;
141  }
142  }
143 
144  protected override void SetTitle(string title)
145  {
146  paradoxGameForm.Title = title;
147  }
148 
149  internal override void Resize(int width, int height)
150  {
151  paradoxGameForm.Size = new Size(width, height);
152  }
153 
154  public override bool IsBorderLess
155  {
156  get
157  {
158  return true;
159  }
160  set
161  {
162  }
163  }
164 
165  public override bool AllowUserResizing
166  {
167  get
168  {
169  return true;
170  }
171  set
172  {
173  }
174  }
175 
176  public override Rectangle ClientBounds
177  {
178  get
179  {
180  return new Rectangle(0, 0, paradoxGameForm.Size.Width, paradoxGameForm.Size.Height);
181  }
182  }
183 
184  public override DisplayOrientation CurrentOrientation
185  {
186  get
187  {
188  return DisplayOrientation.Default;
189  }
190  }
191 
192  public override bool IsMinimized
193  {
194  get
195  {
196  return paradoxGameForm.WindowState == OpenTK.WindowState.Minimized;
197  }
198  }
199 
200  public override bool IsMouseVisible
201  {
202  get { return false; }
203  set {}
204  }
205 
206  protected override void Destroy()
207  {
208  if (paradoxGameForm != null)
209  {
210  paradoxGameForm.Load -= gameForm_Resume;
211  paradoxGameForm.OnPause -= gameForm_OnPause;
212  paradoxGameForm.Unload -= gameForm_Unload;
213  paradoxGameForm.RenderFrame -= gameForm_RenderFrame;
214 
215  paradoxGameForm.GraphicsContext.MakeCurrent(null);
216  paradoxGameForm.GraphicsContext.Dispose();
217  ((AndroidWindow)paradoxGameForm.WindowInfo).TerminateDisplay();
218  //paradoxGameForm.Close();
219  paradoxGameForm.Holder.RemoveCallback(paradoxGameForm);
220  paradoxGameForm.Dispose();
221  paradoxGameForm = null;
222  }
223 
224  base.Destroy();
225  }
226  }
227 }
228 #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