Extract scheduled Action

This commit is contained in:
Chikashi Miyama 2019-12-06 22:21:24 +01:00
parent f3e53b8597
commit 1582a10ce2
3 changed files with 58 additions and 26 deletions

View file

@ -4,7 +4,9 @@ using UnityEngine.Playables;
namespace cylvester namespace cylvester
{ {
public enum CYLCommand public class CylMidiTransitionController : MonoBehaviour
{
private enum CylCommand
{ {
OneBarLoopButton = 94, OneBarLoopButton = 94,
FourBarLoopButton = 86, FourBarLoopButton = 86,
@ -13,9 +15,6 @@ namespace cylvester
InstantTrigger = 2, InstantTrigger = 2,
} }
public class CylMidiTransitionController : MonoBehaviour
{
[SerializeField] private PlayableDirector playableDirector; [SerializeField] private PlayableDirector playableDirector;
[SerializeField, Range(1, 16)] private int channel = 1; [SerializeField, Range(1, 16)] private int channel = 1;
[SerializeField] private StateManager stateManager; [SerializeField] private StateManager stateManager;
@ -25,12 +24,21 @@ namespace cylvester
private const int FourBarTrigger = OneBarTrigger * 4; private const int FourBarTrigger = OneBarTrigger * 4;
private const float TransitionLength = 16; private const float TransitionLength = 16;
private bool instantChangeActive_; private ScheduledAction scheduledAction_;
private int currentTick_; private int currentTick_;
private float restTime_ = 1f; private float restTime_ = 1f;
private int currentSelectedScene_ ; private int currentSelectedScene_ ;
private int nextSelectedScene_; private int nextSelectedScene_;
private void Start()
{
scheduledAction_ = new ScheduledAction(() =>
{
stateManager.SelectedState = currentSelectedScene_;
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
});
}
public void OnSyncReceived(MidiSync midiSync, int counter) public void OnSyncReceived(MidiSync midiSync, int counter)
{ {
currentTick_ = counter; currentTick_ = counter;
@ -40,33 +48,26 @@ namespace cylvester
{ {
if (mes.Status - 176 != channel - 1) return; if (mes.Status - 176 != channel - 1) return;
var command = (CYLCommand)mes.Data1; var command = (CylCommand)mes.Data1;
switch (command) switch (command)
{ {
case CYLCommand.NextSelectedScene: case CylCommand.NextSelectedScene:
{ {
nextSelectedScene_ = mes.Data2; nextSelectedScene_ = mes.Data2;
break; break;
} }
case CYLCommand.InstantTrigger: case CylCommand.InstantTrigger:
{ {
instantChangeActive_ = true; scheduledAction_.Ready();
break; break;
} }
case CYLCommand.CurrentSelectedScene: case CylCommand.CurrentSelectedScene:
{ {
currentSelectedScene_ = mes.Data2; currentSelectedScene_ = mes.Data2;
scheduledAction_.Go();
if (instantChangeActive_)
{
stateManager.SelectedState = currentSelectedScene_;
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
instantChangeActive_ = false;
}
break; break;
} }
case CYLCommand.FourBarLoopButton: case CylCommand.FourBarLoopButton:
{ {
if (nextSelectedScene_ > currentSelectedScene_) if (nextSelectedScene_ > currentSelectedScene_)
{ {
@ -83,7 +84,7 @@ namespace cylvester
break; break;
} }
case CYLCommand.OneBarLoopButton: case CylCommand.OneBarLoopButton:
{ {
UpdateRestTime(OneBarTrigger - currentTick_ % OneBarTrigger); UpdateRestTime(OneBarTrigger - currentTick_ % OneBarTrigger);
UpdateTimelinePlaybackSpeed(1f); UpdateTimelinePlaybackSpeed(1f);

View file

@ -0,0 +1,28 @@
using System;
namespace cylvester
{
public class ScheduledAction
{
public ScheduledAction(Action action)
{
action_ = action;
}
public void Ready()
{
standBy_ = true;
}
public void Go()
{
if (!standBy_) return;
action_.Invoke();
standBy_ = false;
}
private bool standBy_;
private readonly Action action_;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3911173c24224aae853febfafe73c56c
timeCreated: 1575666666