InputDeviceHandPartTurnLeftRightEvent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand {
  9. public class InputDeviceHandPartTurnLeftRightEvent : InputDeviceHandPartEventBase {
  10. public InputDeviceHandPartTurnLeftRightEvent(InputDevicePartDispatchEventHand inputDevicePartDispatchEventHand) : base(inputDevicePartDispatchEventHand) {
  11. }
  12. enum TurnAround {
  13. Left = -1,
  14. Right = 1,
  15. }
  16. List<Vector3> DeltaTrendList = new List<Vector3>(new Vector3[6]);
  17. int DirectionResult = 0;
  18. protected int currentNum = 0;
  19. protected float timer = 0;
  20. protected override void OnUpdateEvent() {
  21. currentEvent = HandEventType.Null;
  22. timer += Time.deltaTime;
  23. if(timer >= samplingTime) {
  24. timer = 0;
  25. if(handInfo.isLost == false) {
  26. DeltaTrendList[currentNum] = handInfo.finger[(int)FINGER.ring].joint[(int)JOINT.Four].localPosition - handInfo.finger[(int)FINGER.forefinger].joint[(int)JOINT.Four].localPosition;
  27. } else {
  28. DeltaTrendList[currentNum] = Vector3.zero;
  29. }
  30. //int j = 0;
  31. //DebugMy.Log("trendList * 1000 =========== " + DeltaTrendList[currentNum] * 1000 + " ==========", this);
  32. //foreach(var trend in DeltaTrendList) {
  33. // if(currentNum == j) {
  34. // DebugMy.Log("trendList * 1000:[" + j++ + "]:" + trend * 1000 + "<===", this);
  35. // } else {
  36. // DebugMy.Log("trendList * 1000:[" + j++ + "]:" + trend * 1000, this);
  37. // }
  38. //}
  39. DirectionResult = 0;
  40. for(int i = DeltaTrendList.Count - 1; i >= 0; i--) {
  41. //XDirectionResult
  42. if(DeltaTrendList[i].x > (DeltaTrendList[((i - 1) < 0) ? DeltaTrendList.Count - 1 : i - 1].x + noise)) {
  43. DirectionResult += (int)TurnAround.Right;
  44. DebugMy.Log("trendList :" + TurnAround.Right + " " + DirectionResult, this);
  45. } else if((DeltaTrendList[i].x + noise) < DeltaTrendList[((i - 1) < 0) ? DeltaTrendList.Count - 1 : i - 1].x) {
  46. DirectionResult += (int)TurnAround.Left;
  47. DebugMy.Log("trendList :" + TurnAround.Left + " " + DirectionResult, this);
  48. }
  49. //Debug.Log("xxxx:: "+ i+"::"+(((i - 1) < 0) ? HandTrendList.Count - 1 : i - 1));
  50. }
  51. ///此log可用于查看noise大小,正常手不动DirectionResult为0,当不为0时,调大 noise
  52. //DebugMy.Log("Noise ----- ----- Turn---" + DirectionResult, this);
  53. if(DirectionResult >= effect) {
  54. //DebugMy.Log("Event ----- ----- TurnAround Rigth---", this);
  55. for(int i = 0; i < DeltaTrendList.Count; i++) {
  56. DeltaTrendList[i] = Vector3.zero;
  57. }
  58. currentEvent = HandEventType.TurnLeft;
  59. } else if(DirectionResult <= -effect) {
  60. //DebugMy.Log("Event ----- ----- TurnAround Left ---", this);
  61. for(int i = 0; i < DeltaTrendList.Count; i++) {
  62. DeltaTrendList[i] = Vector3.zero;
  63. }
  64. currentEvent = HandEventType.TurnRight;
  65. }
  66. currentNum++;
  67. if(currentNum == DeltaTrendList.Count) {
  68. currentNum = 0;
  69. }
  70. }
  71. }
  72. }
  73. }