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 action, bool persistent = false ) { var scheduler = Unique.Get(); return scheduler._ScheduleEventIn( timeLine, offset, action, persistent ); } public static int ScheduleEventAt( RJTimeLine timeLine, double position, Action action, bool persistent = false ) { var scheduler = Unique.Get(); return scheduler._ScheduleEventAt( timeLine, position, action, persistent ); } int _ScheduleEventAt( RJTimeLine timeLine, double position, Action action, bool persistent = false ) { AttachListeners(); var tm = Unique.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 action, bool persistent = false ) { var tm = Unique.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 action, bool persistent = false ) { var scheduler = Unique.Get(); return scheduler._ScheduleSpan( timeLine, start, end, action, persistent ); } public static int ScheduleSpanIn( RJTimeLine timeLine, double offset, double duration, Action action, bool persistent = false ) { var scheduler = Unique.Get(); var tm = Unique.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 action, bool persistent = false ) { AttachListeners(); var tm = Unique.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 _persistentEventsAndSpans = new HashSet(); Dictionary> _eventActions = new Dictionary>(); Dictionary> _spanActions = new Dictionary>(); void AttachListeners() { if ( _listenersAttached ) { return; } _listenersAttached = true; var tm = Unique.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 ); } } }