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/MessageBoard.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'src/MessageBoard.cpp') 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(); -- cgit v1.2.3