mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
FormSpec: 9-slice images, animated_images, and fgimg_middle (#12453)
* FormSpec: 9-slice images and animated_images * Add fgimg_middle; clean up code * Address issues, add tests * Fix stupid error; bump formspec version * Re-add image[] elements without a size
This commit is contained in:
parent
5a562a597c
commit
f7bcf7fa46
15 changed files with 222 additions and 204 deletions
|
@ -9,40 +9,37 @@
|
|||
#include <vector>
|
||||
|
||||
GUIAnimatedImage::GUIAnimatedImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent,
|
||||
s32 id, const core::rect<s32> &rectangle, const std::string &texture_name,
|
||||
s32 frame_count, s32 frame_duration, ISimpleTextureSource *tsrc) :
|
||||
gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle), m_tsrc(tsrc)
|
||||
s32 id, const core::rect<s32> &rectangle) :
|
||||
gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle)
|
||||
{
|
||||
m_texture = m_tsrc->getTexture(texture_name);
|
||||
|
||||
m_frame_count = std::max(frame_count, 1);
|
||||
m_frame_duration = std::max(frame_duration, 0);
|
||||
|
||||
if (m_texture != nullptr) {
|
||||
core::dimension2d<u32> size = m_texture->getOriginalSize();
|
||||
if (size.Height < (u64)m_frame_count)
|
||||
m_frame_count = size.Height;
|
||||
} else {
|
||||
// No need to step an animation if we have nothing to draw
|
||||
m_frame_count = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void GUIAnimatedImage::draw()
|
||||
{
|
||||
// Render the current frame
|
||||
if (m_texture != nullptr) {
|
||||
video::IVideoDriver *driver = Environment->getVideoDriver();
|
||||
if (m_texture == nullptr)
|
||||
return;
|
||||
|
||||
video::IVideoDriver *driver = Environment->getVideoDriver();
|
||||
|
||||
core::dimension2d<u32> size = m_texture->getOriginalSize();
|
||||
|
||||
if ((u32)m_frame_count > size.Height)
|
||||
m_frame_count = size.Height;
|
||||
if (m_frame_idx >= m_frame_count)
|
||||
m_frame_idx = m_frame_count - 1;
|
||||
|
||||
size.Height /= m_frame_count;
|
||||
|
||||
core::rect<s32> rect(core::position2d<s32>(0, size.Height * m_frame_idx), size);
|
||||
core::rect<s32> *cliprect = NoClip ? nullptr : &AbsoluteClippingRect;
|
||||
|
||||
if (m_middle.getArea() == 0) {
|
||||
const video::SColor color(255, 255, 255, 255);
|
||||
const video::SColor colors[] = {color, color, color, color};
|
||||
|
||||
core::dimension2d<u32> size = m_texture->getOriginalSize();
|
||||
size.Height /= m_frame_count;
|
||||
|
||||
draw2DImageFilterScaled(driver, m_texture, AbsoluteRect,
|
||||
core::rect<s32>(core::position2d<s32>(0, size.Height * m_frame_idx), size),
|
||||
NoClip ? nullptr : &AbsoluteClippingRect, colors, true);
|
||||
draw2DImageFilterScaled(driver, m_texture, AbsoluteRect, rect, cliprect,
|
||||
colors, true);
|
||||
} else {
|
||||
draw2DImage9Slice(driver, m_texture, AbsoluteRect, rect, m_middle, cliprect);
|
||||
}
|
||||
|
||||
// Step the animation
|
||||
|
@ -55,7 +52,7 @@ void GUIAnimatedImage::draw()
|
|||
m_global_time = new_global_time;
|
||||
|
||||
// Advance by the number of elapsed frames, looping if necessary
|
||||
m_frame_idx += u32(m_frame_time / m_frame_duration);
|
||||
m_frame_idx += (u32)(m_frame_time / m_frame_duration);
|
||||
m_frame_idx %= m_frame_count;
|
||||
|
||||
// If 1 or more frames have elapsed, reset the frame time counter with
|
||||
|
@ -63,11 +60,3 @@ void GUIAnimatedImage::draw()
|
|||
m_frame_time %= m_frame_duration;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GUIAnimatedImage::setFrameIndex(s32 frame)
|
||||
{
|
||||
s32 idx = std::max(frame, 0);
|
||||
if (idx > 0 && idx < m_frame_count)
|
||||
m_frame_idx = idx;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue