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;
}
///
/// Trigger
///
/// this object
///
public delegate void CVRValueElementEventHandler(object sender, CVR_ValueElementEventArgs e);
///
/// 交互触发器Value
///
public class CVR_ValueElement : SerializedMonoBehaviour, IVR_OperationElement
{
private CVR_SimulationTool simulationTool;
///
/// 可被操作工具类型
///
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());
simulationTool = e.triggerTool.GetComponent();
simulationTool.ExitSimulationMode += ToolCloseSimulation;
}
///
/// 设置Value状态
///
///
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 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;
}
}
}