Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
416 changes: 415 additions & 1 deletion RetroFE/Source/Graphics/Animate/Tween.cpp

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion RetroFE/Source/Graphics/Animate/Tween.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
#include <string>
#include <unordered_map>
#include <optional>
#include <vector>
#include <cstddef>
#include <cstdint>

class ViewInfo;

class Tween {
public:
using EasingKernel = float (*)(float, float, float);

Tween(TweenProperty property, TweenAlgorithm type, float start, float end, float duration, const std::string& playlistFilter = "");

Expand All @@ -36,11 +40,24 @@ class Tween {

static TweenAlgorithm getTweenType(const std::string& name);
static std::optional<TweenProperty> getTweenProperty(const std::string& name);
bool matchesPlaylist(const std::string& currentPlaylist) const;
static bool matchesPlaylistTokens(const std::vector<std::string>& tokens, const std::string& currentPlaylist);
static uint32_t playlistTokenId(const std::string& token);
static bool matchesPlaylistTokenIds(const std::vector<uint32_t>& tokenIds, uint32_t currentPlaylistId, bool hasCurrentPlaylist);
TweenAlgorithm algorithm() const { return type; }
float startValue() const { return start; }
float endValue() const { return end; }
static EasingKernel getKernel(TweenAlgorithm type);
static void evaluateBatch(TweenAlgorithm type, const float* progress, const float* start, const float* change, float* out, size_t count);

TweenProperty property;
float duration;
bool startDefined{ true };
std::string playlistFilter;
const std::vector<std::string>& playlistTokens() const { return playlistFilterTokens; }
const std::vector<uint32_t>& playlistTokenIds() const { return playlistFilterTokenIds; }
std::vector<std::string> playlistFilterTokens;
std::vector<uint32_t> playlistFilterTokenIds;

private:
// Easing functions use a normalized progress value for calculation.
Expand Down Expand Up @@ -70,8 +87,10 @@ class Tween {

static std::unordered_map<std::string, TweenAlgorithm> tweenTypeMap_;
static std::unordered_map<std::string, TweenProperty> tweenPropertyMap_;
static std::unordered_map<std::string, uint32_t> playlistTokenIdMap_;
static uint32_t nextPlaylistTokenId_;

TweenAlgorithm type;
float start;
float end;
};
};
66 changes: 47 additions & 19 deletions RetroFE/Source/Graphics/Animate/TweenSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,75 @@

TweenSet::TweenSet() = default;

TweenSet::TweenSet(const TweenSet& copy) {
set_.reserve(copy.set_.size());
for (const auto& tween : copy.set_) {
set_.push_back(std::make_unique<Tween>(*tween));
}
TweenSet::CompiledTweenEntry TweenSet::compileTween(const Tween& tween) {
const float duration = tween.duration;
const float deltaValue = tween.endValue() - tween.startValue();
return CompiledTweenEntry {
tween.property,
tween.algorithm(),
duration,
duration > 0.0f ? (1.0f / duration) : 0.0f,
tween.startDefined,
tween.startValue(),
tween.endValue(),
deltaValue,
tween.playlistTokenIds()
};
}

TweenSet::TweenSet(const TweenSet& copy)
: compiledSet_(copy.compiledSet_),
compiledByAlgorithm_(copy.compiledByAlgorithm_) {
}

TweenSet& TweenSet::operator=(const TweenSet& other) {
if (this != &other) {
set_.clear(); // Clear existing tweens
set_.reserve(other.set_.size());
for (const auto& tween : other.set_) {
set_.push_back(std::make_unique<Tween>(*tween));
}
compiledSet_ = other.compiledSet_;
compiledByAlgorithm_ = other.compiledByAlgorithm_;
}
return *this;
}


TweenSet::~TweenSet()
{
clear();
}

void TweenSet::push(std::unique_ptr<Tween> tween) {
set_.push_back(std::move(tween));
if (tween) {
pushCompiled(compileTween(*tween));
}
}

void TweenSet::clear() {
set_.clear();

void TweenSet::pushCompiled(CompiledTweenEntry tween) {
tween.deltaValue = tween.endValue - tween.startValue;
tween.invDuration = tween.duration > 0.0f ? (1.0f / tween.duration) : 0.0f;

const size_t algorithmIndex = static_cast<size_t>(tween.algorithm);
if (algorithmIndex < kTweenAlgorithmCount) {
compiledByAlgorithm_[algorithmIndex].push_back(compiledSet_.size());
}

compiledSet_.push_back(std::move(tween));
}

Tween* TweenSet::getTween(unsigned int index) const {
if (index < set_.size()) {
return set_[index].get();
void TweenSet::clear() {
compiledSet_.clear();
for (auto& bucket : compiledByAlgorithm_) {
bucket.clear();
}
return nullptr;
}

const std::vector<TweenSet::CompiledTweenEntry>& TweenSet::compiledTweens() const {
return compiledSet_;
}

const std::array<std::vector<size_t>, TweenSet::kTweenAlgorithmCount>& TweenSet::compiledTweensByAlgorithm() const {
return compiledByAlgorithm_;
}

size_t TweenSet::size() const
{
return set_.size();
return compiledSet_.size();
}
25 changes: 23 additions & 2 deletions RetroFE/Source/Graphics/Animate/TweenSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,41 @@
#include "Tween.h"
#include <vector>
#include <memory>
#include <cstdint>
#include <array>

class TweenSet
{
public:
static constexpr size_t kTweenAlgorithmCount = static_cast<size_t>(EASE_INOUT_CIRCULAR) + 1;

struct CompiledTweenEntry {
TweenProperty property;
TweenAlgorithm algorithm;
float duration;
float invDuration;
bool startDefined;
float startValue;
float endValue;
float deltaValue;
std::vector<uint32_t> playlistTokenIds;
};

TweenSet();
TweenSet(const TweenSet& copy);
TweenSet& operator=(const TweenSet& other);
~TweenSet();
void push(std::unique_ptr<Tween> tween);
void pushCompiled(CompiledTweenEntry tween);
void clear();
Tween* getTween(unsigned int index) const;
const std::vector<CompiledTweenEntry>& compiledTweens() const;
const std::array<std::vector<size_t>, kTweenAlgorithmCount>& compiledTweensByAlgorithm() const;

size_t size() const;

private:
std::vector<std::unique_ptr<Tween>> set_;
static CompiledTweenEntry compileTween(const Tween& tween);

std::vector<CompiledTweenEntry> compiledSet_;
std::array<std::vector<size_t>, kTweenAlgorithmCount> compiledByAlgorithm_;
};
Loading