diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-05-09 03:30:41 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-05-09 03:30:44 +0200 |
commit | cd686f3f6099457a3115e758bf3da7e148ba432d (patch) | |
tree | 0f8212ef3d705bfc223990425389f8ad6a4ad733 | |
parent | 161b5dbbf43d505b727e0ed3cae15458a72147f9 (diff) |
Add avatar to users side panel
-rw-r--r-- | include/UsersSidePanel.hpp | 3 | ||||
-rw-r--r-- | src/ContextMenu.cpp | 4 | ||||
-rw-r--r-- | src/MessageBoard.cpp | 5 | ||||
-rw-r--r-- | src/UsersSidePanel.cpp | 52 | ||||
-rw-r--r-- | src/main.cpp | 2 |
5 files changed, 55 insertions, 11 deletions
diff --git a/include/UsersSidePanel.hpp b/include/UsersSidePanel.hpp index 07e790c..9e48ee6 100644 --- a/include/UsersSidePanel.hpp +++ b/include/UsersSidePanel.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Cache.hpp" #include <SFML/Graphics/RenderWindow.hpp> namespace dchat @@ -9,7 +10,7 @@ namespace dchat class UsersSidePanel { public: - static void draw(sf::RenderWindow &window); + static void draw(sf::RenderWindow &window, Cache &cache); static float getWidth(); }; } diff --git a/src/ContextMenu.cpp b/src/ContextMenu.cpp index 317d34a..da4b8dd 100644 --- a/src/ContextMenu.cpp +++ b/src/ContextMenu.cpp @@ -51,7 +51,7 @@ namespace dchat void ContextMenu::processEvent(const sf::Event &event) { - if(!visible) return; + if(!visible || menuItems.empty()) return; if(event.type == sf::Event::MouseButtonPressed) { @@ -83,7 +83,7 @@ namespace dchat void ContextMenu::draw(sf::RenderWindow &window) { - if(!visible) return; + if(!visible || menuItems.empty()) return; const sf::Font *menuItemFont = ResourceCache::getFont(MENU_FONT_PATH); float boxHeight = menuItemFont->getLineSpacing(MENU_ITEM_FONT_SIZE) * 1.5f; diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index f2796e5..7df5018 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -122,6 +122,8 @@ namespace dchat sf::RectangleShape lineRect(sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * Settings::getScaling() * 2.0f, LINE_HEIGHT)); lineRect.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10)); + sf::Shader *circleShader = ResourceCache::getShader("shaders/circleMask.glsl", sf::Shader::Fragment); + sf::Vector2<double> position(ChannelSidePanel::getWidth() + PADDING_SIDE * Settings::getScaling(), ChannelTopPanel::getHeight() + PADDING_TOP); double startHeight = position.y; position.y += scroll; @@ -171,7 +173,6 @@ namespace dchat const ContentByUrlResult avatarResult = cache.getContentByUrl(message->user->avatarUrl, 1024 * 1024); if(avatarResult.type == ContentByUrlResult::Type::CACHED) { - sf::Shader *circleShader = ResourceCache::getShader("shaders/circleMask.glsl", sf::Shader::Fragment); circleShader->setUniform("texture", sf::Shader::CurrentTexture); if(avatarResult.cachedType == ContentByUrlResult::CachedType::TEXTURE) @@ -366,7 +367,7 @@ namespace dchat scroll += scrollSpeed; - double deltaTimeScrollMultiplier = deltaTimeMicro * 0.00004; + double deltaTimeScrollMultiplier = deltaTimeMicro * 0.0001; if(scrollSpeed > 0.0) { scrollSpeed -= deltaTimeScrollMultiplier; diff --git a/src/UsersSidePanel.cpp b/src/UsersSidePanel.cpp index 672dffa..c3f2a78 100644 --- a/src/UsersSidePanel.cpp +++ b/src/UsersSidePanel.cpp @@ -4,7 +4,10 @@ #include "../include/Settings.hpp" #include "../include/Channel.hpp" #include "../include/ColorScheme.hpp" +#include "../include/Gif.hpp" #include <SFML/Graphics/RectangleShape.hpp> +#include <SFML/Graphics/CircleShape.hpp> +#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Text.hpp> #include <vector> #include <cmath> @@ -16,7 +19,11 @@ namespace dchat const float WIDTH = 250.0f; const unsigned int FONT_SIZE = 20; - void UsersSidePanel::draw(sf::RenderWindow &window) + const float AVATAR_DIAMETER = 40.0f; + const float AVATAR_PADDING_SIDE = 10.0f; + const float PADDING_BOTTOM = 20.0f; + + void UsersSidePanel::draw(sf::RenderWindow &window, Cache &cache) { float posY = ChannelTopPanel::getHeight(); auto windowSize = window.getSize(); @@ -34,21 +41,56 @@ namespace dchat // TODO: Remove this shit sf::String str = "Online - "; str += to_string(currentChannel->getUsers().size()); - sf::Text text(str, *font, FONT_SIZE * Settings::getScaling() * 1.25f); + sf::Text text(str, *font, FONT_SIZE * Settings::getScaling() * 1.0f); text.setPosition(position); - text.setFillColor(ColorScheme::getTextRegularColor()); + text.setFillColor(ColorScheme::getTextRegularColor() * sf::Color(255, 255, 255, 100)); window.draw(text); position.y += floor(font->getLineSpacing(text.getCharacterSize())); + position.y += (PADDING_BOTTOM * Settings::getScaling()); + + sf::Shader *circleShader = ResourceCache::getShader("shaders/circleMask.glsl", sf::Shader::Fragment); + const float textHeight = font->getLineSpacing(FONT_SIZE * Settings::getScaling()); for(User *user : currentChannel->getUsers()) { + // Max avatar size = 1mb + const ContentByUrlResult avatarResult = cache.getContentByUrl(user->avatarUrl, 1024 * 1024); + if(avatarResult.type == ContentByUrlResult::Type::CACHED) + { + circleShader->setUniform("texture", sf::Shader::CurrentTexture); + + if(avatarResult.cachedType == ContentByUrlResult::CachedType::TEXTURE) + { + // 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.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->setScale(sf::Vector2f(AVATAR_DIAMETER * Settings::getScaling() / (float)gifSize.x, AVATAR_DIAMETER * Settings::getScaling() / (float)gifSize.y)); + avatarResult.gif->draw(window, circleShader); + } + } + else + { + sf::CircleShape avatarCircle(AVATAR_DIAMETER * 0.5f * Settings::getScaling(), 60 * Settings::getScaling()); + avatarCircle.setPosition(sf::Vector2f(floor(position.x), floor(position.y))); + avatarCircle.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(30, 30, 30)); + window.draw(avatarCircle); + } + // TODO: Remove this shit sf::String str = sf::String::fromUtf8(user->getName().begin(), user->getName().end()); sf::Text text(str, *font, FONT_SIZE * Settings::getScaling()); - text.setPosition(position); + text.setPosition(floor(position.x + (AVATAR_DIAMETER + AVATAR_PADDING_SIDE) * Settings::getScaling()), floor(position.y + AVATAR_DIAMETER * 0.5f - textHeight * 0.5f)); text.setFillColor(sf::Color(15, 192, 252)); window.draw(text); - position.y += floor(font->getLineSpacing(FONT_SIZE * Settings::getScaling())); + position.y += ((AVATAR_DIAMETER + PADDING_BOTTOM) * Settings::getScaling()); } } diff --git a/src/main.cpp b/src/main.cpp index 910724e..18f10fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -658,7 +658,7 @@ int main(int argc, char **argv) window.clear(ColorScheme::getBackgroundColor()); ChannelSidePanel::draw(window); currentChannel->draw(window, cache); - UsersSidePanel::draw(window); + UsersSidePanel::draw(window, cache); ChannelTopPanel::draw(window); GlobalContextMenu::draw(window); |