CVR_ValueElement.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. namespace ChivaXR.VR
  2. {
  3. using System.Collections.Generic;
  4. using Sirenix.OdinInspector;
  5. using System.Linq;
  6. using UnityEngine;
  7. using System;
  8. public interface IVR_OperationElement
  9. {
  10. bool GetActive();
  11. CVR_ToolType GetToolType();
  12. void Trigger(CVR_InteractableTriggerEventArgs e);
  13. }
  14. public struct CVR_ValueElementEventArgs
  15. {
  16. public CVR_ValueElement valueElement;
  17. public float value;
  18. }
  19. /// <summary>
  20. /// Trigger
  21. /// </summary>
  22. /// <param name="sender">this object</param>
  23. /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
  24. public delegate void CVRValueElementEventHandler(object sender, CVR_ValueElementEventArgs e);
  25. /// <summary>
  26. /// 交互触发器Value
  27. /// </summary>
  28. public class CVR_ValueElement : SerializedMonoBehaviour, IVR_OperationElement
  29. {
  30. private CVR_SimulationTool simulationTool;
  31. /// <summary>
  32. /// 可被操作工具类型
  33. /// </summary>
  34. public bool isActive = false;
  35. private bool isSimulation = false;
  36. private float value;
  37. public float Value
  38. {
  39. get { return value; }
  40. set
  41. {
  42. if (this.value != value)
  43. {
  44. this.value = value;
  45. ValueChanged?.Invoke(this, new CVR_ValueElementEventArgs() { valueElement = this, value = Value });
  46. }
  47. }
  48. }
  49. public CVR_ToolType toolType;
  50. [Header("触发点动画")]
  51. [TypeFilter("GetFilteredTypeList")]
  52. public CVR_ValueAniBase valueAniBase;
  53. [HideInInspector]
  54. public CVRValueElementEventHandler ElementTrigger;
  55. [HideInInspector]
  56. public CVRValueElementEventHandler ValueChanged;
  57. public void Trigger(CVR_InteractableTriggerEventArgs e)
  58. {
  59. isSimulation = true;
  60. valueAniBase.Init(this, e.triggerTool.GetComponent<CVR_SimulationTool>());
  61. simulationTool = e.triggerTool.GetComponent<CVR_SimulationTool>();
  62. simulationTool.ExitSimulationMode += ToolCloseSimulation;
  63. }
  64. /// <summary>
  65. /// 设置Value状态
  66. /// </summary>
  67. /// <param name="value"></param>
  68. public void ChangeValueState(float value)
  69. {
  70. this.Value = value;
  71. valueAniBase.SetValueState(value);
  72. }
  73. private void Update()
  74. {
  75. if (!isActive || !isSimulation || valueAniBase == null) return;
  76. valueAniBase.SimulationUpdate();
  77. }
  78. protected virtual void ToolCloseSimulation(object sender, CVR_SimulationEventArgs e)
  79. {
  80. simulationTool.ExitSimulationMode -= ToolCloseSimulation;
  81. isSimulation = false;
  82. valueAniBase.CloseSimulation();
  83. }
  84. public IEnumerable<Type> GetFilteredTypeList()
  85. {
  86. var result = typeof(CVR_ValueAniBase).Assembly.GetTypes()
  87. .Where(temp => !temp.IsAbstract)
  88. .Where(temp => !temp.IsGenericTypeDefinition)
  89. .Where(temp => typeof(CVR_ValueAniBase).IsAssignableFrom(temp));
  90. return result;
  91. }
  92. public bool GetActive()
  93. {
  94. return isActive;
  95. }
  96. public CVR_ToolType GetToolType()
  97. {
  98. return toolType;
  99. }
  100. }
  101. }