UI: Add a simple tween class.

This commit is contained in:
Unknown W. Brackets 2017-12-03 09:39:50 -08:00
parent 7ab3e58dec
commit b4b850bce8
8 changed files with 168 additions and 0 deletions

View File

@ -955,6 +955,8 @@ add_library(native STATIC
ext/native/ui/ui_context.h
ext/native/ui/ui_screen.cpp
ext/native/ui/ui_screen.h
ext/native/ui/ui_tween.cpp
ext/native/ui/ui_tween.h
ext/native/ui/view.cpp
ext/native/ui/view.h
ext/native/ui/viewgroup.cpp

View File

@ -376,6 +376,7 @@
<ClInclude Include="..\..\ext\native\ui\ui.h" />
<ClInclude Include="..\..\ext\native\ui\ui_context.h" />
<ClInclude Include="..\..\ext\native\ui\ui_screen.h" />
<ClInclude Include="..\..\ext\native\ui\ui_tween.h" />
<ClInclude Include="..\..\ext\native\ui\view.h" />
<ClInclude Include="..\..\ext\native\ui\viewgroup.h" />
<ClInclude Include="..\..\ext\native\util\const_map.h" />
@ -1290,6 +1291,7 @@
<ClCompile Include="..\..\ext\native\ui\ui.cpp" />
<ClCompile Include="..\..\ext\native\ui\ui_context.cpp" />
<ClCompile Include="..\..\ext\native\ui\ui_screen.cpp" />
<ClCompile Include="..\..\ext\native\ui\ui_tween.cpp" />
<ClCompile Include="..\..\ext\native\ui\view.cpp" />
<ClCompile Include="..\..\ext\native\ui\viewgroup.cpp" />
<ClCompile Include="..\..\ext\native\util\hash\hash.cpp" />

View File

@ -154,6 +154,9 @@
<ClCompile Include="..\..\ext\native\ui\ui_screen.cpp">
<Filter>ui</Filter>
</ClCompile>
<ClCompile Include="..\..\ext\native\ui\ui_tween.cpp">
<Filter>ui</Filter>
</ClCompile>
<ClCompile Include="..\..\ext\native\ui\view.cpp">
<Filter>ui</Filter>
</ClCompile>
@ -590,6 +593,9 @@
<ClInclude Include="..\..\ext\native\ui\ui_screen.h">
<Filter>ui</Filter>
</ClInclude>
<ClInclude Include="..\..\ext\native\ui\ui_tween.h">
<Filter>ui</Filter>
</ClInclude>
<ClInclude Include="..\..\ext\native\ui\view.h">
<Filter>ui</Filter>
</ClInclude>

View File

@ -91,6 +91,7 @@ LOCAL_SRC_FILES :=\
ui/viewgroup.cpp \
ui/ui.cpp \
ui/ui_screen.cpp \
ui/ui_tween.cpp \
ui/ui_context.cpp \
ui/screen.cpp \
util/text/utf8.cpp \

View File

@ -285,6 +285,7 @@
<ClInclude Include="ui\ui.h" />
<ClInclude Include="ui\ui_context.h" />
<ClInclude Include="ui\ui_screen.h" />
<ClInclude Include="ui\ui_tween.h" />
<ClInclude Include="ui\view.h" />
<ClInclude Include="ui\viewgroup.h" />
<ClInclude Include="util\random\rng.h" />
@ -752,6 +753,7 @@
<ClCompile Include="ui\ui.cpp" />
<ClCompile Include="ui\ui_context.cpp" />
<ClCompile Include="ui\ui_screen.cpp" />
<ClCompile Include="ui\ui_tween.cpp" />
<ClCompile Include="ui\view.cpp" />
<ClCompile Include="ui\viewgroup.cpp" />
<ClCompile Include="util\hash\hash.cpp" />

View File

@ -329,6 +329,9 @@
<ClInclude Include="thin3d\DataFormat.h">
<Filter>thin3d</Filter>
</ClInclude>
<ClInclude Include="ui\ui_tween.h">
<Filter>ui</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@ -796,6 +799,9 @@
<ClCompile Include="thin3d\VulkanRenderManager.cpp">
<Filter>thin3d</Filter>
</ClCompile>
<ClCompile Include="ui\ui_tween.cpp">
<Filter>ui</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">

View File

@ -0,0 +1,31 @@
#include "base/colorutil.h"
#include "ui/ui_tween.h"
#include "ui/view.h"
namespace UI {
uint32_t ColorTween::Current() {
return colorBlend(to_, from_, Position());
}
void TextColorTween::Apply(View *view) {
// TODO: No validation without RTTI?
TextView *tv = (TextView *)view;
tv->SetTextColor(Current());
}
void VisibilityTween::Apply(View *view) {
view->SetVisibility(Current());
}
Visibility VisibilityTween::Current() {
// Prefer V_VISIBLE over V_GONE/V_INVISIBLE.
float p = Position();
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

118
ext/native/ui/ui_tween.h Normal file
View File

@ -0,0 +1,118 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include "base/timeutil.h"
#include "ui/view.h"
namespace UI {
// This is the class to use in Update().
class Tween {
public:
Tween(float duration, float (*curve)(float)) : duration_(duration), curve_(curve) {
start_ = time_now();
}
virtual ~Tween() {
}
// Actually apply the tween to a view.
virtual void Apply(View *view) = 0;
bool Finished() {
return time_now() >= start_ + duration_;
}
protected:
float DurationOffset() {
return time_now() - start_;
}
float Position() {
return curve_(std::min(1.0f, DurationOffset() / duration_));
}
float start_;
float duration_;
float (*curve_)(float);
};
// This is the class all tweens inherit from. Shouldn't be used directly, see below.
template <typename Value>
class TweenBase: public Tween {
public:
TweenBase(Value from, Value to, float duration, float (*curve)(float) = [](float f) { return f; })
: Tween(duration, curve), from_(from), to_(to) {
}
// Use this to change the destination value.
// Useful when a state flips while the tween is half-way through.
void Divert(const Value &newTo) {
const Value newFrom = Current();
// Are we already part way through another transition?
if (!Finished()) {
if (newTo == to_) {
// Already on course. Don't change.
} else if (newTo == from_) {
// Reversing, adjust start_ to be smooth from the current value.
float newOffset = duration_ - DurationOffset();
start_ = time_now() - newOffset;
} else {
// Otherwise, start over.
start_ = time_now();
}
} else {
// Already finished, so restart.
start_ = time_now();
}
from_ = newFrom;
to_ = newTo;
}
// Stop animating the value.
void Stop() {
Reset(Current());
}
// Use when the value is explicitly reset. Implicitly stops the tween.
void Reset(const Value &newFrom) {
from_ = newFrom;
to_ = newFrom;
}
protected:
virtual Value Current() = 0;
Value from_;
Value to_;
};
// Generic - subclass this for specific color handling.
class ColorTween : public TweenBase<uint32_t> {
public:
using TweenBase::TweenBase;
protected:
uint32_t Current() override;
};
class TextColorTween : public ColorTween {
public:
using ColorTween::ColorTween;
void Apply(View *view) override;
};
class VisibilityTween : public TweenBase<Visibility> {
public:
using TweenBase::TweenBase;
void Apply(View *view) override;
protected:
Visibility Current() override;
};
} // namespace