120 lines
3.3 KiB
C#
120 lines
3.3 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author JiphuTzu
|
||
//@create 20220910
|
||
//@company Umawerse
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
namespace Umawerse.FiniteStateMachines
|
||
{
|
||
public abstract class DrawingState : FiniteState<ToolsBootstrap>
|
||
{
|
||
protected static readonly int COLOR_ID = Shader.PropertyToID("_Color");
|
||
protected List<Vector3> vertices;
|
||
protected GameObject target;
|
||
private int _minVertexCount;
|
||
protected int _maxVertexCount;
|
||
protected StateName currentName;
|
||
|
||
|
||
protected DrawingState(ToolsBootstrap root, StateName name, int minCount, int maxCount) : base(root, name)
|
||
{
|
||
_minVertexCount = minCount;
|
||
_maxVertexCount = maxCount;
|
||
}
|
||
|
||
public override void Enter()
|
||
{
|
||
currentName = name;
|
||
root.tapGesture.onOneClick.AddListener(() =>
|
||
{
|
||
OnTap();
|
||
});
|
||
root.tapGesture.onDoubleClick.AddListener(() =>
|
||
{
|
||
OnDoubleTap();
|
||
});
|
||
root.tapGesture.SetLayerMask(1<<8);
|
||
Init();
|
||
}
|
||
|
||
private void OnTap()
|
||
{
|
||
if (root.tapGesture.CheckGuiRaycastObjects()) return;
|
||
|
||
Vector3 posTemp = root.tapGesture.HitTest(Input.mousePosition, false).point;
|
||
OnTap(posTemp);
|
||
}
|
||
|
||
private void OnDoubleTap()
|
||
{
|
||
if (root.tapGesture.CheckGuiRaycastObjects()) return;
|
||
Vector3 posTemp = root.tapGesture.HitTest(Input.mousePosition, false).point;
|
||
OnDoubleTap(posTemp);
|
||
|
||
Exit();
|
||
Enter();
|
||
|
||
}
|
||
|
||
|
||
|
||
protected abstract void OnTap(Vector3 pos);
|
||
protected abstract void OnDoubleTap(Vector3 pos);
|
||
|
||
protected abstract void UpdatePreview(Vector3 pos);
|
||
|
||
public virtual void Init()
|
||
{
|
||
vertices = new List<Vector3>();
|
||
target = new GameObject($"{name}_{Time.frameCount:D5}");
|
||
root.instructs.Add(target);
|
||
}
|
||
|
||
public abstract void Complete();
|
||
|
||
// private int GetClickCount()
|
||
// {
|
||
// return root.inputSystem.GetClickCount();
|
||
// }
|
||
|
||
public override StateName Update()
|
||
{
|
||
if (vertices.Count >= _maxVertexCount) return StateName.none;
|
||
if (vertices.Count > 0)
|
||
{
|
||
var hit = root.tapGesture.HitTest();
|
||
if (hit.collider != null)
|
||
UpdatePreview(new Vector3(hit.point.x, 580f, hit.point.z));
|
||
}
|
||
return currentName;
|
||
}
|
||
|
||
|
||
public GameObject GetObject()
|
||
{
|
||
return target;
|
||
}
|
||
public abstract void AddPoint(Vector3 position, bool showImmediately = true);
|
||
|
||
|
||
public override void Exit()
|
||
{
|
||
root.tapGesture.onOneClick.RemoveAllListeners();
|
||
root.tapGesture.onDoubleClick.RemoveAllListeners();
|
||
if (target.transform.childCount <= 1)
|
||
{
|
||
Object.Destroy(target);
|
||
}
|
||
if (vertices.Count >= _minVertexCount)
|
||
{
|
||
var h = vertices.Max(v => v.y);
|
||
}
|
||
}
|
||
}
|
||
}
|