Skip to content

Commit

Permalink
feat: new prop image-individual-animation
Browse files Browse the repository at this point in the history
it is now possible to use the same animated image(apng) at the same time with different frames.
  • Loading branch information
mehah authored Apr 11, 2023
1 parent 7949cfe commit 64c088e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/framework/ui/uiwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ enum FlagProp : uint32_t
PropImageFixedRatio = 1 << 19,
PropImageRepeated = 1 << 20,
PropImageSmooth = 1 << 21,
PropImageAutoResize = 1 << 22
PropImageAutoResize = 1 << 22,
propImageIndividualAnimation = 1 << 23
};

// @bindclass
Expand Down Expand Up @@ -462,6 +463,8 @@ class UIWidget : public LuaObject
Rect m_imageRect;
Color m_imageColor{ Color::white };
Point m_iconOffset;
Timer m_imageAnimatorTimer;
uint32_t m_currentFrame{ 0 };

EdgeGroup<int> m_imageBorder;

Expand All @@ -480,6 +483,7 @@ class UIWidget : public LuaObject
void setImageRepeated(bool repeated) { setProp(PropImageRepeated, repeated); updateImageCache(); }
void setImageSmooth(bool smooth) { setProp(PropImageSmooth, smooth); }
void setImageAutoResize(bool autoResize) { setProp(PropImageAutoResize, autoResize); }
void setImageIndividualAnimation(bool v) { setProp(propImageIndividualAnimation, v); }
void setImageBorderTop(int border) { m_imageBorder.top = border; configureBorderImage(); }
void setImageBorderRight(int border) { m_imageBorder.right = border; configureBorderImage(); }
void setImageBorderBottom(int border) { m_imageBorder.bottom = border; configureBorderImage(); }
Expand All @@ -499,6 +503,7 @@ class UIWidget : public LuaObject
bool isImageFixedRatio() { return hasProp(PropImageFixedRatio); }
bool isImageSmooth() { return hasProp(PropImageSmooth); }
bool isImageAutoResize() { return hasProp(PropImageAutoResize); }
bool isImageIndividualAnimation() { return hasProp(propImageIndividualAnimation); }
int getImageBorderTop() { return m_imageBorder.top; }
int getImageBorderRight() { return m_imageBorder.right; }
int getImageBorderBottom() { return m_imageBorder.bottom; }
Expand Down
5 changes: 5 additions & 0 deletions src/framework/ui/uiwidgetimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode)
setImageBorder(node->value<int>());
else if (node->tag() == "image-auto-resize")
setImageAutoResize(node->value<bool>());
else if (node->tag() == "image-individual-animation")
setImageIndividualAnimation(node->value<bool>());
}
}

Expand Down Expand Up @@ -164,6 +166,9 @@ void UIWidget::drawImage(const Rect& screenCoords)
//m_imageTexture->setSmooth(m_imageSmooth);
const bool useRepeated = hasProp(PropImageBordered) || hasProp(PropImageRepeated);
for (const auto& [dest, src] : m_imageCoordsCache) {
const auto& texture = m_imageTexture->isAnimatedTexture() && isImageIndividualAnimation() ?
std::static_pointer_cast<AnimatedTexture>(m_imageTexture)->get(m_currentFrame, m_imageAnimatorTimer) : m_imageTexture;

if (useRepeated)
g_drawPool.addTexturedRepeatedRect(dest, m_imageTexture, src, m_imageColor);
else
Expand Down

0 comments on commit 64c088e

Please sign in to comment.