aboutsummaryrefslogtreecommitdiff
path: root/src/UsersSidePanel.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-09 03:30:41 +0200
committerdec05eba <dec05eba@protonmail.com>2018-05-09 03:30:44 +0200
commitcd686f3f6099457a3115e758bf3da7e148ba432d (patch)
tree0f8212ef3d705bfc223990425389f8ad6a4ad733 /src/UsersSidePanel.cpp
parent161b5dbbf43d505b727e0ed3cae15458a72147f9 (diff)
Add avatar to users side panel
Diffstat (limited to 'src/UsersSidePanel.cpp')
-rw-r--r--src/UsersSidePanel.cpp52
1 files changed, 47 insertions, 5 deletions
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());
}
}