fix formatting and style error

This commit is contained in:
Chikashi Miyama 2019-12-06 21:49:52 +01:00
parent 24c7c805bc
commit f3e53b8597

View file

@ -1,25 +1,16 @@
using UnityEngine; using System;
using UnityEngine.Events; using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables; using UnityEngine.Playables;
namespace cylvester namespace cylvester
{ {
public enum CYLCommand
public enum CYL_Command
{ {
//these are the midi CCs coming from the CYL_Axoloti box
OneBarLoopButton = 94, OneBarLoopButton = 94,
FourBarLoopButton = 86, FourBarLoopButton = 86,
NextScelectedScene = 18, NextSelectedScene = 18,
CurrentSelectedScene = 17, CurrentSelectedScene = 17,
instaTrig = 2, InstantTrigger = 2,
}
public enum Timeline_Command
{
Forwards = 1,
Backwards = -1
} }
public class CylMidiTransitionController : MonoBehaviour public class CylMidiTransitionController : MonoBehaviour
@ -27,86 +18,93 @@ namespace cylvester
[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] StateManager stateManager; [SerializeField] private StateManager stateManager;
[SerializeField] float instaTransitionSpeed = 10; [SerializeField] private float instaTransitionSpeed = 10;
private const int oneBarTrigger = 96; private const int OneBarTrigger = 96;
private const int fourBarTrigger = 384; private const int FourBarTrigger = OneBarTrigger * 4;
private const float TransitionLength = 16;
private bool instaChangeActive;
private bool instantChangeActive_;
private int currentTick; private int currentTick_;
private float transitionLength = 16; //sets the duration in Seconds, how long a transition has to be in "TimeLine" to be played back correctly when CYLVESTER is hooked up correctly private float restTime_ = 1f;
private float restTimeS = 1f; //init transTime is 1 Second private int currentSelectedScene_ ;
private int nextSelectedScene_;
int currentSelectedScene = 0;
int nextSelectedScene = 0;
public void OnSyncReceived(MidiSync midiSync, int counter) public void OnSyncReceived(MidiSync midiSync, int counter)
{ {
currentTick = counter; currentTick_ = counter;
} }
public void OnMidiMessageReceived(MidiMessage mes) public void OnMidiMessageReceived(MidiMessage mes)
{ {
if (mes.Status - 176 != channel - 1) return;
if (mes.Status - 176 == channel - 1) //Choose Midi-Channel var command = (CYLCommand)mes.Data1;
switch (command)
{ {
case CYLCommand.NextSelectedScene:
switch (mes.Data1)
{ {
case (byte) CYL_Command.NextScelectedScene: nextSelectedScene_ = mes.Data2;
nextSelectedScene = mes.Data2; //Get next selected Scene
break; break;
}
case (byte) CYL_Command.instaTrig: case CYLCommand.InstantTrigger:
instaChangeActive = true; {
instantChangeActive_ = true;
break; break;
}
case CYLCommand.CurrentSelectedScene:
{
currentSelectedScene_ = mes.Data2;
case (byte) CYL_Command.CurrentSelectedScene: if (instantChangeActive_)
currentSelectedScene = mes.Data2; //Get current selected Scene {
stateManager.SelectedState = currentSelectedScene_;
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
instantChangeActive_ = false;
}
if (instaChangeActive) //This triggers instant Switch between states
{
stateManager.SelectedState = currentSelectedScene;
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
instaChangeActive = false;
}
break; break;
}
case CYLCommand.FourBarLoopButton:
{
if (nextSelectedScene_ > currentSelectedScene_)
{
UpdateRestTime(FourBarTrigger - currentTick_ % FourBarTrigger);
UpdateTimelinePlaybackSpeed(1f);
stateManager.SelectedState = nextSelectedScene_;
}
else
{
UpdateRestTime(FourBarTrigger - currentTick_ % FourBarTrigger);
UpdateTimelinePlaybackSpeed(-1f);
stateManager.SelectedState = nextSelectedScene_ + 2;
}
case (byte) CYL_Command.FourBarLoopButton:
if (nextSelectedScene > currentSelectedScene)
{
RestTime(fourBarTrigger - currentTick % fourBarTrigger);
TimelinePlaybackSpeed((int) Timeline_Command.Forwards);
stateManager.SelectedState = nextSelectedScene;
}
else
{
RestTime(fourBarTrigger - currentTick % fourBarTrigger);
TimelinePlaybackSpeed((int)Timeline_Command.Backwards);
stateManager.SelectedState = nextSelectedScene + 2;
}
break;
case (byte) CYL_Command.OneBarLoopButton:
RestTime(oneBarTrigger - currentTick % oneBarTrigger);
TimelinePlaybackSpeed((int) Timeline_Command.Forwards);
stateManager.SelectedState = nextSelectedScene;
break; break;
}
case CYLCommand.OneBarLoopButton:
{
UpdateRestTime(OneBarTrigger - currentTick_ % OneBarTrigger);
UpdateTimelinePlaybackSpeed(1f);
stateManager.SelectedState = nextSelectedScene_;
break;
}
default:
throw new Exception("Unexpected CYL command");
} }
} }
private void UpdateRestTime(int restTicks)
{
restTime_ = restTicks / 24.0f / stateManager.CurrentState.Bpm * 60f;
} }
public void RestTime(int restTick) private void UpdateTimelinePlaybackSpeed(float speed)
{ {
restTimeS = restTick / 24.0f / stateManager.CurrentState.Bpm * 60; var timelinePlaybackSpeed = TransitionLength / Mathf.Clamp(restTime_, 0.001f, TransitionLength);
} playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(timelinePlaybackSpeed * speed); //set playbackspeed of Timeline
public void TimelinePlaybackSpeed(int direction)
{
float timelinePlaybackSpeed = transitionLength / Mathf.Clamp(restTimeS, 0.001f, transitionLength);
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(timelinePlaybackSpeed * direction); //set playbackspeed of Timeline
} }
} }
} }