Skip to content

Commit

Permalink
Added Animation class to help with animations
Browse files Browse the repository at this point in the history
Add GetParent API to UIElement
  • Loading branch information
MatthewColvin committed Oct 17, 2023
1 parent efa2d4a commit 73b9e7d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Platformio/OmoteUI/core/Animation.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions Platformio/OmoteUI/core/Animation.hpp
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Platformio/OmoteUI/core/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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();
}
Expand Down
21 changes: 20 additions & 1 deletion Platformio/OmoteUI/core/UIElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,22 @@ class UIElement {
/// @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<void(lv_event_t *anEvent)> aLvglEventHandler) {
mLvglEventHandler = aLvglEventHandler;
}

/// @brief Set KeyEvent to the UI element to see if it wants to handle it
virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);
Expand All @@ -117,12 +131,17 @@ class UIElement {
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<void(lv_event_t *)> mLvglEventHandler = nullptr;

/// @brief Elements that are currently in this element
std::vector<UIElement::Ptr> mContainedElements;
Expand Down

0 comments on commit 73b9e7d

Please sign in to comment.