237 lines
7.3 KiB
C#
237 lines
7.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using Ichni;
|
|
using Ichni.Editor;
|
|
using Ichni.RhythmGame;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class FlexibleFloatTab : MonoBehaviour
|
|
{
|
|
public GraphicalFlexibleFloatWindow FatherWindow;
|
|
public List<EventPoint> eventPoints;
|
|
public RectTransform Area;
|
|
public RectTransform BeatArea;
|
|
public RectTransform XBeatArea;
|
|
public EventPoint eventPoint;
|
|
public GameObject BeatLine;
|
|
public string Title;
|
|
public GameObject FirstBeatLine;
|
|
public FlexibleFloat connectFloat;
|
|
public int BeatDeviver => FatherWindow.BeatDeviver;
|
|
public int BeatNextDeviver => FatherWindow.BeatNextDeviver;
|
|
|
|
// 初始化函数
|
|
public void Initialize(FlexibleFloat flexibleFloat, string title)
|
|
{
|
|
Title = title;
|
|
ClearChildren(Area);
|
|
ClearChildren(BeatArea);
|
|
eventPoints = new List<EventPoint>();
|
|
connectFloat = flexibleFloat;
|
|
CreateBeatLines();
|
|
CreateEventPoints();
|
|
RedrawEventPoints();
|
|
Area.localPosition = new Vector3(FatherWindow.songBeat * BeatDeviver, 0, 0);
|
|
}
|
|
|
|
// 清除子节点
|
|
private void ClearChildren(RectTransform parent)
|
|
{
|
|
for (int i = 0; i < parent.childCount; i++)
|
|
{
|
|
Destroy(parent.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
|
|
// 创建节拍线
|
|
private void CreateBeatLines()
|
|
{
|
|
for (int i = 0; i < (int)EditorManager.instance.songInformation.song.length / FatherWindow.timePerBeat; i++)
|
|
{
|
|
GameObject u = Instantiate(BeatLine, BeatArea);
|
|
u.transform.localPosition = new Vector3(BeatDeviver * i, 0, 0);
|
|
if (i == 1) FirstBeatLine = u;
|
|
}
|
|
}
|
|
|
|
// 创建事件点
|
|
private void CreateEventPoints()
|
|
{
|
|
for (int i = 0; i <= connectFloat.animations.Count - 1; i++)
|
|
{
|
|
AnimatedFloat animatedFloat = connectFloat.animations[i];
|
|
EventPoint eventPoint = Instantiate(this.eventPoint, Area);
|
|
eventPoint.FatherTab = this;
|
|
eventPoint.Initialize(animatedFloat);
|
|
eventPoints.Add(eventPoint);
|
|
LinkEventPoints(i, eventPoint);
|
|
}
|
|
}
|
|
|
|
// 连接事件点
|
|
private void LinkEventPoints(int index, EventPoint eventPoint)
|
|
{
|
|
if (index - 1 >= 0)
|
|
{
|
|
eventPoint.LastEventPoint = eventPoints[index - 1];
|
|
eventPoint.LastEventPoint.NextEventPoint = eventPoint;
|
|
}
|
|
else
|
|
{
|
|
eventPoint.LastEventPoint = null;
|
|
}
|
|
if (index == connectFloat.animations.Count - 1)
|
|
{
|
|
eventPoint.NextEventPoint = null;
|
|
}
|
|
}
|
|
|
|
// 重绘事件点
|
|
private void RedrawEventPoints()
|
|
{
|
|
foreach (var i in eventPoints)
|
|
{
|
|
i.ReDraw(scalevalue);
|
|
}
|
|
}
|
|
|
|
// 更新函数
|
|
public void Update()
|
|
{
|
|
Vector3 newPosition = new Vector3(-FatherWindow.songBeat * BeatDeviver, 0, 0);
|
|
Area.localPosition = newPosition;
|
|
BeatArea.localPosition = newPosition;
|
|
XBeatArea.localPosition = newPosition;
|
|
}
|
|
|
|
// 添加事件
|
|
public void AddEvent()
|
|
{
|
|
if (Keyboard.current.ctrlKey.isPressed)
|
|
{
|
|
EventPoint eventPoint = Instantiate(this.eventPoint, Area);
|
|
eventPoint.FatherTab = this;
|
|
eventPoint.Initialize(new AnimatedFloat(GetBeat(),
|
|
GetBeat() + (float.Parse(FatherWindow.EventMultiplier.text) * FatherWindow.timePerBeat), 0, 0, AnimationCurveType.Linear));
|
|
eventPoints.Insert(FindInsertIndex(eventPoint.animatedFloat.startTime), eventPoint);
|
|
LinkNewEventPoint(eventPoint);
|
|
eventPoint.ReDraw(scalevalue);
|
|
eventPoint.selectButton.onClick.Invoke();
|
|
FatherWindow.ChangeValue();
|
|
connectFloat.Add(eventPoint.animatedFloat);
|
|
connectFloat.Sort();
|
|
}
|
|
else
|
|
{
|
|
if (FatherWindow.ConnectedPoint != null) FatherWindow.ConnectedPoint.UpLoad();
|
|
}
|
|
}
|
|
public void SpawnEvent(AnimatedFloat animatedFloat)
|
|
{
|
|
EventPoint eventPoint = Instantiate(this.eventPoint, Area);
|
|
eventPoint.FatherTab = this;
|
|
eventPoint.Initialize(animatedFloat);
|
|
eventPoints.Insert(FindInsertIndex(eventPoint.animatedFloat.startTime), eventPoint);
|
|
LinkNewEventPoint(eventPoint);
|
|
eventPoint.ReDraw(scalevalue);
|
|
connectFloat.Add(eventPoint.animatedFloat);
|
|
connectFloat.Sort();
|
|
}
|
|
// 连接新事件点
|
|
private void LinkNewEventPoint(EventPoint eventPoint)
|
|
{
|
|
int index = eventPoints.IndexOf(eventPoint);
|
|
if (index - 1 >= 0)
|
|
{
|
|
eventPoint.LastEventPoint = eventPoints[index - 1];
|
|
eventPoint.LastEventPoint.NextEventPoint = eventPoint;
|
|
eventPoint.LastEventPoint.ReDraw(scalevalue);
|
|
}
|
|
if (index + 1 < eventPoints.Count)
|
|
{
|
|
eventPoint.NextEventPoint = eventPoints[index + 1];
|
|
eventPoint.animatedFloat.endTime = eventPoint.NextEventPoint.animatedFloat.startTime;
|
|
eventPoint.Initialize(eventPoint.animatedFloat);
|
|
eventPoint.NextEventPoint.LastEventPoint = eventPoint;
|
|
}
|
|
}
|
|
|
|
// 查找插入索引
|
|
public int FindInsertIndex(float startTime)
|
|
{
|
|
int low = 0;
|
|
int high = eventPoints.Count - 1;
|
|
while (low <= high)
|
|
{
|
|
int mid = (low + high) / 2;
|
|
if (eventPoints[mid].animatedFloat.startTime < startTime)
|
|
{
|
|
low = mid + 1;
|
|
}
|
|
else
|
|
{
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
return low;
|
|
}
|
|
|
|
// 获取节拍
|
|
public float GetBeat()
|
|
{
|
|
float mouseInputX = Mouse.current.position.ReadValue().x;
|
|
float far = BeatArea.transform.position.x;
|
|
float Beat = 0;
|
|
while (far < mouseInputX)
|
|
{
|
|
far += (FirstBeatLine.transform.position.x - BeatArea.transform.position.x) / BeatNextDeviver;
|
|
Beat += 1f / BeatNextDeviver;
|
|
}
|
|
return FatherWindow.timePerBeat * (Beat - (1f / BeatNextDeviver));
|
|
}
|
|
|
|
public float scalevalue => FatherWindow.scalevalue;
|
|
|
|
// 曲线缩放
|
|
public void CurveScale(float value)
|
|
{
|
|
foreach (EventPoint i in eventPoints)
|
|
{
|
|
i.ReDraw(value);
|
|
}
|
|
}
|
|
|
|
// 改变X轴节拍
|
|
public void XbeatCnange(int num)
|
|
{
|
|
ClearChildren(XBeatArea);
|
|
for (int i = 1; i < num; i++)
|
|
{
|
|
foreach (Transform child in BeatArea)
|
|
{
|
|
GameObject newChild = Instantiate(child.gameObject, XBeatArea);
|
|
CanvasGroup canvasGroup = newChild.GetComponent<CanvasGroup>();
|
|
newChild.transform.localPosition = new Vector3(child.localPosition.x + ((float)BeatDeviver) / num * i, child.localPosition.y, child.localPosition.z);
|
|
if (canvasGroup == null)
|
|
{
|
|
canvasGroup = newChild.AddComponent<CanvasGroup>();
|
|
}
|
|
canvasGroup.alpha = 0.1f;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 移除动画
|
|
public void remoceAnim(AnimatedFloat a)
|
|
{
|
|
if (connectFloat.animations.Contains(a))
|
|
{
|
|
connectFloat.animations.Remove(a);
|
|
}
|
|
}
|
|
}
|