mirror-chatterino2/src/messages/messageref.cpp

441 lines
12 KiB
C++
Raw Normal View History

2017-09-24 18:43:24 +02:00
#include "messages/messageref.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/emotemanager.hpp"
#include "singletons/settingsmanager.hpp"
#include <QDebug>
#define MARGIN_LEFT (int)(8 * this->scale)
#define MARGIN_RIGHT (int)(8 * this->scale)
#define MARGIN_TOP (int)(4 * this->scale)
#define MARGIN_BOTTOM (int)(4 * this->scale)
2017-12-28 17:46:36 +01:00
#define COMPACT_EMOTES_OFFSET 6
2017-04-14 17:52:22 +02:00
using namespace chatterino::messages;
2017-04-12 17:46:44 +02:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace messages {
2017-09-21 12:15:01 +02:00
MessageRef::MessageRef(SharedMessage _message)
: message(_message)
, wordParts()
, collapsed(_message->getCollapsedDefault())
{
}
2017-04-12 17:46:44 +02:00
Message *MessageRef::getMessage()
{
2017-09-21 12:15:01 +02:00
return this->message.get();
2017-04-12 17:46:44 +02:00
}
int MessageRef::getHeight() const
{
2017-09-21 12:15:01 +02:00
return this->height;
2017-04-12 17:46:44 +02:00
}
// return true if redraw is required
bool MessageRef::layout(int width, float scale)
{
2017-12-31 22:58:35 +01:00
auto &emoteManager = singletons::EmoteManager::getInstance();
2017-12-17 02:18:13 +01:00
bool rebuildRequired = false, layoutRequired = false;
2017-04-12 17:46:44 +02:00
// check if width changed
2017-12-17 00:06:24 +01:00
bool widthChanged = width != this->currentLayoutWidth;
layoutRequired |= widthChanged;
this->currentLayoutWidth = width;
2017-12-17 00:06:24 +01:00
// check if emotes changed
2017-12-17 02:18:13 +01:00
bool imagesChanged = this->emoteGeneration != emoteManager.getGeneration();
layoutRequired |= imagesChanged;
2017-12-17 02:18:13 +01:00
this->emoteGeneration = emoteManager.getGeneration();
2017-12-17 00:06:24 +01:00
// check if text changed
2017-12-31 22:58:35 +01:00
bool textChanged = this->fontGeneration != singletons::FontManager::getInstance().getGeneration();
layoutRequired |= textChanged;
2017-12-31 22:58:35 +01:00
this->fontGeneration = singletons::FontManager::getInstance().getGeneration();
2017-12-17 00:06:24 +01:00
// check if work mask changed
2017-12-17 00:06:24 +01:00
bool wordMaskChanged =
2017-12-31 22:58:35 +01:00
this->currentWordTypes != singletons::SettingManager::getInstance().getWordTypeMask();
layoutRequired |= wordMaskChanged;
2017-12-31 22:58:35 +01:00
this->currentWordTypes = singletons::SettingManager::getInstance().getWordTypeMask();
2017-12-17 00:06:24 +01:00
// check if dpi changed
bool scaleChanged = this->scale != scale;
layoutRequired |= scaleChanged;
this->scale = scale;
imagesChanged |= scaleChanged;
textChanged |= scaleChanged;
2017-12-17 00:06:24 +01:00
// update word sizes if needed
if (imagesChanged) {
this->updateImageSizes();
this->buffer = nullptr;
}
if (textChanged) {
this->updateTextSizes();
this->buffer = nullptr;
}
if (widthChanged || wordMaskChanged) {
this->buffer = nullptr;
}
2017-04-12 17:46:44 +02:00
// return if no layout is required
if (!layoutRequired) {
2017-04-12 17:46:44 +02:00
return false;
}
this->actuallyLayout(width);
2017-04-12 17:46:44 +02:00
return true;
}
2017-04-12 17:46:44 +02:00
void MessageRef::actuallyLayout(int width)
{
2017-12-31 22:58:35 +01:00
auto &settings = singletons::SettingManager::getInstance();
2017-02-17 23:51:35 +01:00
const int spaceWidth = 4;
const int right = width - MARGIN_RIGHT;
2017-12-28 17:46:36 +01:00
bool overlapEmotes = true;
// clear word parts
this->wordParts.clear();
2017-04-12 17:46:44 +02:00
// layout
int x = MARGIN_LEFT;
int y = MARGIN_TOP;
2017-02-17 23:51:35 +01:00
int lineNumber = 0;
int lineStart = 0;
int lineHeight = 0;
int firstLineHeight = -1;
bool first = true;
uint32_t flags = settings.getWordTypeMask();
if (this->collapsed) {
flags |= Word::Collapsed;
}
// loop throught all the words and add them when a line is full
2017-09-21 12:15:01 +02:00
for (auto it = this->message->getWords().begin(); it != this->message->getWords().end(); ++it) {
Word &word = *it;
// Check if given word is supposed to be rendered by comparing it to the current setting
if ((word.getFlags() & flags) == Word::None) {
continue;
}
int xOffset = 0, yOffset = 0;
/// if (enableEmoteMargins) {
/// if (word.isImage() && word.getImage().isHat()) {
/// xOffset = -word.getWidth() + 2;
/// } else {
xOffset = word.getXOffset();
yOffset = word.getYOffset();
/// }
/// }
// word wrapping
if (word.isText() && word.getWidth(this->scale) + MARGIN_LEFT > right) {
// align and end the current line
2017-12-28 17:46:36 +01:00
this->_alignWordParts(lineStart, lineHeight, width, firstLineHeight);
y += lineHeight;
int currentPartStart = 0;
int currentLineWidth = 0;
// go through the text, break text when it doesn't fit in the line anymore
for (int i = 1; i <= word.getText().length(); i++) {
currentLineWidth += word.getCharWidth(i - 1, this->scale);
if (currentLineWidth + MARGIN_LEFT > right) {
// add the current line
QString mid = word.getText().mid(currentPartStart, i - currentPartStart - 1);
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y, currentLineWidth,
word.getHeight(this->scale), lineNumber, mid,
mid, false, currentPartStart));
2017-09-21 02:20:02 +02:00
y += word.getFontMetrics(this->scale).height();
currentPartStart = i - 1;
currentLineWidth = 0;
2017-02-17 23:51:35 +01:00
lineNumber++;
}
}
QString mid(word.getText().mid(currentPartStart));
currentLineWidth = word.getFontMetrics(this->scale).width(mid);
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(this->scale),
currentLineWidth, word.getHeight(this->scale),
lineNumber, mid, mid, true, currentPartStart));
x = currentLineWidth + MARGIN_LEFT + spaceWidth;
2017-12-28 17:46:36 +01:00
lineHeight = this->_updateLineHeight(0, word, overlapEmotes);
2017-09-21 12:15:01 +02:00
lineStart = this->wordParts.size() - 1;
}
// fits in the current line
else if (first || x + word.getWidth(this->scale) + xOffset <= right) {
this->wordParts.push_back(WordPart(word, x, y - word.getHeight(this->scale), scale,
lineNumber, word.getCopyText()));
x += word.getWidth(this->scale) + xOffset;
x += spaceWidth;
2017-12-28 17:46:36 +01:00
lineHeight = this->_updateLineHeight(lineHeight, word, overlapEmotes);
}
// doesn't fit in the line
else {
// align and end the current line
2017-12-28 17:46:36 +01:00
this->_alignWordParts(lineStart, lineHeight, width, firstLineHeight);
y += lineHeight;
2017-09-12 19:06:16 +02:00
lineNumber++;
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(this->scale),
this->scale, lineNumber, word.getCopyText()));
2017-09-21 12:15:01 +02:00
lineStart = this->wordParts.size() - 1;
2017-12-28 17:46:36 +01:00
lineHeight = this->_updateLineHeight(0, word, overlapEmotes);
x = word.getWidth(this->scale) + MARGIN_LEFT;
x += spaceWidth;
}
2017-12-28 17:46:36 +01:00
first = false;
}
// align and end the current line
2017-12-28 17:46:36 +01:00
this->_alignWordParts(lineStart, lineHeight, width, firstLineHeight);
this->collapsedHeight = firstLineHeight == -1 ? (int)(24 * this->scale)
: firstLineHeight + MARGIN_TOP + MARGIN_BOTTOM;
// update height
int oldHeight = this->height;
if (this->isCollapsed()) {
this->height = this->collapsedHeight;
} else {
this->height = y + lineHeight + MARGIN_BOTTOM;
}
// invalidate buffer if height changed
if (oldHeight != this->height) {
this->buffer = nullptr;
}
updateBuffer = true;
}
2017-09-12 19:06:16 +02:00
void MessageRef::updateTextSizes()
{
for (auto &word : this->message->getWords()) {
if (!word.isText()) {
continue;
}
word.updateSize();
}
}
void MessageRef::updateImageSizes()
{
for (auto &word : this->message->getWords()) {
if (!word.isImage())
continue;
word.updateSize();
}
}
2017-04-12 17:46:44 +02:00
const std::vector<WordPart> &MessageRef::getWordParts() const
{
2017-09-21 12:15:01 +02:00
return this->wordParts;
2017-04-12 17:46:44 +02:00
}
2017-12-28 17:46:36 +01:00
void MessageRef::_alignWordParts(int lineStart, int lineHeight, int width, int &firstLineHeight)
{
2017-12-28 17:46:36 +01:00
bool compactEmotes = true;
if (firstLineHeight == -1) {
firstLineHeight = lineHeight;
}
int xOffset = 0;
2017-09-21 12:15:01 +02:00
if (this->message->centered && this->wordParts.size() > 0) {
xOffset = (width - this->wordParts.at(this->wordParts.size() - 1).getRight()) / 2;
}
2017-09-21 12:15:01 +02:00
for (size_t i = lineStart; i < this->wordParts.size(); i++) {
2017-12-28 17:46:36 +01:00
WordPart &wordPart = this->wordParts.at(i);
int yExtra = compactEmotes && wordPart.getWord().isImage()
? (COMPACT_EMOTES_OFFSET / 2) * this->scale
: 0;
2017-12-28 17:46:36 +01:00
wordPart.setPosition(wordPart.getX() + xOffset, wordPart.getY() + lineHeight + yExtra);
}
}
2017-02-17 23:51:35 +01:00
2017-12-28 17:46:36 +01:00
int MessageRef::_updateLineHeight(int currentLineHeight, Word &word, bool compactEmotes)
{
int newLineHeight = word.getHeight(this->scale);
// fourtf: doesn't care about the height of a normal line
if (compactEmotes && word.isImage()) {
newLineHeight -= COMPACT_EMOTES_OFFSET * this->scale;
}
return std::max(currentLineHeight, newLineHeight);
}
2017-09-12 19:06:16 +02:00
const Word *MessageRef::tryGetWordPart(QPoint point)
2017-02-17 23:51:35 +01:00
{
2017-04-12 17:46:44 +02:00
// go through all words and return the first one that contains the point.
2017-09-21 12:15:01 +02:00
for (WordPart &wordPart : this->wordParts) {
2017-02-17 23:51:35 +01:00
if (wordPart.getRect().contains(point)) {
2017-09-12 19:06:16 +02:00
return &wordPart.getWord();
2017-02-17 23:51:35 +01:00
}
}
2017-09-12 19:06:16 +02:00
return nullptr;
2017-02-17 23:51:35 +01:00
}
// XXX(pajlada): This is probably not the optimal way to calculate this
int MessageRef::getLastCharacterIndex() const
{
// find out in which line the cursor is
int lineNumber = 0, lineStart = 0, lineEnd = 0;
for (size_t i = 0; i < this->wordParts.size(); i++) {
const WordPart &part = this->wordParts[i];
if (part.getLineNumber() != lineNumber) {
lineStart = i;
lineNumber = part.getLineNumber();
}
lineEnd = i + 1;
}
// count up to the cursor
int index = 0;
for (int i = 0; i < lineStart; i++) {
const WordPart &part = this->wordParts[i];
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
}
for (int i = lineStart; i < lineEnd; i++) {
const WordPart &part = this->wordParts[i];
index += part.getCharacterLength();
}
return index;
}
2017-04-12 17:46:44 +02:00
int MessageRef::getSelectionIndex(QPoint position)
2017-02-17 23:51:35 +01:00
{
2017-09-21 12:15:01 +02:00
if (this->wordParts.size() == 0) {
2017-02-17 23:51:35 +01:00
return 0;
}
// find out in which line the cursor is
int lineNumber = 0, lineStart = 0, lineEnd = 0;
2017-09-21 12:15:01 +02:00
for (size_t i = 0; i < this->wordParts.size(); i++) {
WordPart &part = this->wordParts[i];
2017-02-17 23:51:35 +01:00
2017-04-24 23:00:26 +02:00
if (part.getLineNumber() != 0 && position.y() < part.getY()) {
2017-02-17 23:51:35 +01:00
break;
}
if (part.getLineNumber() != lineNumber) {
2017-09-12 19:06:16 +02:00
lineStart = i;
2017-02-17 23:51:35 +01:00
lineNumber = part.getLineNumber();
}
2017-09-12 19:06:16 +02:00
lineEnd = i + 1;
2017-02-17 23:51:35 +01:00
}
// count up to the cursor
int index = 0;
for (int i = 0; i < lineStart; i++) {
2017-09-21 12:15:01 +02:00
WordPart &part = this->wordParts[i];
2017-02-17 23:51:35 +01:00
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
}
for (int i = lineStart; i < lineEnd; i++) {
2017-09-21 12:15:01 +02:00
WordPart &part = this->wordParts[i];
2017-02-17 23:51:35 +01:00
// curser is left of the word part
if (position.x() < part.getX()) {
break;
}
// cursor is right of the word part
2017-04-24 23:00:26 +02:00
if (position.x() > part.getX() + part.getWidth()) {
2017-09-12 19:06:16 +02:00
index += part.getCharacterLength();
2017-02-17 23:51:35 +01:00
continue;
}
// cursor is over the word part
if (part.getWord().isImage()) {
2017-09-12 19:06:16 +02:00
if (position.x() - part.getX() > part.getWidth() / 2) {
index++;
}
2017-02-17 23:51:35 +01:00
} else {
2017-09-12 19:06:16 +02:00
// TODO: use word.getCharacterWidthCache();
auto text = part.getText();
2017-02-17 23:51:35 +01:00
int x = part.getX();
for (int j = 0; j < text.length(); j++) {
if (x > position.x()) {
break;
}
index++;
x = part.getX() + part.getWord().getFontMetrics(this->scale).width(text, j + 1);
2017-02-17 23:51:35 +01:00
}
}
break;
}
return index;
}
2017-05-27 16:16:39 +02:00
bool MessageRef::isCollapsed() const
{
return this->collapsed;
}
void MessageRef::setCollapsed(bool value)
{
if (this->collapsed != value) {
this->currentLayoutWidth = 0;
this->collapsed = value;
}
}
int MessageRef::getCollapsedHeight() const
{
return this->collapsedHeight;
}
2017-05-27 16:16:39 +02:00
} // namespace messages
} // namespace chatterino