Remove "custom on success" logic from Image.
Create a new message element and message layout element type for mod badges.
This commit is contained in:
@@ -349,9 +349,6 @@ void Image::actuallyLoad()
|
|||||||
if (!shared)
|
if (!shared)
|
||||||
return Failure;
|
return Failure;
|
||||||
|
|
||||||
if (shared->customOnSuccess_)
|
|
||||||
return shared->customOnSuccess_(shared, result);
|
|
||||||
|
|
||||||
auto data = result.getData();
|
auto data = result.getData();
|
||||||
|
|
||||||
// const cast since we are only reading from it
|
// const cast since we are only reading from it
|
||||||
@@ -396,11 +393,4 @@ bool Image::operator!=(const Image &other) const
|
|||||||
return !this->operator==(other);
|
return !this->operator==(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImagePtr Image::setCustomOnSuccess(
|
|
||||||
std::function<Outcome(ImagePtr, NetworkResult)> customOnSuccess)
|
|
||||||
{
|
|
||||||
this->customOnSuccess_ = customOnSuccess;
|
|
||||||
return shared_from_this();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|||||||
@@ -68,18 +68,13 @@ public:
|
|||||||
bool operator==(const Image &image) const;
|
bool operator==(const Image &image) const;
|
||||||
bool operator!=(const Image &image) const;
|
bool operator!=(const Image &image) const;
|
||||||
|
|
||||||
ImagePtr setCustomOnSuccess(
|
|
||||||
std::function<Outcome(ImagePtr, NetworkResult)> onSuccess);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Image();
|
Image();
|
||||||
Image(const Url &url, qreal scale);
|
Image(const Url &url, qreal scale);
|
||||||
Image(qreal scale);
|
Image(qreal scale);
|
||||||
|
|
||||||
public:
|
|
||||||
void setPixmap(const QPixmap &pixmap);
|
void setPixmap(const QPixmap &pixmap);
|
||||||
|
|
||||||
private:
|
|
||||||
void actuallyLoad();
|
void actuallyLoad();
|
||||||
|
|
||||||
Url url_{};
|
Url url_{};
|
||||||
@@ -88,6 +83,5 @@ private:
|
|||||||
bool shouldLoad_{false};
|
bool shouldLoad_{false};
|
||||||
std::unique_ptr<detail::Frames> frames_{};
|
std::unique_ptr<detail::Frames> frames_{};
|
||||||
QObject object_{};
|
QObject object_{};
|
||||||
std::function<Outcome(ImagePtr, NetworkResult)> customOnSuccess_{};
|
|
||||||
};
|
};
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container,
|
|||||||
QSize(int(container.getScale() * image->width() * emoteScale),
|
QSize(int(container.getScale() * image->width() * emoteScale),
|
||||||
int(container.getScale() * image->height() * emoteScale));
|
int(container.getScale() * image->height() * emoteScale));
|
||||||
|
|
||||||
container.addElement((new ImageLayoutElement(*this, image, size))
|
container.addElement(this->makeImageLayoutElement(image, size)
|
||||||
->setLink(this->getLink()));
|
->setLink(this->getLink()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -159,6 +159,27 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MessageLayoutElement *EmoteElement::makeImageLayoutElement(
|
||||||
|
const ImagePtr &image, const QSize &size)
|
||||||
|
{
|
||||||
|
return new ImageLayoutElement(*this, image, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MOD BADGE
|
||||||
|
ModBadgeElement::ModBadgeElement(const EmotePtr &data,
|
||||||
|
MessageElementFlags flags_)
|
||||||
|
: EmoteElement(data, flags_)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageLayoutElement *ModBadgeElement::makeImageLayoutElement(
|
||||||
|
const ImagePtr &image, const QSize &size)
|
||||||
|
{
|
||||||
|
static const QColor modBadgeBackgroundColor("#34AE0A");
|
||||||
|
return new ImageWithBackgroundLayoutElement(*this, image, size,
|
||||||
|
modBadgeBackgroundColor);
|
||||||
|
}
|
||||||
|
|
||||||
// BADGE
|
// BADGE
|
||||||
BadgeElement::BadgeElement(const EmotePtr &emote, MessageElementFlags flags)
|
BadgeElement::BadgeElement(const EmotePtr &emote, MessageElementFlags flags)
|
||||||
: MessageElement(flags)
|
: MessageElement(flags)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
class Channel;
|
class Channel;
|
||||||
struct MessageLayoutContainer;
|
struct MessageLayoutContainer;
|
||||||
|
class MessageLayoutElement;
|
||||||
|
|
||||||
class Image;
|
class Image;
|
||||||
using ImagePtr = std::shared_ptr<Image>;
|
using ImagePtr = std::shared_ptr<Image>;
|
||||||
@@ -209,11 +210,26 @@ public:
|
|||||||
MessageElementFlags flags_) override;
|
MessageElementFlags flags_) override;
|
||||||
EmotePtr getEmote() const;
|
EmotePtr getEmote() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual MessageLayoutElement *makeImageLayoutElement(const ImagePtr &image,
|
||||||
|
const QSize &size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<TextElement> textElement_;
|
std::unique_ptr<TextElement> textElement_;
|
||||||
EmotePtr emote_;
|
EmotePtr emote_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Behaves like an emote element, except it creates a different image layout element that draws the mod badge background
|
||||||
|
class ModBadgeElement : public EmoteElement
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ModBadgeElement(const EmotePtr &data, MessageElementFlags flags_);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
MessageLayoutElement *makeImageLayoutElement(const ImagePtr &image,
|
||||||
|
const QSize &size) override;
|
||||||
|
};
|
||||||
|
|
||||||
class BadgeElement : public MessageElement
|
class BadgeElement : public MessageElement
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -168,6 +168,33 @@ int ImageLayoutElement::getXFromIndex(int index)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// IMAGE WITH BACKGROUND
|
||||||
|
//
|
||||||
|
ImageWithBackgroundLayoutElement::ImageWithBackgroundLayoutElement(
|
||||||
|
MessageElement &creator, ImagePtr image, const QSize &size, QColor color)
|
||||||
|
: ImageLayoutElement(creator, image, size)
|
||||||
|
, color_(color)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImageWithBackgroundLayoutElement::paint(QPainter &painter)
|
||||||
|
{
|
||||||
|
if (this->image_ == nullptr)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto pixmap = this->image_->pixmapOrLoad();
|
||||||
|
if (pixmap && !this->image_->animated())
|
||||||
|
{
|
||||||
|
painter.fillRect(QRectF(this->getRect()), this->color_);
|
||||||
|
|
||||||
|
// fourtf: make it use qreal values
|
||||||
|
painter.drawPixmap(QRectF(this->getRect()), *pixmap, QRectF());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// TEXT
|
// TEXT
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -72,10 +72,22 @@ protected:
|
|||||||
int getMouseOverIndex(const QPoint &abs) const override;
|
int getMouseOverIndex(const QPoint &abs) const override;
|
||||||
int getXFromIndex(int index) override;
|
int getXFromIndex(int index) override;
|
||||||
|
|
||||||
private:
|
|
||||||
ImagePtr image_;
|
ImagePtr image_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ImageWithBackgroundLayoutElement : public ImageLayoutElement
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ImageWithBackgroundLayoutElement(MessageElement &creator, ImagePtr image,
|
||||||
|
const QSize &size, QColor color);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paint(QPainter &painter) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QColor color_;
|
||||||
|
};
|
||||||
|
|
||||||
// TEXT
|
// TEXT
|
||||||
class TextLayoutElement : public MessageLayoutElement
|
class TextLayoutElement : public MessageLayoutElement
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -15,30 +15,6 @@
|
|||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
namespace {
|
namespace {
|
||||||
Outcome addModBadgeBackground(ImagePtr image, NetworkResult result)
|
|
||||||
{
|
|
||||||
auto data = result.getData();
|
|
||||||
|
|
||||||
QBuffer buffer(const_cast<QByteArray *>(&data));
|
|
||||||
buffer.open(QIODevice::ReadOnly);
|
|
||||||
QImageReader reader(&buffer);
|
|
||||||
if (reader.imageCount() == 0)
|
|
||||||
return Failure;
|
|
||||||
|
|
||||||
QPixmap badgeOverlay = QPixmap::fromImageReader(&reader);
|
|
||||||
QPixmap badgePixmap(badgeOverlay.width(), badgeOverlay.height());
|
|
||||||
|
|
||||||
// the default mod badge green color
|
|
||||||
badgePixmap.fill(QColor("#34AE0A"));
|
|
||||||
QPainter painter(&badgePixmap);
|
|
||||||
QRectF rect(0, 0, badgeOverlay.width(), badgeOverlay.height());
|
|
||||||
painter.drawPixmap(rect, badgeOverlay, rect);
|
|
||||||
|
|
||||||
image->setPixmap(badgePixmap);
|
|
||||||
|
|
||||||
return Success;
|
|
||||||
}
|
|
||||||
|
|
||||||
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
|
Url getEmoteLink(const QJsonObject &urls, const QString &emoteScale)
|
||||||
{
|
{
|
||||||
auto emote = urls.value(emoteScale);
|
auto emote = urls.value(emoteScale);
|
||||||
@@ -123,16 +99,11 @@ namespace {
|
|||||||
auto modBadge3x = getEmoteLink(modUrls, "4");
|
auto modBadge3x = getEmoteLink(modUrls, "4");
|
||||||
|
|
||||||
auto modBadgeImageSet = ImageSet{
|
auto modBadgeImageSet = ImageSet{
|
||||||
Image::fromUrl(modBadge1x, 1)
|
Image::fromUrl(modBadge1x, 1),
|
||||||
->setCustomOnSuccess(addModBadgeBackground),
|
modBadge2x.string.isEmpty() ? Image::getEmpty()
|
||||||
modBadge2x.string.isEmpty()
|
: Image::fromUrl(modBadge2x, 0.5),
|
||||||
? Image::getEmpty()
|
modBadge3x.string.isEmpty() ? Image::getEmpty()
|
||||||
: Image::fromUrl(modBadge2x, 0.5)
|
: Image::fromUrl(modBadge3x, 0.25),
|
||||||
->setCustomOnSuccess(addModBadgeBackground),
|
|
||||||
modBadge3x.string.isEmpty()
|
|
||||||
? Image::getEmpty()
|
|
||||||
: Image::fromUrl(modBadge3x, 0.25)
|
|
||||||
->setCustomOnSuccess(addModBadgeBackground),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
modBadge = std::make_shared<Emote>(Emote{
|
modBadge = std::make_shared<Emote>(Emote{
|
||||||
|
|||||||
@@ -1171,7 +1171,7 @@ void TwitchMessageBuilder::appendTwitchBadges()
|
|||||||
{
|
{
|
||||||
if (auto customModBadge = this->twitchChannel->ffzCustomModBadge())
|
if (auto customModBadge = this->twitchChannel->ffzCustomModBadge())
|
||||||
{
|
{
|
||||||
this->emplace<BadgeElement>(
|
this->emplace<ModBadgeElement>(
|
||||||
customModBadge.get(),
|
customModBadge.get(),
|
||||||
MessageElementFlag::BadgeChannelAuthority)
|
MessageElementFlag::BadgeChannelAuthority)
|
||||||
->setTooltip((*customModBadge)->tooltip.string);
|
->setTooltip((*customModBadge)->tooltip.string);
|
||||||
|
|||||||
Reference in New Issue
Block a user