diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-04-29 12:29:01 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-04-29 12:29:13 +0200 |
commit | 607b15dbc2e1dfa8633e7ae679b709fe21c94599 (patch) | |
tree | 6e323ec65de8c5f362a08abf8fef5c12b8398d4f | |
parent | e7caed2208893723181892d5e197b924311373fb (diff) |
Fix image ratio, implement scroll locking (cant scroll outside messages)
-rw-r--r-- | include/Chatbar.hpp | 2 | ||||
-rw-r--r-- | include/ColorScheme.hpp | 14 | ||||
-rw-r--r-- | include/Gif.hpp | 4 | ||||
-rw-r--r-- | include/MessageBoard.hpp | 1 | ||||
-rw-r--r-- | src/ChannelSidePanel.cpp | 3 | ||||
-rw-r--r-- | src/ChannelTopPanel.cpp | 5 | ||||
-rw-r--r-- | src/Chatbar.cpp | 7 | ||||
-rw-r--r-- | src/ColorScheme.cpp | 20 | ||||
-rw-r--r-- | src/Gif.cpp | 10 | ||||
-rw-r--r-- | src/MessageBoard.cpp | 35 | ||||
-rw-r--r-- | src/Text.cpp | 10 | ||||
-rw-r--r-- | src/UsersSidePanel.cpp | 3 |
12 files changed, 91 insertions, 23 deletions
diff --git a/include/Chatbar.hpp b/include/Chatbar.hpp index b6c6be8..6ae2190 100644 --- a/include/Chatbar.hpp +++ b/include/Chatbar.hpp @@ -30,6 +30,8 @@ namespace dchat void processEvent(const sf::Event &event, Channel *channel); void draw(sf::RenderWindow &window); + + static float getHeight(); private: void processChatCommand(const StringView &cmd); private: diff --git a/include/ColorScheme.hpp b/include/ColorScheme.hpp index 07010ba..64dbeec 100644 --- a/include/ColorScheme.hpp +++ b/include/ColorScheme.hpp @@ -7,9 +7,17 @@ namespace dchat class ColorScheme { public: - static sf::Color getBackgroundColor() { return sf::Color(40, 40, 40); } - static sf::Color getPanelColor() { return sf::Color(35, 35, 35); } + enum class Type + { + LIGHT, + DARK + }; - static sf::Color getTextRegularColor() { return sf::Color(240, 240, 240); } + static Type getType(); + static void setType(Type type); + + static sf::Color getBackgroundColor(); + static sf::Color getPanelColor(); + static sf::Color getTextRegularColor(); }; } diff --git a/include/Gif.hpp b/include/Gif.hpp index 87e6956..84299e9 100644 --- a/include/Gif.hpp +++ b/include/Gif.hpp @@ -28,8 +28,10 @@ namespace dchat Gif(StringView &&fileContent); ~Gif(); + sf::Vector2u getSize() const; + void setPosition(const sf::Vector2f &position); - void setSize(const sf::Vector2f &size); + void setScale(const sf::Vector2f &scale); void draw(sf::RenderTarget &target); static bool isDataGif(const StringView &data); diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index b431af8..739f161 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -35,5 +35,6 @@ namespace dchat double scroll; double scrollSpeed; sf::Clock frameTimer; + double totalHeight; }; } 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<Channel*> 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 <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/Sprite.hpp> @@ -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<double> 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())); } |