VirtualFramework/Assets/Scripts/Actions/HighLightFlashAction.cs

154 lines
4.7 KiB
C#
Raw Normal View History

2025-02-06 16:26:44 +08:00
using HighlightPlus;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace QFramework
{
internal class HighLightFlashAction : IAction
{
public System.Action OnFinished { get; set; }
private HighLightFlashAction()
{
}
private static readonly SimpleObjectPool<HighLightFlashAction> mPool =
new SimpleObjectPool<HighLightFlashAction>(() => new HighLightFlashAction(), null, 10);
string path;
Color color = Color.green;
bool isHigh = true;
string deviceName = string.Empty;
string isIndependent;
string count;
string time;
string finishedEvent;
2025-02-07 13:07:29 +08:00
string visibility;
2025-02-06 16:26:44 +08:00
public static HighLightFlashAction 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.count = datas.ContainsKey("count") ? datas["count"] : string.Empty;
retNode.time = datas.ContainsKey("time") ? datas["time"] : string.Empty;
retNode.finishedEvent = datas.ContainsKey("finishedEvent") ? datas["finishedEvent"] : string.Empty;
2025-02-07 13:07:29 +08:00
retNode.visibility = datas.ContainsKey("visibility") ? datas["visibility"] : string.Empty;
2025-02-06 16:26:44 +08:00
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)
{
var flash = obj.GetOrAddComponent<HighLightFlashItem>();
if (isHigh)
{
var effect = obj.GetOrAddComponent<HighlightEffect>();
effect.outlineColor = color;
effect.highlighted = true;
2025-02-07 13:07:29 +08:00
Visibility visibility = Visibility.Normal;
Enum.TryParse(this.visibility, out visibility);
effect.outlineVisibility = visibility;
2025-02-06 16:26:44 +08:00
obj.GetOrAddComponent<HighLightOnStepChanged>();
if (string.IsNullOrEmpty(isIndependent) == false)
{
switch (isIndependent)
{
case "true":
effect.outlineIndependent = true;
break;
case "false":
effect.outlineIndependent = false;
break;
}
}
float time = 1;
int count = -1;
float.TryParse(this.time, out time);
int.TryParse(this.count, out count);
flash.Init(time, count, finishedEvent);
}
else
{
var effect = obj.GetComponent<HighlightEffect>();
if (effect)
{
effect.highlighted = false;
}
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; }
}
}