/
TouchPanelCapabilities.cs
115 lines (103 loc) · 3.95 KB
/
TouchPanelCapabilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
#if ANDROID
using Android.Content.PM;
#endif
#if IOS
using UIKit;
#endif
namespace Microsoft.Xna.Framework.Input.Touch
{
/// <summary>
/// Allows retrieval of capabilities information from touch panel device.
/// </summary>
public struct TouchPanelCapabilities
{
private bool hasPressure;
private bool isConnected;
private int maximumTouchCount;
private bool initialized;
internal void Initialize()
{
if (!initialized)
{
initialized = true;
// There does not appear to be a way of finding out if a touch device supports pressure.
// XNA does not expose a pressure value, so let's assume it doesn't support it.
hasPressure = false;
#if WINDOWS_UAP
// Is a touch device present?
// Iterate through all pointer devices and find the maximum number of concurrent touches possible
maximumTouchCount = 0;
var pointerDevices = Windows.Devices.Input.PointerDevice.GetPointerDevices();
foreach (var pointerDevice in pointerDevices)
{
maximumTouchCount = Math.Max(maximumTouchCount, (int)pointerDevice.MaxContacts);
if (pointerDevice.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch)
isConnected = true;
}
#elif WINDOWS
maximumTouchCount = GetSystemMetrics(SM_MAXIMUMTOUCHES);
isConnected = (maximumTouchCount > 0);
#elif ANDROID
// http://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_TOUCHSCREEN
var pm = Game.Activity.PackageManager;
isConnected = pm.HasSystemFeature(PackageManager.FeatureTouchscreen);
if (pm.HasSystemFeature(PackageManager.FeatureTouchscreenMultitouchJazzhand))
maximumTouchCount = 5;
else if (pm.HasSystemFeature(PackageManager.FeatureTouchscreenMultitouchDistinct))
maximumTouchCount = 2;
else
maximumTouchCount = 1;
#elif IOS
//iPhone supports 5, iPad 11
isConnected = true;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
maximumTouchCount = 5;
else //Pad
maximumTouchCount = 11;
#else
//Touch isn't implemented in OpenTK, so no linux or mac https://github.com/opentk/opentk/issues/80
isConnected = false;
#endif
}
}
/// <summary>
/// Returns <see langword="true"/> if a touch device supports pressure.
/// </summary>
public bool HasPressure
{
get
{
return hasPressure;
}
}
/// <summary>
/// Returns true if a device is available for use.
/// </summary>
public bool IsConnected
{
get
{
return isConnected;
}
}
/// <summary>
/// Returns the maximum number of touch locations tracked by the touch panel device.
/// </summary>
public int MaximumTouchCount
{
get
{
return maximumTouchCount;
}
}
#if WINDOWS
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
static extern int GetSystemMetrics(int nIndex);
const int SM_MAXIMUMTOUCHES = 95;
#endif
}
}