Permalink
Browse files

UI: Persist tween data on view recreate.

  • Loading branch information...
unknownbrackets committed Dec 9, 2017
1 parent 6e3cb0c commit cf6ccfca9977da42fc0f3f3b8d50f2c20e8099c5
Showing with 50 additions and 0 deletions.
  1. +40 −0 ext/native/ui/ui_tween.cpp
  2. +5 −0 ext/native/ui/ui_tween.h
  3. +5 −0 ext/native/ui/view.cpp
@@ -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);
}
@@ -23,6 +23,8 @@ class Tween {
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 @@ class Tween {
};
// 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 @@ class TweenBase: public Tween {
return Current(Position());
}
void PersistData(PersistStatus status, std::string anonId, PersistMap &storage) override;
protected:
virtual Value Current(float pos) = 0;
@@ -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) {

0 comments on commit cf6ccfc

Please sign in to comment.