2017-12-03 17:39:50 +00:00
|
|
|
|
#include "base/colorutil.h"
|
|
|
|
|
#include "ui/ui_tween.h"
|
|
|
|
|
#include "ui/view.h"
|
|
|
|
|
|
|
|
|
|
namespace UI {
|
|
|
|
|
|
2017-12-03 18:28:53 +00:00
|
|
|
|
void Tween::Apply(View *view) {
|
2017-12-10 08:05:10 +00:00
|
|
|
|
if (!valid_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (DurationOffset() >= duration_)
|
2017-12-03 18:28:53 +00:00
|
|
|
|
finishApplied_ = true;
|
|
|
|
|
|
|
|
|
|
float pos = Position();
|
|
|
|
|
DoApply(view, pos);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-09 22:10:42 +00:00
|
|
|
|
template <typename Value>
|
|
|
|
|
void TweenBase<Value>::PersistData(PersistStatus status, std::string anonId, PersistMap &storage) {
|
|
|
|
|
struct TweenData {
|
|
|
|
|
float start;
|
|
|
|
|
float duration;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
float delay;
|
2017-12-09 22:10:42 +00:00
|
|
|
|
Value from;
|
|
|
|
|
Value to;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
bool valid;
|
2017-12-09 22:10:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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_;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
data.delay = delay_;
|
2017-12-09 22:10:42 +00:00
|
|
|
|
data.from = from_;
|
|
|
|
|
data.to = to_;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
data.valid = valid_;
|
2017-12-09 22:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case UI::PERSIST_RESTORE:
|
|
|
|
|
if (buffer.size() >= sizeof(TweenData) / sizeof(int)) {
|
|
|
|
|
TweenData data = *(TweenData *)&buffer[0];
|
|
|
|
|
start_ = data.start;
|
|
|
|
|
duration_ = data.duration;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
delay_ = data.delay;
|
2017-12-09 22:10:42 +00:00
|
|
|
|
from_ = data.from;
|
|
|
|
|
to_ = data.to;
|
2017-12-10 08:05:10 +00:00
|
|
|
|
valid_ = data.valid;
|
2017-12-10 02:18:23 +00:00
|
|
|
|
// We skip finishApplied_ here so that the tween will reapply.
|
|
|
|
|
// This does mean it's important to remember to update tweens even after finish.
|
2017-12-09 22:10:42 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
2017-12-03 18:28:53 +00:00
|
|
|
|
uint32_t ColorTween::Current(float pos) {
|
|
|
|
|
return colorBlend(to_, from_, pos);
|
2017-12-03 17:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 18:28:53 +00:00
|
|
|
|
void TextColorTween::DoApply(View *view, float pos) {
|
2017-12-03 17:39:50 +00:00
|
|
|
|
// TODO: No validation without RTTI?
|
|
|
|
|
TextView *tv = (TextView *)view;
|
2017-12-03 18:28:53 +00:00
|
|
|
|
tv->SetTextColor(Current(pos));
|
2017-12-03 17:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 18:28:53 +00:00
|
|
|
|
void VisibilityTween::DoApply(View *view, float pos) {
|
|
|
|
|
view->SetVisibility(Current(pos));
|
2017-12-03 17:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 18:28:53 +00:00
|
|
|
|
Visibility VisibilityTween::Current(float p) {
|
2017-12-03 17:39:50 +00:00
|
|
|
|
// Prefer V_VISIBLE over V_GONE/V_INVISIBLE.
|
|
|
|
|
if (from_ == V_VISIBLE && p < 1.0f)
|
|
|
|
|
return from_;
|
|
|
|
|
if (to_ == V_VISIBLE && p > 0.0f)
|
|
|
|
|
return to_;
|
|
|
|
|
return p >= 1.0f ? to_ : from_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|