#pragma once /* Copyright (c) 2015 S.Percentage Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ // InterpolateFunc // using InterpolateFuncT = std::function; InterpolateFuncT Inv(InterpolateFuncT origin) { return std::bind(EaseOut, origin, std::placeholders::_1); } InterpolateFuncT Bipolar(InterpolateFuncT origin) { return std::bind(EaseInOut, origin, std::placeholders::_1); } template double Decimate(double v) { return uint32_t(v * res) / static_cast(res); } // Math::Lerp overloading(for parameter tuple (float, float, double)) namespace s3d { namespace Math { inline float Lerp(float a, float b, double p) { return Lerp(a, b, static_cast(p)); } } } class AnimationManager; class IAnimatedVariable { friend class AnimationManager; virtual void ResetIterator() = 0; protected: struct CtorAccess{}; }; template class CAnimatedVariable : public IAnimatedVariable { friend class AnimationManager; struct Section { double time; ValueT value; InterpolateFuncT interpF; Section(double t, const ValueT& v, InterpolateFuncT f) : time(t), value(v), interpF(f) {} }; using SectionListT = typename std::list
; ValueT initValue; SectionListT sectionList; bool loop = false; size_t loopSkips = 0; double finishTime = 0.0, accumOffset = 0.0; typename SectionListT::const_iterator iterCurrent; void ResetIterator() override { this->accumOffset = 0.0; this->iterCurrent = std::begin(this->sectionList); } const AnimationManager* pManager; public: CAnimatedVariable(CtorAccess&&, const AnimationManager* pm) : iterCurrent(std::begin(this->sectionList)), pManager(pm) {} CAnimatedVariable& SetInit(const ValueT& value) { this->initValue = value; return *this; } CAnimatedVariable& AddSection(double time, const ValueT& value, InterpolateFuncT interpF) { this->sectionList.emplace_back(time, value, interpF); this->finishTime += time; this->ResetIterator(); return *this; } CAnimatedVariable& AddEmptySection(double time) { if (this->sectionList.empty()) { this->sectionList.emplace_back(time, this->initValue, Easing::Linear); } else { this->sectionList.emplace_back(time, this->sectionList.back().value, Easing::Linear); } this->finishTime += time; this->ResetIterator(); return *this; } CAnimatedVariable& EnableLoop(size_t loopSkips = 0) { this->loop = true; this->loopSkips = loopSkips; return *this; } CAnimatedVariable& DisableLoop() { this->loop = false; return *this; } bool IsSequenceFinished() const { return this->iterCurrent == std::end(this->sectionList); } double GetFinishTime() const { return this->finishTime; } ValueT operator()() { if (this->sectionList.empty()) return this->initValue; if (this->IsSequenceFinished()) return this->sectionList.back().value; // skip elapsed keyframes auto tms = this->pManager->GetCurrentTime(); while (!this->IsSequenceFinished() && this->iterCurrent->time < tms - this->accumOffset) { this->accumOffset += this->iterCurrent->time; this->iterCurrent++; if (this->loop && this->IsSequenceFinished()) { this->iterCurrent = std::begin(this->sectionList); for (size_t i = 0; i < this->loopSkips; i++) { this->iterCurrent++; if (this->IsSequenceFinished()) break; } } } if (this->IsSequenceFinished()) return this->sectionList.back().value; // calc(lerping) value(s) by section's interpF const auto tmsOffset = tms - this->accumOffset; auto prevValue = this->initValue; if (this->iterCurrent != std::begin(this->sectionList)) { this->iterCurrent--; prevValue = this->iterCurrent->value; this->iterCurrent++; } return Math::Lerp(prevValue, this->iterCurrent->value, this->iterCurrent->interpF(tmsOffset / this->iterCurrent->time)); } }; template struct AnimatedVariable { ValueT value() { if (auto p = this->ref.lock()) return (*p.get())(); else return ValueT(); } // CAnimatedVariable Exports(Settings) AnimatedVariable& SetInit(const ValueT& value) { if (auto p = this->ref.lock()) p->SetInit(value); return *this; } AnimatedVariable& AddSection(double time, const ValueT& value, InterpolateFuncT interpF) { if (auto p = this->ref.lock()) p->AddSection(time, value, interpF); return *this; } AnimatedVariable& AddEmptySection(double time) { if (auto p = this->ref.lock()) p->AddEmptySection(time); return *this; } AnimatedVariable& EnableLoop(size_t loopSkips = 0) { if (auto p = this->ref.lock()) p->EnableLoop(loopSkips); return *this; } AnimatedVariable& DisableLoop() { if (auto p = this->ref.lock()) p->DisableLoop(); return *this; } bool IsSequenceFinished() const { if (auto p = this->ref.lock()) return p->IsSequenceFinished(); return false; } double GetFinishTime() const { if (auto p = this->ref.lock()) return p->GetFinishTime(); return 0.0; } ValueT operator()() { if (auto p = this->ref.lock()) return (*p.get())(); return ValueT(); } private: friend class AnimationManager; friend struct s3d::GetAlign>; AnimatedVariable() = default; AnimatedVariable(const std::shared_ptr>& refOrigin) : ref(refOrigin) {} std::weak_ptr> ref; }; class AnimationManager { TimerMillisec timer; std::unordered_map> bindedVariables; public: template AnimatedVariable Var(const String& str) { auto iter = this->bindedVariables.find(str); if (iter != std::end(this->bindedVariables)) { return AnimatedVariable(std::dynamic_pointer_cast>(iter->second)); } this->bindedVariables[str] = std::make_shared>(IAnimatedVariable::CtorAccess{}, this); return AnimatedVariable(std::dynamic_pointer_cast>(this->bindedVariables.at(str))); } template ValueT Value(const String& str) { auto iter = this->bindedVariables.find(str); if (iter != std::end(this->bindedVariables)) { return (*dynamic_cast*>(iter->second.get()))(); } return ValueT(); } void ResetIterators() { for (auto& e : this->bindedVariables) e.second->ResetIterator(); } void Start() { this->timer.start(); } void Pause() { this->timer.pause(); } void Reset() { this->ResetIterators(); this->timer.reset(); } void Restart() { this->ResetIterators(); this->timer.restart(); } double GetCurrentTime() const { return static_cast(this->timer.elapsed()); } };