HandModelBase.cs
3.15 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
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
/** HandModelBase defines abstract methods as a template for building Leap hand models*/
namespace Leap.Unity {
public enum Chirality { Left, Right };
public enum ModelType { Graphics, Physics };
[ExecuteInEditMode]
public abstract class HandModelBase : MonoBehaviour {
public event Action OnBegin;
public event Action OnFinish;
private bool isTracked = false;
public bool IsTracked {
get { return isTracked; }
}
public abstract Chirality Handedness { get; set; }
public abstract ModelType HandModelType { get; }
public virtual void InitHand() {
}
public virtual void BeginHand() {
if (OnBegin != null) {
OnBegin();
}
isTracked = true;
}
public abstract void UpdateHand();
public virtual void FinishHand() {
if (OnFinish != null) {
OnFinish();
}
isTracked = false;
}
public abstract Hand GetLeapHand();
public abstract void SetLeapHand(Hand hand);
/// <summary>
/// Returns whether or not this hand model supports editor persistence. This is false by default and must be
/// opt-in by a developer making their own hand model script if they want editor persistence.
/// </summary>
public virtual bool SupportsEditorPersistence() {
return false;
}
[NonSerialized]
public HandPool.ModelGroup group;
#if UNITY_EDITOR
void Update() {
if (!EditorApplication.isPlaying && SupportsEditorPersistence()) {
Transform editorPoseSpace;
LeapServiceProvider leapServiceProvider = FindObjectOfType<LeapServiceProvider>();
LeapTransform poseTransform = LeapTransform.Identity;
if (leapServiceProvider != null) {
editorPoseSpace = leapServiceProvider.transform;
poseTransform = TestHandFactory.GetTestPoseLeftHandTransform(leapServiceProvider.editTimePose);
} else {
editorPoseSpace = transform;
}
Hand hand = TestHandFactory.MakeTestHand(Handedness == Chirality.Left, poseTransform).TransformedCopy(UnityMatrixExtension.GetLeapMatrix(editorPoseSpace));
//Hand hand = TestHandFactory.MakeTestHand(0, 0, Handedness == Chirality.Left).TransformedCopy(UnityMatrixExtension.GetLeapMatrix(editorPoseSpace));
if (GetLeapHand() == null) {
SetLeapHand(hand);
InitHand();
BeginHand();
UpdateHand();
} else {
SetLeapHand(hand);
UpdateHand();
}
}
}
#endif
}
}