diff options
Diffstat (limited to 'src/MessageBoard.cpp')
-rw-r--r-- | src/MessageBoard.cpp | 51 |
1 files changed, 38 insertions, 13 deletions
diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index aa92d46..2c89be3 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -36,7 +36,7 @@ namespace dchat const float TEXT_LINE_SPACING = 5.0f; const float USERNAME_TIMESTAMP_SIDE_PADDING = 15.0f; const float AVATAR_DIAMETER = 70.0f; - const float AVATAR_PADDING_SIDE = 30.0f; + const float AVATAR_PADDING_SIDE = 20.0f; const double SCROLL_MAX_SPEED = 20.0; // Merge messages from same user that are sent within one minute @@ -52,7 +52,7 @@ namespace dchat visibleMessageEndIndex(-1) { scrollbar.backgroundColor = sf::Color(49, 52, 57); - scrollbar.scrollColor = sf::Color(42, 44, 49); + scrollbar.scrollColor = sf::Color(37, 39, 44); } MessageBoard::~MessageBoard() @@ -81,16 +81,24 @@ namespace dchat dirty = true; } - void MessageBoard::addMessage(Message *message, const odhtdb::Hash &id) + bool MessageBoard::addMessage(Message *message, const odhtdb::Hash &id) { lock_guard<mutex> lock(messageProcessMutex); + bool emptyHash = id.isEmpty(); + if(!emptyHash && messageIdMap.find(id) != messageIdMap.end()) + { + delete message; + return false; + } auto positionToAddMessage = findPositionToInsertMessageByTimestamp(message); if(positionToAddMessage == messages.size()) scrollToBottom = true; messages.insert(messages.begin() + positionToAddMessage, message); message->id = id; - messageIdMap[id] = message; + if(!emptyHash) + messageIdMap[id] = message; dirty = true; + return true; } void MessageBoard::deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) @@ -167,6 +175,8 @@ namespace dchat } bool visible = false; + + float startX = floor(position.x + LINE_SIDE_PADDING * Settings::getScaling() - PADDING_SIDE * Settings::getScaling()); if(!mergeTextWithPrev) { @@ -174,9 +184,19 @@ namespace dchat if(position.y + usernameTextHeight > 0.0f && position.y < backgroundPos.y + backgroundSize.y) { visible = true; - sf::Text usernameText(sf::String::fromUtf8(message->user->getName().begin(), message->user->getName().end()), *usernameFont, usernameTextCharacterSize); + string usernameStr; + if(message->user->type == User::Type::ONLINE_DISCORD_USER) + { + usernameStr = "(Discord) "; + usernameStr += message->user->getName(); + } + else + { + usernameStr = message->user->getName(); + } + sf::Text usernameText(sf::String::fromUtf8(usernameStr.begin(), usernameStr.end()), *usernameFont, usernameTextCharacterSize); usernameText.setFillColor(sf::Color(15, 192, 252)); - usernameText.setPosition(sf::Vector2f(floor(position.x + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()), floor(position.y))); + usernameText.setPosition(sf::Vector2f(floor(startX + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()), floor(position.y))); window.draw(usernameText); if(message->timestampSeconds) @@ -188,7 +208,7 @@ namespace dchat sf::Text timestamp(date, *timestampFont, timestampTextCharacterSize); timestamp.setFillColor(ColorScheme::getTextRegularColor() * sf::Color(255, 255, 255, 50)); - timestamp.setPosition(sf::Vector2f(floor(position.x + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling() + usernameText.getLocalBounds().width + USERNAME_TIMESTAMP_SIDE_PADDING * Settings::getScaling()), floor(position.y + 2.0f * Settings::getScaling() + usernameTextHeight * 0.5f - timestampTextHeight * 0.5f))); + timestamp.setPosition(sf::Vector2f(floor(startX + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling() + usernameText.getLocalBounds().width + USERNAME_TIMESTAMP_SIDE_PADDING * Settings::getScaling()), floor(position.y + 2.0f * Settings::getScaling() + usernameTextHeight * 0.5f - timestampTextHeight * 0.5f))); window.draw(timestamp); } @@ -203,14 +223,14 @@ namespace dchat // TODO: Store this sprite somewhere, might not be efficient to create a new sprite object every frame sf::Sprite sprite(*avatarResult.texture); auto textureSize = avatarResult.texture->getSize(); - sprite.setPosition(sf::Vector2f(floor(position.x), floor(position.y))); + sprite.setPosition(sf::Vector2f(startX, floor(position.y))); sprite.setScale(sf::Vector2f(AVATAR_DIAMETER * Settings::getScaling() / (float)textureSize.x, AVATAR_DIAMETER * Settings::getScaling() / (float)textureSize.y)); window.draw(sprite, circleShader); } else if(avatarResult.cachedType == ContentByUrlResult::CachedType::GIF) { auto gifSize = avatarResult.gif->getSize(); - avatarResult.gif->setPosition(sf::Vector2f(floor(position.x), floor(position.y))); + avatarResult.gif->setPosition(sf::Vector2f(startX, floor(position.y))); avatarResult.gif->setScale(sf::Vector2f(AVATAR_DIAMETER * Settings::getScaling() / (float)gifSize.x, AVATAR_DIAMETER * Settings::getScaling() / (float)gifSize.y)); avatarResult.gif->setColor(sf::Color::White); avatarResult.gif->draw(window, circleShader); @@ -219,7 +239,7 @@ namespace dchat else { sf::CircleShape avatarCircle(AVATAR_DIAMETER * 0.5f * Settings::getScaling(), 60 * Settings::getScaling()); - avatarCircle.setPosition(sf::Vector2f(floor(position.x), floor(position.y))); + avatarCircle.setPosition(sf::Vector2f(startX, floor(position.y))); avatarCircle.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(30, 30, 30)); window.draw(avatarCircle); } @@ -230,8 +250,8 @@ namespace dchat // No need to perform culling here, that is done in @Text draw function message->text.setCharacterSize(18.0f * Settings::getScaling()); - message->text.setMaxWidth(backgroundSize.x - (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()); - message->text.setPosition(sf::Vector2f(floor(position.x + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()), floor(position.y))); + message->text.setMaxWidth(lineRect.getSize().x - (AVATAR_DIAMETER + AVATAR_PADDING_SIDE * 2.0f) * Settings::getScaling()); + message->text.setPosition(sf::Vector2f(floor(startX + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()), floor(position.y))); bool textDrawn = message->text.draw(window, cache); if(!visible) visible = textDrawn; @@ -243,7 +263,7 @@ namespace dchat if(position.y + LINE_HEIGHT > 0.0f && position.y < backgroundPos.y + backgroundSize.y && i + 1 != numMessages) { visible = true; - lineRect.setPosition(sf::Vector2f(position.x + LINE_SIDE_PADDING * Settings::getScaling() - PADDING_SIDE * Settings::getScaling(), floor(position.y))); + lineRect.setPosition(sf::Vector2f(startX, floor(position.y))); window.draw(lineRect); //drawGradientLine(sf::Vector2f(position.x + LINE_SIDE_PADDING, floor(position.y)), sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * 2.0f, LINE_HEIGHT), LINE_COLOR, window); } @@ -459,4 +479,9 @@ namespace dchat return messages.back(); return nullptr; } + + const std::vector<Message*>& MessageBoard::getMessages() const + { + return messages; + } } |