From 607b15dbc2e1dfa8633e7ae679b709fe21c94599 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 29 Apr 2018 12:29:01 +0200 Subject: Fix image ratio, implement scroll locking (cant scroll outside messages) --- src/ChannelSidePanel.cpp | 3 ++- src/ChannelTopPanel.cpp | 5 ++--- src/Chatbar.cpp | 7 +++++++ src/ColorScheme.cpp | 20 ++++++++++++++++++++ src/Gif.cpp | 10 +++++++--- src/MessageBoard.cpp | 35 ++++++++++++++++++++++++++--------- src/Text.cpp | 10 ++++++++-- src/UsersSidePanel.cpp | 3 ++- 8 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 src/ColorScheme.cpp (limited to 'src') diff --git a/src/ChannelSidePanel.cpp b/src/ChannelSidePanel.cpp index cbda247..8d0b714 100644 --- a/src/ChannelSidePanel.cpp +++ b/src/ChannelSidePanel.cpp @@ -14,7 +14,7 @@ using namespace std; namespace dchat { vector channels; - const float WIDTH = 200.0f; + const float WIDTH = 300.0f; const unsigned int FONT_SIZE = 20; const float PADDING_BOTTOM = 10.0f; const float CHANNEL_NAME_BOX_HEIGHT_RATIO = 1.5f; @@ -60,6 +60,7 @@ namespace dchat str += sf::String::fromUtf8(channel->getName().begin(), channel->getName().end()); sf::Text text(str, *font, fontSize); text.setPosition(sf::Vector2f(position.x, floor(position.y + channelBoxHeight * 0.5f - fontHeight * 0.5f))); + text.setFillColor(ColorScheme::getTextRegularColor()); window.draw(text); position.y += floor(fontHeight + PADDING_BOTTOM); } diff --git a/src/ChannelTopPanel.cpp b/src/ChannelTopPanel.cpp index 9f57d27..8d9b0c8 100644 --- a/src/ChannelTopPanel.cpp +++ b/src/ChannelTopPanel.cpp @@ -17,7 +17,6 @@ namespace dchat const float BOTTOM_LINE_HEIGHT = 2.0f; const float PADDING_TOP = 20.0f; const float PADDING_BOTTOM = PADDING_TOP; - const sf::Font *FONT = ResourceCache::getFont("fonts/Roboto-Regular.ttf"); static void drawGradientLine(const sf::Vector2f &position, const sf::Vector2f &size, const LineColor &color, sf::RenderWindow &window) { @@ -62,7 +61,7 @@ namespace dchat sf::String str = "# "; str += sf::String::fromUtf8(currentChannel->getName().begin(), currentChannel->getName().end()); float fontSize = FONT_SIZE * Settings::getScaling(); - sf::Text text(str, *FONT, fontSize); + sf::Text text(str, *ResourceCache::getFont("fonts/Roboto-Regular.ttf"), fontSize); auto textBounds = text.getLocalBounds(); text.setPosition(floor((float)windowSize.x * 0.5f - textBounds.width * 0.5f), PADDING_TOP); text.setFillColor(ColorScheme::getTextRegularColor()); @@ -72,6 +71,6 @@ namespace dchat float ChannelTopPanel::getHeight() { float fontSize = FONT_SIZE * Settings::getScaling(); - return floor(FONT->getLineSpacing(fontSize) + PADDING_TOP + PADDING_BOTTOM); + return floor(ResourceCache::getFont("fonts/Roboto-Regular.ttf")->getLineSpacing(fontSize) + PADDING_TOP + PADDING_BOTTOM); } } diff --git a/src/Chatbar.cpp b/src/Chatbar.cpp index e88986a..f68d363 100644 --- a/src/Chatbar.cpp +++ b/src/Chatbar.cpp @@ -300,4 +300,11 @@ namespace dchat if(blinkElapsedTime > BLINK_TIME_VISIBLE_MS + BLINK_TIME_INVISIBLE_MS) blinkTimer.restart(); } + + float Chatbar::getHeight() + { + const float fontSize = FONT_SIZE * Settings::getScaling(); + const float fontHeight = ResourceCache::getFont("fonts/Roboto-Regular.ttf")->getLineSpacing(fontSize); + return PADDING_TOP + floor(fontHeight * 1.7f + BOX_PADDING_Y * 2.0f) + PADDING_BOTTOM; + } } diff --git a/src/ColorScheme.cpp b/src/ColorScheme.cpp new file mode 100644 index 0000000..e2138c3 --- /dev/null +++ b/src/ColorScheme.cpp @@ -0,0 +1,20 @@ +#include "../include/ColorScheme.hpp" + +namespace dchat +{ + ColorScheme::Type colorSchemeType = ColorScheme::Type::DARK; + + ColorScheme::Type ColorScheme::getType() + { + return colorSchemeType; + } + + void ColorScheme::setType(Type type) + { + colorSchemeType = type; + } + + sf::Color ColorScheme::getBackgroundColor(){ return sf::Color(40, 40, 40); } + sf::Color ColorScheme::getPanelColor() { return sf::Color(35, 35, 35); } + sf::Color ColorScheme::getTextRegularColor() { return sf::Color(240, 240, 240); } +} diff --git a/src/Gif.cpp b/src/Gif.cpp index eed5b98..5b83158 100644 --- a/src/Gif.cpp +++ b/src/Gif.cpp @@ -117,15 +117,19 @@ namespace dchat delete fileContent.data; } + sf::Vector2u Gif::getSize() const + { + return sprite.getTexture()->getSize(); + } + void Gif::setPosition(const sf::Vector2f &position) { sprite.setPosition(position); } - void Gif::setSize(const sf::Vector2f &size) + void Gif::setScale(const sf::Vector2f &scale) { - sf::Vector2u textureSize = sprite.getTexture()->getSize(); - sprite.setScale(size.x / (float)textureSize.x, size.y / (float)textureSize.y); + sprite.setScale(scale); } void Gif::draw(sf::RenderTarget &target) diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index fbc7484..d16d16c 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -5,6 +5,7 @@ #include "../include/ChannelSidePanel.hpp" #include "../include/UsersSidePanel.hpp" #include "../include/ChannelTopPanel.hpp" +#include "../include/Chatbar.hpp" #include "../include/ColorScheme.hpp" #include #include @@ -58,7 +59,8 @@ namespace dchat selectingText(false), leftMouseButtonPressed(false), scroll(0.0), - scrollSpeed(0.0) + scrollSpeed(0.0), + totalHeight(0.0) { updateStaticContentTexture(size); } @@ -133,8 +135,9 @@ namespace dchat void MessageBoard::draw(sf::RenderWindow &window, Cache &cache) { auto windowSize = window.getSize(); - sf::Vector2f backgroundSizeWithoutPadding(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth()), floor(windowSize.y - ChannelTopPanel::getHeight())); - sf::Vector2u backgroundSize(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth() - PADDING_SIDE * 2.0f), floor(windowSize.y)); + sf::Vector2f backgroundSizeWithoutPadding(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth()), floor(windowSize.y - ChannelTopPanel::getHeight() - Chatbar::getHeight())); + sf::Vector2u backgroundSize(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth() - PADDING_SIDE * 2.0f), floor(windowSize.y - ChannelTopPanel::getHeight() - Chatbar::getHeight() - PADDING_TOP)); + sf::Vector2f backgroundPos(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight()); //if(backgroundSize != staticContentTexture.getSize()) // updateStaticContentTexture(backgroundSize); @@ -154,19 +157,17 @@ namespace dchat double deltaTimeMicro = (double)frameTimer.getElapsedTime().asMicroseconds(); frameTimer.restart(); - scroll += scrollSpeed; - scrollSpeed /= (deltaTimeMicro * 0.0001); - if(dirty) { - sf::Vector2f position(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight() + PADDING_TOP); + sf::Vector2 position(ChannelSidePanel::getWidth(), ChannelTopPanel::getHeight() + PADDING_TOP); + double startHeight = position.y; position.y += scroll; for(Message *message : messages) { position.y += MESSAGE_PADDING_TOP; sf::Text usernameText(message->user->getName(), *usernameFont, 20 * Settings::getScaling()); float usernameTextHeight = usernameText.getFont()->getLineSpacing(usernameText.getCharacterSize()); - if(position.y + usernameTextHeight > 0.0f && position.y < backgroundSize.y) + if(position.y + usernameTextHeight > 0.0f && position.y < backgroundPos.y + backgroundSize.y) { usernameText.setFillColor(sf::Color(15, 192, 252)); usernameText.setPosition(sf::Vector2f(floor(position.x + PADDING_SIDE), floor(position.y))); @@ -181,9 +182,25 @@ namespace dchat message->text.draw(window, cache); position.y += message->text.getHeight() + MESSAGE_PADDING_BOTTOM; - if(position.y + LINE_HEIGHT > 0.0f && position.y < backgroundSize.y) + if(position.y + LINE_HEIGHT > 0.0f && position.y < backgroundPos.y + backgroundSize.y) drawGradientLine(sf::Vector2f(position.x + LINE_SIDE_PADDING, floor(position.y)), sf::Vector2f(backgroundSizeWithoutPadding.x - LINE_SIDE_PADDING * 2.0f, LINE_HEIGHT), LINE_COLOR, window); } + totalHeight = (position.y - scroll) - startHeight; + } + + scroll += scrollSpeed; + scrollSpeed /= (deltaTimeMicro * 0.0001); + + double textOverflow = backgroundSize.y - totalHeight; + if(scroll > 0.0 || textOverflow > 0.0) + { + scroll = 0.0; + scrollSpeed = 0.0; + } + else if(textOverflow < 0.0 && scroll < textOverflow) + { + scroll = textOverflow; + scrollSpeed = 0.0; } //staticContentTexture.display(); diff --git a/src/Text.cpp b/src/Text.cpp index 204c16f..3cafc69 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -347,16 +347,22 @@ namespace dchat { if(imageByUrlResult.isGif) { + auto gifSize = imageByUrlResult.gif->getSize(); + float widthToHeightRatio = (float)gifSize.x / (float)gifSize.y; + imageByUrlResult.gif->setPosition(pos); - imageByUrlResult.gif->setSize(size); + imageByUrlResult.gif->setScale(sf::Vector2f(size.x / (float)gifSize.x * widthToHeightRatio, size.y / (float)gifSize.y)); imageByUrlResult.gif->draw(target); } else { + auto textureSize = imageByUrlResult.texture->getSize(); + float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y; + // TODO: Store this sprite somewhere, might not be efficient to create a new sprite object every frame sf::Sprite sprite(*imageByUrlResult.texture); sprite.setPosition(pos); - sprite.setScale(size.x / (float)imageByUrlResult.texture->getSize().x, size.y / (float)imageByUrlResult.texture->getSize().y); + sprite.setScale(size.x / (float)textureSize.x * widthToHeightRatio, size.y / (float)textureSize.y); target.draw(sprite); } } diff --git a/src/UsersSidePanel.cpp b/src/UsersSidePanel.cpp index bea6296..f665d1f 100644 --- a/src/UsersSidePanel.cpp +++ b/src/UsersSidePanel.cpp @@ -13,7 +13,7 @@ using namespace std; namespace dchat { - const float WIDTH = 200.0f; + const float WIDTH = 250.0f; const unsigned int FONT_SIZE = 20; void UsersSidePanel::draw(sf::RenderWindow &window) @@ -37,6 +37,7 @@ namespace dchat 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.setFillColor(sf::Color(15, 192, 252)); window.draw(text); position.y += floor(font->getLineSpacing(FONT_SIZE * Settings::getScaling())); } -- cgit v1.2.3