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
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
FancyButton::FancyButton(BaseWidget *parent)
|
|
|
|
: BaseWidget(parent)
|
2017-04-12 17:46:44 +02:00
|
|
|
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
connect(&effectTimer, &QTimer::timeout, this, &FancyButton::onMouseEffectTimeout);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->effectTimer.setInterval(20);
|
|
|
|
this->effectTimer.start();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::setMouseEffectColor(QColor color)
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
this->mouseEffectColor = color;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::paintEvent(QPaintEvent *)
|
|
|
|
{
|
2017-09-16 00:05:06 +02:00
|
|
|
QPainter painter(this);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->fancyPaint(painter);
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::fancyPaint(QPainter &painter)
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
QColor &c = this->mouseEffectColor;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-26 16:41:20 +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);
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
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 *)
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
this->mouseOver = true;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::leaveEvent(QEvent *)
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
this->mouseOver = false;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->clickEffects.push_back(ClickEffect(event->pos()));
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
this->mouseDown = true;
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-06-26 16:41:20 +02:00
|
|
|
this->mousePos = event->pos();
|
2017-04-12 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyButton::onMouseEffectTimeout()
|
|
|
|
{
|
|
|
|
bool performUpdate = false;
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-06-26 16:41:20 +02:00
|
|
|
} 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 {
|
2017-06-26 16:41:20 +02:00
|
|
|
if (this->hoverMultiplier != 0) {
|
|
|
|
this->hoverMultiplier = std::max(0.0, this->hoverMultiplier - 0.3);
|
2017-04-12 17:46:44 +02:00
|
|
|
performUpdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
if (this->clickEffects.size() != 0) {
|
2017-04-12 17:46:44 +02:00
|
|
|
performUpdate = true;
|
|
|
|
|
2017-06-26 16:41:20 +02:00
|
|
|
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) {
|
2017-06-26 16:41:20 +02:00
|
|
|
it = this->clickEffects.erase(it);
|
2017-04-12 17:46:44 +02:00
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (performUpdate) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 10:09:24 +02:00
|
|
|
|
|
|
|
} // namespace widgets
|
|
|
|
} // namespace chatterino
|