guiAnimatedImage.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "guiAnimatedImage.h"
  2. #include "client/guiscalingfilter.h"
  3. #include "client/tile.h" // ITextureSource
  4. #include "log.h"
  5. #include "porting.h"
  6. #include "util/string.h"
  7. #include <string>
  8. #include <vector>
  9. GUIAnimatedImage::GUIAnimatedImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent,
  10. s32 id, const core::rect<s32> &rectangle) :
  11. gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle)
  12. {
  13. }
  14. void GUIAnimatedImage::draw()
  15. {
  16. if (m_texture == nullptr)
  17. return;
  18. video::IVideoDriver *driver = Environment->getVideoDriver();
  19. core::dimension2d<u32> size = m_texture->getOriginalSize();
  20. if ((u32)m_frame_count > size.Height)
  21. m_frame_count = size.Height;
  22. if (m_frame_idx >= m_frame_count)
  23. m_frame_idx = m_frame_count - 1;
  24. size.Height /= m_frame_count;
  25. core::rect<s32> rect(core::position2d<s32>(0, size.Height * m_frame_idx), size);
  26. core::rect<s32> *cliprect = NoClip ? nullptr : &AbsoluteClippingRect;
  27. if (m_middle.getArea() == 0) {
  28. const video::SColor color(255, 255, 255, 255);
  29. const video::SColor colors[] = {color, color, color, color};
  30. draw2DImageFilterScaled(driver, m_texture, AbsoluteRect, rect, cliprect,
  31. colors, true);
  32. } else {
  33. draw2DImage9Slice(driver, m_texture, AbsoluteRect, rect, m_middle, cliprect);
  34. }
  35. // Step the animation
  36. if (m_frame_count > 1 && m_frame_duration > 0) {
  37. // Determine the delta time to step
  38. u64 new_global_time = porting::getTimeMs();
  39. if (m_global_time > 0)
  40. m_frame_time += new_global_time - m_global_time;
  41. m_global_time = new_global_time;
  42. // Advance by the number of elapsed frames, looping if necessary
  43. m_frame_idx += (u32)(m_frame_time / m_frame_duration);
  44. m_frame_idx %= m_frame_count;
  45. // If 1 or more frames have elapsed, reset the frame time counter with
  46. // the remainder
  47. m_frame_time %= m_frame_duration;
  48. }
  49. }