guiAnimatedImage.cpp 1.7 KB

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