From 4c392178dac1de9a299beb78989c4e0f3fecade9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 20 May 2018 11:15:15 +0200 Subject: Add image preview and url/image open in browser --- include/Channel.hpp | 2 +- include/Chatbar.hpp | 2 +- include/Gif.hpp | 2 ++ include/ImagePreview.hpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ include/Message.hpp | 2 ++ include/MessageBoard.hpp | 4 +++- include/StringUtils.hpp | 18 +++++++++++++++++ include/Text.hpp | 9 ++++++--- 8 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 include/ImagePreview.hpp create mode 100644 include/StringUtils.hpp (limited to 'include') diff --git a/include/Channel.hpp b/include/Channel.hpp index 6e944c3..16c2fb3 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -55,7 +55,7 @@ namespace dchat void replaceLocalUser(OnlineLocalUser *newOnlineLocalUser); void changeNick(const std::string &newNick); - void processEvent(const sf::Event &event); + void processEvent(const sf::Event &event, Cache &cache); void draw(sf::RenderWindow &window, Cache &cache); static void setCurrent(Channel *channel); diff --git a/include/Chatbar.hpp b/include/Chatbar.hpp index 5d69448..df492ac 100644 --- a/include/Chatbar.hpp +++ b/include/Chatbar.hpp @@ -21,7 +21,7 @@ namespace dchat bool isFocused() const; - void processEvent(const sf::Event &event, Channel *channel); + void processEvent(const sf::Event &event, Cache &cache, Channel *channel); void draw(sf::RenderWindow &window, Cache &cache); static float getHeight(); diff --git a/include/Gif.hpp b/include/Gif.hpp index 8f5d4e7..1341049 100644 --- a/include/Gif.hpp +++ b/include/Gif.hpp @@ -31,6 +31,8 @@ namespace dchat sf::Vector2u getSize() const; void setPosition(const sf::Vector2f &position); + sf::Vector2f getPosition() const; + void setScale(const sf::Vector2f &scale); void draw(sf::RenderTarget &target, const sf::RenderStates &renderStates = sf::RenderStates::Default); diff --git a/include/ImagePreview.hpp b/include/ImagePreview.hpp new file mode 100644 index 0000000..1d20fe8 --- /dev/null +++ b/include/ImagePreview.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace dchat +{ + class Gif; + + class ImagePreview + { + public: + // set @texture to nullptr if you wish to end preview + static void preview(sf::Texture *texture, const std::string &url = ""); + // set gif to nullptr if you wish to end preview + static void preview(Gif *gif, const std::string &url = ""); + static void* getPreviewContentPtr(); + static sf::Int32 getTimeSinceLastSeenMs(); + + static void processEvent(const sf::Event &event); + static void draw(sf::RenderWindow &window); + private: + enum class ContentType + { + NONE, + TEXTURE, + GIF + }; + + ImagePreview() : texture(nullptr), contentType(ContentType::NONE) {} + ImagePreview(const ImagePreview&) = delete; + ImagePreview& operator=(const ImagePreview&) = delete; + static ImagePreview* getInstance(); + + sf::Vector2u calculateImageSize(sf::Vector2u windowSize) const; + private: + sf::Sprite sprite; + sf::Vector2u size; + sf::Clock lastSeenTimer; + + union + { + sf::Texture *texture; + Gif *gif; + }; + ContentType contentType; + }; +} diff --git a/include/Message.hpp b/include/Message.hpp index 8c7f57a..a2d67e8 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -4,6 +4,7 @@ #include "Text.hpp" #include #include +#include namespace dchat { @@ -23,5 +24,6 @@ namespace dchat Text text; const u64 timestampSeconds; Type type; + odhtdb::Hash id; }; } diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index a947c1b..861d195 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -22,7 +22,7 @@ namespace dchat MessageBoard(Channel *channel); ~MessageBoard(); - void processEvent(const sf::Event &event); + void processEvent(const sf::Event &event, Cache &cache); void draw(sf::RenderWindow &window, Cache &cache); private: usize findPositionToInsertMessageByTimestamp(Message *message); @@ -47,5 +47,7 @@ namespace dchat sf::Vector2f backgroundSize; sf::Vector2f backgroundPos; std::mutex messageProcessMutex; + usize visibleMessageStartIndex; + usize visibleMessageEndIndex; }; } diff --git a/include/StringUtils.hpp b/include/StringUtils.hpp new file mode 100644 index 0000000..6b237dd --- /dev/null +++ b/include/StringUtils.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace dchat +{ + static std::string stringReplaceChar(const std::string &str, const std::string &from, const std::string &to) + { + std::string result = str; + size_t pos = 0; + while((pos = result.find(from, pos)) != std::string::npos) + { + result.replace(pos, from.size(), to); + pos += to.size(); + } + return result; + } +} diff --git a/include/Text.hpp b/include/Text.hpp index d7d1e06..925dc94 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -57,10 +57,13 @@ namespace dchat // Warning: won't update until @draw is called float getHeight() const; - void processEvent(const sf::Event &event); + void processEvent(const sf::Event &event, Cache &cache); - // Performs culling. @updateGeometry is called even if text is not visible if text is dirty, because updateGeometry might change the dimension of the text and make is visible - void draw(sf::RenderTarget &target, Cache &cache); + // Performs culling. @updateGeometry is called even if text is not visible if text is dirty, because updateGeometry might change the dimension of the text and make is visible. + // Returns true if text was drawn on screen (if text is within window borders) + bool draw(sf::RenderTarget &target, Cache &cache); + private: + void onMouseClick(const sf::Event::MouseButtonEvent &event, Cache &cache); private: enum class CaretMoveDirection : u8 { -- cgit v1.2.3