Added Animation class to help with animations
Add GetParent API to UIElement
This commit is contained in:
parent
efa2d4a137
commit
73b9e7dbc7
4 changed files with 78 additions and 1 deletions
24
Platformio/OmoteUI/core/Animation.cpp
Normal file
24
Platformio/OmoteUI/core/Animation.cpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#include "Animation.hpp"
|
||||||
|
|
||||||
|
using namespace UI;
|
||||||
|
|
||||||
|
Animation::Animation(std::function<void(int32_t)> aAnimator,
|
||||||
|
int32_t aAnimationTime, int32_t aStart, int32_t aEnd)
|
||||||
|
: mAnimator(aAnimator) {
|
||||||
|
lv_anim_init(&mAnimation);
|
||||||
|
mAnimation.user_data = this;
|
||||||
|
lv_anim_set_custom_exec_cb(&mAnimation, AnimatorImpl);
|
||||||
|
lv_anim_set_values(&mAnimation, aStart, aEnd);
|
||||||
|
lv_anim_set_time(&mAnimation, aAnimationTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
Animation::~Animation() { lv_anim_custom_del(&mAnimation, AnimatorImpl); }
|
||||||
|
|
||||||
|
void Animation::Start() { lv_anim_start(&mAnimation); }
|
||||||
|
|
||||||
|
//////////////////////// Statics /////////////////////////////////////////
|
||||||
|
|
||||||
|
void Animation::AnimatorImpl(lv_anim_t *aSelf, int32_t aNextValue) {
|
||||||
|
auto self = reinterpret_cast<Animation *>(aSelf->user_data);
|
||||||
|
self->mAnimator(aNextValue);
|
||||||
|
}
|
23
Platformio/OmoteUI/core/Animation.hpp
Normal file
23
Platformio/OmoteUI/core/Animation.hpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
#include <functional>
|
||||||
|
#include <lvgl.h>
|
||||||
|
|
||||||
|
namespace UI {
|
||||||
|
|
||||||
|
class Animation {
|
||||||
|
public:
|
||||||
|
Animation(std::function<void(int32_t)> aAnimator, int32_t aAnimationTime,
|
||||||
|
int32_t aStart = 0, int32_t aEnd = 100);
|
||||||
|
|
||||||
|
virtual ~Animation();
|
||||||
|
|
||||||
|
void Start();
|
||||||
|
|
||||||
|
private:
|
||||||
|
lv_anim_t mAnimation;
|
||||||
|
std::function<void(int32_t)> mAnimator = nullptr;
|
||||||
|
|
||||||
|
static void AnimatorImpl(lv_anim_t *aSelf, int32_t aNextValue);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace UI
|
|
@ -24,9 +24,20 @@ UIElement::~UIElement() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UIElement *UIElement::GetParent() {
|
||||||
|
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||||
|
if (auto parent = lv_obj_get_parent(mLvglSelf); parent) {
|
||||||
|
if (auto parentElem = parent->user_data; parentElem) {
|
||||||
|
return reinterpret_cast<UIElement *>(parentElem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
UIElement *UIElement::AddElement(UIElement::Ptr anUIElement) {
|
UIElement *UIElement::AddElement(UIElement::Ptr anUIElement) {
|
||||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||||
lv_obj_set_parent(anUIElement->mLvglSelf, mLvglSelf);
|
lv_obj_set_parent(anUIElement->mLvglSelf, mLvglSelf);
|
||||||
|
anUIElement->OnAdded(this);
|
||||||
mContainedElements.push_back(std::move(anUIElement));
|
mContainedElements.push_back(std::move(anUIElement));
|
||||||
return mContainedElements[mContainedElements.size() - 1].get();
|
return mContainedElements[mContainedElements.size() - 1].get();
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,8 +105,22 @@ protected:
|
||||||
/// @brief Override in child class to run something after element is hidden
|
/// @brief Override in child class to run something after element is hidden
|
||||||
virtual void OnHide();
|
virtual void OnHide();
|
||||||
|
|
||||||
|
/// @brief Override to run something when element is added to a parent
|
||||||
|
/// @param aNewParent - Parent UIElement just added to
|
||||||
|
virtual void OnAdded(UIElement *aNewParent){};
|
||||||
|
|
||||||
// Override in object to handle LVGL events for that object
|
// Override in object to handle LVGL events for that object
|
||||||
virtual void OnLvglEvent(lv_event_t *anEvent){};
|
virtual void OnLvglEvent(lv_event_t *anEvent) {
|
||||||
|
if (mLvglEventHandler) {
|
||||||
|
mLvglEventHandler(anEvent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief Register a callback to run for Lvgl Events for objects that
|
||||||
|
/// are created from base classes.
|
||||||
|
void OnLvglEvent(std::function<void(lv_event_t *anEvent)> aLvglEventHandler) {
|
||||||
|
mLvglEventHandler = aLvglEventHandler;
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief Set KeyEvent to the UI element to see if it wants to handle it
|
/// @brief Set KeyEvent to the UI element to see if it wants to handle it
|
||||||
virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
|
virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
|
||||||
|
@ -117,12 +131,17 @@ protected:
|
||||||
virtual bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) = 0;
|
virtual bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/// @brief Get Pointer to Parent Element
|
||||||
|
/// @return - nullptr Parent was not wrapped or did not exist
|
||||||
|
UIElement *GetParent();
|
||||||
|
|
||||||
static void LvglEventHandler(lv_event_t *anEvent);
|
static void LvglEventHandler(lv_event_t *anEvent);
|
||||||
|
|
||||||
lv_obj_t *mLvglSelf;
|
lv_obj_t *mLvglSelf;
|
||||||
const ID mId;
|
const ID mId;
|
||||||
uint32_t mLvglKeepAliveTime = 0;
|
uint32_t mLvglKeepAliveTime = 0;
|
||||||
bool mIsHandlingLvglEvents = true;
|
bool mIsHandlingLvglEvents = true;
|
||||||
|
std::function<void(lv_event_t *)> mLvglEventHandler = nullptr;
|
||||||
|
|
||||||
/// @brief Elements that are currently in this element
|
/// @brief Elements that are currently in this element
|
||||||
std::vector<UIElement::Ptr> mContainedElements;
|
std::vector<UIElement::Ptr> mContainedElements;
|
||||||
|
|
Loading…
Add table
Reference in a new issue