173 lines
5.0 KiB
C#
173 lines
5.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class PrepController : MonoBehaviour
|
|
{
|
|
public Transform cam;
|
|
public AudioSource source;
|
|
public GameObject markerPrefab;
|
|
public GameObject flashIndicator;
|
|
public Sprite playIcon;
|
|
public Sprite pauseIcon;
|
|
public Button playBtn;
|
|
public Button pinBtn;
|
|
public Button removePinBtn;
|
|
public Toggle followSeekerToggle;
|
|
public TMP_Text playbackTimeTxt;
|
|
public TMP_Text hitCountTxt;
|
|
public static List<GameObject> markers = new List<GameObject>();
|
|
|
|
public static void Reset(){
|
|
markers = new List<GameObject>();
|
|
}
|
|
|
|
public static PrepController instance;
|
|
void Awake(){
|
|
playBtn.onClick.AddListener(TogglePlay);
|
|
pinBtn.onClick.AddListener(AddMarker);
|
|
removePinBtn.onClick.AddListener(RemoveMarker);
|
|
instance = this;
|
|
|
|
Screen.orientation = ScreenOrientation.LandscapeLeft;
|
|
}
|
|
void Update()
|
|
{
|
|
if(Input.GetKeyDown(KeyCode.RightArrow)){
|
|
AddMarker();
|
|
}
|
|
if(Input.GetKeyDown(KeyCode.LeftArrow)){
|
|
|
|
float lastTime=0f;
|
|
for(int i=0; i < markers.Count; i++){
|
|
if(markers[i].GetComponent<Marker>().myTime < source.time){
|
|
lastTime = markers[i].GetComponent<Marker>().myTime;
|
|
}else{
|
|
break;
|
|
}
|
|
}
|
|
Vector3 newCamPos = new Vector3(lastTime * AudioAnalyzer.instance.zoomingYMult, cam.position.y, cam.position.z);
|
|
if(source.isPlaying){
|
|
TogglePlay();
|
|
cam.position = newCamPos;
|
|
TogglePlay();
|
|
}else{
|
|
cam.position = newCamPos;
|
|
}
|
|
|
|
if(Input.GetKey(KeyCode.RightShift)){
|
|
RemoveMarker();
|
|
}
|
|
|
|
}
|
|
hitCountTxt.text = markers.Count.ToString();
|
|
playbackTimeTxt.text = source.time.ToString("n3");
|
|
|
|
if(followSeekerToggle.isOn){
|
|
if(source.isPlaying){
|
|
cam.position = new Vector3(source.time * AudioAnalyzer.instance.zoomingYMult, cam.position.y, cam.position.z);
|
|
source.time = startOffset + (Time.time - startTime);
|
|
}else{
|
|
source.time = cam.position.x / AudioAnalyzer.instance.zoomingYMult;
|
|
}
|
|
}
|
|
bool anythingInProximity = false;
|
|
for(int i=0; i < transform.childCount; i++){
|
|
if(Mathf.Abs(transform.GetChild(i).position.x - source.time) < 0.1f){
|
|
anythingInProximity = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
flashIndicator.SetActive(anythingInProximity);
|
|
}
|
|
|
|
float startTime;
|
|
float startOffset;
|
|
public void TogglePlay(){
|
|
if(source.isPlaying){
|
|
source.Pause();
|
|
playBtn.GetComponent<Image>().sprite = playIcon;
|
|
}else{
|
|
source.time = cam.position.x / AudioAnalyzer.instance.zoomingYMult;
|
|
source.Play();
|
|
playBtn.GetComponent<Image>().sprite = pauseIcon;
|
|
startTime = Time.time;
|
|
startOffset = source.time;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void SetMarkers(List<float> times){
|
|
foreach(GameObject go in markers){
|
|
Destroy(go);
|
|
}
|
|
|
|
markers = new List<GameObject>();
|
|
|
|
foreach(float time in times){
|
|
AddMarker(time);
|
|
}
|
|
}
|
|
|
|
public void AddMarker(){
|
|
GameObject go = Instantiate(markerPrefab, transform);
|
|
// go.transform.position = new Vector3(cam.position.x, 0);
|
|
go.GetComponent<Marker>().myTime = cam.position.x / AudioAnalyzer.instance.zoomingYMult;
|
|
|
|
|
|
markers.Add(go);
|
|
}
|
|
|
|
public void AddMarker(float time){
|
|
GameObject go = Instantiate(markerPrefab, transform);
|
|
// go.transform.position = new Vector3(time, 0);
|
|
go.GetComponent<Marker>().myTime = time;
|
|
markers.Add(go);
|
|
}
|
|
|
|
public void RemoveMarker(){
|
|
if(markers.Count <= 0){ return; }
|
|
|
|
GameObject closestOne = markers[0];
|
|
foreach(GameObject marker in markers){
|
|
float diff = Mathf.Abs(marker.transform.position.x - cam.position.x);
|
|
float closestDiff = Mathf.Abs(closestOne.transform.position.x - cam.position.x);
|
|
if(diff < closestDiff){
|
|
closestOne = marker;
|
|
}
|
|
}
|
|
|
|
int indexToRemove = 0;
|
|
for(int i =0; i < markers.Count; i++){
|
|
if(markers[i] == closestOne){
|
|
indexToRemove = i;
|
|
}
|
|
}
|
|
markers.RemoveAt(indexToRemove);
|
|
Destroy(closestOne);
|
|
}
|
|
|
|
|
|
public void OnSeekerDragged(Vector3 newCamPos){
|
|
if(source.isPlaying){
|
|
TogglePlay();
|
|
}
|
|
}
|
|
|
|
|
|
bool playAfterDragEnd = false;
|
|
public void OnSeekerDown(){
|
|
playAfterDragEnd=source.isPlaying;
|
|
}
|
|
|
|
public void OnSeekerUp(){
|
|
if(playAfterDragEnd){
|
|
TogglePlay();
|
|
}
|
|
}
|
|
}
|