From 15c4434de0c2cd12e09c2f41e898c0b124194a97 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 8 May 2018 18:06:43 +0200 Subject: Add context menu, add context menu to delete message --- include/Channel.hpp | 9 ++++++++- include/ColorScheme.hpp | 4 ++++ include/ContextMenu.hpp | 45 +++++++++++++++++++++++++++++++++++++++++++ include/GlobalContextMenu.hpp | 19 ++++++++++++++++++ include/Message.hpp | 7 +++++++ include/MessageBoard.hpp | 15 ++++++++------- include/Text.hpp | 3 +++ 7 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 include/ContextMenu.hpp create mode 100644 include/GlobalContextMenu.hpp (limited to 'include') diff --git a/include/Channel.hpp b/include/Channel.hpp index 1acfcd1..7d4cb08 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace odhtdb { @@ -19,7 +20,9 @@ namespace dchat { enum class ChannelDataType : u8 { - MESSAGE, + ADD_MESSAGE, + EDIT_MESSAGE, + DELETE_MESSAGE, NICKNAME_CHANGE }; @@ -42,7 +45,11 @@ namespace dchat // If timestamp is 0, then current time is used void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); + void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds, const odhtdb::Hash &id); void addMessage(const std::string &msg); + void deleteLocalMessage(const odhtdb::Hash &id); + void deleteMessage(const odhtdb::Hash &id); + void addUserLocally(User *user); bool addUser(const odhtdb::Signature::PublicKey &userId, const std::string &groupId); void replaceLocalUser(User *newLocalUser); diff --git a/include/ColorScheme.hpp b/include/ColorScheme.hpp index 64dbeec..93aa640 100644 --- a/include/ColorScheme.hpp +++ b/include/ColorScheme.hpp @@ -19,5 +19,9 @@ namespace dchat static sf::Color getBackgroundColor(); static sf::Color getPanelColor(); static sf::Color getTextRegularColor(); + + static sf::Color getContextMenuBackgroundColor(); + static sf::Color getContextMenuTextColor(); + static sf::Color getContextMenuSelectedItemBackgroundColor(); }; } diff --git a/include/ContextMenu.hpp b/include/ContextMenu.hpp new file mode 100644 index 0000000..1e08d7d --- /dev/null +++ b/include/ContextMenu.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include "types.hpp" +#include +#include +#include +#include +#include +#include + +namespace dchat +{ + class ContextMenuItem; + using MenuItemClickCallbackFunc = std::function; + + class ContextMenuItem + { + public: + ContextMenuItem(const std::string &text, MenuItemClickCallbackFunc clickCallbackFunc); + + sf::Text text; + MenuItemClickCallbackFunc clickCallbackFunc; + }; + + class ContextMenu + { + public: + ContextMenu(); + + void addItem(const ContextMenuItem &item); + void setPosition(const sf::Vector2f &position); + void setVisible(bool visible); + + ContextMenuItem& getItemByIndex(usize index); + + void processEvent(const sf::Event &event); + void draw(sf::RenderWindow &window); + private: + std::vector menuItems; + sf::Vector2f position; + int focusedItem; + int pressedItem; + bool visible; + }; +} diff --git a/include/GlobalContextMenu.hpp b/include/GlobalContextMenu.hpp new file mode 100644 index 0000000..ab45e50 --- /dev/null +++ b/include/GlobalContextMenu.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "ContextMenu.hpp" +#include +#include + +namespace dchat +{ + class GlobalContextMenu + { + public: + static ContextMenu* getEditMessageContextMenu(); + static void setClickEditMessageCallbackFunc(MenuItemClickCallbackFunc callbackFunc); + static void setClickDeleteMessageCallbackFunc(MenuItemClickCallbackFunc callbackFunc); + + static void processEvent(const sf::Event &event); + static void draw(sf::RenderWindow &window); + }; +} diff --git a/include/Message.hpp b/include/Message.hpp index 2e52603..8c7f57a 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -10,11 +10,18 @@ namespace dchat class Message { public: + enum class Type + { + REGULAR, + EDITED + }; + // 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; + Type type; }; } diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index a4dc5a6..a6867d2 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -6,7 +6,9 @@ #include #include #include +#include #include +#include namespace dchat { @@ -16,25 +18,23 @@ namespace dchat { friend class Channel; public: - MessageBoard(const sf::Vector2u &size); + MessageBoard(Channel *channel); ~MessageBoard(); void processEvent(const sf::Event &event); void draw(sf::RenderWindow &window, Cache &cache); private: void updateStaticContentTexture(const sf::Vector2u &newSize); - void addMessage(Message *message); + void addMessage(Message *message, const odhtdb::Hash &id); + void deleteMessage(const odhtdb::Hash &id); void drawDefault(sf::RenderWindow &window, Cache &cache); void drawSimple(sf::RenderWindow &window, Cache &cache); private: - sf::RenderTexture staticContentTexture; + Channel *channel; bool dirty; - bool selectingText; - bool leftMouseButtonPressed; - sf::Vector2f mousePos; - sf::Vector2f selectingTextStart; std::vector messages; + odhtdb::MapHash messageIdMap; double scroll; double scrollSpeed; sf::Clock frameTimer; @@ -43,5 +43,6 @@ namespace dchat sf::Vector2f backgroundSizeWithoutPadding; sf::Vector2f backgroundSize; sf::Vector2f backgroundPos; + std::mutex messageProcessMutex; }; } diff --git a/include/Text.hpp b/include/Text.hpp index d2809de..d7d1e06 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -40,7 +40,10 @@ namespace dchat void setPosition(float x, float y); void setPosition(const sf::Vector2f &position); + sf::Vector2f getPosition() const; + void setMaxWidth(float maxWidth); + float getMaxWidth() const; void setCharacterSize(unsigned int characterSize); unsigned int getCharacterSize() const; -- cgit v1.2.3