-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKinectHoverButton.cs
More file actions
117 lines (99 loc) · 3.86 KB
/
KinectHoverButton.cs
File metadata and controls
117 lines (99 loc) · 3.86 KB
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
116
117
//------------------------------------------------------------------------------
// <copyright file="KinectHoverButton.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.ControlsBasics
{
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Kinect.Toolkit.Controls;
/// <summary>
/// A button that continually triggers a click when the mouse or hand pointer hovers over it
/// </summary>
internal class KinectHoverButton : KinectButtonBase
{
/// <summary>
/// IsHandPointerOver dependency property for use in the control template triggers
/// </summary>
public static readonly DependencyProperty IsHandPointerOverProperty = DependencyProperty.Register(
"IsHandPointerOver", typeof(bool), typeof(KinectHoverButton), new PropertyMetadata(false));
// Trigger a click 60 times per second
private const int ButtonRepeatIntervalMilliseconds = 1000 / 60;
/// <summary>
/// Boolean value to tell us if the control is being displayed in the Visual Studio designer
/// </summary>
private static readonly bool IsInDesignMode = DesignerProperties.GetIsInDesignMode(new DependencyObject());
/// <summary>
/// Timer to handle triggering the click events
/// </summary>
private readonly DispatcherTimer repeatTimer;
private HandPointer activeHandpointer;
public KinectHoverButton()
{
if (!IsInDesignMode)
{
this.InitializeKinectHoverButton();
this.repeatTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(ButtonRepeatIntervalMilliseconds) };
this.repeatTimer.Tick += this.RepeatTimerTick;
}
}
/// <summary>
/// Boolean value that returns true if a mouse or hand pointer is over this button
/// </summary>
public bool IsHandPointerOver
{
get
{
return (bool)this.GetValue(IsHandPointerOverProperty);
}
set
{
this.SetValue(IsHandPointerOverProperty, value);
}
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseEnter(e);
this.IsHandPointerOver = true;
this.repeatTimer.Start();
}
protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseLeave(e);
this.IsHandPointerOver = false;
this.repeatTimer.Stop();
}
private void InitializeKinectHoverButton()
{
KinectRegion.AddHandPointerEnterHandler(this, this.OnHandPointerEnter);
KinectRegion.AddHandPointerLeaveHandler(this, this.OnHandPointerLeave);
}
private void RepeatTimerTick(object sender, EventArgs e)
{
this.OnClick();
}
private void OnHandPointerEnter(object sender, HandPointerEventArgs e)
{
if (!e.HandPointer.IsPrimaryHandOfUser || !e.HandPointer.IsPrimaryUser)
{
return;
}
this.activeHandpointer = e.HandPointer;
this.IsHandPointerOver = true;
this.repeatTimer.Start();
}
private void OnHandPointerLeave(object sender, HandPointerEventArgs e)
{
if (this.activeHandpointer != e.HandPointer)
{
return;
}
this.activeHandpointer = null;
this.IsHandPointerOver = false;
this.repeatTimer.Stop();
}
}
}