mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
added graphics cache for drawn messages
This commit is contained in:
parent
a92c3dc2a4
commit
759e0aea50
7 changed files with 143 additions and 86 deletions
|
@ -15,7 +15,7 @@ template <typename T>
|
||||||
class LimitedQueue
|
class LimitedQueue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LimitedQueue(int limit = 10, int buffer = 5)
|
LimitedQueue(int limit = 100, int buffer = 25)
|
||||||
: vector(new std::vector<T>(limit + buffer))
|
: vector(new std::vector<T>(limit + buffer))
|
||||||
, vectorPtr(this->vector)
|
, vectorPtr(this->vector)
|
||||||
, mutex()
|
, mutex()
|
||||||
|
|
|
@ -29,8 +29,8 @@ public:
|
||||||
|
|
||||||
T const &operator[](int index) const
|
T const &operator[](int index) const
|
||||||
{
|
{
|
||||||
assert(index >= 0);
|
// assert(index >= 0);
|
||||||
assert(index < length);
|
// assert(index < length);
|
||||||
|
|
||||||
return vector->at(index + offset);
|
return vector->at(index + offset);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#include "emotes.h"
|
#include "emotes.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
#define MARGIN_LEFT 8
|
#define MARGIN_LEFT 8
|
||||||
#define MARGIN_RIGHT 8
|
#define MARGIN_RIGHT 8
|
||||||
#define MARGIN_TOP 8
|
#define MARGIN_TOP 8
|
||||||
|
@ -14,6 +16,7 @@ MessageRef::MessageRef(std::shared_ptr<Message> message)
|
||||||
: message(message.get())
|
: message(message.get())
|
||||||
, messagePtr(message)
|
, messagePtr(message)
|
||||||
, wordParts()
|
, wordParts()
|
||||||
|
, buffer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,46 +25,50 @@ MessageRef::layout(int width, bool enableEmoteMargins)
|
||||||
{
|
{
|
||||||
auto &settings = Settings::getInstance();
|
auto &settings = Settings::getInstance();
|
||||||
|
|
||||||
width = width - (width % 2);
|
bool sizeChanged = width != this->currentLayoutWidth;
|
||||||
|
bool redraw = width != this->currentLayoutWidth;
|
||||||
int mediumTextLineHeight = Fonts::getFontMetrics(Fonts::Medium).height();
|
|
||||||
int spaceWidth = 4;
|
int spaceWidth = 4;
|
||||||
|
|
||||||
bool redraw = width != this->currentLayoutWidth || this->relayoutRequested;
|
{
|
||||||
|
int mediumTextLineHeight =
|
||||||
|
Fonts::getFontMetrics(Fonts::Medium).height();
|
||||||
|
|
||||||
bool recalculateImages = this->emoteGeneration != Emotes::getGeneration();
|
bool recalculateImages =
|
||||||
bool recalculateText = this->fontGeneration != Fonts::getGeneration();
|
this->emoteGeneration != Emotes::getGeneration();
|
||||||
|
bool recalculateText = this->fontGeneration != Fonts::getGeneration();
|
||||||
|
|
||||||
qreal emoteScale = settings.emoteScale.get();
|
qreal emoteScale = settings.emoteScale.get();
|
||||||
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
|
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
|
||||||
|
|
||||||
if (recalculateImages || recalculateText) {
|
if (recalculateImages || recalculateText) {
|
||||||
this->emoteGeneration = Emotes::getGeneration();
|
this->emoteGeneration = Emotes::getGeneration();
|
||||||
this->fontGeneration = Fonts::getGeneration();
|
this->fontGeneration = Fonts::getGeneration();
|
||||||
|
|
||||||
redraw = true;
|
redraw = true;
|
||||||
|
|
||||||
for (auto &word : this->message->getWords()) {
|
for (auto &word : this->message->getWords()) {
|
||||||
if (word.isImage()) {
|
if (word.isImage()) {
|
||||||
if (recalculateImages) {
|
if (recalculateImages) {
|
||||||
auto &image = word.getImage();
|
auto &image = word.getImage();
|
||||||
|
|
||||||
qreal w = image.getWidth();
|
qreal w = image.getWidth();
|
||||||
qreal h = image.getHeight();
|
qreal h = image.getHeight();
|
||||||
|
|
||||||
if (scaleEmotesByLineHeight) {
|
if (scaleEmotesByLineHeight) {
|
||||||
word.setSize(w * mediumTextLineHeight / h * emoteScale,
|
word.setSize(
|
||||||
mediumTextLineHeight * emoteScale);
|
w * mediumTextLineHeight / h * emoteScale,
|
||||||
} else {
|
mediumTextLineHeight * emoteScale);
|
||||||
word.setSize(w * image.getScale() * emoteScale,
|
} else {
|
||||||
h * image.getScale() * emoteScale);
|
word.setSize(w * image.getScale() * emoteScale,
|
||||||
|
h * image.getScale() * emoteScale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (recalculateText) {
|
||||||
|
QFontMetrics &metrics = word.getFontMetrics();
|
||||||
|
word.setSize(metrics.width(word.getText()),
|
||||||
|
metrics.height());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (recalculateText) {
|
|
||||||
QFontMetrics &metrics = word.getFontMetrics();
|
|
||||||
word.setSize(metrics.width(word.getText()),
|
|
||||||
metrics.height());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,6 +78,8 @@ MessageRef::layout(int width, bool enableEmoteMargins)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->currentLayoutWidth = width;
|
||||||
|
|
||||||
int x = MARGIN_LEFT;
|
int x = MARGIN_LEFT;
|
||||||
int y = MARGIN_TOP;
|
int y = MARGIN_TOP;
|
||||||
|
|
||||||
|
@ -184,7 +193,16 @@ MessageRef::layout(int width, bool enableEmoteMargins)
|
||||||
|
|
||||||
this->alignWordParts(lineStart, lineHeight);
|
this->alignWordParts(lineStart, lineHeight);
|
||||||
|
|
||||||
this->height = y + lineHeight;
|
if (this->height != y + lineHeight) {
|
||||||
|
sizeChanged = true;
|
||||||
|
this->height = y + lineHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sizeChanged) {
|
||||||
|
this->buffer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
this->updateBuffer = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
#ifndef MESSAGEREF_H
|
#ifndef MESSAGEREF_H
|
||||||
#define MESSAGEREF_H
|
#define MESSAGEREF_H
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include "messages/message.h"
|
#include "messages/message.h"
|
||||||
|
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
namespace messages {
|
namespace messages {
|
||||||
|
|
||||||
|
@ -26,18 +28,15 @@ public:
|
||||||
|
|
||||||
bool layout(int width, bool enableEmoteMargins = true);
|
bool layout(int width, bool enableEmoteMargins = true);
|
||||||
|
|
||||||
void
|
|
||||||
requestRelayout()
|
|
||||||
{
|
|
||||||
relayoutRequested = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<WordPart> &
|
const std::vector<WordPart> &
|
||||||
getWordParts() const
|
getWordParts() const
|
||||||
{
|
{
|
||||||
return wordParts;
|
return wordParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<QPixmap> buffer = nullptr;
|
||||||
|
bool updateBuffer = false;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Message *message;
|
Message *message;
|
||||||
std::shared_ptr<Message> messagePtr;
|
std::shared_ptr<Message> messagePtr;
|
||||||
|
@ -47,7 +46,6 @@ private:
|
||||||
int height = 0;
|
int height = 0;
|
||||||
|
|
||||||
int currentLayoutWidth = -1;
|
int currentLayoutWidth = -1;
|
||||||
bool relayoutRequested = true;
|
|
||||||
int fontGeneration = -1;
|
int fontGeneration = -1;
|
||||||
int emoteGeneration = -1;
|
int emoteGeneration = -1;
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,7 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *parent)
|
||||||
this->leftMenu.addAction("Close split", this, SLOT(menuCloseSplit()),
|
this->leftMenu.addAction("Close split", this, SLOT(menuCloseSplit()),
|
||||||
QKeySequence(tr("Ctrl+W")));
|
QKeySequence(tr("Ctrl+W")));
|
||||||
this->leftMenu.addAction("Move split", this, SLOT(menuMoveSplit()));
|
this->leftMenu.addAction("Move split", this, SLOT(menuMoveSplit()));
|
||||||
|
this->leftMenu.addAction("Popup", this, SLOT(menuPopup()));
|
||||||
this->leftMenu.addSeparator();
|
this->leftMenu.addSeparator();
|
||||||
this->leftMenu.addAction("Change channel", this, SLOT(menuChangeChannel()),
|
this->leftMenu.addAction("Change channel", this, SLOT(menuChangeChannel()),
|
||||||
QKeySequence(tr("Ctrl+R")));
|
QKeySequence(tr("Ctrl+R")));
|
||||||
|
@ -182,6 +183,13 @@ ChatWidgetHeader::menuMoveSplit()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
void
|
void
|
||||||
|
ChatWidgetHeader::menuPopup()
|
||||||
|
{
|
||||||
|
auto widget = new ChatWidget();
|
||||||
|
widget->setChannelName(this->chatWidget->getChannelName());
|
||||||
|
widget->show();
|
||||||
|
}
|
||||||
|
void
|
||||||
ChatWidgetHeader::menuChangeChannel()
|
ChatWidgetHeader::menuChangeChannel()
|
||||||
{
|
{
|
||||||
this->chatWidget->showChangeChannelPopup();
|
this->chatWidget->showChangeChannelPopup();
|
||||||
|
|
|
@ -61,6 +61,7 @@ private slots:
|
||||||
void menuAddSplit();
|
void menuAddSplit();
|
||||||
void menuCloseSplit();
|
void menuCloseSplit();
|
||||||
void menuMoveSplit();
|
void menuMoveSplit();
|
||||||
|
void menuPopup();
|
||||||
void menuChangeChannel();
|
void menuChangeChannel();
|
||||||
void menuClearChat();
|
void menuClearChat();
|
||||||
void menuOpenChannel();
|
void menuOpenChannel();
|
||||||
|
|
|
@ -37,17 +37,33 @@ ChatWidgetView::~ChatWidgetView()
|
||||||
bool
|
bool
|
||||||
ChatWidgetView::layoutMessages()
|
ChatWidgetView::layoutMessages()
|
||||||
{
|
{
|
||||||
bool showScrollbar = false;
|
|
||||||
|
|
||||||
auto messages = chatWidget->getMessagesSnapshot();
|
auto messages = chatWidget->getMessagesSnapshot();
|
||||||
|
|
||||||
bool redraw = false;
|
if (messages.getLength() == 0) {
|
||||||
|
this->scrollbar.setVisible(false);
|
||||||
|
|
||||||
// for (std::shared_ptr<messages::Message> &message : messages) {
|
return false;
|
||||||
// redraw |= message.get()->layout(this->width(), true);
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
redraw = true;
|
bool showScrollbar = false, redraw = false;
|
||||||
|
|
||||||
|
int start = this->scrollbar.getCurrentValue();
|
||||||
|
|
||||||
|
int y = -(messages[start].get()->getHeight() *
|
||||||
|
(fmod(this->scrollbar.getCurrentValue(), 1)));
|
||||||
|
|
||||||
|
for (int i = start; i < messages.getLength(); ++i) {
|
||||||
|
auto messagePtr = messages[i];
|
||||||
|
auto message = messagePtr.get();
|
||||||
|
|
||||||
|
redraw |= message->layout(this->width(), true);
|
||||||
|
|
||||||
|
y += message->getHeight();
|
||||||
|
|
||||||
|
if (y >= height()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int h = this->height() - 8;
|
int h = this->height() - 8;
|
||||||
|
|
||||||
|
@ -87,18 +103,15 @@ ChatWidgetView::resizeEvent(QResizeEvent *)
|
||||||
void
|
void
|
||||||
ChatWidgetView::paintEvent(QPaintEvent *)
|
ChatWidgetView::paintEvent(QPaintEvent *)
|
||||||
{
|
{
|
||||||
QPainter painter(this);
|
QPainter _painter(this);
|
||||||
|
|
||||||
painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
_painter.setRenderHint(QPainter::SmoothPixmapTransform);
|
||||||
|
|
||||||
// auto c = this->chatWidget->getChannel();
|
|
||||||
|
|
||||||
QColor color;
|
|
||||||
|
|
||||||
ColorScheme &scheme = ColorScheme::getInstance();
|
ColorScheme &scheme = ColorScheme::getInstance();
|
||||||
|
|
||||||
// code for tesing colors
|
// code for tesing colors
|
||||||
/*
|
/*
|
||||||
|
QColor color;
|
||||||
static ConcurrentMap<qreal, QImage *> imgCache;
|
static ConcurrentMap<qreal, QImage *> imgCache;
|
||||||
|
|
||||||
std::function<QImage *(qreal)> getImg = [&scheme](qreal light) {
|
std::function<QImage *(qreal)> getImg = [&scheme](qreal light) {
|
||||||
|
@ -129,11 +142,6 @@ ChatWidgetView::paintEvent(QPaintEvent *)
|
||||||
|
|
||||||
painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/
|
painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/
|
||||||
|
|
||||||
// if (c == NULL)
|
|
||||||
// return;
|
|
||||||
|
|
||||||
// auto messages = c->getMessageSnapshot();
|
|
||||||
|
|
||||||
auto messages = chatWidget->getMessagesSnapshot();
|
auto messages = chatWidget->getMessagesSnapshot();
|
||||||
|
|
||||||
int start = this->scrollbar.getCurrentValue();
|
int start = this->scrollbar.getCurrentValue();
|
||||||
|
@ -148,40 +156,64 @@ ChatWidgetView::paintEvent(QPaintEvent *)
|
||||||
for (int i = start; i < messages.getLength(); ++i) {
|
for (int i = start; i < messages.getLength(); ++i) {
|
||||||
messages::MessageRef *messageRef = messages[i].get();
|
messages::MessageRef *messageRef = messages[i].get();
|
||||||
|
|
||||||
for (messages::WordPart const &wordPart : messageRef->getWordParts()) {
|
std::shared_ptr<QPixmap> bufferPtr = messageRef->buffer;
|
||||||
painter.setPen(QColor(255, 0, 0));
|
QPixmap *buffer = bufferPtr.get();
|
||||||
painter.drawRect(wordPart.getX(), wordPart.getY() + y,
|
|
||||||
wordPart.getWidth(), wordPart.getHeight());
|
|
||||||
|
|
||||||
// image
|
bool updateBuffer = messageRef->updateBuffer;
|
||||||
if (wordPart.getWord().isImage()) {
|
|
||||||
messages::LazyLoadedImage &lli = wordPart.getWord().getImage();
|
|
||||||
|
|
||||||
const QPixmap *image = lli.getPixmap();
|
if (buffer == nullptr) {
|
||||||
|
buffer = new QPixmap(this->width(), messageRef->getHeight());
|
||||||
|
bufferPtr = std::shared_ptr<QPixmap>(buffer);
|
||||||
|
updateBuffer = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (image != NULL) {
|
if (updateBuffer) {
|
||||||
painter.drawPixmap(
|
QPainter painter(buffer);
|
||||||
QRect(wordPart.getX(), wordPart.getY() + y,
|
painter.fillRect(buffer->rect(), scheme.ChatBackground);
|
||||||
wordPart.getWidth(), wordPart.getHeight()),
|
|
||||||
*image);
|
for (messages::WordPart const &wordPart :
|
||||||
|
messageRef->getWordParts()) {
|
||||||
|
painter.setPen(QColor(255, 0, 0));
|
||||||
|
painter.drawRect(wordPart.getX(), wordPart.getY(),
|
||||||
|
wordPart.getWidth(), wordPart.getHeight());
|
||||||
|
|
||||||
|
// image
|
||||||
|
if (wordPart.getWord().isImage()) {
|
||||||
|
messages::LazyLoadedImage &lli =
|
||||||
|
wordPart.getWord().getImage();
|
||||||
|
|
||||||
|
const QPixmap *image = lli.getPixmap();
|
||||||
|
|
||||||
|
if (image != NULL) {
|
||||||
|
painter.drawPixmap(
|
||||||
|
QRect(wordPart.getX(), wordPart.getY(),
|
||||||
|
wordPart.getWidth(), wordPart.getHeight()),
|
||||||
|
*image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// text
|
||||||
|
else {
|
||||||
|
QColor color = wordPart.getWord().getColor();
|
||||||
|
|
||||||
|
ColorScheme::getInstance().normalizeColor(color);
|
||||||
|
|
||||||
|
painter.setPen(color);
|
||||||
|
painter.setFont(wordPart.getWord().getFont());
|
||||||
|
|
||||||
|
painter.drawText(
|
||||||
|
QRectF(wordPart.getX(), wordPart.getY(), 10000, 10000),
|
||||||
|
wordPart.getText(),
|
||||||
|
QTextOption(Qt::AlignLeft | Qt::AlignTop));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// text
|
|
||||||
else {
|
|
||||||
QColor color = wordPart.getWord().getColor();
|
|
||||||
|
|
||||||
ColorScheme::getInstance().normalizeColor(color);
|
messageRef->updateBuffer = false;
|
||||||
|
|
||||||
painter.setPen(color);
|
|
||||||
painter.setFont(wordPart.getWord().getFont());
|
|
||||||
|
|
||||||
painter.drawText(
|
|
||||||
QRectF(wordPart.getX(), wordPart.getY() + y, 10000, 10000),
|
|
||||||
wordPart.getText(),
|
|
||||||
QTextOption(Qt::AlignLeft | Qt::AlignTop));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
messageRef->buffer = bufferPtr;
|
||||||
|
|
||||||
|
_painter.drawPixmap(0, y, *buffer);
|
||||||
|
|
||||||
y += messageRef->getHeight();
|
y += messageRef->getHeight();
|
||||||
|
|
||||||
if (y > height()) {
|
if (y > height()) {
|
||||||
|
|
Loading…
Reference in a new issue