From 1620f75fe5917f4eb4773afa7c4635783fd24b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sun, 16 Aug 2015 23:02:11 +0300 Subject: [PATCH] Scripting|libcore: Scheduler's Clock can override the default context Each Clock running the scheduled scripts may use its own context for the processes. --- .../libcore/include/de/scriptsys/scheduler.h | 2 +- .../sdk/libcore/src/scriptsys/scheduler.cpp | 79 ++++++++++--------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/doomsday/sdk/libcore/include/de/scriptsys/scheduler.h b/doomsday/sdk/libcore/include/de/scriptsys/scheduler.h index cffe63e565..1a19fff9e8 100644 --- a/doomsday/sdk/libcore/include/de/scriptsys/scheduler.h +++ b/doomsday/sdk/libcore/include/de/scriptsys/scheduler.h @@ -68,7 +68,7 @@ class DENG2_PUBLIC Scheduler class DENG2_PUBLIC Clock { public: - Clock(Scheduler const &schedule); + Clock(Scheduler const &schedule, Record *context = nullptr); /** * Returns the current time of the clock. diff --git a/doomsday/sdk/libcore/src/scriptsys/scheduler.cpp b/doomsday/sdk/libcore/src/scriptsys/scheduler.cpp index 0d05979680..2307f3137c 100644 --- a/doomsday/sdk/libcore/src/scriptsys/scheduler.cpp +++ b/doomsday/sdk/libcore/src/scriptsys/scheduler.cpp @@ -84,42 +84,6 @@ DENG2_PIMPL(Scheduler) } }; -DENG2_PIMPL_NOREF(Scheduler::Clock) -{ - typedef Scheduler::Instance::Event Event; - typedef Scheduler::Instance::Events Events; // Events not owned - - Scheduler const *scheduler = nullptr; - TimeDelta at = 0.0; - Events events; - - void rewind() - { - at = 0.0; - - // Restore all events in the queue. - events = scheduler->d->events; // copied - } - - void advanceTime(TimeDelta const &elapsed) - { - at += elapsed; - - while(!events.empty()) - { - Event const *ev = events.top(); - if(ev->at > at) break; - - events.pop(); - - // Execute the script in the specified context. - Process process(scheduler->d->context); - process.run(ev->script); - process.execute(); - } - } -}; - Scheduler::Scheduler() : d(new Instance(this)) {} @@ -147,7 +111,6 @@ void Scheduler::addFromInfo(Record const &timelineRecord) for(String key : ScriptedInfo::sortRecordsBySource(scripts)) { auto const &def = *scripts[key]; - qDebug() << def.asText(); try { addScript(def.getd("at", 0.0), @@ -163,10 +126,50 @@ void Scheduler::addFromInfo(Record const &timelineRecord) } } -Scheduler::Clock::Clock(Scheduler const &schedule) +//---------------------------------------------------------------------------- + +DENG2_PIMPL_NOREF(Scheduler::Clock) +{ + typedef Scheduler::Instance::Event Event; + typedef Scheduler::Instance::Events Events; // Events not owned + + Record *context = nullptr; + Scheduler const *scheduler = nullptr; + TimeDelta at = 0.0; + Events events; + + void rewind() + { + at = 0.0; + + // Restore all events in the queue. + events = scheduler->d->events; // copied + } + + void advanceTime(TimeDelta const &elapsed) + { + at += elapsed; + + while(!events.empty()) + { + Event const *ev = events.top(); + if(ev->at > at) break; + + events.pop(); + + // Execute the script in the specified context. + Process process(context? context : scheduler->d->context); + process.run(ev->script); + process.execute(); + } + } +}; + +Scheduler::Clock::Clock(Scheduler const &schedule, Record *context) : d(new Instance) { d->scheduler = &schedule; + d->context = context; d->rewind(); }