From 2183b74ee7dd87cc59545181772788388084cf67 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 6 Oct 2019 00:00:00 +0000 Subject: [PATCH] [add] Added ScheduledControllerInterface. --- .../sjplayer/ScheduledControllerInterface.d | 62 +++++++++++++++++++ sjplayer/src/sjplayer/util/compare.d | 14 +++++ 2 files changed, 76 insertions(+) create mode 100644 sjplayer/src/sjplayer/ScheduledControllerInterface.d create mode 100644 sjplayer/src/sjplayer/util/compare.d diff --git a/sjplayer/src/sjplayer/ScheduledControllerInterface.d b/sjplayer/src/sjplayer/ScheduledControllerInterface.d new file mode 100644 index 0000000..404238d --- /dev/null +++ b/sjplayer/src/sjplayer/ScheduledControllerInterface.d @@ -0,0 +1,62 @@ +/// License: MIT +module sjplayer.ScheduledControllerInterface; + +import std.algorithm, + std.array; + +import sjscript; + +import sjplayer.util.compare; + +/// +interface ScheduledControllerInterface { + public: + /// + void Operate(float time); +} + +/// +abstract class AbstractScheduledController : ScheduledControllerInterface { + public: + /// The operations must be sorted. + this(in ParametersBlock[] operations) { + operations_ = operations; + } + + override void Operate(float time) { + scope(exit) last_operation_time_ = time; + + if (next_operation_index_ >= 1) { + assert(next_operation_index_ <= operations_.length); + + const last_operation = &operations_[next_operation_index_-1]; + if (IsTimeInPeriod(time, last_operation.period)) { + ProcessOperation(*last_operation); + return; + } + FinishOperation(*last_operation); + } + + if (next_operation_index_ >= operations_.length) return; + + const next_operation = &operations_[next_operation_index_]; + if (IsTimeInPeriod(time, next_operation.period)) { + PrepareOperation(*next_operation); + ++next_operation_index_; + } + } + + protected: + abstract void PrepareOperation(ref in ParametersBlock params); + + abstract void ProcessOperation(ref in ParametersBlock params); + + abstract void FinishOperation(ref in ParametersBlock params); + + private: + const ParametersBlock[] operations_; + + float last_operation_time_ = -1; + + size_t next_operation_index_; +} diff --git a/sjplayer/src/sjplayer/util/compare.d b/sjplayer/src/sjplayer/util/compare.d new file mode 100644 index 0000000..f1616d0 --- /dev/null +++ b/sjplayer/src/sjplayer/util/compare.d @@ -0,0 +1,14 @@ +/// License: MIT +module sjplayer.util.compare; + +import sjscript; + +/// +bool IsTimeInPeriod(float time, in Period period) { + return period.start <= time && time < period.end; +} + +/// +bool IsPeriodIntersectedToPeriod(in Period p1, in Period p2) { + return p1.start < p2.end && p2.start < p1.end; +}