Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InputManager.iOS.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_IOS
4 
5 using System;
6 using System.Drawing;
7 using MonoTouch.UIKit;
8 using MonoTouch.Foundation;
9 using OpenTK.Platform.iPhoneOS;
10 using SiliconStudio.Core;
11 using SiliconStudio.Core.Mathematics;
12 
13 namespace SiliconStudio.Paradox.Input
14 {
15  public partial class InputManager
16  {
17  private UIWindow window;
18  private iPhoneOSGameView view;
19 
20  public InputManager(IServiceRegistry registry) : base(registry)
21  {
22  HasKeyboard = true;
23  HasMouse = false;
24  HasPointer = true;
25  }
26 
27  public override void Initialize()
28  {
29  view = Game.Context.GameView;
30  window = Game.Context.MainWindow;
31 
32  var gameController = Game.Context.GameViewController;
33 
34  window.UserInteractionEnabled = true;
35  window.MultipleTouchEnabled = true;
36  gameController.TouchesBeganDelegate += (touchesSet, _) => HandleTouches(touchesSet);
37  gameController.TouchesMovedDelegate += (touchesSet, _) => HandleTouches(touchesSet);
38  gameController.TouchesEndedDelegate += (touchesSet, _) => HandleTouches(touchesSet);
39  gameController.TouchesCancelledDelegate += (touchesSet, _) => HandleTouches(touchesSet);
40  view.Resize += OnResize;
41 
42  OnResize(null, EventArgs.Empty);
43  }
44 
45  private void OnResize(object sender, EventArgs eventArgs)
46  {
47  ControlHeight = view.Frame.Height;
48  ControlWidth = view.Frame.Width;
49  }
50 
51  private void HandleTouches(NSSet touchesSet)
52  {
53  var touches = touchesSet.ToArray<UITouch>();
54 
55  if (touches != null)
56  {
57  foreach (var uitouch in touches)
58  {
59  var id = uitouch.Handle.ToInt32();
60  var position = NormalizeScreenPosition(PointFToVector2(uitouch.LocationInView(view)));
61 
62  HandlePointerEvents(id, position, GetState(uitouch));
63  }
64  }
65  }
66 
67  private PointerState GetState(UITouch touch)
68  {
69  switch (touch.Phase)
70  {
71  case UITouchPhase.Began:
72  return PointerState.Down;
73  case UITouchPhase.Moved:
74  case UITouchPhase.Stationary:
75  return PointerState.Move;
76  case UITouchPhase.Ended:
77  return PointerState.Up;
78  case UITouchPhase.Cancelled:
79  return PointerState.Cancel;
80  }
81 
82  throw new ArgumentException("Got an invalid Touch event in GetState");
83  }
84 
85  private Vector2 PointFToVector2(PointF point)
86  {
87  return new Vector2(point.X, point.Y);
88  }
89 
90  public override bool MultiTouchEnabled
91  {
92  get { return Game.Context.GameView.MultipleTouchEnabled; }
93  set { Game.Context.GameView.MultipleTouchEnabled = value; }
94  }
95  }
96 }
97 #endif
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
SiliconStudio.Core.IServiceRegistry IServiceRegistry
Definition: ModelRenderer.cs:9
PointerState
State of a pointer event.
Definition: PointerState.cs:8