1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:28:12 +00:00
serenity/Userland/Services/WindowServer/Animation.cpp
Andreas Kling 75f870a93f WindowServer: Add a more generic mechanism for animations
This patch adds the WindowServer::Animation class, which represents
a simple animation driven by the compositor.

An animation has a length (in milliseconds) and two hooks:

- on_update: called whenever the animation should render something.
- on_stop: called when the animation is finished and/or stopped.

This patch also ports the window minimization animation to this new
mechanism. :^)
2021-06-27 19:38:11 +02:00

53 lines
965 B
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Animation.h"
#include "Compositor.h"
#include <AK/Badge.h>
namespace WindowServer {
Animation::Animation()
{
Compositor::the().register_animation({}, *this);
}
Animation::~Animation()
{
Compositor::the().unregister_animation({}, *this);
}
void Animation::set_length(int length_in_ms)
{
m_length = length_in_ms;
}
void Animation::start()
{
m_running = true;
m_timer.start();
}
void Animation::stop()
{
m_running = false;
if (on_stop)
on_stop();
}
void Animation::update(Badge<Compositor>, Gfx::Painter& painter, Screen& screen, Gfx::DisjointRectSet& flush_rects)
{
int elapsed_ms = m_timer.elapsed();
float progress = min((float)elapsed_ms / (float)m_length, 1.0f);
if (on_update)
on_update(progress, painter, screen, flush_rects);
if (progress >= 1.0f)
stop();
}
}