123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- namespace ChivaXR.VR
- {
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- using System.Linq;
- using UnityEngine;
- using System;
- public interface IVR_OperationElement
- {
- bool GetActive();
- CVR_ToolType GetToolType();
- void Trigger(CVR_InteractableTriggerEventArgs e);
- }
- public struct CVR_ValueElementEventArgs
- {
- public CVR_ValueElement valueElement;
- public float value;
- }
- /// <summary>
- /// Trigger
- /// </summary>
- /// <param name="sender">this object</param>
- /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
- public delegate void CVRValueElementEventHandler(object sender, CVR_ValueElementEventArgs e);
- /// <summary>
- /// 交互触发器Value
- /// </summary>
- public class CVR_ValueElement : SerializedMonoBehaviour, IVR_OperationElement
- {
- private CVR_SimulationTool simulationTool;
- /// <summary>
- /// 可被操作工具类型
- /// </summary>
- public bool isActive = false;
- private bool isSimulation = false;
- private float value;
- public float Value
- {
- get { return value; }
- set
- {
- if (this.value != value)
- {
- this.value = value;
- ValueChanged?.Invoke(this, new CVR_ValueElementEventArgs() { valueElement = this, value = Value });
- }
- }
- }
- public CVR_ToolType toolType;
- [Header("触发点动画")]
- [TypeFilter("GetFilteredTypeList")]
- public CVR_ValueAniBase valueAniBase;
- [HideInInspector]
- public CVRValueElementEventHandler ElementTrigger;
- [HideInInspector]
- public CVRValueElementEventHandler ValueChanged;
- public void Trigger(CVR_InteractableTriggerEventArgs e)
- {
- isSimulation = true;
- valueAniBase.Init(this, e.triggerTool.GetComponent<CVR_SimulationTool>());
- simulationTool = e.triggerTool.GetComponent<CVR_SimulationTool>();
- simulationTool.ExitSimulationMode += ToolCloseSimulation;
- }
- /// <summary>
- /// 设置Value状态
- /// </summary>
- /// <param name="value"></param>
- public void ChangeValueState(float value)
- {
- this.Value = value;
- valueAniBase.SetValueState(value);
- }
- private void Update()
- {
- if (!isActive || !isSimulation || valueAniBase == null) return;
- valueAniBase.SimulationUpdate();
- }
- protected virtual void ToolCloseSimulation(object sender, CVR_SimulationEventArgs e)
- {
- simulationTool.ExitSimulationMode -= ToolCloseSimulation;
- isSimulation = false;
- valueAniBase.CloseSimulation();
- }
- public IEnumerable<Type> GetFilteredTypeList()
- {
- var result = typeof(CVR_ValueAniBase).Assembly.GetTypes()
- .Where(temp => !temp.IsAbstract)
- .Where(temp => !temp.IsGenericTypeDefinition)
- .Where(temp => typeof(CVR_ValueAniBase).IsAssignableFrom(temp));
- return result;
- }
- public bool GetActive()
- {
- return isActive;
- }
- public CVR_ToolType GetToolType()
- {
- return toolType;
- }
- }
- }
|