LeapHandController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Leap.Unity
  5. {
  6. public class LeapHandController : MonoBehaviour
  7. {
  8. public bool GraphicsEnabled
  9. {
  10. get
  11. {
  12. return this.graphicsEnabled;
  13. }
  14. set
  15. {
  16. this.graphicsEnabled = value;
  17. }
  18. }
  19. public bool PhysicsEnabled
  20. {
  21. get
  22. {
  23. return this.physicsEnabled;
  24. }
  25. set
  26. {
  27. this.physicsEnabled = value;
  28. }
  29. }
  30. private void OnDrawGizmos()
  31. {
  32. Gizmos.matrix = Matrix4x4.Scale(5f * Vector3.one);
  33. Gizmos.DrawIcon(base.transform.position, "leap_motion.png");
  34. }
  35. protected virtual void OnEnable()
  36. {
  37. this.provider = this.requireComponent<LeapProvider>();
  38. this.factory = this.requireComponent<HandFactory>();
  39. this.provider.OnUpdateFrame += this.OnUpdateFrame;
  40. this.provider.OnFixedFrame += this.OnFixedFrame;
  41. }
  42. protected virtual void OnDisable()
  43. {
  44. this.provider.OnUpdateFrame -= this.OnUpdateFrame;
  45. this.provider.OnFixedFrame -= this.OnFixedFrame;
  46. }
  47. protected virtual void OnUpdateFrame(Frame frame)
  48. {
  49. if (frame != null && this.graphicsEnabled)
  50. {
  51. this.UpdateHandRepresentations(this.graphicsReps, ModelType.Graphics, frame);
  52. }
  53. }
  54. protected virtual void OnFixedFrame(Frame frame)
  55. {
  56. if (frame != null && this.physicsEnabled)
  57. {
  58. this.UpdateHandRepresentations(this.physicsReps, ModelType.Physics, frame);
  59. }
  60. }
  61. protected virtual void UpdateHandRepresentations(Dictionary<int, HandRepresentation> all_hand_reps, ModelType modelType, Frame frame)
  62. {
  63. foreach (Hand hand in frame.Hands)
  64. {
  65. HandRepresentation handRepresentation;
  66. if (!all_hand_reps.TryGetValue(hand.Id, out handRepresentation))
  67. {
  68. handRepresentation = this.factory.MakeHandRepresentation(hand, modelType);
  69. if (handRepresentation != null)
  70. {
  71. all_hand_reps.Add(hand.Id, handRepresentation);
  72. }
  73. }
  74. if (handRepresentation != null)
  75. {
  76. handRepresentation.IsMarked = true;
  77. handRepresentation.UpdateRepresentation(hand);
  78. handRepresentation.LastUpdatedTime = (int)frame.Timestamp;
  79. }
  80. }
  81. HandRepresentation handRepresentation2 = null;
  82. foreach (KeyValuePair<int, HandRepresentation> keyValuePair in all_hand_reps)
  83. {
  84. if (keyValuePair.Value != null)
  85. {
  86. if (keyValuePair.Value.IsMarked)
  87. {
  88. keyValuePair.Value.IsMarked = false;
  89. }
  90. else
  91. {
  92. handRepresentation2 = keyValuePair.Value;
  93. }
  94. }
  95. }
  96. if (handRepresentation2 != null)
  97. {
  98. all_hand_reps.Remove(handRepresentation2.HandID);
  99. handRepresentation2.Finish();
  100. }
  101. }
  102. private T requireComponent<T>() where T : Component
  103. {
  104. T component = base.GetComponent<T>();
  105. if (component == null)
  106. {
  107. string name = typeof(T).Name;
  108. Debug.LogError(string.Concat(new string[]
  109. {
  110. "LeapHandController could not find a ",
  111. name,
  112. " and has been disabled. Make sure there is a ",
  113. name,
  114. " on the same gameObject."
  115. }));
  116. base.enabled = false;
  117. }
  118. return component;
  119. }
  120. protected LeapProvider provider;
  121. protected HandFactory factory;
  122. protected Dictionary<int, HandRepresentation> graphicsReps = new Dictionary<int, HandRepresentation>();
  123. protected Dictionary<int, HandRepresentation> physicsReps = new Dictionary<int, HandRepresentation>();
  124. protected const float GIZMO_SCALE = 5f;
  125. protected bool graphicsEnabled = true;
  126. protected bool physicsEnabled = true;
  127. }
  128. }