Animation.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Animation.h"
  7. #include "Compositor.h"
  8. #include <AK/Badge.h>
  9. namespace WindowServer {
  10. Animation::~Animation()
  11. {
  12. if (m_running)
  13. Compositor::the().unregister_animation({}, *this);
  14. }
  15. void Animation::set_duration(int duration_in_ms)
  16. {
  17. m_duration = duration_in_ms;
  18. }
  19. void Animation::start()
  20. {
  21. if (m_running)
  22. return;
  23. m_running = true;
  24. m_timer.start();
  25. Compositor::the().register_animation({}, *this);
  26. }
  27. void Animation::stop()
  28. {
  29. if (!m_running)
  30. return;
  31. m_running = false;
  32. Compositor::the().unregister_animation({}, *this);
  33. if (on_stop)
  34. on_stop();
  35. }
  36. void Animation::call_stop_handler(Badge<Compositor>)
  37. {
  38. if (on_stop)
  39. on_stop();
  40. }
  41. void Animation::was_removed(Badge<Compositor>)
  42. {
  43. m_running = false;
  44. }
  45. bool Animation::update(Gfx::Painter& painter, Screen& screen, Gfx::DisjointIntRectSet& flush_rects)
  46. {
  47. i64 const elapsed_ms = m_timer.elapsed();
  48. float progress = min((float)elapsed_ms / (float)m_duration, 1.0f);
  49. if (on_update)
  50. on_update(progress, painter, screen, flush_rects);
  51. return progress < 1.0f;
  52. }
  53. }