Skip to content

Commit

Permalink
UI transition improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
amzeratul committed Sep 2, 2018
1 parent 0d844ac commit d20c3f2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 16 deletions.
Expand Up @@ -14,7 +14,9 @@ namespace Halley {
};

public:
UITransitionSlideBehaviour(Time length, UIAnchor base, UIAnchor fadeIn, UIAnchor fadeOut);
using TransitionCurve = std::function<float(float)>;

UITransitionSlideBehaviour(Time length, UIAnchor base, UIAnchor startPos, UIAnchor endPos, TransitionCurve curve);

void init() override;
void deInit() override;
Expand All @@ -26,9 +28,10 @@ namespace Halley {
Time length;
Time time;
Mode mode;
TransitionCurve curve;

UIAnchor base;
UIAnchor fadeIn;
UIAnchor fadeOut;
UIAnchor startPos;
UIAnchor endPos;
};
}
4 changes: 4 additions & 0 deletions src/engine/ui/include/halley/ui/ui_behaviour.h
Expand Up @@ -18,10 +18,14 @@ namespace Halley {
virtual bool isAlive() const;
UIWidget* getWidget() const;

virtual void setReversed(bool reversed);
virtual bool isReversed() const;

private:
friend class UIWidget;

UIWidget* widget = nullptr;
bool reversed = false;

void doInit(UIWidget& widget);
void doDeInit();
Expand Down
1 change: 1 addition & 0 deletions src/engine/ui/include/halley/ui/ui_widget.h
Expand Up @@ -130,6 +130,7 @@ namespace Halley {

void addBehaviour(std::shared_ptr<UIBehaviour> behaviour);
void clearBehaviours();
const std::vector<std::shared_ptr<UIBehaviour>>& getBehaviours() const;

void playSound(const String& eventName);

Expand Down
19 changes: 8 additions & 11 deletions src/engine/ui/src/ui/behaviours/ui_transition_slide_behaviour.cpp
Expand Up @@ -2,20 +2,21 @@
#include "ui_widget.h"
using namespace Halley;

UITransitionSlideBehaviour::UITransitionSlideBehaviour(Time length, UIAnchor base, UIAnchor fadeIn, UIAnchor fadeOut)
UITransitionSlideBehaviour::UITransitionSlideBehaviour(Time length, UIAnchor base, UIAnchor startPos, UIAnchor endPos, TransitionCurve curve)
: length(length)
, time(0.0)
, mode(Mode::FadeIn)
, curve(std::move(curve))
, base(std::move(base))
, fadeIn(std::move(fadeIn))
, fadeOut(std::move(fadeOut))
, startPos(std::move(startPos))
, endPos(std::move(endPos))
{
}

void UITransitionSlideBehaviour::init()
{
getWidget()->setEnabled(false);
getWidget()->setAnchor(fadeIn);
getWidget()->setAnchor(startPos);
}

void UITransitionSlideBehaviour::deInit()
Expand All @@ -28,20 +29,16 @@ void UITransitionSlideBehaviour::update(Time dt)
time += dt;

const float x = clamp(mode == Mode::FadeIn ? float(time / length) : 1.0f - float(time / length), 0.0f, 1.0f);
constexpr float pi = 3.141592653592f;
//const float t = smoothCos(x);
//const float t = ::sin(1.7f * x * x) / ::sin(1.7f);
//const float t = (1.0f - x) * ::sin(2.5f * 3.1415926535 * ::pow(x, 5.0f)) * 1.2 + x;
const float t = float(pow(1.0f - x, 3.0f) * sin(2.5f * pi * pow(x, 3.0f)) * 4.0f + (1.0f - pow(1.0f - x, 3.0f)) * sin(x * pi * 0.5f));
const float t = curve(x);

if (mode == Mode::FadeIn) {
if (time >= length) {
mode = Mode::Normal;
getWidget()->setEnabled(true);
}
getWidget()->setAnchor(lerp(fadeIn, base, t));
getWidget()->setAnchor(lerp(isReversed() ? endPos : startPos, base, t));
} else if (mode == Mode::FadeOut) {
getWidget()->setAnchor(lerp(base, fadeOut, 1.0f - t));
getWidget()->setAnchor(lerp(base, isReversed() ? startPos : endPos, 1.0f - t));
if (time >= length) {
getWidget()->destroy();
}
Expand Down
10 changes: 10 additions & 0 deletions src/engine/ui/src/ui/ui_behaviour.cpp
Expand Up @@ -30,6 +30,16 @@ UIWidget* UIBehaviour::getWidget() const
return widget;
}

void UIBehaviour::setReversed(bool r)
{
reversed = r;
}

bool UIBehaviour::isReversed() const
{
return reversed;
}

void UIBehaviour::doInit(UIWidget& w)
{
widget = &w;
Expand Down
5 changes: 5 additions & 0 deletions src/engine/ui/src/ui/ui_widget.cpp
Expand Up @@ -408,6 +408,11 @@ void UIWidget::clearBehaviours()
behaviours.clear();
}

const std::vector<std::shared_ptr<UIBehaviour>>& UIWidget::getBehaviours() const
{
return behaviours;
}

UIInputType UIWidget::getLastInputType() const
{
return lastInputType;
Expand Down
11 changes: 9 additions & 2 deletions src/engine/utils/include/halley/utils/utils.h
Expand Up @@ -163,8 +163,15 @@ namespace Halley {

// Smoothing
template <typename T>
constexpr inline T smoothCos(T a) {
return T((1-cos(a * 3.1415926535897932384626433832795))*0.5);
constexpr inline T smoothCos(T a)
{
return (T(1) - T(std::cos(a * pi()))) * T(0.5);
}

template <typename T>
constexpr inline T overshootCurve(T x)
{
return T(pow(1.0f - x, 3.0f) * sin(2.5f * pi() * pow(x, 3.0f)) * 4.0f + (1.0f - pow(1.0f - x, 3.0f)) * sin(x * pi() * 0.5f));
}

// ASR (attack-sustain-release) envelope
Expand Down

0 comments on commit d20c3f2

Please sign in to comment.