Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameWindowWindowsRuntimeSwapChainPanel.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 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
24 
25 using System;
26 using SiliconStudio.Paradox.Graphics;
27 using SiliconStudio.Core.Mathematics;
28 using Windows.UI.ViewManagement;
29 using Windows.UI.Xaml;
30 using Windows.UI.Xaml.Controls;
31 using Windows.UI.Xaml.Media;
32 
33 namespace SiliconStudio.Paradox.Games
34 {
35  /// <summary>
36  /// An abstract window.
37  /// </summary>
38  internal class GameWindowWindowsRuntimeSwapChainPanel : GameWindow
39  {
40 #region Fields
41 
42  private SwapChainPanel swapChainPanel;
43  private WindowHandle windowHandle;
44  private int currentWidth;
45  private int currentHeight;
46 
47  #endregion
48 
49 #region Public Properties
50 
51  public override bool AllowUserResizing
52  {
53  get
54  {
55  return true;
56  }
57  set
58  {
59  }
60  }
61 
62  public override Rectangle ClientBounds
63  {
64  get
65  {
66  return new Rectangle(0, 0, (int)(this.swapChainPanel.ActualWidth * swapChainPanel.CompositionScaleX + 0.5f), (int)(this.swapChainPanel.ActualHeight * swapChainPanel.CompositionScaleY + 0.5f));
67  }
68  }
69 
70  public override DisplayOrientation CurrentOrientation
71  {
72  get
73  {
74  return DisplayOrientation.Default;
75  }
76  }
77 
78  public override bool IsMinimized
79  {
80  get
81  {
82  return false;
83  }
84  }
85 
86  public override bool IsMouseVisible { get; set; }
87 
88  public override WindowHandle NativeWindow
89  {
90  get
91  {
92  return windowHandle;
93  }
94  }
95 
96  /// <summary>
97  /// Gets or sets a value indicating whether this <see cref="GameWindow" /> is visible.
98  /// </summary>
99  /// <value><c>true</c> if visible; otherwise, <c>false</c>.</value>
100  public override bool Visible
101  {
102  get
103  {
104  return true;
105  }
106  set
107  {
108  }
109  }
110 
111  /// <inheritdoc/>
112  public override bool IsBorderLess
113  {
114  get
115  {
116  return true;
117  }
118  set
119  {
120  }
121  }
122 
123  #endregion
124 
125 #region Public Methods and Operators
126 
127  public override void BeginScreenDeviceChange(bool willBeFullScreen)
128  {
129  }
130 
131  public override void EndScreenDeviceChange(int clientWidth, int clientHeight)
132  {
133  }
134 
135  #endregion
136 
137 #region Methods
138 
139  internal override bool CanHandle(GameContext windowContext)
140  {
141  return windowContext.ContextType == AppContextType.WindowsRuntime;
142  }
143 
144  internal override void Initialize(GameContext windowContext)
145  {
146  if (windowContext != null)
147  {
148  swapChainPanel = windowContext.Control as SwapChainPanel;
149  if (swapChainPanel == null)
150  {
151  throw new NotSupportedException(string.Format("Unsupported window context [{0}]. Only SwapChainPanel", windowContext.Control.GetType().FullName));
152  }
153  windowHandle = new WindowHandle(AppContextType.WindowsRuntime, swapChainPanel);
154 
155  //clientBounds = new DrawingRectangle(0, 0, (int)swapChainPanel.ActualWidth, (int)swapChainPanel.ActualHeight);
156  swapChainPanel.SizeChanged += swapChainPanel_SizeChanged;
157  swapChainPanel.CompositionScaleChanged += swapChainPanel_CompositionScaleChanged;
158  }
159  }
160 
161  void swapChainPanel_CompositionScaleChanged(SwapChainPanel sender, object args)
162  {
163  OnClientSizeChanged(sender, EventArgs.Empty);
164  }
165 
166  private void swapChainPanel_SizeChanged(object sender, SizeChangedEventArgs e)
167  {
168  var bounds = e.NewSize;
169 
170  // Only apply SwapChain resize when effective orientation is matching current orientation
171  // TODO: This code is just a workaround. We need to improve orientation handling PDX-1140
172  var effectiveOrientation = bounds.Width > bounds.Height ? ApplicationViewOrientation.Landscape : ApplicationViewOrientation.Portrait;
173  var currentOrientation = ApplicationView.GetForCurrentView().Orientation;
174  if (effectiveOrientation == currentOrientation
175  && bounds.Width > 0 && bounds.Height > 0 && currentWidth > 0 && currentHeight > 0)
176  {
177  double panelWidth;
178  double panelHeight;
179  panelWidth = bounds.Width;
180  panelHeight = bounds.Height;
181  var panelRatio = panelWidth/panelHeight;
182  var currentRatio = currentWidth/currentHeight;
183 
184  if (panelRatio < currentRatio)
185  {
186  panelWidth = bounds.Width;
187  panelHeight = (int)(currentHeight*bounds.Width/currentWidth);
188  }
189  else
190  {
191  panelHeight = bounds.Height;
192  panelWidth = (int)(currentWidth*bounds.Height/currentHeight);
193  }
194 
195  if (swapChainPanel.Width != panelWidth || swapChainPanel.Height != panelHeight)
196  {
197  // Center the panel
198  swapChainPanel.HorizontalAlignment = HorizontalAlignment.Center;
199  swapChainPanel.VerticalAlignment = VerticalAlignment.Center;
200 
201  swapChainPanel.Width = panelWidth;
202  swapChainPanel.Height = panelHeight;
203  }
204  }
205 
206  OnClientSizeChanged(sender, EventArgs.Empty);
207  }
208 
209  internal override void Resize(int width, int height)
210  {
211  currentWidth = width;
212  currentHeight = height;
213  }
214 
215  internal override void Run()
216  {
217  // Initialize GameBase
218  InitCallback();
219 
220  // Perform the rendering loop
221  CompositionTarget.Rendering += CompositionTarget_Rendering;
222  }
223 
224  void CompositionTarget_Rendering(object sender, object e)
225  {
226  RunCallback();
227  }
228 
229  protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
230  {
231  // Desktop doesn't have orientation (unless on Windows 8?)
232  }
233 
234  protected override void SetTitle(string title)
235  {
236 
237  }
238 
239  protected override void Destroy()
240  {
241  CompositionTarget.Rendering -= CompositionTarget_Rendering;
242  base.Destroy();
243  }
244  #endregion
245  }
246 }
247 
248 #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
Android.Widget.Orientation Orientation
Definition: Section.cs:9