2019-10-27 21:09:18 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
|
{
|
2019-11-01 12:33:23 +00:00
|
|
|
|
|
|
|
|
|
public interface IStateReader
|
|
|
|
|
{
|
2019-11-01 16:13:16 +00:00
|
|
|
|
State? PreviousState { get; }
|
2019-11-01 12:33:23 +00:00
|
|
|
|
State CurrentState { get; }
|
2019-11-01 16:13:16 +00:00
|
|
|
|
State? NextState { get; }
|
2019-11-01 12:33:23 +00:00
|
|
|
|
}
|
2019-10-27 21:09:18 +00:00
|
|
|
|
|
2019-11-01 12:33:23 +00:00
|
|
|
|
public interface IStateManager : IStateReader
|
2019-10-27 21:09:18 +00:00
|
|
|
|
{
|
2019-11-01 10:41:06 +00:00
|
|
|
|
int SelectedState { set; }
|
|
|
|
|
State[] States { get; }
|
2019-10-27 21:09:18 +00:00
|
|
|
|
}
|
2019-11-01 10:41:06 +00:00
|
|
|
|
|
|
|
|
|
public struct State
|
|
|
|
|
{
|
|
|
|
|
public string Title;
|
|
|
|
|
public string Note;
|
2019-11-01 12:33:23 +00:00
|
|
|
|
public int Bpm;
|
2019-11-01 10:41:06 +00:00
|
|
|
|
|
2019-11-01 12:33:23 +00:00
|
|
|
|
public float Speed => Bpm / 60f; // 60 BPM as speed 1f
|
2019-11-01 10:41:06 +00:00
|
|
|
|
}
|
2019-10-27 21:09:18 +00:00
|
|
|
|
|
2019-11-01 12:33:23 +00:00
|
|
|
|
[Serializable]
|
|
|
|
|
public class UnityStateEvent : UnityEvent<IStateReader>
|
|
|
|
|
{}
|
|
|
|
|
|
2019-10-27 21:09:18 +00:00
|
|
|
|
public class StateManager : MonoBehaviour, IStateManager
|
|
|
|
|
{
|
2019-10-29 12:45:12 +00:00
|
|
|
|
private enum Operation
|
|
|
|
|
{
|
|
|
|
|
Rewind = 0,
|
|
|
|
|
Previous = 1,
|
|
|
|
|
Next = 2
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 21:09:18 +00:00
|
|
|
|
[SerializeField] private string csvFileName = "qlist";
|
|
|
|
|
[SerializeField] private UnityStateEvent onStateChanged;
|
|
|
|
|
[SerializeField] private int sceneSelection;
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
var content = System.IO.File.ReadAllText(Application.streamingAssetsPath + "/" + csvFileName + ".csv");
|
2019-11-01 10:41:06 +00:00
|
|
|
|
var lines = content.Split('\n');
|
|
|
|
|
|
|
|
|
|
var numberOfEntries = lines.Length - 1;
|
|
|
|
|
States = new State[numberOfEntries];
|
|
|
|
|
for (var i = 0; i < numberOfEntries; ++i)
|
2019-10-27 21:09:18 +00:00
|
|
|
|
{
|
2019-11-01 10:41:06 +00:00
|
|
|
|
var columns = (lines[i+1].Trim()).Split(',');
|
|
|
|
|
|
|
|
|
|
States[i].Title = columns[0];
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-01 12:33:23 +00:00
|
|
|
|
States[i].Bpm = int.Parse(columns[1]);
|
2019-11-01 10:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
2019-11-01 12:33:23 +00:00
|
|
|
|
States[i].Bpm = 60; // gracefully fail
|
2019-11-01 10:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
States[i].Note = columns[2];
|
2019-10-27 21:09:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-01 10:41:06 +00:00
|
|
|
|
|
|
|
|
|
public State[] States { get; private set; }
|
2019-10-27 21:09:18 +00:00
|
|
|
|
|
2019-11-01 11:06:54 +00:00
|
|
|
|
public State CurrentState => States[sceneSelection];
|
2019-10-29 13:32:55 +00:00
|
|
|
|
|
2019-11-01 11:06:54 +00:00
|
|
|
|
public State? PreviousState => sceneSelection == 0 ? (State?) null : States[sceneSelection-1];
|
2019-10-29 13:32:55 +00:00
|
|
|
|
|
2019-11-01 11:06:54 +00:00
|
|
|
|
public State? NextState => sceneSelection == States.Length - 1 ? (State?) null : States[sceneSelection + 1];
|
2019-10-29 13:32:55 +00:00
|
|
|
|
|
2019-11-01 10:41:06 +00:00
|
|
|
|
public int SelectedState
|
2019-10-27 21:09:18 +00:00
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
sceneSelection = value;
|
2019-10-29 13:32:55 +00:00
|
|
|
|
onStateChanged.Invoke(this);
|
2019-10-27 21:09:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-27 21:28:47 +00:00
|
|
|
|
|
|
|
|
|
public void OnMidiReceived(MidiMessage message)
|
|
|
|
|
{
|
2019-10-29 12:45:12 +00:00
|
|
|
|
if (message.Status != 176 || message.Data1 != 127) return;
|
|
|
|
|
|
|
|
|
|
switch ((Operation)message.Data2)
|
2019-10-27 21:28:47 +00:00
|
|
|
|
{
|
2019-10-29 12:45:12 +00:00
|
|
|
|
case Operation.Rewind:
|
|
|
|
|
if (sceneSelection == 0) return;
|
|
|
|
|
sceneSelection = 0;
|
|
|
|
|
break;
|
|
|
|
|
case Operation.Previous:
|
|
|
|
|
if (sceneSelection == 0) return;
|
|
|
|
|
sceneSelection--;
|
|
|
|
|
break;
|
|
|
|
|
case Operation.Next:
|
2019-11-01 10:41:06 +00:00
|
|
|
|
if (sceneSelection >= States.Length - 1) return;
|
2019-10-29 12:45:12 +00:00
|
|
|
|
sceneSelection++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
2019-10-27 21:28:47 +00:00
|
|
|
|
}
|
2019-10-29 13:32:55 +00:00
|
|
|
|
onStateChanged.Invoke(this);
|
2019-10-27 21:09:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 21:36:57 +00:00
|
|
|
|
}
|