Attempt to add GUI animations
This commit is contained in:
parent
fe04cbb597
commit
4e17d6e40c
4 changed files with 79 additions and 0 deletions
|
|
@ -9,6 +9,8 @@
|
|||
|
||||
BezierEasing BezierEasing::linear{0.0, 1.0, 0.0, 1.0};
|
||||
|
||||
BezierEasing::BezierEasing() {}
|
||||
|
||||
BezierEasing::BezierEasing(Eigen::Vector4f c):
|
||||
BezierEasing(c.x(), c.y(), c.z(), c.w()) {}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,8 +11,10 @@
|
|||
|
||||
class BezierEasing {
|
||||
public:
|
||||
BezierEasing();
|
||||
BezierEasing(Eigen::Vector4f c);
|
||||
BezierEasing(float x1, float y1, float x2, float y2);
|
||||
BezierEasing& operator=(const BezierEasing& b) = default;
|
||||
float operator() (float x) const;
|
||||
static BezierEasing linear;
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "LayoutElement.hpp"
|
||||
#include "Helpers/ConvertVec.hpp"
|
||||
#include "../GUIManager.hpp"
|
||||
|
||||
namespace GUIStuff {
|
||||
|
||||
|
|
@ -10,6 +11,59 @@ void LayoutElement::layout(const Clay_ElementId& id, const std::function<void(La
|
|||
layout(this, id);
|
||||
}
|
||||
|
||||
void LayoutElement::update() {
|
||||
for(auto& [id, animation] : animations) {
|
||||
switch(animation.state) {
|
||||
case InternalFloatAnimation::State::RUN: {
|
||||
animation.currentTime += gui.io.deltaTime;
|
||||
if(animation.currentTime >= animation.anim.duration) {
|
||||
animation.currentTime = animation.anim.duration;
|
||||
animation.state = InternalFloatAnimation::State::DONE;
|
||||
}
|
||||
gui.set_to_layout();
|
||||
break;
|
||||
}
|
||||
case InternalFloatAnimation::State::RUN_REVERSE: {
|
||||
animation.currentTime -= gui.io.deltaTime;
|
||||
if(animation.currentTime <= 0.0f) {
|
||||
animation.currentTime = 0.0f;
|
||||
animation.state = InternalFloatAnimation::State::DONE;
|
||||
}
|
||||
gui.set_to_layout();
|
||||
break;
|
||||
}
|
||||
case InternalFloatAnimation::State::DONE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float LayoutElement::get_float_animation(const char* animationID, const FloatAnimation& animation) {
|
||||
InternalFloatAnimation internalAnim;
|
||||
internalAnim.anim = animation;
|
||||
auto [it, placed] = animations.emplace(animationID, internalAnim);
|
||||
const InternalFloatAnimation& actualInternalAnim = it->second;
|
||||
return std::lerp(actualInternalAnim.anim.startVal, actualInternalAnim.anim.endVal, actualInternalAnim.anim.easing(it->second.currentTime / actualInternalAnim.anim.duration));
|
||||
}
|
||||
|
||||
void LayoutElement::animation_trigger(const char* animationID) {
|
||||
auto it = animations.find(animationID);
|
||||
if(it != animations.end()) {
|
||||
InternalFloatAnimation& animation = it->second;
|
||||
if(animation.currentTime < animation.anim.duration)
|
||||
animation.state = InternalFloatAnimation::State::RUN;
|
||||
}
|
||||
}
|
||||
|
||||
void LayoutElement::animation_trigger_reverse(const char* animationID) {
|
||||
auto it = animations.find(animationID);
|
||||
if(it != animations.end()) {
|
||||
InternalFloatAnimation& animation = it->second;
|
||||
if(animation.currentTime > 0.0f)
|
||||
animation.state = InternalFloatAnimation::State::RUN_REVERSE;
|
||||
}
|
||||
}
|
||||
|
||||
void LayoutElement::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
|
||||
if(c.mouseButton) c.mouseButton(this, button);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include "Element.hpp"
|
||||
#include "Helpers/BezierEasing.hpp"
|
||||
|
||||
namespace GUIStuff {
|
||||
|
||||
|
|
@ -12,12 +13,32 @@ class LayoutElement : public Element {
|
|||
std::function<void(LayoutElement* l, const InputManager::MouseWheelCallbackArgs& wheel)> mouseWheel;
|
||||
std::function<void(LayoutElement* l, const InputManager::KeyCallbackArgs& key)> key;
|
||||
};
|
||||
struct FloatAnimation {
|
||||
float startVal = 0.0f;
|
||||
float endVal = 1.0f;
|
||||
float duration = 1.0f;
|
||||
BezierEasing easing = BezierEasing::linear;
|
||||
};
|
||||
void layout(const Clay_ElementId& id, const std::function<void(LayoutElement*, const Clay_ElementId&)>& layout, const Callbacks& c = {});
|
||||
virtual void update() override;
|
||||
float get_float_animation(const char* animationID, const FloatAnimation& animation);
|
||||
void animation_trigger(const char* animationID);
|
||||
void animation_trigger_reverse(const char* animationID);
|
||||
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override;
|
||||
virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override;
|
||||
virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) override;
|
||||
virtual void input_key_callback(const InputManager::KeyCallbackArgs& key) override;
|
||||
private:
|
||||
struct InternalFloatAnimation {
|
||||
enum class State {
|
||||
RUN,
|
||||
RUN_REVERSE,
|
||||
DONE
|
||||
} state = State::DONE;
|
||||
float currentTime = 0.0f;
|
||||
FloatAnimation anim;
|
||||
};
|
||||
std::unordered_map<const char*, InternalFloatAnimation> animations;
|
||||
Callbacks c;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue