Moved out glare effect to separate file.

This commit is contained in:
23rd 2022-10-11 16:13:20 +03:00 committed by John Preston
parent b7647fbcc1
commit cf54d9fb12
5 changed files with 116 additions and 62 deletions

View File

@ -0,0 +1,76 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/effects/glare.h"
#include "styles/style_boxes.h"
namespace Ui {
namespace {
constexpr auto kMaxGlareOpaque = 0.5;
} // namespace
float64 GlareEffect::progress(crl::time now) const {
return (now - glare.birthTime)
/ float64(glare.deathTime - glare.birthTime);
}
void GlareEffect::validate(
const QColor &color,
Fn<void()> updateCallback,
crl::time timeout,
crl::time duration) {
if (anim::Disabled()) {
return;
}
if (!width) {
width = st::gradientButtonGlareWidth;
}
animation.init([=](crl::time now) {
if (const auto diff = (now - glare.deathTime); diff > 0) {
if (diff > timeout && !paused) {
glare = {
.birthTime = now,
.deathTime = now + duration,
};
updateCallback();
}
} else {
updateCallback();
}
});
animation.start();
{
auto newPixmap = QPixmap(QSize(width, 1)
* style::DevicePixelRatio());
newPixmap.setDevicePixelRatio(style::DevicePixelRatio());
newPixmap.fill(Qt::transparent);
{
auto p = QPainter(&newPixmap);
auto gradient = QLinearGradient(
QPointF(0, 0),
QPointF(width, 0));
auto tempColor = color;
tempColor.setAlphaF(0);
const auto edge = tempColor;
tempColor.setAlphaF(kMaxGlareOpaque);
const auto middle = tempColor;
gradient.setStops({
{ 0., edge },
{ .5, middle },
{ 1., edge },
});
p.fillRect(newPixmap.rect(), QBrush(gradient));
}
pixmap = std::move(newPixmap);
}
}
} // namespace Ui

View File

@ -0,0 +1,30 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Ui {
struct GlareEffect final {
void validate(
const QColor &color,
Fn<void()> updateCallback,
crl::time timeout,
crl::time duration);
[[nodiscard]] float64 progress(crl::time now) const;
Ui::Animations::Basic animation;
struct {
crl::time birthTime = 0;
crl::time deathTime = 0;
} glare;
QPixmap pixmap;
int width = 0;
bool paused = false;
};
} // namespace Ui

View File

@ -11,11 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/style_boxes.h"
namespace Ui {
namespace {
constexpr auto kMaxGlareOpaque = 0.5;
} // namespace
GradientButton::GradientButton(QWidget *widget, QGradientStops stops)
: RippleButton(widget, st::defaultRippleAnimation)
@ -37,8 +32,7 @@ void GradientButton::paintGlare(QPainter &p) {
if (!_glare.glare.birthTime) {
return;
}
const auto progress = (crl::now() - _glare.glare.birthTime)
/ float64(_glare.glare.deathTime - _glare.glare.birthTime);
const auto progress = _glare.progress(crl::now());
const auto x = (-_glare.width) + (width() + _glare.width * 2) * progress;
const auto h = height();
@ -86,49 +80,11 @@ void GradientButton::setGlarePaused(bool paused) {
}
void GradientButton::validateGlare() {
if (anim::Disabled()) {
return;
}
_glare.width = st::gradientButtonGlareWidth;
_glare.animation.init([=](crl::time now) {
if (const auto diff = (now - _glare.glare.deathTime); diff > 0) {
if (diff > st::gradientButtonGlareTimeout && !_glare.paused) {
_glare.glare = Glare{
.birthTime = now,
.deathTime = now + st::gradientButtonGlareDuration,
};
update();
}
} else {
update();
}
});
_glare.animation.start();
{
auto pixmap = QPixmap(QSize(_glare.width, 1)
* style::DevicePixelRatio());
pixmap.setDevicePixelRatio(style::DevicePixelRatio());
pixmap.fill(Qt::transparent);
{
auto p = QPainter(&pixmap);
auto gradient = QLinearGradient(
QPointF(0, 0),
QPointF(_glare.width, 0));
auto color = st::premiumButtonFg->c;
color.setAlphaF(0);
const auto edge = color;
color.setAlphaF(kMaxGlareOpaque);
const auto middle = color;
gradient.setStops({
{ 0., edge },
{ .5, middle },
{ 1., edge },
});
p.fillRect(pixmap.rect(), QBrush(gradient));
}
_glare.pixmap = std::move(pixmap);
}
_glare.validate(
st::premiumButtonFg->c,
[=] { update(); },
st::gradientButtonGlareTimeout,
st::gradientButtonGlareDuration);
}
void GradientButton::startGlareAnimation() {

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#pragma once
#include "ui/widgets/buttons.h"
#include "ui/effects/glare.h"
namespace Ui {
@ -24,21 +25,10 @@ private:
void validateBg();
void validateGlare();
struct Glare final {
crl::time birthTime = 0;
crl::time deathTime = 0;
};
QGradientStops _stops;
QImage _bg;
struct {
Ui::Animations::Basic animation;
Glare glare;
QPixmap pixmap;
int width = 0;
bool paused = false;
} _glare;
GlareEffect _glare;
};

View File

@ -234,6 +234,8 @@ PRIVATE
ui/controls/window_outdated_bar.h
ui/effects/fireworks_animation.cpp
ui/effects/fireworks_animation.h
ui/effects/glare.cpp
ui/effects/glare.h
ui/effects/premium_graphics.cpp
ui/effects/premium_graphics.h
ui/effects/premium_stars.cpp