Tanoda
HandModelBase.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System;
11#if UNITY_EDITOR
12using UnityEditor;
13#endif
14
16namespace Leap.Unity {
17 public enum Chirality { Left, Right };
18 public enum ModelType { Graphics, Physics };
19
20 [ExecuteInEditMode]
21 public abstract class HandModelBase : MonoBehaviour {
22
23 public event Action OnBegin;
24 public event Action OnFinish;
27 public event Action OnUpdate;
28
29 private bool isTracked = false;
30 public bool IsTracked {
31 get { return isTracked; }
32 }
33
34 public abstract Chirality Handedness { get; set; }
35 public abstract ModelType HandModelType { get; }
36 public virtual void InitHand() { }
37
38 public virtual void BeginHand() {
39 if (OnBegin != null) {
40 OnBegin();
41 }
42 isTracked = true;
43 }
44 public abstract void UpdateHand();
45 public void UpdateHandWithEvent() {
46 UpdateHand();
47 if (OnUpdate != null) { OnUpdate(); }
48 }
49 public virtual void FinishHand() {
50 if (OnFinish != null) {
51 OnFinish();
52 }
53 isTracked = false;
54 }
55 public abstract Hand GetLeapHand();
56 public abstract void SetLeapHand(Hand hand);
57
62 public virtual bool SupportsEditorPersistence() {
63 return false;
64 }
65
66 [NonSerialized]
68
69#if UNITY_EDITOR
70 void Update() {
71 if (!EditorApplication.isPlaying && SupportsEditorPersistence()) {
72 LeapProvider provider = null;
73
74 //First try to get the provider from a parent HandModelManager
75 if (transform.parent != null) {
76 var manager = transform.parent.GetComponent<HandModelManager>();
77 if (manager != null) {
78 provider = manager.leapProvider;
79 }
80 }
81
82 //If not found, use any old provider from the Hands.Provider getter
83 if (provider == null) {
84 provider = Hands.Provider;
85 }
86
87 Hand hand = null;
88 //If we found a provider, pull the hand from that
89 if (provider != null) {
90 var frame = provider.CurrentFrame;
91
92 if (frame != null) {
93 hand = frame.Get(Handedness);
94 }
95 }
96
97 //If we still have a null hand, construct one manually
98 if (hand == null) {
99 hand = TestHandFactory.MakeTestHand(Handedness == Chirality.Left, unitType: TestHandFactory.UnitType.LeapUnits);
100 hand.Transform(transform.GetLeapMatrix());
101 }
102
103 if (GetLeapHand() == null) {
104 SetLeapHand(hand);
105 InitHand();
106 BeginHand();
107 UpdateHand();
108 } else {
109 SetLeapHand(hand);
110 UpdateHand();
111 }
112 }
113 }
114#endif
115 }
116}
The Hand class reports the physical characteristics of a detected hand.
Definition: Hand.cs:26
virtual void FinishHand()
abstract ModelType HandModelType
virtual void BeginHand()
HandModelManager.ModelGroup group
Action OnUpdate
Called directly after the HandModelBase's UpdateHand().
virtual void InitHand()
abstract Chirality Handedness
abstract Hand GetLeapHand()
abstract void SetLeapHand(Hand hand)
virtual bool SupportsEditorPersistence()
Returns whether or not this hand model supports editor persistence. This is false by default and must...
abstract void UpdateHand()
The HandModelManager manages a pool of HandModelBases and makes HandRepresentations when a it detects...
Provides Frame object data to the Unity application by firing events as soon as Frame data is availab...
Definition: LeapProvider.cs:21
abstract Frame CurrentFrame
The current frame for this update cycle, in world space.
Definition: LeapProvider.cs:37