UI: Persist tween data on view recreate.

This commit is contained in:
Unknown W. Brackets 2017-12-09 14:10:42 -08:00
parent 6e3cb0cd48
commit cf6ccfca99
3 changed files with 50 additions and 0 deletions

View File

@ -12,6 +12,46 @@ void Tween::Apply(View *view) {
DoApply(view, pos);
}
template <typename Value>
void TweenBase<Value>::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
struct TweenData {
float start;
float duration;
Value from;
Value to;
bool finishApplied;
};
PersistBuffer &buffer = storage["TweenBase::" + anonId];
switch (status) {
case UI::PERSIST_SAVE:
buffer.resize(sizeof(TweenData) / sizeof(int));
{
TweenData &data = *(TweenData *)&buffer[0];
data.start = start_;
data.duration = duration_;
data.from = from_;
data.to = to_;
data.finishApplied = finishApplied_;
}
break;
case UI::PERSIST_RESTORE:
if (buffer.size() >= sizeof(TweenData) / sizeof(int)) {
TweenData data = *(TweenData *)&buffer[0];
start_ = data.start;
duration_ = data.duration;
from_ = data.from;
to_ = data.to;
finishApplied_ = data.finishApplied;
}
break;
}
}
template void TweenBase<uint32_t>::PersistData(PersistStatus status, std::string anonId, PersistMap &storage);
template void TweenBase<Visibility>::PersistData(PersistStatus status, std::string anonId, PersistMap &storage);
uint32_t ColorTween::Current(float pos) {
return colorBlend(to_, from_, pos);
}

View File

@ -23,6 +23,8 @@ public:
return finishApplied_ && time_now() >= start_ + duration_;
}
virtual void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) = 0;
protected:
float DurationOffset() {
return time_now() - start_;
@ -41,6 +43,7 @@ protected:
};
// This is the class all tweens inherit from. Shouldn't be used directly, see below.
// Note: Value cannot safely be a pointer (without overriding PersistData.)
template <typename Value>
class TweenBase: public Tween {
public:
@ -102,6 +105,8 @@ public:
return Current(Position());
}
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
protected:
virtual Value Current(float pos) = 0;

View File

@ -224,6 +224,11 @@ void View::PersistData(PersistStatus status, std::string anonId, PersistMap &sto
}
break;
}
ITOA stringify;
for (int i = 0; i < (int)tweens_.size(); ++i) {
tweens_[i]->PersistData(status, tag + "/" + stringify.p((int)i), storage);
}
}
Point View::GetFocusPosition(FocusDirection dir) {