rj-action-library/Runtime/Time/TimeLineScheduler.cs

138 lines
3.8 KiB
C#

using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System;
using Godot;
namespace Rokojori
{
[GlobalClass]
public partial class TimeLineScheduler:Node
{
public static int ScheduleEventIn( RJTimeLine timeLine, double offset, Action<int> action, bool persistent = false )
{
var scheduler = Unique<TimeLineScheduler>.Get();
return scheduler._ScheduleEventIn( timeLine, offset, action, persistent );
}
public static int ScheduleEventAt( RJTimeLine timeLine, double position, Action<int> action, bool persistent = false )
{
var scheduler = Unique<TimeLineScheduler>.Get();
return scheduler._ScheduleEventAt( timeLine, position, action, persistent );
}
int _ScheduleEventAt( RJTimeLine timeLine, double position, Action<int> action, bool persistent = false )
{
AttachListeners();
var tm = Unique<TimeLineManager>.Get();
var tIndex = tm.GetTimeLineIndex( timeLine );
var id = tm.CreateID();
_eventActions[ id ] = action;
tm.ScheduleEvent( tIndex, position, id, persistent );
return id;
}
int _ScheduleEventIn( RJTimeLine timeLine, double offset, Action<int> action, bool persistent = false )
{
var tm = Unique<TimeLineManager>.Get();
var tIndex = tm.GetTimeLineIndex( timeLine );
var position = tm.GetPosition( tIndex ) + offset;
return _ScheduleEventAt( timeLine, position, action, persistent );
}
public static int ScheduleSpanAt( RJTimeLine timeLine, double start, double end, Action<int,int> action, bool persistent = false )
{
var scheduler = Unique<TimeLineScheduler>.Get();
return scheduler._ScheduleSpan( timeLine, start, end, action, persistent );
}
public static int ScheduleSpanIn( RJTimeLine timeLine, double offset, double duration, Action<int,int> action, bool persistent = false )
{
var scheduler = Unique<TimeLineScheduler>.Get();
var tm = Unique<TimeLineManager>.Get();
var tIndex = tm.GetTimeLineIndex( timeLine );
var position = tm.GetPosition( tIndex );
var start = position + offset;
var end = start + duration;
return scheduler._ScheduleSpan( timeLine, start, end, action, persistent );
}
int _ScheduleSpan( RJTimeLine timeLine, double start, double end, Action<int,int> action, bool persistent = false )
{
AttachListeners();
var tm = Unique<TimeLineManager>.Get();
var tIndex = tm.GetTimeLineIndex( timeLine );
var id = tm.CreateID();
_spanActions[ id ] = action;
tm.ScheduleSpan( tIndex, start, end, id, persistent );
return id;
}
bool _listenersAttached = false;
HashSet<int> _persistentEventsAndSpans = new HashSet<int>();
Dictionary<int,Action<int>> _eventActions = new Dictionary<int, Action<int>>();
Dictionary<int,Action<int,int>> _spanActions = new Dictionary<int, Action<int,int>>();
void AttachListeners()
{
if ( _listenersAttached )
{
return;
}
_listenersAttached = true;
var tm = Unique<TimeLineManager>.Get();
tm.OnEvent += ( id ) => OnEvent( id );
tm.OnSpan += ( id, type ) => onSpan( id, type );
}
void OnEvent( long s_id )
{
var id = (int) s_id;
if ( ! _eventActions.ContainsKey( id ) )
{
return;
}
_eventActions[ id ]( id );
if ( _persistentEventsAndSpans.Contains( id ) )
{
return;
}
_eventActions.Remove( id );
}
void onSpan( long s_id, long s_type )
{
var id = (int) s_id;
var type = (int) s_type;
if ( ! _spanActions.ContainsKey( id ) )
{
return;
}
_spanActions[ id ]( id, type );
if ( _persistentEventsAndSpans.Contains( id ) )
{
return;
}
_spanActions.Remove( id );
}
}
}