From 4dbe2b02543b143197314955e51a4d9382e5ff19 Mon Sep 17 00:00:00 2001 From: fourtf Date: Fri, 13 Jan 2017 18:59:11 +0100 Subject: [PATCH] added emote loading --- .vs/chatterino/v14/.suo | Bin 9728 -> 0 bytes channel.cpp | 4 +- channel.h | 8 +- chatterino.pro | 11 ++- chatwidgetview.cpp | 19 +++-- emojis.cpp | 2 +- emojis.h | 2 +- emotes.cpp | 70 ++++++++-------- emotes.h | 54 +++++++------ ircmanager.cpp | 1 + lazyloadedimage.cpp | 29 ++++--- lazyloadedimage.h | 26 +++--- main.cpp | 11 ++- message.cpp | 174 ++++++++++++++++++++++++---------------- resources.cpp | 46 +++++++++++ resources.h | 110 +++++++++++++++++++++++++ word.h | 10 +-- wordpart.h | 6 ++ 18 files changed, 402 insertions(+), 181 deletions(-) delete mode 100644 .vs/chatterino/v14/.suo create mode 100644 resources.cpp create mode 100644 resources.h diff --git a/.vs/chatterino/v14/.suo b/.vs/chatterino/v14/.suo deleted file mode 100644 index 8379e15cbc748f3437a5e1f7d0f8dd852cb71ef5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9728 zcmeHNeQZ=k5T7d`RuqfUqO~fmPzhkuV!t$@X{1eQtWrSu$A+e~*Ms(YEq8@l8a1N9 zL{j?)L^QUFF);)Qi4o#gVnh=)glJ<-h$J<|#IN9Iv}uB&_4nI*dwq9(@7`T0XKQX> z_GWfwcV~BYW_RAc8~k$7@M~{nUXX}XCUazLJXNkS(Oo87(nRq{8yg=VCyO}HWST`9 z*d*yvfHR<2bMwpbzo99 zlU=x18q)&ci;&4rrrD{1MEZaE#X}+g7aAUt_P;Ce z|ByX$1%kas|GgXRr2hwXq^*|wC-(uf-;*~FbnV|a04GiF0ZIR-HfDFy@f+8eX96M5 zaqjoD36|-qTx;(CEH4D)8BRN3`6j@dk$JANoB_DRz;6Z2H1I6I+YS6qz@-MB19+E# zF9*yuaQ2lyj)SNHmQ)YTWrOiuH^H(1T~m-?q%oop+b7en|FgmW8N(#~e`@|8v>jRm zy@;A*+Ja1ceLl87^K(zAWMD^J>*Ri^LyLgAxeNer!aXJ5CfvBetdk1dS2p4;qYY)= zFtbJ#xSfE!@8s=fr?ld?UAj=SLp@&9)4Ue6ZW0gLvp2VFN7)0sP4!%lUi@h1g;bBa zFQp_uZcKxHo!L-zIZ)&JdVQHI#q;gq3eL~$F~n8ZyU<|+gy+c)86NIsbcx#CL>Nc|oCnsOEHi_DJ)SKU6(_@Ql^$KCjN2e{Su@?*>j~sIZbNKljw% zy}y?Kle;!&@8Wk>j19!Q{!b)!`Q6IsTVV15Wv6`$f2^){r~mTyK>Ew8Pn=t2HEq&t z6Lm0=;$Qza61@KQ{?A>Z84rD*I|IL@ctiH_{fX6GR_i}8ckBXF@c-Gq{{M&bepW)Y{k#1{N!7QBXbJ=KXc-r4orhz z@)>RM+p#|PM{@k#eYK<6E0&L*?>}^U6ID8~>phd;{hP5_01sm|cW8_i<$r$iz}bC| zJos1s%8UJPs=pvij4_B=o5H>Xk3V<-^M3(6fP)ccS;@mEf3Gen?mh6H@1<>ix|B)7 z(MB)w-;7@e5W(^HAF1ei(N)&^?(?HV&mQl2btaFWZwb`?cEEPLH85BYlwKNYdFaDq z-i0qb-Bb1(uh(PNe}d2N$nbSZY2~VnE!!@nW~QC@WHt52Gj!IKN%5NlD diff --git a/channel.cpp b/channel.cpp index 718471127..68213072d 100644 --- a/channel.cpp +++ b/channel.cpp @@ -9,11 +9,11 @@ Channel Channel::mentions(QString("/mentions")); QMap Channel::channels = QMap(); Channel::Channel(QString channel) - : m_name((channel.length() > 0 && channel[0] == '#') ? channel.mid(1) + : m_messages() + , m_name((channel.length() > 0 && channel[0] == '#') ? channel.mid(1) : channel) , m_bttvChannelEmotes() , m_ffzChannelEmotes() - , m_messages() , m_messageMutex() , m_subLink("https://www.twitch.tv/" + m_name + "/subscribe?ref=in_chat_subscriber_link") diff --git a/channel.h b/channel.h index 09cdfde67..a8ea9e974 100644 --- a/channel.h +++ b/channel.h @@ -104,10 +104,6 @@ public: private: Channel(QString channel); - ConcurrentMap m_bttvChannelEmotes; - ConcurrentMap m_ffzChannelEmotes; - QMutex m_messageMutex; - int m_referenceCount = 0; QVector> m_messages; @@ -115,6 +111,10 @@ private: QString m_name; int m_roomID; + ConcurrentMap m_bttvChannelEmotes; + ConcurrentMap m_ffzChannelEmotes; + QMutex m_messageMutex; + QString m_subLink; QString m_channelLink; QString m_popoutPlayerLink; diff --git a/chatterino.pro b/chatterino.pro index de419f370..535ef437e 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -8,6 +8,9 @@ QT += core gui network CONFIG += communi COMMUNI += core model util +win32:LIBS += -LC:/OpenSSL-Win32/lib/openssl.lib +INCLUDEPATH += C:/OpenSSL-Win32/include + include(lib/libcommuni/src/src.pri) greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -28,7 +31,7 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += main.cpp\ - mainwindow.cpp \ + mainwindow.cpp \ chatwidget.cpp \ notebook.cpp \ notebooktab.cpp \ @@ -57,7 +60,8 @@ SOURCES += main.cpp\ fonts.cpp \ appsettings.cpp \ emojis.cpp \ - wordpart.cpp + wordpart.cpp \ + resources.cpp HEADERS += mainwindow.h \ chatwidget.h \ @@ -91,7 +95,8 @@ HEADERS += mainwindow.h \ appsettings.h \ emojis.h \ wordpart.h \ - common.h + common.h \ + resources.h PRECOMPILED_HEADER = diff --git a/chatwidgetview.cpp b/chatwidgetview.cpp index 7d5061886..3405cf32d 100644 --- a/chatwidgetview.cpp +++ b/chatwidgetview.cpp @@ -32,7 +32,8 @@ ChatWidgetView::resizeEvent(QResizeEvent *) auto messages = c->getMessagesClone(); for (std::shared_ptr &message : messages) { - message.get()->layout(width(), true); + qInfo(QString::number(width()).toStdString().c_str()); + message.get()->layout(this->width(), true); } } @@ -48,18 +49,22 @@ ChatWidgetView::paintEvent(QPaintEvent *) auto messages = c->getMessagesClone(); - int y = 0; + int y = 32; for (std::shared_ptr const &message : messages) { for (WordPart const &wordPart : message.get()->wordParts()) { + painter.setPen(QColor(255, 0, 0)); + painter.drawRect(wordPart.x(), wordPart.y() + y, wordPart.width(), + wordPart.height()); + // image if (wordPart.word().isImage()) { LazyLoadedImage &lli = wordPart.word().getImage(); - const QImage *image = lli.image(); + const QPixmap *image = lli.pixmap(); if (image != NULL) { - painter.drawImage( + painter.drawPixmap( QRect(wordPart.x(), wordPart.y() + y, wordPart.width(), wordPart.height()), *image); @@ -70,8 +75,10 @@ ChatWidgetView::paintEvent(QPaintEvent *) painter.setPen(wordPart.word().color()); painter.setFont(wordPart.word().getFont()); - painter.drawText(wordPart.x(), wordPart.y() + y, - wordPart.getText()); + painter.drawText( + QRectF(wordPart.x(), wordPart.y() + y, 10000, 10000), + wordPart.getText(), + QTextOption(Qt::AlignLeft | Qt::AlignTop)); } } diff --git a/emojis.cpp b/emojis.cpp index f6657c27c..18bc37680 100644 --- a/emojis.cpp +++ b/emojis.cpp @@ -76,7 +76,7 @@ Emojis::parseEmojis(std::vector> &vector, } void -Emojis::initEmojis() +Emojis::loadEmojis() { QFile file(":/emojidata.txt"); file.open(QFile::ReadOnly); diff --git a/emojis.h b/emojis.h index 469f958fb..42421a3c9 100644 --- a/emojis.h +++ b/emojis.h @@ -16,7 +16,7 @@ public: std::vector> &vector, const QString &text); - static void initEmojis(); + static void loadEmojis(); static QString replaceShortCodes(const QString &text); diff --git a/emotes.cpp b/emotes.cpp index edc2a9f1f..17d75cebd 100644 --- a/emotes.cpp +++ b/emotes.cpp @@ -1,35 +1,17 @@ #include "emotes.h" +#include "resources.h" -ConcurrentMap *Emotes::m_twitchEmotes = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_bttvEmotes = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_ffzEmotes = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_chatterinoEmotes = - new ConcurrentMap(); -ConcurrentMap - *Emotes::m_bttvChannelEmoteFromCaches = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_ffzChannelEmoteFromCaches = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_twitchEmoteFromCache = - new ConcurrentMap(); -ConcurrentMap *Emotes::m_miscImageFromCache = - new ConcurrentMap(); +QString Emotes::m_twitchEmoteTemplate( + "http://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}.0"); -LazyLoadedImage *Emotes::m_cheerBadge100000 = - new LazyLoadedImage(new QImage(":/cheer100000")); -LazyLoadedImage *Emotes::m_cheerBadge10000 = - new LazyLoadedImage(new QImage(":/cheer10000")); -LazyLoadedImage *Emotes::m_cheerBadge5000 = - new LazyLoadedImage(new QImage(":/cheer5000")); -LazyLoadedImage *Emotes::m_cheerBadge1000 = - new LazyLoadedImage(new QImage(":/cheer1000")); -LazyLoadedImage *Emotes::m_cheerBadge100 = - new LazyLoadedImage(new QImage(":/cheer100")); -LazyLoadedImage *Emotes::m_cheerBadge1 = - new LazyLoadedImage(new QImage(":/cheer1")); +ConcurrentMap Emotes::m_twitchEmotes; +ConcurrentMap Emotes::m_bttvEmotes; +ConcurrentMap Emotes::m_ffzEmotes; +ConcurrentMap Emotes::m_chatterinoEmotes; +ConcurrentMap Emotes::m_bttvChannelEmoteFromCaches; +ConcurrentMap Emotes::m_ffzChannelEmoteFromCaches; +ConcurrentMap Emotes::m_twitchEmoteFromCache; +ConcurrentMap Emotes::m_miscImageFromCache; Emotes::Emotes() { @@ -38,9 +20,21 @@ Emotes::Emotes() LazyLoadedImage * Emotes::getTwitchEmoteById(const QString &name, long id) { -#pragma message WARN("xD") - return new LazyLoadedImage(NULL); - // return m_twitchEmoteFromCache->getOrAdd() + return m_twitchEmoteFromCache.getOrAdd(id, [&name, id] { + qreal scale; + QString url = getTwitchEmoteLink(id, scale); + + return new LazyLoadedImage(url, scale, name, name + "\nTwitch Emote"); + }); +} + +QString +Emotes::getTwitchEmoteLink(long id, qreal &scale) +{ + scale = .5; + + return m_twitchEmoteTemplate.replace("{id}", QString::number(id)) + .replace("{scale}", "2"); } LazyLoadedImage * @@ -54,16 +48,16 @@ LazyLoadedImage * Emotes::getCheerBadge(long long amount) { if (amount >= 100000) { - return m_cheerBadge100000; + return Resources::cheerBadge100000(); } else if (amount >= 10000) { - return m_cheerBadge10000; + return Resources::cheerBadge10000(); } else if (amount >= 5000) { - return m_cheerBadge5000; + return Resources::cheerBadge5000(); } else if (amount >= 1000) { - return m_cheerBadge1000; + return Resources::cheerBadge1000(); } else if (amount >= 100) { - return m_cheerBadge100; + return Resources::cheerBadge100(); } else { - return m_cheerBadge1; + return Resources::cheerBadge1(); } } diff --git a/emotes.h b/emotes.h index f716c7712..87d911da8 100644 --- a/emotes.h +++ b/emotes.h @@ -13,42 +13,49 @@ public: static ConcurrentMap & twitchEmotes() { - return *m_twitchEmotes; + return m_twitchEmotes; } + static ConcurrentMap & bttvEmotes() { - return *m_bttvEmotes; + return m_bttvEmotes; } + static ConcurrentMap & ffzEmotes() { - return *m_ffzEmotes; + return m_ffzEmotes; } + static ConcurrentMap & chatterinoEmotes() { - return *m_chatterinoEmotes; + return m_chatterinoEmotes; } + static ConcurrentMap & bttvChannelEmoteFromCaches() { - return *m_bttvChannelEmoteFromCaches; + return m_bttvChannelEmoteFromCaches; } + static ConcurrentMap & ffzChannelEmoteFromCaches() { - return *m_ffzChannelEmoteFromCaches; + return m_ffzChannelEmoteFromCaches; } - static ConcurrentMap & + + static ConcurrentMap & twitchEmoteFromCache() { - return *m_twitchEmoteFromCache; + return m_twitchEmoteFromCache; } + static ConcurrentMap & miscImageFromCache() { - return *m_miscImageFromCache; + return m_miscImageFromCache; } static void loadGlobalEmotes(); @@ -62,23 +69,20 @@ public: private: Emotes(); - static ConcurrentMap *m_twitchEmotes; - static ConcurrentMap *m_bttvEmotes; - static ConcurrentMap *m_ffzEmotes; - static ConcurrentMap *m_chatterinoEmotes; - static ConcurrentMap - *m_bttvChannelEmoteFromCaches; - static ConcurrentMap - *m_ffzChannelEmoteFromCaches; - static ConcurrentMap *m_twitchEmoteFromCache; - static ConcurrentMap *m_miscImageFromCache; + static QString m_twitchEmoteTemplate; - static LazyLoadedImage *m_cheerBadge100000; - static LazyLoadedImage *m_cheerBadge10000; - static LazyLoadedImage *m_cheerBadge5000; - static LazyLoadedImage *m_cheerBadge1000; - static LazyLoadedImage *m_cheerBadge100; - static LazyLoadedImage *m_cheerBadge1; + static ConcurrentMap m_twitchEmotes; + static ConcurrentMap m_bttvEmotes; + static ConcurrentMap m_ffzEmotes; + static ConcurrentMap m_chatterinoEmotes; + static ConcurrentMap + m_bttvChannelEmoteFromCaches; + static ConcurrentMap + m_ffzChannelEmoteFromCaches; + static ConcurrentMap m_twitchEmoteFromCache; + static ConcurrentMap m_miscImageFromCache; + + static QString getTwitchEmoteLink(long id, qreal &scale); }; #endif // EMOTES_H diff --git a/ircmanager.cpp b/ircmanager.cpp index ab16e6210..44a2edeb9 100644 --- a/ircmanager.cpp +++ b/ircmanager.cpp @@ -121,6 +121,7 @@ IrcManager::beginConnecting() c->setRealName("justinfan123"); c->sendRaw("JOIN #fourtf"); c->sendRaw("JOIN #ian678"); + c->sendRaw("JOIN #nuuls"); c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/commands")); c->sendCommand(IrcCommand::createCapability("REQ", "twitch.tv/tags")); diff --git a/lazyloadedimage.cpp b/lazyloadedimage.cpp index 603c322bb..83da3b5ff 100644 --- a/lazyloadedimage.cpp +++ b/lazyloadedimage.cpp @@ -11,7 +11,7 @@ LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, const QString &name, const QString &tooltip, const QMargins &margin, bool isHat) - : m_image(NULL) + : m_pixmap(NULL) , m_url(url) , m_name(name) , m_tooltip(tooltip) @@ -23,16 +23,17 @@ LazyLoadedImage::LazyLoadedImage(const QString &url, qreal scale, { } -LazyLoadedImage::LazyLoadedImage(QImage *image, qreal scale, +LazyLoadedImage::LazyLoadedImage(QPixmap *image, qreal scale, const QString &name, const QString &tooltip, const QMargins &margin, bool isHat) - : m_name(name) + : m_pixmap(image) + , m_url() + , m_name(name) , m_tooltip(tooltip) , m_animated(false) , m_margin(margin) , m_ishat(isHat) , m_scale(scale) - , m_image(image) , m_isLoading(true) { } @@ -40,13 +41,21 @@ LazyLoadedImage::LazyLoadedImage(QImage *image, qreal scale, void LazyLoadedImage::loadImage() { - QString url = m_url; + // QThreadPool::globalInstance()->start(new LambdaQRunnable([=] { + QUrl url(m_url); + QNetworkRequest request(url); - async_exec([url] { - QNetworkRequest req(QUrl(url)); + QNetworkReply *reply = IrcManager::accessManager().get(request); - QNetworkReply *reply = IrcManager::accessManager().get(req); + QObject::connect(reply, &QNetworkReply::finished, [=] { + QPixmap *pixmap = new QPixmap(); + pixmap->loadFromData(reply->readAll()); - QObject::connect(reply, &QNetworkReply::finished, [=] {}); - }) + if (pixmap->isNull()) { + return; + } + + m_pixmap = pixmap; + }); + //})); } diff --git a/lazyloadedimage.h b/lazyloadedimage.h index 289c301c4..4c77d31d4 100644 --- a/lazyloadedimage.h +++ b/lazyloadedimage.h @@ -1,7 +1,7 @@ #ifndef LAZYLOADEDIMAGE_H #define LAZYLOADEDIMAGE_H -#include +#include #include class LazyLoadedImage @@ -10,19 +10,19 @@ public: LazyLoadedImage(const QString &url, qreal scale = 1, const QString &name = "", const QString &tooltip = "", const QMargins &margin = QMargins(), bool isHat = false); - LazyLoadedImage(QImage *image, qreal scale = 1, const QString &name = "", + LazyLoadedImage(QPixmap *pixmap, qreal scale = 1, const QString &name = "", const QString &tooltip = "", const QMargins &margin = QMargins(), bool isHat = false); - const QImage * - image() + const QPixmap * + pixmap() { if (!m_isLoading) { m_isLoading = true; loadImage(); } - return m_image; + return m_pixmap; } qreal @@ -67,27 +67,26 @@ public: return m_ishat; } - const long + int width() const { - if (m_image == NULL) { + if (m_pixmap == NULL) { return 16; } - return m_image->width(); + return m_pixmap->width(); } - const long + int height() const { - if (m_image == NULL) { + if (m_pixmap == NULL) { return 16; } - return m_image->height(); + return m_pixmap->height(); } private: - QImage *m_image; - qreal m_scale; + QPixmap *m_pixmap; QString m_url; QString m_name; @@ -95,6 +94,7 @@ private: bool m_animated; QMargins m_margin; bool m_ishat; + qreal m_scale; bool m_isLoading; diff --git a/main.cpp b/main.cpp index c1106f5b9..082c6c8a0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,16 +1,19 @@ -#include "mainwindow.h" #include "colorscheme.h" -#include "ircmanager.h" #include "emojis.h" +#include "ircmanager.h" +#include "mainwindow.h" +#include "resources.h" #include #include -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { QApplication a(argc, argv); - Emojis::initEmojis(); + Resources::load(); + Emojis::loadEmojis(); ColorScheme::instance().setColors(0, -0.8); diff --git a/message.cpp b/message.cpp index 25de2cd22..d868e0c5b 100644 --- a/message.cpp +++ b/message.cpp @@ -7,6 +7,7 @@ #include "ircmanager.h" #include "link.h" #include "qcolor.h" +#include "resources.h" #include #include @@ -18,21 +19,6 @@ #define MARGIN_TOP 8 #define MARGIN_BOTTOM 8 -LazyLoadedImage *Message::badgeStaff = - new LazyLoadedImage(new QImage(":/images/staff_bg.png")); -LazyLoadedImage *Message::badgeAdmin = - new LazyLoadedImage(new QImage(":/images/admin_bg.png")); -LazyLoadedImage *Message::badgeModerator = - new LazyLoadedImage(new QImage(":/images/moderator_bg.png")); -LazyLoadedImage *Message::badgeGlobalmod = - new LazyLoadedImage(new QImage(":/images/globalmod_bg.png")); -LazyLoadedImage *Message::badgeTurbo = - new LazyLoadedImage(new QImage(":/images/turbo_bg.png")); -LazyLoadedImage *Message::badgeBroadcaster = - new LazyLoadedImage(new QImage(":/images/broadcaster_bg.png")); -LazyLoadedImage *Message::badgePremium = - new LazyLoadedImage(new QImage(":/images/twitchprime_bg.png")); - QRegularExpression *Message::cheerRegex = new QRegularExpression("cheer[1-9][0-9]*"); @@ -60,12 +46,14 @@ Message::Message(const IrcPrivateMessage &ircMessage, const Channel &channel, // timestamps iterator = tags.find("tmi-sent-ts"); + std::time_t time = std::time(NULL); - if (iterator != tags.end()) { - time = strtoll(iterator.value().toString().toStdString().c_str(), NULL, - 10); - } + // if (iterator != tags.end()) { + // time = strtoll(iterator.value().toString().toStdString().c_str(), + // NULL, + // 10); + // } char timeStampBuffer[69]; @@ -96,29 +84,31 @@ Message::Message(const IrcPrivateMessage &ircMessage, const Channel &channel, Emotes::getCheerBadge(cheer), Word::BadgeCheer, QString(), QString("Twitch Cheer" + QString::number(cheer)))); } else if (badge == "staff/1") { - words.push_back(Word(badgeStaff, Word::BadgeStaff, QString(), - QString("Twitch Staff"))); + words.push_back(Word(Resources::badgeStaff(), Word::BadgeStaff, + QString(), QString("Twitch Staff"))); } else if (badge == "admin/1") { - words.push_back(Word(badgeAdmin, Word::BadgeAdmin, QString(), - QString("Twitch Admin"))); + words.push_back(Word(Resources::badgeAdmin(), Word::BadgeAdmin, + QString(), QString("Twitch Admin"))); } else if (badge == "global_mod/1") { - words.push_back(Word(badgeGlobalmod, Word::BadgeGlobalMod, - QString(), QString("Global Moderator"))); + words.push_back(Word(Resources::badgeGlobalmod(), + Word::BadgeGlobalMod, QString(), + QString("Global Moderator"))); } else if (badge == "moderator/1") { #pragma message WARN("xD") - words.push_back( - Word(badgeTurbo, Word::BadgeModerator, QString(), - QString("Channel Moderator"))); // custom badge + words.push_back(Word( + Resources::badgeTurbo(), Word::BadgeModerator, QString(), + QString("Channel Moderator"))); // custom badge } else if (badge == "turbo/1") { - words.push_back(Word(badgeStaff, Word::BadgeTurbo, QString(), - QString("Turbo Subscriber"))); + words.push_back(Word(Resources::badgeStaff(), Word::BadgeTurbo, + QString(), QString("Turbo Subscriber"))); } else if (badge == "broadcaster/1") { - words.push_back(Word(badgeBroadcaster, Word::BadgeBroadcaster, - QString(), + words.push_back(Word(Resources::badgeBroadcaster(), + Word::BadgeBroadcaster, QString(), QString("Channel Broadcaster"))); } else if (badge == "premium/1") { - words.push_back(Word(badgePremium, Word::BadgePremium, - QString(), QString("Twitch Prime"))); + words.push_back(Word(Resources::badgePremium(), + Word::BadgePremium, QString(), + QString("Twitch Prime"))); } } } @@ -390,14 +380,13 @@ Message::Message(const IrcPrivateMessage &ircMessage, const Channel &channel, // } } -// static void normalize - bool Message::layout(int width, bool enableEmoteMargins) { width = width - (width % 2); int mediumTextLineHeight = Fonts::getFontMetrics(Fonts::Medium).height(); + int spaceWidth = 4; bool redraw = width != m_currentLayoutWidth || m_relayoutRequested; @@ -436,56 +425,103 @@ Message::layout(int width, bool enableEmoteMargins) m_recalculateText = false; } - if (redraw) { - int x = MARGIN_LEFT; - int y = MARGIN_TOP; + if (!redraw) { + return false; + } - int right = width - MARGIN_RIGHT; + int x = MARGIN_LEFT; + int y = MARGIN_TOP; - std::list *parts = new std::list(); + int right = width - MARGIN_RIGHT - MARGIN_LEFT; - auto lineStart = m_wordParts->begin(); - int lineHeight = 0; + std::list *parts = new std::list(); - for (auto it = m_words.begin(); it != m_words.end(); ++it) { - Word &word = *it; + auto lineStart = parts->begin(); + int lineHeight = 0; - int xOffset = 0, yOffset = 0; + auto alignParts = [&lineStart, &lineHeight, &parts, this] { + for (auto it2 = lineStart; true; it2++) { + WordPart &wordPart2 = *it2; - if (enableEmoteMargins) { - if (word.isImage() && word.getImage().isHat()) { - xOffset = -word.width() + 2; - } else { - xOffset = word.xOffset(); - yOffset = word.yOffset(); - } + wordPart2.setY(wordPart2.y() + lineHeight); - lineHeight = std::max(word.height(), lineHeight); + if (it2 == parts->end()) { + break; } + } + }; - if (x + word.width() + xOffset <= right) { - parts->push_back(WordPart(word, x, y, word.copyText())); - x += word.width() + xOffset; - } - // else if (word.isText() && word.getText().length() > 2) - // { + int flags = AppSettings::wordTypeMask(); - // } - else { - parts->push_back(WordPart(word, x, y, word.copyText())); + for (auto it = m_words.begin(); it != m_words.end(); ++it) { + Word &word = *it; - lineHeight = std::max(word.height(), lineHeight); + if ((word.type() & flags) == Word::None) { + continue; + } + + int xOffset = 0, yOffset = 0; + + if (enableEmoteMargins) { + if (word.isImage() && word.getImage().isHat()) { + xOffset = -word.width() + 2; + } else { + xOffset = word.xOffset(); + yOffset = word.yOffset(); } } - auto tmp = m_wordParts; - m_wordParts = parts; - delete tmp; + // fits in the line + if (x + word.width() + xOffset <= right) { + parts->push_back( + WordPart(word, x, y - word.height(), word.copyText())); - m_height = y + lineHeight; + x += word.width() + xOffset; + + if (word.hasTrailingSpace()) { + x += spaceWidth; + } + + lineHeight = std::max(word.height(), lineHeight); + } + + // else if (word.isText() && word.getText().length() + // > 2) + // { + // } + + // doesn't fit in the line + else { + alignParts(); + + y += lineHeight; + + parts->push_back(WordPart(word, MARGIN_LEFT, y - word.height(), + word.copyText())); + + lineStart = parts->end(); + + lineHeight = word.height(); + + x = word.width(); + + if (word.hasTrailingSpace()) { + x += spaceWidth; + } + } } - return redraw; + alignParts(); + + qInfo("words: %d, parts: %d", m_words.size(), parts->size()); + + auto tmp = m_wordParts; + m_wordParts = parts; + delete tmp; + + m_height = y + lineHeight; + + return true; } bool diff --git a/resources.cpp b/resources.cpp new file mode 100644 index 000000000..30ac9b47e --- /dev/null +++ b/resources.cpp @@ -0,0 +1,46 @@ +#include "resources.h" +#include "QPixmap" + +LazyLoadedImage *Resources::m_badgeStaff; +LazyLoadedImage *Resources::m_badgeAdmin; +LazyLoadedImage *Resources::m_badgeModerator; +LazyLoadedImage *Resources::m_badgeGlobalmod; +LazyLoadedImage *Resources::m_badgeTurbo; +LazyLoadedImage *Resources::m_badgeBroadcaster; +LazyLoadedImage *Resources::m_badgePremium; + +LazyLoadedImage *Resources::m_cheerBadge100000; +LazyLoadedImage *Resources::m_cheerBadge10000; +LazyLoadedImage *Resources::m_cheerBadge5000; +LazyLoadedImage *Resources::m_cheerBadge1000; +LazyLoadedImage *Resources::m_cheerBadge100; +LazyLoadedImage *Resources::m_cheerBadge1; + +Resources::Resources() +{ +} + +void +Resources::load() +{ + // badges + m_badgeStaff = new LazyLoadedImage(new QPixmap(":/images/staff_bg.png")); + m_badgeAdmin = new LazyLoadedImage(new QPixmap(":/images/admin_bg.png")); + m_badgeModerator = + new LazyLoadedImage(new QPixmap(":/images/moderator_bg.png")); + m_badgeGlobalmod = + new LazyLoadedImage(new QPixmap(":/images/globalmod_bg.png")); + m_badgeTurbo = new LazyLoadedImage(new QPixmap(":/images/turbo_bg.png")); + m_badgeBroadcaster = + new LazyLoadedImage(new QPixmap(":/images/broadcaster_bg.png")); + m_badgePremium = + new LazyLoadedImage(new QPixmap(":/images/twitchprime_bg.png")); + + // cheer badges + m_cheerBadge100000 = new LazyLoadedImage(new QPixmap(":/cheer100000")); + m_cheerBadge10000 = new LazyLoadedImage(new QPixmap(":/cheer10000")); + m_cheerBadge5000 = new LazyLoadedImage(new QPixmap(":/cheer5000")); + m_cheerBadge1000 = new LazyLoadedImage(new QPixmap(":/cheer1000")); + m_cheerBadge100 = new LazyLoadedImage(new QPixmap(":/cheer100")); + m_cheerBadge1 = new LazyLoadedImage(new QPixmap(":/cheer1")); +} diff --git a/resources.h b/resources.h new file mode 100644 index 000000000..94ffbdde7 --- /dev/null +++ b/resources.h @@ -0,0 +1,110 @@ +#ifndef RESOURCES_H +#define RESOURCES_H + +#include + +class Resources +{ +public: + static void load(); + + // badges + static LazyLoadedImage * + badgeStaff() + { + return m_badgeStaff; + } + + static LazyLoadedImage * + badgeAdmin() + { + return m_badgeAdmin; + } + + static LazyLoadedImage * + badgeGlobalmod() + { + return m_badgeGlobalmod; + } + + static LazyLoadedImage * + badgeModerator() + { + return m_badgeModerator; + } + + static LazyLoadedImage * + badgeTurbo() + { + return m_badgeTurbo; + } + + static LazyLoadedImage * + badgeBroadcaster() + { + return m_badgeBroadcaster; + } + + static LazyLoadedImage * + badgePremium() + { + return m_badgePremium; + } + + // cheer badges + static LazyLoadedImage * + cheerBadge100000() + { + return m_cheerBadge100000; + } + + static LazyLoadedImage * + cheerBadge10000() + { + return m_cheerBadge10000; + } + + static LazyLoadedImage * + cheerBadge5000() + { + return m_cheerBadge5000; + } + + static LazyLoadedImage * + cheerBadge1000() + { + return m_cheerBadge1000; + } + + static LazyLoadedImage * + cheerBadge100() + { + return m_cheerBadge100; + } + + static LazyLoadedImage * + cheerBadge1() + { + return m_cheerBadge1; + } + +private: + Resources(); + + static LazyLoadedImage *m_badgeStaff; + static LazyLoadedImage *m_badgeAdmin; + static LazyLoadedImage *m_badgeGlobalmod; + static LazyLoadedImage *m_badgeModerator; + static LazyLoadedImage *m_badgeTurbo; + static LazyLoadedImage *m_badgeBroadcaster; + static LazyLoadedImage *m_badgePremium; + + static LazyLoadedImage *m_cheerBadge100000; + static LazyLoadedImage *m_cheerBadge10000; + static LazyLoadedImage *m_cheerBadge5000; + static LazyLoadedImage *m_cheerBadge1000; + static LazyLoadedImage *m_cheerBadge100; + static LazyLoadedImage *m_cheerBadge1; +}; + +#endif // RESOURCES_H diff --git a/word.h b/word.h index 5d808148a..7a54a3990 100644 --- a/word.h +++ b/word.h @@ -51,7 +51,7 @@ public: Default = TimestampNoSeconds | Badges | Username | Bits | FfzEmoteImage | BttvEmoteImage | BttvGifEmoteImage | - TwitchEmoteImage | BitsAmount + TwitchEmoteImage | BitsAmount | Text }; explicit Word(LazyLoadedImage *m_image, Type type, const QString ©text, @@ -180,10 +180,10 @@ private: QString m_copyText; QString m_tooltip; - int m_width; - int m_height; - int m_xOffset; - int m_yOffset; + int m_width = 16; + int m_height = 16; + int m_xOffset = 0; + int m_yOffset = 0; bool m_hasTrailingSpace; Fonts::Type m_font = Fonts::Medium; diff --git a/wordpart.h b/wordpart.h index 67e762aea..5da733fde 100644 --- a/wordpart.h +++ b/wordpart.h @@ -49,6 +49,12 @@ public: m_y = y; } + void + setY(int y) + { + m_y = y; + } + int right() const {