2016-11-15 11:56:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-11-15 11:56:49 +00:00
|
|
|
*/
|
|
|
|
#include "ui/effects/ripple_animation.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
class RippleAnimation::Ripple {
|
|
|
|
public:
|
2018-10-25 10:02:37 +00:00
|
|
|
Ripple(const style::RippleAnimation &st, QPoint origin, int startRadius, const QPixmap &mask, Fn<void()> update);
|
|
|
|
Ripple(const style::RippleAnimation &st, const QPixmap &mask, Fn<void()> update);
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void paint(QPainter &p, const QPixmap &mask, crl::time ms, const QColor *colorOverride);
|
2016-11-15 11:56:49 +00:00
|
|
|
|
|
|
|
void stop();
|
2016-11-16 12:06:02 +00:00
|
|
|
void unstop();
|
|
|
|
void finish();
|
2018-10-25 10:02:37 +00:00
|
|
|
void clearCache();
|
2016-11-15 11:56:49 +00:00
|
|
|
bool finished() const {
|
|
|
|
return _hiding && !_hide.animating();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const style::RippleAnimation &_st;
|
2018-10-25 10:02:37 +00:00
|
|
|
Fn<void()> _update;
|
2016-11-15 11:56:49 +00:00
|
|
|
|
|
|
|
QPoint _origin;
|
|
|
|
int _radiusFrom = 0;
|
|
|
|
int _radiusTo = 0;
|
|
|
|
|
|
|
|
bool _hiding = false;
|
2016-12-07 13:32:25 +00:00
|
|
|
Animation _show;
|
|
|
|
Animation _hide;
|
2016-11-15 11:56:49 +00:00
|
|
|
QPixmap _cache;
|
|
|
|
QImage _frame;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
RippleAnimation::Ripple::Ripple(const style::RippleAnimation &st, QPoint origin, int startRadius, const QPixmap &mask, Fn<void()> update)
|
2016-11-15 11:56:49 +00:00
|
|
|
: _st(st)
|
|
|
|
, _update(update)
|
|
|
|
, _origin(origin)
|
|
|
|
, _radiusFrom(startRadius)
|
|
|
|
, _frame(mask.size(), QImage::Format_ARGB32_Premultiplied) {
|
|
|
|
_frame.setDevicePixelRatio(mask.devicePixelRatio());
|
|
|
|
|
|
|
|
QPoint points[] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ _frame.width() / cIntRetinaFactor(), 0 },
|
|
|
|
{ _frame.width() / cIntRetinaFactor(), _frame.height() / cIntRetinaFactor() },
|
|
|
|
{ 0, _frame.height() / cIntRetinaFactor() },
|
|
|
|
};
|
|
|
|
for (auto point : points) {
|
|
|
|
accumulate_max(_radiusTo, style::point::dotProduct(_origin - point, _origin - point));
|
|
|
|
}
|
|
|
|
_radiusTo = qRound(sqrt(_radiusTo));
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
_show.start(_update, 0., 1., _st.showDuration, anim::easeOutQuint);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
RippleAnimation::Ripple::Ripple(const style::RippleAnimation &st, const QPixmap &mask, Fn<void()> update)
|
2016-11-16 12:06:02 +00:00
|
|
|
: _st(st)
|
|
|
|
, _update(update)
|
|
|
|
, _origin(mask.width() / (2 * cIntRetinaFactor()), mask.height() / (2 * cIntRetinaFactor()))
|
|
|
|
, _radiusFrom(mask.width() + mask.height())
|
|
|
|
, _frame(mask.size(), QImage::Format_ARGB32_Premultiplied) {
|
|
|
|
_frame.setDevicePixelRatio(mask.devicePixelRatio());
|
|
|
|
_radiusTo = _radiusFrom;
|
2018-10-25 10:02:37 +00:00
|
|
|
_hide.start(_update, 0., 1., _st.hideDuration);
|
2016-11-16 12:06:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void RippleAnimation::Ripple::paint(QPainter &p, const QPixmap &mask, crl::time ms, const QColor *colorOverride) {
|
2016-11-15 11:56:49 +00:00
|
|
|
auto opacity = _hide.current(ms, _hiding ? 0. : 1.);
|
|
|
|
if (opacity == 0.) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_cache.isNull() || colorOverride != nullptr) {
|
2016-11-15 11:56:49 +00:00
|
|
|
auto radius = anim::interpolate(_radiusFrom, _radiusTo, _show.current(ms, 1.));
|
|
|
|
_frame.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&_frame);
|
|
|
|
p.setPen(Qt::NoPen);
|
2016-12-02 19:16:35 +00:00
|
|
|
if (colorOverride) {
|
|
|
|
p.setBrush(*colorOverride);
|
|
|
|
} else {
|
|
|
|
p.setBrush(_st.color);
|
|
|
|
}
|
2016-12-03 12:10:35 +00:00
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawEllipse(_origin, radius, radius);
|
|
|
|
}
|
2016-11-15 11:56:49 +00:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
p.drawPixmap(0, 0, mask);
|
|
|
|
}
|
2016-12-05 11:01:08 +00:00
|
|
|
if (radius == _radiusTo && colorOverride == nullptr) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_cache = App::pixmapFromImageInPlace(std::move(_frame));
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto saved = p.opacity();
|
|
|
|
if (opacity != 1.) p.setOpacity(saved * opacity);
|
|
|
|
if (_cache.isNull()) {
|
|
|
|
p.drawImage(0, 0, _frame);
|
|
|
|
} else {
|
|
|
|
p.drawPixmap(0, 0, _cache);
|
|
|
|
}
|
|
|
|
if (opacity != 1.) p.setOpacity(saved);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RippleAnimation::Ripple::stop() {
|
|
|
|
_hiding = true;
|
2018-10-25 10:02:37 +00:00
|
|
|
_hide.start(_update, 1., 0., _st.hideDuration);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 12:06:02 +00:00
|
|
|
void RippleAnimation::Ripple::unstop() {
|
|
|
|
if (_hiding) {
|
|
|
|
if (_hide.animating()) {
|
2018-10-25 10:02:37 +00:00
|
|
|
_hide.start(_update, 0., 1., _st.hideDuration);
|
2016-11-16 12:06:02 +00:00
|
|
|
}
|
|
|
|
_hiding = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RippleAnimation::Ripple::finish() {
|
|
|
|
if (_update) {
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
_show.finish();
|
|
|
|
_hide.finish();
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
void RippleAnimation::Ripple::clearCache() {
|
|
|
|
_cache = QPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
RippleAnimation::RippleAnimation(const style::RippleAnimation &st, QImage mask, Fn<void()> callback)
|
2016-11-15 11:56:49 +00:00
|
|
|
: _st(st)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _mask(App::pixmapFromImageInPlace(std::move(mask)))
|
2016-11-20 12:54:07 +00:00
|
|
|
, _update(callback) {
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RippleAnimation::add(QPoint origin, int startRadius) {
|
2016-11-16 12:06:02 +00:00
|
|
|
lastStop();
|
2018-10-25 10:02:37 +00:00
|
|
|
_ripples.push_back(std::make_unique<Ripple>(_st, origin, startRadius, _mask, _update));
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 12:06:02 +00:00
|
|
|
void RippleAnimation::addFading() {
|
|
|
|
lastStop();
|
2018-10-25 10:02:37 +00:00
|
|
|
_ripples.push_back(std::make_unique<Ripple>(_st, _mask, _update));
|
2016-11-16 12:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RippleAnimation::lastStop() {
|
2018-10-25 10:02:37 +00:00
|
|
|
if (!_ripples.empty()) {
|
2016-11-15 11:56:49 +00:00
|
|
|
_ripples.back()->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:06:02 +00:00
|
|
|
void RippleAnimation::lastUnstop() {
|
2018-10-25 10:02:37 +00:00
|
|
|
if (!_ripples.empty()) {
|
2016-11-16 12:06:02 +00:00
|
|
|
_ripples.back()->unstop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RippleAnimation::lastFinish() {
|
2018-10-25 10:02:37 +00:00
|
|
|
if (!_ripples.empty()) {
|
2016-11-16 12:06:02 +00:00
|
|
|
_ripples.back()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
void RippleAnimation::forceRepaint() {
|
|
|
|
for (const auto &ripple : _ripples) {
|
|
|
|
ripple->clearCache();
|
|
|
|
}
|
|
|
|
if (_update) {
|
|
|
|
_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void RippleAnimation::paint(QPainter &p, int x, int y, int outerWidth, crl::time ms, const QColor *colorOverride) {
|
2018-10-25 10:02:37 +00:00
|
|
|
if (_ripples.empty()) {
|
2016-11-15 11:56:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rtl()) x = outerWidth - x - (_mask.width() / cIntRetinaFactor());
|
|
|
|
p.translate(x, y);
|
2018-10-25 10:02:37 +00:00
|
|
|
for (const auto &ripple : _ripples) {
|
2016-12-02 19:16:35 +00:00
|
|
|
ripple->paint(p, _mask, ms, colorOverride);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
p.translate(-x, -y);
|
|
|
|
clearFinished();
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
QImage RippleAnimation::maskByDrawer(QSize size, bool filled, Fn<void(QPainter &p)> drawer) {
|
2016-11-16 16:04:25 +00:00
|
|
|
auto result = QImage(size * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
2016-12-05 08:45:56 +00:00
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
2016-11-16 16:04:25 +00:00
|
|
|
result.fill(filled ? QColor(255, 255, 255) : Qt::transparent);
|
|
|
|
if (drawer) {
|
|
|
|
Painter p(&result);
|
2016-12-03 12:10:35 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(QColor(255, 255, 255));
|
|
|
|
drawer(p);
|
|
|
|
}
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage RippleAnimation::rectMask(QSize size) {
|
2018-06-04 15:35:11 +00:00
|
|
|
return maskByDrawer(size, true, Fn<void(QPainter&)>());
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage RippleAnimation::roundRectMask(QSize size, int radius) {
|
|
|
|
return maskByDrawer(size, false, [size, radius](QPainter &p) {
|
|
|
|
p.drawRoundedRect(0, 0, size.width(), size.height(), radius, radius);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage RippleAnimation::ellipseMask(QSize size) {
|
|
|
|
return maskByDrawer(size, false, [size](QPainter &p) {
|
|
|
|
p.drawEllipse(0, 0, size.width(), size.height());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
void RippleAnimation::clearFinished() {
|
2018-10-25 10:02:37 +00:00
|
|
|
while (!_ripples.empty() && _ripples.front()->finished()) {
|
2016-11-15 11:56:49 +00:00
|
|
|
_ripples.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RippleAnimation::clear() {
|
2018-10-25 10:02:37 +00:00
|
|
|
_ripples.clear();
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:02:37 +00:00
|
|
|
RippleAnimation::~RippleAnimation() = default;
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
} // namespace Ui
|