mirror-chatterino2/src/widgets/fancybutton.cpp

142 lines
3.4 KiB
C++
Raw Normal View History

2017-06-11 09:31:45 +02:00
#include "fancybutton.hpp"
2017-04-12 17:46:44 +02:00
#include <QDebug>
#include <QPainter>
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-04-12 17:46:44 +02:00
FancyButton::FancyButton(BaseWidget *parent)
: BaseWidget(parent)
2017-04-12 17:46:44 +02:00
{
connect(&effectTimer, &QTimer::timeout, this, &FancyButton::onMouseEffectTimeout);
2017-04-12 17:46:44 +02:00
this->effectTimer.setInterval(20);
this->effectTimer.start();
2017-04-12 17:46:44 +02:00
}
void FancyButton::setMouseEffectColor(QColor color)
{
this->mouseEffectColor = color;
2017-04-12 17:46:44 +02:00
}
void FancyButton::paintEvent(QPaintEvent *)
{
QPainter painter;
this->fancyPaint(painter);
2017-04-12 17:46:44 +02:00
}
void FancyButton::fancyPaint(QPainter &painter)
{
QColor &c = this->mouseEffectColor;
2017-04-12 17:46:44 +02:00
if (this->hoverMultiplier > 0) {
QRadialGradient gradient(mousePos.x(), mousePos.y(), 50, mousePos.x(), mousePos.y());
2017-04-12 17:46:44 +02:00
2017-08-12 12:09:26 +02:00
gradient.setColorAt(
0, QColor(c.red(), c.green(), c.blue(), (int)(24 * this->hoverMultiplier)));
gradient.setColorAt(
1, QColor(c.red(), c.green(), c.blue(), (int)(12 * this->hoverMultiplier)));
2017-04-12 17:46:44 +02:00
painter.fillRect(this->rect(), gradient);
}
for (auto effect : this->clickEffects) {
2017-04-12 17:46:44 +02:00
QRadialGradient gradient(effect.position.x(), effect.position.y(),
effect.progress * (float)width() * 2, effect.position.x(),
effect.position.y());
gradient.setColorAt(
0, QColor(c.red(), c.green(), c.blue(), (int)((1 - effect.progress) * 95)));
gradient.setColorAt(
0.9999, QColor(c.red(), c.green(), c.blue(), (int)((1 - effect.progress) * 95)));
gradient.setColorAt(1, QColor(c.red(), c.green(), c.blue(), (int)(0)));
painter.fillRect(this->rect(), gradient);
}
}
void FancyButton::enterEvent(QEvent *)
{
this->mouseOver = true;
2017-04-12 17:46:44 +02:00
}
void FancyButton::leaveEvent(QEvent *)
{
this->mouseOver = false;
2017-04-12 17:46:44 +02:00
}
void FancyButton::mousePressEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton) {
return;
}
this->clickEffects.push_back(ClickEffect(event->pos()));
2017-04-12 17:46:44 +02:00
this->mouseDown = true;
2017-04-12 17:46:44 +02:00
}
void FancyButton::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton) {
return;
}
this->mouseDown = false;
2017-09-15 17:23:49 +02:00
if (this->rect().contains(event->pos())) {
emit clicked();
}
2017-04-12 17:46:44 +02:00
}
void FancyButton::mouseMoveEvent(QMouseEvent *event)
{
this->mousePos = event->pos();
2017-04-12 17:46:44 +02:00
}
void FancyButton::onMouseEffectTimeout()
{
bool performUpdate = false;
if (selected) {
if (this->hoverMultiplier != 0) {
this->hoverMultiplier = std::max(0.0, this->hoverMultiplier - 0.1);
2017-04-12 17:46:44 +02:00
performUpdate = true;
}
} else if (mouseOver) {
if (this->hoverMultiplier != 1) {
this->hoverMultiplier = std::min(1.0, this->hoverMultiplier + 0.5);
2017-04-12 17:46:44 +02:00
performUpdate = true;
}
} else {
if (this->hoverMultiplier != 0) {
this->hoverMultiplier = std::max(0.0, this->hoverMultiplier - 0.3);
2017-04-12 17:46:44 +02:00
performUpdate = true;
}
}
if (this->clickEffects.size() != 0) {
2017-04-12 17:46:44 +02:00
performUpdate = true;
for (auto it = this->clickEffects.begin(); it != this->clickEffects.end();) {
(*it).progress += mouseDown ? 0.02 : 0.07;
2017-04-12 17:46:44 +02:00
if ((*it).progress >= 1.0) {
it = this->clickEffects.erase(it);
2017-04-12 17:46:44 +02:00
} else {
it++;
}
}
}
if (performUpdate) {
update();
}
}
} // namespace widgets
} // namespace chatterino