Skip to content

Commit

Permalink
Merge branch 'chatterino7' of https://github.com/SevenTV/chatterino7
Browse files Browse the repository at this point in the history
…into chatterino7
  • Loading branch information
Nerixyz committed Oct 16, 2023
2 parents ba543e2 + 9fd5cf1 commit c7a11eb
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 146 deletions.
17 changes: 9 additions & 8 deletions src/messages/layouts/MessageLayoutElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,22 +444,23 @@ void TextLayoutElement::paint(QPainter &painter,
text.prepend(RTL_EMBED);
}

const auto font = getApp()->getFonts()->getFont(this->style_, this->scale_);
auto font = getApp()->getFonts()->getFont(this->style_, this->scale_);

const bool isNametag =
this->getLink().type == chatterino::Link::UserInfo ||
this->getLink().type == chatterino::Link::UserWhisper;
const bool drawPaint = isNametag && getSettings()->displaySevenTVPaints;
const auto seventvPaint =
bool isNametag = this->getLink().type == chatterino::Link::UserInfo ||
this->getLink().type == chatterino::Link::UserWhisper;
bool drawPaint = isNametag && getSettings()->displaySevenTVPaints;
auto seventvPaint =
getApp()->seventvPaints->getPaint(this->getLink().value.toLower());
if (drawPaint && seventvPaint.has_value())
{
if (seventvPaint.value()->animated())
{
return;
}

const auto paint = seventvPaint.value();
const auto &paint = seventvPaint.value();

const auto paintPixmap =
auto paintPixmap =
paint->getPixmap(this->getText(), font, this->color_,
this->getRect().size(), this->scale_);

Expand Down
28 changes: 12 additions & 16 deletions src/providers/seventv/paints/LinearGradientPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ LinearGradientPaint::LinearGradientPaint(
bool repeat, float angle, std::vector<PaintDropShadow> dropShadows)
: Paint(std::move(id))
, name_(std::move(name))
, color_(std::move(color))
, color_(color)
, stops_(std::move(stops))
, repeat_(repeat)
, angle_(angle)
Expand Down Expand Up @@ -48,7 +48,7 @@ QBrush LinearGradientPaint::asBrush(const QColor userColor,

QLineF gradientAxis;
gradientAxis.setP1(drawingRect.center());
gradientAxis.setAngle(90.0f - this->angle_);
gradientAxis.setAngle(90.0F - this->angle_);

QLineF colorStartAxis;
colorStartAxis.setP1(startPoint);
Expand All @@ -60,13 +60,8 @@ QBrush LinearGradientPaint::asBrush(const QColor userColor,

QPointF gradientStart;
QPointF gradientEnd;
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
gradientAxis.intersects(colorStartAxis, &gradientStart);
gradientAxis.intersects(colorStopAxis, &gradientEnd);
#else
gradientAxis.intersect(colorStartAxis, &gradientStart);
gradientAxis.intersect(colorStopAxis, &gradientEnd);
#endif

if (this->repeat_)
{
Expand All @@ -77,24 +72,25 @@ QBrush LinearGradientPaint::asBrush(const QColor userColor,

QLinearGradient gradient(gradientStart, gradientEnd);

const auto spread =
auto spread =
this->repeat_ ? QGradient::RepeatSpread : QGradient::PadSpread;
gradient.setSpread(spread);

for (auto const &[position, color] : this->stops_)
for (const auto &[position, color] : this->stops_)
{
const auto combinedColor = this->overlayColors(userColor, color);
const float offsetPosition =
this->repeat_
? this->offsetRepeatingStopPosition(position, this->stops_)
: position;
auto combinedColor =
LinearGradientPaint::overlayColors(userColor, color);
auto offsetPosition =
this->repeat_ ? LinearGradientPaint::offsetRepeatingStopPosition(
position, this->stops_)
: position;
gradient.setColorAt(offsetPosition, combinedColor);
}

return QBrush(gradient);
return {gradient};
}

std::vector<PaintDropShadow> LinearGradientPaint::getDropShadows() const
const std::vector<PaintDropShadow> &LinearGradientPaint::getDropShadows() const
{
return this->dropShadows_;
}
Expand Down
3 changes: 1 addition & 2 deletions src/providers/seventv/paints/LinearGradientPaint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Paint.hpp"

#include <optional>
#include <utility>

namespace chatterino {

Expand All @@ -15,7 +14,7 @@ class LinearGradientPaint : public Paint
std::vector<PaintDropShadow> dropShadows);

QBrush asBrush(QColor userColor, QRectF drawingRect) const override;
std::vector<PaintDropShadow> getDropShadows() const override;
const std::vector<PaintDropShadow> &getDropShadows() const override;
bool animated() const override;

private:
Expand Down
58 changes: 30 additions & 28 deletions src/providers/seventv/paints/Paint.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "providers/seventv/paints/Paint.hpp"

#include "Application.hpp"
#include "common/Literals.hpp"
#include "singletons/Theme.hpp"

#include <QLabel>
#include <QPainter>

namespace chatterino {

QPixmap Paint::getPixmap(const QString text, const QFont font,
const QColor userColor, const QSize size,
const float scale) const
using namespace literals;

QPixmap Paint::getPixmap(const QString &text, const QFont &font,
QColor userColor, QSize size, float scale) const
{
QPixmap pixmap(size);
pixmap.fill(Qt::transparent);
Expand Down Expand Up @@ -45,68 +47,68 @@ QPixmap Paint::getPixmap(const QString text, const QFont font,
for (const auto &shadow : this->getDropShadows())
{
if (!shadow.isValid())
{
continue;
}

// HACK: create a QLabel from the pixmap to apply drop shadows
QLabel label;

auto scaledShadow = shadow.scaled(scale / label.devicePixelRatioF());
auto scaledShadow = shadow.scaled(
scale / static_cast<float>(label.devicePixelRatioF()));

// NOTE: avoid scaling issues on high DPI displays
pixmap.setDevicePixelRatio(label.devicePixelRatioF());

label.setPixmap(pixmap);

auto dropShadow = scaledShadow.getGraphicsEffect();
label.setGraphicsEffect(dropShadow);
QGraphicsDropShadowEffect dropShadow;
scaledShadow.apply(dropShadow);
label.setGraphicsEffect(&dropShadow);

pixmap = label.grab();
pixmap.setDevicePixelRatio(1);

label.deleteLater();
delete dropShadow;
}

if (drawColon)
{
const auto colonColor =
getApp()->getThemes()->messages.textColors.regular;
auto colonColor = getApp()->getThemes()->messages.textColors.regular;

pixmapPainter.begin(&pixmap);

pixmapPainter.setPen(QPen(colonColor));
pixmapPainter.setFont(font);

const QRectF colonBoundingRect(nametagBoundingRect.right(), 0, 10000,
10000);
pixmapPainter.drawText(colonBoundingRect, ":",
QRectF colonBoundingRect(nametagBoundingRect.right(), 0, 10000, 10000);
pixmapPainter.drawText(colonBoundingRect, u":"_s,
QTextOption(Qt::AlignLeft | Qt::AlignTop));
pixmapPainter.end();
}

return pixmap;
}

QColor Paint::overlayColors(const QColor background,
const QColor foreground) const
QColor Paint::overlayColors(QColor background, QColor foreground)
{
const auto alpha = foreground.alphaF();
auto alpha = foreground.alphaF();

const auto r = (1 - alpha) * background.red() + alpha * foreground.red();
const auto g =
(1 - alpha) * background.green() + alpha * foreground.green();
const auto b = (1 - alpha) * background.blue() + alpha * foreground.blue();
auto r = (1 - alpha) * static_cast<float>(background.red()) +
alpha * static_cast<float>(foreground.red());
auto g = (1 - alpha) * static_cast<float>(background.green()) +
alpha * static_cast<float>(foreground.green());
auto b = (1 - alpha) * static_cast<float>(background.blue()) +
alpha * static_cast<float>(foreground.blue());

return QColor(r, g, b);
return {static_cast<int>(r), static_cast<int>(g), static_cast<int>(b)};
}

float Paint::offsetRepeatingStopPosition(const float position,
const QGradientStops stops) const
qreal Paint::offsetRepeatingStopPosition(const qreal position,
const QGradientStops &stops)
{
const float gradientStart = stops.first().first;
const float gradientEnd = stops.last().first;
const float gradientLength = gradientEnd - gradientStart;
const float offsetPosition = (position - gradientStart) / gradientLength;
const qreal gradientStart = stops.first().first;
const qreal gradientEnd = stops.last().first;
const qreal gradientLength = gradientEnd - gradientStart;
const qreal offsetPosition = (position - gradientStart) / gradientLength;

return offsetPosition;
}
Expand Down
18 changes: 11 additions & 7 deletions src/providers/seventv/paints/Paint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ class Paint
{
public:
virtual QBrush asBrush(QColor userColor, QRectF drawingRect) const = 0;
virtual std::vector<PaintDropShadow> getDropShadows() const = 0;
virtual const std::vector<PaintDropShadow> &getDropShadows() const = 0;
virtual bool animated() const = 0;

QPixmap getPixmap(QString text, QFont font, QColor userColor, QSize size,
float scale) const;
QPixmap getPixmap(const QString &text, const QFont &font, QColor userColor,
QSize size, float scale) const;

Paint(QString id)
: id(std::move(id)){};
virtual ~Paint() = default;

Paint(const Paint &) = default;
Paint(Paint &&) = delete;
Paint &operator=(const Paint &) = default;
Paint &operator=(Paint &&) = delete;

QString id;

protected:
QColor overlayColors(const QColor background,
const QColor foreground) const;
float offsetRepeatingStopPosition(const float position,
const QGradientStops stops) const;
static QColor overlayColors(QColor background, QColor foreground);
static qreal offsetRepeatingStopPosition(qreal position,
const QGradientStops &stops);
};

} // namespace chatterino
27 changes: 12 additions & 15 deletions src/providers/seventv/paints/PaintDropShadow.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "PaintDropShadow.hpp"
#include "providers/seventv/paints/PaintDropShadow.hpp"

namespace chatterino {

PaintDropShadow::PaintDropShadow(const float xOffset, const float yOffset,
const float radius, const QColor color)
PaintDropShadow::PaintDropShadow(float xOffset, float yOffset, float radius,
QColor color)
: xOffset_(xOffset)
, yOffset_(yOffset)
, radius_(radius)
Expand All @@ -16,22 +16,19 @@ bool PaintDropShadow::isValid() const
return radius_ > 0;
}

PaintDropShadow PaintDropShadow::scaled(const float scale) const
PaintDropShadow PaintDropShadow::scaled(float scale) const
{
return PaintDropShadow(this->xOffset_ * scale, this->yOffset_ * scale,
this->radius_ * scale, this->color_);
return {this->xOffset_ * scale, this->yOffset_ * scale,
this->radius_ * scale, this->color_};
}

QGraphicsDropShadowEffect *PaintDropShadow::getGraphicsEffect() const
void PaintDropShadow::apply(QGraphicsDropShadowEffect &effect) const
{
auto effect = new QGraphicsDropShadowEffect();

effect->setXOffset(this->xOffset_);
effect->setYOffset(this->yOffset_);
effect->setBlurRadius(this->radius_);
effect->setColor(this->color_);

return effect;
// We can't move here
effect.setXOffset(this->xOffset_);
effect.setYOffset(this->yOffset_);
effect.setBlurRadius(this->radius_);
effect.setColor(this->color_);
}

} // namespace chatterino
7 changes: 3 additions & 4 deletions src/providers/seventv/paints/PaintDropShadow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace chatterino {
class PaintDropShadow
{
public:
PaintDropShadow(const float xOffset, const float yOffset,
const float radius, const QColor color);
PaintDropShadow(float xOffset, float yOffset, float radius, QColor color);

bool isValid() const;
PaintDropShadow scaled(const float scale) const;
QGraphicsDropShadowEffect *getGraphicsEffect() const;
PaintDropShadow scaled(float scale) const;
void apply(QGraphicsDropShadowEffect &effect) const;

private:
const float xOffset_;
Expand Down
26 changes: 12 additions & 14 deletions src/providers/seventv/paints/RadialGradientPaint.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "providers/seventv/paints/RadialGradientPaint.hpp"

#include <algorithm>

namespace chatterino {

RadialGradientPaint::RadialGradientPaint(
Expand All @@ -15,41 +13,41 @@ RadialGradientPaint::RadialGradientPaint(
{
}

QBrush RadialGradientPaint::asBrush(const QColor userColor,
const QRectF drawingRect) const
QBrush RadialGradientPaint::asBrush(QColor userColor, QRectF drawingRect) const
{
const double x = drawingRect.x() + drawingRect.width() / 2;
const double y = drawingRect.y() + drawingRect.height() / 2;
double x = drawingRect.x() + drawingRect.width() / 2;
double y = drawingRect.y() + drawingRect.height() / 2;

double radius = std::max(drawingRect.width(), drawingRect.height()) / 2;
radius = this->repeat_ ? radius * this->stops_.back().first : radius;

QRadialGradient gradient(x, y, radius);

const auto spread =
auto spread =
this->repeat_ ? QGradient::RepeatSpread : QGradient::PadSpread;
gradient.setSpread(spread);

for (const auto &[position, color] : this->stops_)
{
const auto combinedColor = this->overlayColors(userColor, color);
const float offsetPosition =
this->repeat_
? this->offsetRepeatingStopPosition(position, this->stops_)
: position;
auto combinedColor =
RadialGradientPaint::overlayColors(userColor, color);
auto offsetPosition =
this->repeat_ ? RadialGradientPaint::offsetRepeatingStopPosition(
position, this->stops_)
: position;

gradient.setColorAt(offsetPosition, combinedColor);
}

return QBrush(gradient);
return {gradient};
}

bool RadialGradientPaint::animated() const
{
return false;
}

std::vector<PaintDropShadow> RadialGradientPaint::getDropShadows() const
const std::vector<PaintDropShadow> &RadialGradientPaint::getDropShadows() const
{
return this->dropShadows_;
}
Expand Down
Loading

0 comments on commit c7a11eb

Please sign in to comment.