Files
ichni_Creator_Studio/Assets/Prefabs/Sample Assiant Window/SampleWindow.cs
2025-05-01 22:54:56 +08:00

194 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Ichni;
using Ichni.Editor;
using Ichni.RhythmGame;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class SampleWindow : MovableWindow//该window高度为300横的要在200和500之间切换
{
public static List<SampleWindow> instances = new List<SampleWindow>();
public TMP_InputField DeviverInputField;
public TMP_InputField verticalInputField;
public TMP_InputField horizontalInputField;
public RectTransform LineMovepoint;
public RectTransform NoteMovepoint;
public List<NoteBase> noteBases;
public bool isFocus = false;
public bool isExpand = false;
public int beatDeviver = 100;
public int Xdevide = 1;
public float realDevider;
public GameObject beatLinePrefabv;
public GameObject beatLinePrefabh;
public GameObject NotePrefab;
public RectTransform secBeatLineh;//用于定位屏幕位置
float songTime => EditorManager.instance.songInformation.songTime;
float songBeat => EditorManager.instance.songInformation.songBeat;
float beatmapStartTime => -EditorManager.instance.songInformation.delay;
float timePerBeat => 60f / EditorManager.instance.songInformation.bpm;
public GameElement gameElement;
public void Initialize(GameElement qgameElement, string title)
{
closeButton.onClick.AddListener(() =>
{
instances.Remove(this);
});
if (instances.Where(i => i.gameElement == qgameElement).Count() != 0)
{
Destroy(this.gameObject);
foreach (SampleWindow i in instances)
{
i.StartCoroutine(WindowAnim.Shake(instances.Where(i => i.gameElement == qgameElement).First().windowRect.gameObject));
}
return;
}
this.gameElement = qgameElement;
InitializeWindow(title);
//
SpawnBeatline();
OnceSpawnNote();
instances.Add(this);
}
public void SpawnBeatline()//添加优化措施
{
for (int i = LineMovepoint.childCount - 1; i >= 0; i--)
{
Destroy(LineMovepoint.GetChild(i).gameObject);
}
for (int i = 0; i < (int)EditorManager.instance.songInformation.song.length / timePerBeat; i++)
{
GameObject u = Instantiate(beatLinePrefabh, LineMovepoint);
u.transform.localPosition = new Vector3(0, i * beatDeviver, 0);
if (i == 1)
{
secBeatLineh = u.GetComponent<RectTransform>();
realDevider = secBeatLineh.position.y - LineMovepoint.position.y;
Debug.Log(realDevider);
}
if (u.transform.localPosition.y > 300)
{
Destroy(u);
break;
}
}
}
public void OnceSpawnNote()
{
if (gameElement is Track track) noteBases = track.GetAllNotes();
else if (gameElement is ElementFolder elementFolder) noteBases = elementFolder.GetAllNotes();
for (int i = NoteMovepoint.childCount - 1; i >= 0; i--)
{
Destroy(NoteMovepoint.GetChild(i).gameObject);
}
foreach (var i in noteBases)
{
SpawnNote(i);
}
}
private void SpawnNote(NoteBase i)
{
GameObject u = Instantiate(NotePrefab, NoteMovepoint);
u.transform.localPosition = new Vector3(0, i.exactJudgeTime / timePerBeat * beatDeviver, 0);
switch (i)
{
case Hold hold:
u.GetComponent<Image>().color = new Color(0, 1, 0, 1);
RawImage a = u.GetComponent<RawImage>();
a.transform.localPosition = new Vector3(0, (hold.holdEndTime - hold.exactJudgeTime) / timePerBeat * beatDeviver / 2, 0);
a.GetComponent<RectTransform>().sizeDelta = new Vector2(0, (hold.holdEndTime - hold.exactJudgeTime) / timePerBeat * beatDeviver);
break;
case Tap:
u.GetComponent<Image>().color = new Color(0, 1, 1, 1);
break;
case Stay:
u.GetComponent<Image>().color = new Color(1, 1, 0, 1);
break;
case Flick:
u.GetComponent<Image>().color = new Color(1, 0.2f, 0, 1);
break;
}
}
void Update()
{
LineMovepoint.localPosition = new(0, -beatDeviver * (songBeat - (int)songBeat), 0);
NoteMovepoint.localPosition = new(0, -beatDeviver * songBeat, 0);
if (isFocus && gameElement is Track track)
{
}
}
public void ChangeFocus()
{
isFocus = !isFocus;
}
public void ChangeXdevide(string devide)
{
Xdevide = int.Parse(devide);
}
public void ChangeBeatdevide(string devide)
{
beatDeviver = int.Parse(devide);
SpawnBeatline();
OnceSpawnNote();
}
public void ChangeExpand()
{
if (isExpand)
{
isExpand = false;
windowRect.sizeDelta = new Vector2(200, windowRect.sizeDelta.y);
}
else
{
isExpand = true;
windowRect.sizeDelta = new Vector2(500, windowRect.sizeDelta.y);
}
}
public void AddNote()
{
// 获取鼠标在 NoteMovepoint 中的相对位置
Vector2 localMousePosition = NoteMovepoint.InverseTransformPoint(Mouse.current.position.ReadValue());
Debug.Log(localMousePosition);
// 使用 realDevider 进行整数倍舍入
float mouseBeat = localMousePosition.y / beatDeviver;
float far = 0f;
while (far < mouseBeat)
{
far += 1f / Xdevide;
}
far -= 1f / Xdevide;
float time = (far * timePerBeat);//idk
//Debug.Log($"Rounded Position Y: {time}");
if (!isExpand)
{
Tap a = Tap.GenerateElement("New Tap", Guid.NewGuid(), new List<string>(), true, gameElement, time);
noteBases.Add(a);
SpawnNote(a);
}
}
}