Add warning when disabling secure.enable_security (#9943)
[oweals/minetest.git] / src / gui / guiAnimatedImage.cpp
1 #include "guiAnimatedImage.h"
2
3 #include "client/guiscalingfilter.h"
4 #include "client/tile.h" // ITextureSource
5 #include "log.h"
6 #include "porting.h"
7 #include "util/string.h"
8 #include <string>
9 #include <vector>
10
11 GUIAnimatedImage::GUIAnimatedImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent,
12         s32 id, const core::rect<s32> &rectangle, const std::string &texture_name,
13         s32 frame_count, s32 frame_duration, ISimpleTextureSource *tsrc) :
14         gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle), m_tsrc(tsrc)
15 {
16         m_texture = m_tsrc->getTexture(texture_name);
17
18         m_frame_count    = std::max(frame_count,    1);
19         m_frame_duration = std::max(frame_duration, 0);
20
21         if (m_texture != nullptr) {
22                 core::dimension2d<u32> size = m_texture->getOriginalSize();
23                 if (size.Height < (u64)m_frame_count)
24                         m_frame_count = size.Height;
25         } else {
26                 // No need to step an animation if we have nothing to draw
27                 m_frame_count = 1;
28         }
29 }
30
31 void GUIAnimatedImage::draw()
32 {
33         // Render the current frame
34         if (m_texture != nullptr) {
35                 video::IVideoDriver *driver = Environment->getVideoDriver();
36
37                 const video::SColor color(255, 255, 255, 255);
38                 const video::SColor colors[] = {color, color, color, color};
39
40                 core::dimension2d<u32> size = m_texture->getOriginalSize();
41                 size.Height /= m_frame_count;
42
43                 draw2DImageFilterScaled(driver, m_texture, AbsoluteRect,
44                         core::rect<s32>(core::position2d<s32>(0, size.Height * m_frame_idx), size),
45                         NoClip ? nullptr : &AbsoluteClippingRect, colors, true);
46         }
47
48         // Step the animation
49         if (m_frame_count > 1 && m_frame_duration > 0) {
50                 // Determine the delta time to step
51                 u64 new_global_time = porting::getTimeMs();
52                 if (m_global_time > 0)
53                         m_frame_time += new_global_time - m_global_time;
54
55                 m_global_time = new_global_time;
56
57                 // Advance by the number of elapsed frames, looping if necessary
58                 m_frame_idx += u32(m_frame_time / m_frame_duration);
59                 m_frame_idx %= m_frame_count;
60
61                 // If 1 or more frames have elapsed, reset the frame time counter with
62                 // the remainder
63                 m_frame_time %= m_frame_duration;
64         }
65 }
66
67
68 void GUIAnimatedImage::setFrameIndex(s32 frame)
69 {
70         s32 idx = std::max(frame, 0);
71         if (idx > 0 && idx < m_frame_count)
72                 m_frame_idx = idx;
73 }