/
ControllerViewModel.cs
63 lines (56 loc) · 1.59 KB
/
ControllerViewModel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpDX.XInput;
namespace Prototype
{
public class ControllerViewModel : ViewModelBase<Controller>
{
public ControllerViewModel(Controller model)
{
Model = model;
isConnected = Model.IsConnected;
}
bool isConnected;
public bool IsConnected
{
get
{
return Model.IsConnected;
}
private set
{
if (isConnected != value)
{
isConnected = value;
if (isConnected)
{
current = Model.GetState();
changedButtons = GamepadButtonFlags.None;
}
RaisePropertyChanged("IsConnected");
}
}
}
State current;
GamepadButtonFlags changedButtons;
internal void Update()
{
IsConnected = Model.IsConnected;
if (!IsConnected) return;
var next = Model.GetState();
changedButtons = current.Gamepad.Buttons ^ next.Gamepad.Buttons;
current = next;
}
public bool IsHeld(GamepadButtonFlags flags)
{
return IsConnected && (current.Gamepad.Buttons & flags) == flags;
}
public bool HasChanged(GamepadButtonFlags flags)
{
return IsConnected && (changedButtons & flags) == flags;
}
}
}