rokojori_action_library/Runtime/Time/TImeLineManager_Scheduling.cs

189 lines
5.5 KiB
C#
Raw Normal View History

2025-01-19 20:35:51 +00:00
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System;
using Godot;
2025-10-24 11:38:51 +00:00
using System.Threading.Tasks;
2025-01-19 20:35:51 +00:00
2026-05-22 12:25:02 +00:00
using Rokojori.Extensions;
2025-01-19 20:35:51 +00:00
namespace Rokojori
{
2026-05-22 12:25:02 +00:00
public partial class TimelineManager:NetworkNode
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
public static void RemoveEvent( Timeline timeline, int id )
2025-01-19 20:35:51 +00:00
{
2025-07-25 08:13:35 +00:00
RJLog.Log( "Removing tick:", timeline, id );
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-01-19 20:35:51 +00:00
var runner = timeline.runner;
runner.RemoveEvent( id );
}
2026-05-22 12:25:02 +00:00
public static TimelineCallback ScheduleCallback( Timeline timeline, System.Action<TimelineCallback> callback )
2025-01-21 20:58:56 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-01-21 20:58:56 +00:00
var runner = timeline.runner;
2026-05-22 12:25:02 +00:00
var eventID = TimelineManager.CreateID();
2025-01-21 20:58:56 +00:00
return runner._ScheduleCallback( callback, eventID );
}
2026-05-22 12:25:02 +00:00
static async Task RunLater( float delay, TimelineEvent te, Action<TimelineEvent> callback, Node context = null )
2025-10-24 11:38:51 +00:00
{
await context.RequestNextFrame();
2026-05-22 12:25:02 +00:00
await context.WaitFor( delay );
2025-10-24 11:38:51 +00:00
callback( te );
}
2026-05-22 12:25:02 +00:00
public static TimelineEvent ScheduleEvent( Timeline timeline, float position, Action<TimelineEvent> callback, Node context = null )
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-10-24 11:38:51 +00:00
if ( Engine.IsEditorHint() )
{
2026-05-22 12:25:02 +00:00
var te = new TimelineEvent();
2025-10-24 11:38:51 +00:00
var time = timeline.position;
RunLater( position - time, te, callback, context );
return te;
}
2025-01-19 20:35:51 +00:00
var runner = timeline.runner;
2026-05-22 12:25:02 +00:00
var eventID = TimelineManager.CreateID();
2025-01-19 20:35:51 +00:00
return runner._ScheduleEvent( position, eventID, false, callback );
}
2026-05-22 12:25:02 +00:00
public static TimelineEvent ScheduleEventIn( Timeline timeline, float offset, Action<TimelineEvent> callback, Node context = null)
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-01-19 20:35:51 +00:00
var position = timeline.position + offset;
2025-10-24 11:38:51 +00:00
return ScheduleEvent( timeline, position, callback, context );
2025-01-19 20:35:51 +00:00
}
2026-05-22 12:25:02 +00:00
public static TimelineEvent ScheduleEventWith( Duration duration, Action<TimelineEvent> callback, Node context = null )
2025-07-25 08:13:35 +00:00
{
2025-10-24 11:38:51 +00:00
return ScheduleEventIn( duration.timeLine, duration.GetDurationInSeconds(), callback, context );
2025-07-25 08:13:35 +00:00
}
2026-05-22 12:25:02 +00:00
public static TimelineEvent ScheduleLoopEvent( Timeline timeline, float duration, float offset, Action<TimelineEvent> callback )
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-01-19 20:35:51 +00:00
var runner = timeline.runner;
2026-05-22 12:25:02 +00:00
var eventID = TimelineManager.CreateID();
2025-01-19 20:35:51 +00:00
return runner._ScheduleLoopEvent( duration, offset, eventID, false, callback );
}
2026-05-22 12:25:02 +00:00
public static TimelineSpan ScheduleSpan( Timeline timeline, float start, float end, Action<TimelineSpan,TimelineSpanUpdateType> callback, Node context = null )
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-10-24 11:38:51 +00:00
var delay = start - timeline.position;
var duration = end - start;
return ScheduleSpanIn( timeline, delay, duration, callback, context );
/*if ( Engine.IsEditorHint() )
{
var span = new TimeLineSpan();
span.id = GodotRandom.Get().IntegerInclusive( 0, 1000000 );
// var root = Root.Window();
// RJLog.Log( root, RokojoriPlugin.Get() );
RunInEditor( span, end - start, callback, context );
return span;
}
2025-01-19 20:35:51 +00:00
timeline = TimeLineManager.Ensure( timeline );
var runner = timeline.runner;
var spanID = TimeLineManager.CreateID();
var duration = end - start;
2025-10-24 11:38:51 +00:00
return runner._ScheduleSpan( start, end, spanID, false, callback );*/
2025-01-19 20:35:51 +00:00
}
2026-05-22 12:25:02 +00:00
public static TimelineSpan ScheduleSpanWith( Duration duration, Action<TimelineSpan,TimelineSpanUpdateType> callback, Node context = null )
2025-07-25 08:13:35 +00:00
{
2025-12-10 14:17:07 +00:00
return ScheduleSpanIn( duration.timeLine, 0, duration.GetDurationInSeconds(), callback, context );
2025-07-25 08:13:35 +00:00
}
2026-05-22 12:25:02 +00:00
static async Task RunSpanInEditor( TimelineSpan span, float duration, Action<TimelineSpan,TimelineSpanUpdateType> callback, Node context = null )
2025-10-24 11:38:51 +00:00
{
if ( context == null )
{
return;
}
for ( int i = 0; i < 3; i++ )
{
await context.RequestNextFrame();
}
var elapsed = 0f;
var isFirst = true;
2026-05-22 12:25:02 +00:00
var startTime = Godot.Time.GetTicksMsec() / 1000.0f;
2025-10-24 11:38:51 +00:00
span.start = startTime;
span.end = startTime + duration;
span.wasInside = true;
2026-05-22 12:25:02 +00:00
span.timeLine = new Timeline();
2025-10-24 11:38:51 +00:00
while ( elapsed < duration )
{
2026-05-22 12:25:02 +00:00
var type = isFirst ? TimelineSpanUpdateType.Start : TimelineSpanUpdateType.InSpan;
2025-10-24 11:38:51 +00:00
isFirst = false;
callback( span, type );
await context.RequestNextFrame();
2026-05-22 12:25:02 +00:00
elapsed = (Godot.Time.GetTicksMsec() / 1000.0f - startTime );
2025-10-24 11:38:51 +00:00
}
2026-05-22 12:25:02 +00:00
callback( span, TimelineSpanUpdateType.End );
2025-10-24 11:38:51 +00:00
}
2026-05-22 12:25:02 +00:00
public static TimelineSpan ScheduleSpanIn( Timeline timeline, float delay, float duration, Action<TimelineSpan,TimelineSpanUpdateType> callback, Node context = null )
2025-01-19 20:35:51 +00:00
{
2026-05-22 12:25:02 +00:00
timeline = TimelineManager.Ensure( timeline );
2025-10-24 11:38:51 +00:00
if ( Engine.IsEditorHint() )
{
2026-05-22 12:25:02 +00:00
var span = new TimelineSpan();
span.id = GodotRandom.Get().IntegerInclusiveInRange( 0, 1000000 );
2025-10-24 11:38:51 +00:00
// var root = Root.Window();
// RJLog.Log( root, RokojoriPlugin.Get() );
RunSpanInEditor( span, duration, callback, context );
return span;
}
2025-01-19 20:35:51 +00:00
var runner = timeline.runner;
2025-06-19 17:22:25 +00:00
if ( runner == null )
{
return null;
}
2025-01-19 20:35:51 +00:00
var start = timeline.position + delay;
var end = start + duration;
2026-05-22 12:25:02 +00:00
var spanID = TimelineManager.CreateID();
2025-01-19 20:35:51 +00:00
return runner._ScheduleSpan( start, end, spanID, false, callback );
}
}
}