From 9e576f9fbcbcc4603689b0b1215cf3d526bd9616 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 30 Apr 2018 13:54:10 +0200 Subject: Add timestamp to messages --- depends/odhtdb | 2 +- include/Channel.hpp | 3 ++- include/Message.hpp | 4 +++- src/Channel.cpp | 8 +++++--- src/Message.cpp | 5 +++-- src/MessageBoard.cpp | 35 ++++++++++++++++++++++++++++++----- src/main.cpp | 3 ++- 7 files changed, 46 insertions(+), 14 deletions(-) diff --git a/depends/odhtdb b/depends/odhtdb index 54de8e3..670e3ee 160000 --- a/depends/odhtdb +++ b/depends/odhtdb @@ -1 +1 @@ -Subproject commit 54de8e3b0b353b8c62b566dabb7b082a1cdd371e +Subproject commit 670e3eed2703dcee1dee0508e45d8454cae78544 diff --git a/include/Channel.hpp b/include/Channel.hpp index 31a17f4..5650eb1 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -28,7 +28,8 @@ namespace dchat const std::vector getUsers() const; User* getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey); - void addLocalMessage(const std::string &msg, User *owner); + // If timestamp is 0, then timestamp is not used + void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); void addMessage(const std::string &msg); void addUser(User *user); diff --git a/include/Message.hpp b/include/Message.hpp index a6edddf..2e52603 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -10,9 +10,11 @@ namespace dchat class Message { public: - Message(User *user, const std::string &text); + // If timestamp is 0, then timestamp is not used + Message(User *user, const std::string &text, u64 timestampSeconds = 0); const User *user; Text text; + const u64 timestampSeconds; }; } diff --git a/src/Channel.cpp b/src/Channel.cpp index 4fa99a0..748be49 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -95,22 +95,24 @@ namespace dchat return nullptr; } - void Channel::addLocalMessage(const std::string &msg, User *owner) + void Channel::addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds) { assert(owner); - messageBoard.addMessage(new Message(owner, msg)); + messageBoard.addMessage(new Message(owner, msg, timestampSeconds)); } void Channel::addMessage(const std::string &msg) { - messageBoard.addMessage(new Message(localUser, msg)); if(database && localUser->type == User::Type::ONLINE) { + addLocalMessage(msg, localUser, database->getSyncedTimestampUtc().seconds); auto onlineUser = static_cast(localUser); assert(onlineUser->databaseUser->getType() == odhtdb::User::Type::LOCAL); database->addData(databaseNodeInfo, static_cast(onlineUser->databaseUser), odhtdb::DataView((void*)msg.data(), msg.size())); database->commit(); } + else + addLocalMessage(msg, localUser, 0); } void Channel::addUser(User *user) diff --git a/src/Message.cpp b/src/Message.cpp index 5cd8445..77e3cfa 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -7,9 +7,10 @@ using namespace std; namespace dchat { - Message::Message(User *_user, const std::string &_text) : + Message::Message(User *_user, const std::string &_text, u64 _timestampSeconds) : user(_user), - text(sf::String::fromUtf8(_text.begin(), _text.end()), ResourceCache::getFont("fonts/Roboto-Regular.ttf"), 18 * Settings::getScaling(), 0.0f, false) + text(sf::String::fromUtf8(_text.begin(), _text.end()), ResourceCache::getFont("fonts/Roboto-Regular.ttf"), 18 * Settings::getScaling(), 0.0f, false), + timestampSeconds(_timestampSeconds) { text.setFillColor(ColorScheme::getTextRegularColor()); } diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index cd73c6d..bcdf5c7 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -23,7 +23,7 @@ namespace dchat sf::Color sideColor, centerColor; }; - const float USERNAME_PADDING_BOTTOM = 0.0f; + const float USERNAME_PADDING_BOTTOM = 5.0f; const float MESSAGE_PADDING_TOP = 25.0f; const float MESSAGE_PADDING_BOTTOM = 30.0f; @@ -31,6 +31,7 @@ namespace dchat const float PADDING_TOP = 0.0f; const float LINE_SIDE_PADDING = 20.0f; const float LINE_HEIGHT = 1.0f; + const float USERNAME_TIMESTAMP_SIDE_PADDING = 10.0f; const LineColor LINE_COLOR { @@ -153,27 +154,47 @@ namespace dchat window.draw(backgroundRect); const sf::Font *usernameFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); + const int usernameTextCharacterSize = 20 * Settings::getScaling(); + const float usernameTextHeight = usernameFont->getLineSpacing(usernameTextCharacterSize); + + const sf::Font *timestampFont = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); + const int timestampTextCharacterSize = 15 * Settings::getScaling(); + const float timestampTextHeight = timestampFont->getLineSpacing(timestampTextCharacterSize); double deltaTimeMicro = (double)frameTimer.getElapsedTime().asMicroseconds(); frameTimer.restart(); if(dirty) { + sf::RectangleShape lineRect(sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * 2.0f, LINE_HEIGHT)); + lineRect.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10)); + sf::Vector2 position(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight() + PADDING_TOP); double startHeight = position.y; position.y += scroll; for(Message *message : messages) { position.y += MESSAGE_PADDING_TOP; - sf::Text usernameText(message->user->getName(), *usernameFont, 20 * Settings::getScaling()); - float usernameTextHeight = usernameText.getFont()->getLineSpacing(usernameText.getCharacterSize()); if(position.y + usernameTextHeight > 0.0f && position.y < backgroundPos.y + backgroundSize.y) { + sf::Text usernameText(message->user->getName(), *usernameFont, usernameTextCharacterSize); usernameText.setFillColor(sf::Color(15, 192, 252)); usernameText.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE), floor(position.y))); window.draw(usernameText); + + if(message->timestampSeconds) + { + time_t time = (time_t)message->timestampSeconds; + struct tm *localTimePtr = localtime(&time); + char *timeStr = asctime(localTimePtr); + + sf::Text timestamp(timeStr, *timestampFont, timestampTextCharacterSize); + timestamp.setFillColor(ColorScheme::getTextRegularColor() * sf::Color(255, 255, 255, 30)); + timestamp.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE + 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); + } } - position.y += usernameTextHeight + USERNAME_PADDING_BOTTOM; + position.y += usernameTextHeight + USERNAME_PADDING_BOTTOM * Settings::getScaling(); // No need to perform culling here, that is done in @Text draw function message->text.setCharacterSize(18 * Settings::getScaling()); @@ -183,7 +204,11 @@ namespace dchat position.y += message->text.getHeight() + MESSAGE_PADDING_BOTTOM; if(position.y + LINE_HEIGHT > 0.0f && position.y < backgroundPos.y + backgroundSize.y) - 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); + { + lineRect.setPosition(sf::Vector2f(position.x + LINE_SIDE_PADDING, 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); + } position.y += LINE_HEIGHT; } diff --git a/src/main.cpp b/src/main.cpp index 68f9ac0..364b2a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include using namespace std; @@ -60,7 +61,7 @@ void channelAddStoredMessages(Channel *channel, const odhtdb::DatabaseStorageObj continue; } string msg((const char*)nodeStorageAddedObject->decryptedObject.data.data, nodeStorageAddedObject->decryptedObject.data.size); - channel->addLocalMessage(msg, user); + channel->addLocalMessage(msg, user, ntp::NtpTimestamp::fromCombined(nodeStorageAddedObject->createdTimestamp).seconds); } } } -- cgit v1.2.3