From 09a8ade6becca2a71f45ff0db5f4bf6d64afb212 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 21 Apr 2018 03:46:58 +0200 Subject: Add support for static image emoji Emoji are downloaded asynchronously using remote program (curl). Need to add support for converting [inline](url) chat message emoji and gifs. --- include/Cache.hpp | 54 +++++++++++++++++++++++++++++++++++++ include/Channel.hpp | 23 ++++++++++++++++ include/ChannelSidePanel.hpp | 16 +++++++++++ include/Message.hpp | 1 + include/MessageBoard.hpp | 3 ++- include/MessagePart.hpp | 17 +++++++++++- include/ResourceCache.hpp | 12 +++++++++ include/env.hpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 include/Cache.hpp create mode 100644 include/Channel.hpp create mode 100644 include/ChannelSidePanel.hpp create mode 100644 include/env.hpp (limited to 'include') diff --git a/include/Cache.hpp b/include/Cache.hpp new file mode 100644 index 0000000..89abe2c --- /dev/null +++ b/include/Cache.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace TinyProcessLib +{ + class Process; +} + +namespace dchat +{ + struct ImageByUrlResult + { + enum class Type + { + CACHED, + DOWNLOADING, + FAILED_DOWNLOAD + }; + + // @texture is null if @type is DOWNLOADING or FAILED_DOWNLOAD + const sf::Texture *texture; + Type type; + }; + + class Cache + { + public: + Cache(); + + // Creates directory if it doesn't exist (recursively). Throws boost exception on failure + static boost::filesystem::path getDchatDir(); + + // Get cached image or downloads it. + // Default download file limit is 12MB + // Returns ImageByUrlResult describing texture status. + const ImageByUrlResult getImageByUrl(const std::string &url, int downloadLimitBytes = 12582912); + private: + struct ImageDownloadInfo + { + TinyProcessLib::Process *process; + std::string url; + }; + + std::thread downloadWaitThread; + std::vector imageDownloadProcesses; + std::mutex imageDownloadMutex; + }; +} diff --git a/include/Channel.hpp b/include/Channel.hpp new file mode 100644 index 0000000..fa52a4b --- /dev/null +++ b/include/Channel.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "MessageBoard.hpp" +#include "Chatbar.hpp" +#include "User.hpp" +#include "Channel.hpp" + +namespace dchat +{ + class Channel + { + public: + Channel(); + ~Channel(); + + void processEvent(const sf::Event &event); + void draw(sf::RenderWindow &window, Cache &cache); + private: + MessageBoard messageBoard; + Chatbar chatbar; + OfflineUser localOfflineUser; + }; +} diff --git a/include/ChannelSidePanel.hpp b/include/ChannelSidePanel.hpp new file mode 100644 index 0000000..604dfcf --- /dev/null +++ b/include/ChannelSidePanel.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace dchat +{ + class Channel; + + class ChannelSidePanel + { + public: + void addChannel(Channel *channel); + private: + std::vector channels; + }; +} diff --git a/include/Message.hpp b/include/Message.hpp index c037eb3..efc1f4c 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -14,6 +14,7 @@ namespace dchat virtual ~Message(); void addText(const std::string &text); + void addImage(const std::string &url); std::vector& getParts(); const User *user; diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index f399761..105d675 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -1,6 +1,7 @@ #pragma once #include "Message.hpp" +#include "../include/Cache.hpp" #include #include #include @@ -18,7 +19,7 @@ namespace dchat void addMessage(Message *message); void processEvent(const sf::Event &event); - void draw(sf::RenderWindow &window); + void draw(sf::RenderWindow &window, Cache &cache); private: sf::RenderTexture staticContentTexture; bool useStaticContentTexture; diff --git a/include/MessagePart.hpp b/include/MessagePart.hpp index e50852a..cbb0f26 100644 --- a/include/MessagePart.hpp +++ b/include/MessagePart.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -11,7 +12,8 @@ namespace dchat public: enum class Type { - TEXT + TEXT, + EMOJI }; MessagePart(Type _type) : type(_type) {} @@ -35,4 +37,17 @@ namespace dchat sf::Text text; }; + + class MessagePartEmoji : public MessagePart + { + public: + MessagePartEmoji(const std::string &url); + + static float getHeightScaled(); + virtual sf::Vector2f getPosition() const override; + virtual sf::Vector2f getSize() const override; + + sf::Sprite sprite; + std::string url; + }; } diff --git a/include/ResourceCache.hpp b/include/ResourceCache.hpp index d35eb8f..256e5a4 100644 --- a/include/ResourceCache.hpp +++ b/include/ResourceCache.hpp @@ -1,13 +1,25 @@ #pragma once #include +#include #include +#include namespace dchat { + class FailedToLoadResourceException : public std::runtime_error + { + public: + FailedToLoadResourceException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + class ResourceCache { public: + // Throws FailedToLoadResourceException on failure static const sf::Font& getFont(const std::string &filepath); + + // Throws FailedToLoadResourceException on failure + static const sf::Texture* getTexture(const std::string &filepath); }; } diff --git a/include/env.hpp b/include/env.hpp new file mode 100644 index 0000000..e0a5b03 --- /dev/null +++ b/include/env.hpp @@ -0,0 +1,63 @@ +#pragma once + +#define OS_FAMILY_WINDOWS 0 +#define OS_FAMILY_POSIX 1 + +#define OS_TYPE_WINDOWS 0 +#define OS_TYPE_LINUX 1 + +#if defined(_WIN32) || defined(_WIN64) + #if defined(_WIN64) + #define OS_ENV_64BIT + #else + #define OS_ENV_32BIT + #endif + #define OS_FAMILY OS_FAMILY_WINDOWS + #define OS_TYPE OS_TYPE_WINDOWS + + #ifndef UNICODE + #define UNICODE + #endif + + #ifndef _UNICODE + #define _UNICODE + #endif + + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + + #include +#endif + +#if defined(__linux__) || defined(__unix__) || defined(__APPLE__) || defined(_POSIX_VERSION) + #define OS_FAMILY OS_FAMILY_POSIX +#endif + +#ifdef __linux__ + #define OS_TYPE OS_TYPE_LINUX +#endif + +#if defined(__GNUC__) + #if defined(__x86_64__) || defined(__pc64__) + #define OS_ENV_64BIT + #else + #define OS_ENV_32BIT + #endif +#endif + +#if !defined(OS_ENV_32BIT) && !defined(OS_ENV_64BIT) + #error "System is not detected as either 32-bit or 64-bit" +#endif + +#if !defined(OS_FAMILY) + #error "System not supported. Only Windows and Posix systems supported right now" +#endif + +#if !defined(OS_TYPE) + #error "System not supported. Only Windows and linux systems supported right now" +#endif + +#if !defined(DEBUG) && !defined(NDEBUG) +#define DEBUG +#endif -- cgit v1.2.3