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"
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-02-03 19:31:51 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
#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-02-02 22:15:09 +01:00
|
|
|
|
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-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
MessageRef::MessageRef(SharedMessage _message)
|
|
|
|
: message(_message)
|
|
|
|
, wordParts()
|
2017-12-19 00:09:38 +01:00
|
|
|
, collapsed(_message->getCollapsedDefault())
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// return true if redraw is required
|
2017-12-23 21:18:13 +01:00
|
|
|
bool MessageRef::layout(int width, float scale)
|
2017-02-02 22:15:09 +01:00
|
|
|
{
|
2017-12-31 22:58:35 +01:00
|
|
|
auto &emoteManager = singletons::EmoteManager::getInstance();
|
2017-12-17 02:18:13 +01:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
bool rebuildRequired = false, layoutRequired = false;
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// check if width changed
|
2017-12-17 00:06:24 +01:00
|
|
|
bool widthChanged = width != this->currentLayoutWidth;
|
2017-10-11 10:34:04 +02:00
|
|
|
layoutRequired |= widthChanged;
|
|
|
|
this->currentLayoutWidth = width;
|
2017-12-17 00:06:24 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// check if emotes changed
|
2017-12-17 02:18:13 +01:00
|
|
|
bool imagesChanged = this->emoteGeneration != emoteManager.getGeneration();
|
2017-10-11 10:34:04 +02:00
|
|
|
layoutRequired |= imagesChanged;
|
2017-12-17 02:18:13 +01:00
|
|
|
this->emoteGeneration = emoteManager.getGeneration();
|
2017-12-17 00:06:24 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// check if text changed
|
2018-01-01 23:29:54 +01:00
|
|
|
bool textChanged =
|
|
|
|
this->fontGeneration != singletons::FontManager::getInstance().getGeneration();
|
2017-10-11 10:34:04 +02:00
|
|
|
layoutRequired |= textChanged;
|
2017-12-31 22:58:35 +01:00
|
|
|
this->fontGeneration = singletons::FontManager::getInstance().getGeneration();
|
2017-12-17 00:06:24 +01:00
|
|
|
|
2017-10-11 10:34:04 +02: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();
|
2017-10-11 10:34:04 +02:00
|
|
|
layoutRequired |= wordMaskChanged;
|
2017-12-31 22:58:35 +01:00
|
|
|
this->currentWordTypes = singletons::SettingManager::getInstance().getWordTypeMask();
|
2017-10-11 10:34:04 +02:00
|
|
|
|
2017-12-17 00:06:24 +01:00
|
|
|
// check if dpi changed
|
2017-12-23 21:18:13 +01:00
|
|
|
bool scaleChanged = this->scale != scale;
|
|
|
|
layoutRequired |= scaleChanged;
|
|
|
|
this->scale = scale;
|
|
|
|
imagesChanged |= scaleChanged;
|
|
|
|
textChanged |= scaleChanged;
|
2017-12-17 00:06:24 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// update word sizes if needed
|
|
|
|
if (imagesChanged) {
|
|
|
|
this->updateImageSizes();
|
2017-12-17 03:26:23 +01:00
|
|
|
this->buffer = nullptr;
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
|
|
|
if (textChanged) {
|
|
|
|
this->updateTextSizes();
|
2017-12-17 03:26:23 +01:00
|
|
|
this->buffer = nullptr;
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
2017-12-17 01:54:15 +01:00
|
|
|
if (widthChanged || wordMaskChanged) {
|
2017-10-11 10:34:04 +02:00
|
|
|
this->buffer = nullptr;
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// return if no layout is required
|
|
|
|
if (!layoutRequired) {
|
2017-04-12 17:46:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
this->actuallyLayout(width);
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-12 17:46:44 +02:00
|
|
|
|
2017-10-11 10:34:04 +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
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
const int spaceWidth = 4;
|
|
|
|
const int right = width - MARGIN_RIGHT;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2018-01-05 01:31:01 +01:00
|
|
|
bool compactEmotes = !this->getMessage()->getDisableCompactEmotes();
|
2017-12-28 17:46:36 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// clear word parts
|
|
|
|
this->wordParts.clear();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
// layout
|
2017-02-02 22:15:09 +01:00
|
|
|
int x = MARGIN_LEFT;
|
|
|
|
int y = MARGIN_TOP;
|
|
|
|
|
2017-02-17 23:51:35 +01:00
|
|
|
int lineNumber = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
int lineStart = 0;
|
|
|
|
int lineHeight = 0;
|
2017-12-19 00:09:38 +01:00
|
|
|
int firstLineHeight = -1;
|
2017-02-02 22:15:09 +01:00
|
|
|
bool first = true;
|
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
uint32_t flags = settings.getWordTypeMask();
|
2017-12-19 00:09:38 +01:00
|
|
|
if (this->collapsed) {
|
|
|
|
flags |= Word::Collapsed;
|
|
|
|
}
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// 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) {
|
2017-02-02 22:15:09 +01:00
|
|
|
Word &word = *it;
|
|
|
|
|
2017-06-11 20:53:43 +02:00
|
|
|
// Check if given word is supposed to be rendered by comparing it to the current setting
|
2017-12-23 21:18:13 +01:00
|
|
|
if ((word.getFlags() & flags) == Word::None) {
|
2017-02-02 22:15:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xOffset = 0, yOffset = 0;
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
/// if (enableEmoteMargins) {
|
|
|
|
/// if (word.isImage() && word.getImage().isHat()) {
|
|
|
|
/// xOffset = -word.getWidth() + 2;
|
|
|
|
/// } else {
|
|
|
|
xOffset = word.getXOffset();
|
|
|
|
yOffset = word.getYOffset();
|
|
|
|
/// }
|
|
|
|
/// }
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
// word wrapping
|
2017-12-23 21:18:13 +01:00
|
|
|
if (word.isText() && word.getWidth(this->scale) + MARGIN_LEFT > right) {
|
2017-10-11 10:34:04 +02:00
|
|
|
// align and end the current line
|
2017-12-28 17:46:36 +01:00
|
|
|
this->_alignWordParts(lineStart, lineHeight, width, firstLineHeight);
|
2017-02-02 22:15:09 +01:00
|
|
|
y += lineHeight;
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
int currentPartStart = 0;
|
|
|
|
int currentLineWidth = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// go through the text, break text when it doesn't fit in the line anymore
|
|
|
|
for (int i = 1; i <= word.getText().length(); i++) {
|
2017-12-23 21:18:13 +01:00
|
|
|
currentLineWidth += word.getCharWidth(i - 1, this->scale);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
if (currentLineWidth + MARGIN_LEFT > right) {
|
|
|
|
// add the current line
|
|
|
|
QString mid = word.getText().mid(currentPartStart, i - currentPartStart - 1);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y, currentLineWidth,
|
2017-12-23 21:18:13 +01:00
|
|
|
word.getHeight(this->scale), lineNumber, mid,
|
|
|
|
mid, false, currentPartStart));
|
2017-09-21 02:20:02 +02:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
y += word.getFontMetrics(this->scale).height();
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
currentPartStart = i - 1;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
currentLineWidth = 0;
|
2017-02-17 23:51:35 +01:00
|
|
|
lineNumber++;
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
QString mid(word.getText().mid(currentPartStart));
|
2017-12-23 21:18:13 +01:00
|
|
|
currentLineWidth = word.getFontMetrics(this->scale).width(mid);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(this->scale),
|
|
|
|
currentLineWidth, word.getHeight(this->scale),
|
|
|
|
lineNumber, mid, mid, true, currentPartStart));
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
x = currentLineWidth + MARGIN_LEFT + spaceWidth;
|
2018-01-05 01:31:01 +01:00
|
|
|
lineHeight = this->_updateLineHeight(0, word, compactEmotes);
|
2017-09-21 12:15:01 +02:00
|
|
|
lineStart = this->wordParts.size() - 1;
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
|
|
|
// fits in the current line
|
2017-12-23 21:18:13 +01:00
|
|
|
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()));
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
x += word.getWidth(this->scale) + xOffset;
|
2017-02-02 22:15:09 +01:00
|
|
|
x += spaceWidth;
|
|
|
|
|
2018-01-05 01:31:01 +01:00
|
|
|
lineHeight = this->_updateLineHeight(lineHeight, word, compactEmotes);
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
|
|
|
// 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);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
2017-09-12 19:06:16 +02:00
|
|
|
lineNumber++;
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(this->scale),
|
|
|
|
this->scale, lineNumber, word.getCopyText()));
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-09-21 12:15:01 +02:00
|
|
|
lineStart = this->wordParts.size() - 1;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2018-01-05 01:31:01 +01:00
|
|
|
lineHeight = this->_updateLineHeight(0, word, compactEmotes);
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
x = word.getWidth(this->scale) + MARGIN_LEFT;
|
2017-02-02 22:15:09 +01:00
|
|
|
x += spaceWidth;
|
|
|
|
}
|
2017-12-28 17:46:36 +01:00
|
|
|
|
|
|
|
first = false;
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// align and end the current line
|
2017-12-28 17:46:36 +01:00
|
|
|
this->_alignWordParts(lineStart, lineHeight, width, firstLineHeight);
|
2017-12-19 00:09:38 +01:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
this->collapsedHeight = firstLineHeight == -1 ? (int)(24 * this->scale)
|
2017-12-19 00:09:38 +01:00
|
|
|
: firstLineHeight + MARGIN_TOP + MARGIN_BOTTOM;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
// update height
|
|
|
|
int oldHeight = this->height;
|
2017-12-19 00:09:38 +01:00
|
|
|
|
|
|
|
if (this->isCollapsed()) {
|
|
|
|
this->height = this->collapsedHeight;
|
|
|
|
} else {
|
|
|
|
this->height = y + lineHeight + MARGIN_BOTTOM;
|
|
|
|
}
|
2017-10-11 10:34:04 +02:00
|
|
|
|
|
|
|
// invalidate buffer if height changed
|
|
|
|
if (oldHeight != this->height) {
|
|
|
|
this->buffer = nullptr;
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
updateBuffer = true;
|
|
|
|
}
|
2017-09-12 19:06:16 +02:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
void MessageRef::updateTextSizes()
|
|
|
|
{
|
|
|
|
for (auto &word : this->message->getWords()) {
|
2017-12-23 21:18:13 +01:00
|
|
|
if (!word.isText()) {
|
2017-10-11 10:34:04 +02:00
|
|
|
continue;
|
2017-12-23 21:18:13 +01:00
|
|
|
}
|
2017-10-11 10:34:04 +02:00
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
word.updateSize();
|
2017-02-03 19:31:51 +01:00
|
|
|
}
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
2017-02-03 19:31:51 +01:00
|
|
|
|
2017-10-11 10:34:04 +02:00
|
|
|
void MessageRef::updateImageSizes()
|
|
|
|
{
|
|
|
|
for (auto &word : this->message->getWords()) {
|
|
|
|
if (!word.isImage())
|
|
|
|
continue;
|
|
|
|
|
2017-12-23 21:18:13 +01:00
|
|
|
word.updateSize();
|
2017-10-11 10:34:04 +02:00
|
|
|
}
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
|
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-02-02 22:15:09 +01:00
|
|
|
{
|
2017-12-28 17:46:36 +01:00
|
|
|
bool compactEmotes = true;
|
|
|
|
|
2017-12-19 00:09:38 +01:00
|
|
|
if (firstLineHeight == -1) {
|
|
|
|
firstLineHeight = lineHeight;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
int xOffset = 0;
|
2017-02-02 22:15:09 +01:00
|
|
|
|
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-16 00:05:06 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2018-01-05 01:31:01 +01:00
|
|
|
const bool isCompactEmote = compactEmotes && wordPart.getWord().isImage() &&
|
|
|
|
wordPart.getWord().getFlags() & Word::EmoteImages;
|
|
|
|
|
|
|
|
int yExtra = 0;
|
|
|
|
if (isCompactEmote) {
|
|
|
|
yExtra = (COMPACT_EMOTES_OFFSET / 2) * this->scale;
|
|
|
|
}
|
2017-09-16 00:05:06 +02:00
|
|
|
|
2017-12-28 17:46:36 +01:00
|
|
|
wordPart.setPosition(wordPart.getX() + xOffset, wordPart.getY() + lineHeight + yExtra);
|
2017-02-02 22:15:09 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
2018-01-05 01:31:01 +01:00
|
|
|
if (compactEmotes && word.isImage() && word.getFlags() & Word::EmoteImages) {
|
2017-12-28 17:46:36 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-04 13:17: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++;
|
2017-12-23 21:18:13 +01:00
|
|
|
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
|
|
|
|
2017-12-19 00:09:38 +01: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;
|
|
|
|
}
|
2018-01-01 23:29:54 +01:00
|
|
|
|
|
|
|
bool MessageRef::isDisabled() const
|
|
|
|
{
|
|
|
|
return this->message->isDisabled();
|
|
|
|
}
|
2017-05-27 16:16:39 +02:00
|
|
|
} // namespace messages
|
|
|
|
} // namespace chatterino
|