From 73b9e7dbc72c43bde1601b5588420faccdc6a7b1 Mon Sep 17 00:00:00 2001 From: MatthewColvin Date: Mon, 16 Oct 2023 20:02:09 -0500 Subject: [PATCH] Added Animation class to help with animations Add GetParent API to UIElement --- Platformio/OmoteUI/core/Animation.cpp | 24 ++++++++++++++++++++++++ Platformio/OmoteUI/core/Animation.hpp | 23 +++++++++++++++++++++++ Platformio/OmoteUI/core/UIElement.cpp | 11 +++++++++++ Platformio/OmoteUI/core/UIElement.hpp | 21 ++++++++++++++++++++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 Platformio/OmoteUI/core/Animation.cpp create mode 100644 Platformio/OmoteUI/core/Animation.hpp diff --git a/Platformio/OmoteUI/core/Animation.cpp b/Platformio/OmoteUI/core/Animation.cpp new file mode 100644 index 0000000..101b966 --- /dev/null +++ b/Platformio/OmoteUI/core/Animation.cpp @@ -0,0 +1,24 @@ +#include "Animation.hpp" + +using namespace UI; + +Animation::Animation(std::function 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(aSelf->user_data); + self->mAnimator(aNextValue); +} \ No newline at end of file diff --git a/Platformio/OmoteUI/core/Animation.hpp b/Platformio/OmoteUI/core/Animation.hpp new file mode 100644 index 0000000..1671f48 --- /dev/null +++ b/Platformio/OmoteUI/core/Animation.hpp @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +namespace UI { + +class Animation { +public: + Animation(std::function aAnimator, int32_t aAnimationTime, + int32_t aStart = 0, int32_t aEnd = 100); + + virtual ~Animation(); + + void Start(); + +private: + lv_anim_t mAnimation; + std::function mAnimator = nullptr; + + static void AnimatorImpl(lv_anim_t *aSelf, int32_t aNextValue); +}; + +} // namespace UI \ No newline at end of file diff --git a/Platformio/OmoteUI/core/UIElement.cpp b/Platformio/OmoteUI/core/UIElement.cpp index 7f965d0..b94b5c6 100644 --- a/Platformio/OmoteUI/core/UIElement.cpp +++ b/Platformio/OmoteUI/core/UIElement.cpp @@ -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(parentElem); + } + } + return nullptr; +} + UIElement *UIElement::AddElement(UIElement::Ptr anUIElement) { auto lock = LvglResourceManager::GetInstance().scopeLock(); lv_obj_set_parent(anUIElement->mLvglSelf, mLvglSelf); + anUIElement->OnAdded(this); mContainedElements.push_back(std::move(anUIElement)); return mContainedElements[mContainedElements.size() - 1].get(); } diff --git a/Platformio/OmoteUI/core/UIElement.hpp b/Platformio/OmoteUI/core/UIElement.hpp index ef610b1..f60555e 100644 --- a/Platformio/OmoteUI/core/UIElement.hpp +++ b/Platformio/OmoteUI/core/UIElement.hpp @@ -105,8 +105,22 @@ protected: /// @brief Override in child class to run something after element is hidden 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 - 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 aLvglEventHandler) { + mLvglEventHandler = aLvglEventHandler; + } /// @brief Set KeyEvent to the UI element to see if it wants to handle it virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent); @@ -117,12 +131,17 @@ protected: virtual bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) = 0; 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); lv_obj_t *mLvglSelf; const ID mId; uint32_t mLvglKeepAliveTime = 0; bool mIsHandlingLvglEvents = true; + std::function mLvglEventHandler = nullptr; /// @brief Elements that are currently in this element std::vector mContainedElements;