2025-03-20 13:21:32 +08:00

146 lines
4.2 KiB
C#

using HighlightPlus;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace QFramework
{
internal class HighLightAction : IAction
{
public System.Action OnFinished { get; set; }
private HighLightAction()
{
}
private static readonly SimpleObjectPool<HighLightAction> mPool =
new SimpleObjectPool<HighLightAction>(() => new HighLightAction(), null, 10);
string path;
Color color = Color.green;
bool isHigh = true;
string deviceName = string.Empty;
string isIndependent;
string visibility;
public static HighLightAction Allocate(string path, Dictionary<string, string> datas, System.Action OnFinished = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.path = path;
if (datas.ContainsKey("color"))
{
retNode.color = Utility.ToColor(datas["color"]);
}
if (datas.ContainsKey("isHigh"))
{
bool.TryParse(datas["isHigh"], out retNode.isHigh);
}
retNode.deviceName = datas.ContainsKey("deviceName") ? datas["deviceName"] : string.Empty;
retNode.isIndependent = datas.ContainsKey("isIndependent") ? datas["isIndependent"] : string.Empty;
retNode.visibility = datas.ContainsKey("visibility") ? datas["visibility"] : string.Empty;
retNode.OnFinished = OnFinished;
return retNode;
}
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
GameObject obj = null;
if (string.IsNullOrEmpty(deviceName) == false)
{
obj = DeviceController.Instance.GetDeviceObj(deviceName);
}
else
{
obj = Utility.FindObj(path);
}
if (obj != null)
{
if (isHigh)
{
var effect = obj.GetOrAddComponent<HighlightEffect>();
effect.outlineColor = color;
effect.highlighted = true;
Visibility visibility = Visibility.Normal;
Enum.TryParse(this.visibility, out visibility);
effect.outlineVisibility = visibility;
obj.GetOrAddComponent<HighLightOnStepChanged>();
if (string.IsNullOrEmpty(isIndependent) == false)
{
switch (isIndependent)
{
case "true":
effect.outlineIndependent = true;
break;
case "false":
effect.outlineIndependent = false;
break;
}
}
#if VR
effect.constantWidth = false;
#endif
}
else
{
var effect = obj.GetComponent<HighlightEffect>();
if (effect)
{
effect.highlighted = false;
}
var flash = obj.GetComponent<HighLightFlashItem>();
if (flash)
{
flash.Stop();
}
}
}
}
public void OnExecute(float dt)
{
this.Finish();
OnFinished?.Invoke();
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
OnFinished = null;
Deinited = true;
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}