From 725ea566a2b6a12e0a02e4f570b6e99102e2d21b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 8 Apr 2019 21:04:12 +0200 Subject: Refactor, remove a lot of code and use dchat core instead --- src/Text.cpp | 191 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 104 insertions(+), 87 deletions(-) (limited to 'src/Text.cpp') diff --git a/src/Text.cpp b/src/Text.cpp index 39e339f..6e3aac9 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -1,12 +1,15 @@ #include "../include/Text.hpp" -#include "../include/Cache.hpp" -#include "../include/Gif.hpp" -#include "../include/WebPagePreview.hpp" #include "../include/ColorScheme.hpp" #include "../include/ImagePreview.hpp" -#include "../include/StringUtils.hpp" +#include "../include/StaticImage.hpp" +#include "../include/Gif.hpp" +#include "../include/WebPagePreview.hpp" +#include "../include/ResourceCache.hpp" #include #include +#include +#include +#include #include namespace dchat @@ -718,75 +721,79 @@ namespace dchat return static_cast(1.0f + position.y / (vspace + lineSpacing)); } - void Text::onMouseClick(const sf::Event::MouseButtonEvent &event, Cache &cache) + void Text::onMouseClick(const sf::Event::MouseButtonEvent &event, Cache *cache) { if(event.button != sf::Mouse::Button::Left) return; float vspace = font->getLineSpacing(characterSize); - + + sf::Vector2f pos = position; + pos.y += floor(vspace); // Origin is at bottom left, we want it to be at top left + if(pos.y + getHeight() <= 0.0f || pos.y >= renderTargetSize.y) + return; + for(TextElement &textElement : textElements) { - if(textElement.type == TextElement::Type::URL) + if(textElement.type != TextElement::Type::URL) + continue; + + sf::Vector2f pos = position; + pos.y += floor(textElement.position.y); + float imageHeight = floor(vspace * IMAGE_HEIGHT_SCALE); + + // TODO: Optimize this (add unordered_map that takes StringViewUtf32 as key) + auto u8Str = sf::String::fromUtf32(textElement.text.data, textElement.text.data + textElement.text.size).toUtf8(); + const std::string &utf8Str = *(std::basic_string*)&u8Str; + const ContentByUrlResult contentByUrlResult = cache->getContentByUrl(utf8Str); + if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { - sf::Vector2f pos = position; - pos.y += floor(textElement.position.y); - float imageHeight = floor(vspace * IMAGE_HEIGHT_SCALE); - - // TODO: Optimize this (add unordered_map that takes StringViewUtf32 as key) - auto u8Str = sf::String::fromUtf32(textElement.text.data, textElement.text.data + textElement.text.size).toUtf8(); - const std::string &utf8Str = *(std::basic_string*)&u8Str; - const ContentByUrlResult contentByUrlResult = cache.getContentByUrl(utf8Str); - if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) + switch(contentByUrlResult.cachedType) { - switch(contentByUrlResult.cachedType) + case ContentByUrlResult::CachedType::STATIC_IMAGE: { - case ContentByUrlResult::CachedType::TEXTURE: + auto *staticImage = static_cast(contentByUrlResult.staticImage); + auto textureSize = staticImage->texture.getSize(); + float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y; + float imageWidth = fmin(imageHeight * widthToHeightRatio, maxWidth); + if(event.x >= pos.x && event.x <= pos.x + imageWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) { - auto textureSize = contentByUrlResult.texture->getSize(); - float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y; - float imageWidth = fmin(imageHeight * widthToHeightRatio, maxWidth); - if(event.x >= pos.x && event.x <= pos.x + imageWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) - { - ImagePreview::preview(contentByUrlResult.texture, utf8Str); - return; - } - break; + ImagePreview::preview(&staticImage->texture, utf8Str); + return; } - case ContentByUrlResult::CachedType::GIF: + break; + } + case ContentByUrlResult::CachedType::GIF: + { + auto textureSize = contentByUrlResult.gif->getSize(); + float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y; + float imageWidth = fmin(imageHeight * widthToHeightRatio, maxWidth); + if(event.x >= pos.x && event.x <= pos.x + imageWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) { - auto textureSize = contentByUrlResult.gif->getSize(); - float widthToHeightRatio = (float)textureSize.x / (float)textureSize.y; - float imageWidth = fmin(imageHeight * widthToHeightRatio, maxWidth); - if(event.x >= pos.x && event.x <= pos.x + imageWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) - { - ImagePreview::preview(contentByUrlResult.gif, utf8Str); - return; - } - break; + ImagePreview::preview(contentByUrlResult.gif, utf8Str); + return; } - case ContentByUrlResult::CachedType::WEB_PAGE_PREVIEW: + break; + } + case ContentByUrlResult::CachedType::WEB_PAGE_PREVIEW: + { + const float previewWidth = fmin(maxWidth, floor(imageHeight * 1.77f)); + if(event.x >= pos.x && event.x <= pos.x + previewWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) { - const float previewWidth = fmin(maxWidth, floor(imageHeight * 1.77f)); - if(event.x >= pos.x && event.x <= pos.x + previewWidth && event.y >= pos.y && event.y <= pos.y + imageHeight) - { - // TODO: Implement for other platforms than linux - std::string escapedUrl = stringReplaceChar(utf8Str, "'", ""); - escapedUrl = stringReplaceChar(escapedUrl, "\\", ""); - std::string cmd = "xdg-open '"; - cmd += escapedUrl; - cmd += "'"; - printf("Clicked on web page preview, opening web page by running command: %s\n", cmd.c_str()); - system(cmd.c_str()); - return; - } - break; + // TODO: Implement for other platforms than linux + std::string cmd = "xdg-open '"; + cmd += escapeCommand(utf8Str); + cmd += "'"; + printf("Clicked on web page preview, opening web page by running command: %s\n", cmd.c_str()); + system(cmd.c_str()); + return; } + break; } } } } } - void Text::processEvent(const sf::Event &event, Cache &cache) + void Text::processEvent(const sf::Event &event, Cache *cache) { if(event.type == sf::Event::MouseButtonReleased) { @@ -890,7 +897,7 @@ namespace dchat } } - bool Text::draw(sf::RenderTarget &target, Cache &cache) + bool Text::draw(sf::RenderTarget &target, Cache *cache) { if(dirtyText) { @@ -923,7 +930,8 @@ namespace dchat //sf::FloatRect textRect(pos.x, pos.y, maxWidth, ) //colRect.contains() //if(pos.x + maxWidth <= 0.0f || pos.x >= maxWidth || pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return; - if(pos.y + getHeight() <= 0.0f || pos.y >= target.getSize().y) + renderTargetSize = target.getSize(); + if(pos.y + getHeight() <= 0.0f || pos.y >= renderTargetSize.y) { if(!editable && visible && lastSeenTimer.getElapsedTime().asMilliseconds() > 3000) { @@ -958,29 +966,33 @@ namespace dchat // TODO: Optimize this (add unordered_map that takes StringViewUtf32 as key) auto u8Str = sf::String::fromUtf32(textElement.text.data, textElement.text.data + textElement.text.size).toUtf8(); const std::string &utf8Str = *(std::basic_string*)&u8Str; - const ContentByUrlResult contentByUrlResult = cache.getContentByUrl(utf8Str); + const ContentByUrlResult contentByUrlResult = cache->getContentByUrl(utf8Str); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { switch(contentByUrlResult.cachedType) { case ContentByUrlResult::CachedType::GIF: { - auto gifSize = contentByUrlResult.gif->getSize(); + auto *gif = static_cast(contentByUrlResult.gif); + gif->update(); + auto gifSize = gif->getSize(); float widthToHeightRatio = (float)gifSize.x / (float)gifSize.y; - - contentByUrlResult.gif->setPosition(pos); - contentByUrlResult.gif->setScale(sf::Vector2f(size.x / (float)gifSize.x * widthToHeightRatio, size.y / (float)gifSize.y)); - contentByUrlResult.gif->setColor(sf::Color::White); - contentByUrlResult.gif->draw(target); + + sf::Sprite sprite(gif->texture); + sprite.setPosition(pos); + sprite.setScale(sf::Vector2f(size.x / (float)gifSize.x * widthToHeightRatio, size.y / (float)gifSize.y)); + sprite.setColor(sf::Color::White); + target.draw(sprite); break; } - case ContentByUrlResult::CachedType::TEXTURE: + case ContentByUrlResult::CachedType::STATIC_IMAGE: { - auto textureSize = contentByUrlResult.texture->getSize(); + auto *staticImage = static_cast(contentByUrlResult.staticImage); + auto textureSize = staticImage->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(*contentByUrlResult.texture); + sf::Sprite sprite(staticImage->texture); sprite.setPosition(pos); sprite.setScale(size.x / (float)textureSize.x * widthToHeightRatio, size.y / (float)textureSize.y); target.draw(sprite); @@ -1008,29 +1020,33 @@ namespace dchat // TODO: Optimize this (add unordered_map that takes StringViewUtf32 as key) auto u8Str = sf::String::fromUtf32(textElement.text.data, textElement.text.data + textElement.text.size).toUtf8(); const std::string &utf8Str = *(std::basic_string*)&u8Str; - const ContentByUrlResult contentByUrlResult = cache.getContentByUrl(utf8Str); + const ContentByUrlResult contentByUrlResult = cache->getContentByUrl(utf8Str); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { switch(contentByUrlResult.cachedType) { case ContentByUrlResult::CachedType::GIF: { - auto gifSize = contentByUrlResult.gif->getSize(); + auto *gif = static_cast(contentByUrlResult.gif); + gif->update(); + auto gifSize = gif->getSize(); float widthToHeightRatio = (float)gifSize.x / (float)gifSize.y; - contentByUrlResult.gif->setPosition(pos); - contentByUrlResult.gif->setScale(sf::Vector2f(imageHeight / (float)gifSize.x * widthToHeightRatio, imageHeight / (float)gifSize.y)); - contentByUrlResult.gif->setColor(sf::Color::White); - contentByUrlResult.gif->draw(target); + sf::Sprite sprite(gif->texture); + sprite.setPosition(pos); + sprite.setScale(sf::Vector2f(imageHeight / (float)gifSize.x * widthToHeightRatio, imageHeight / (float)gifSize.y)); + sprite.setColor(sf::Color::White); + target.draw(sprite); break; } - case ContentByUrlResult::CachedType::TEXTURE: + case ContentByUrlResult::CachedType::STATIC_IMAGE: { - auto textureSize = contentByUrlResult.texture->getSize(); + auto *staticImage = static_cast(contentByUrlResult.staticImage); + auto textureSize = staticImage->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(*contentByUrlResult.texture); + sf::Sprite sprite(staticImage->texture); sprite.setPosition(pos); sprite.setScale(imageHeight / (float)textureSize.x * widthToHeightRatio, imageHeight / (float)textureSize.y); target.draw(sprite); @@ -1038,23 +1054,24 @@ namespace dchat } case ContentByUrlResult::CachedType::WEB_PAGE_PREVIEW: { + auto *webPagePreview = static_cast(contentByUrlResult.webPagePreview); const float previewWidth = fmin(maxWidth, floor(imageHeight * 1.77f)); - contentByUrlResult.webPagePreview->title.setCharacterSize(characterSize); - contentByUrlResult.webPagePreview->title.setMaxWidth(previewWidth); - contentByUrlResult.webPagePreview->title.setPosition(pos); - contentByUrlResult.webPagePreview->title.setLineSpacing(0.0f); - contentByUrlResult.webPagePreview->title.setFillColor(URL_COLOR); - contentByUrlResult.webPagePreview->title.draw(target, cache); + webPagePreview->title.setCharacterSize(characterSize); + webPagePreview->title.setMaxWidth(previewWidth); + webPagePreview->title.setPosition(pos); + webPagePreview->title.setLineSpacing(0.0f); + webPagePreview->title.setFillColor(URL_COLOR); + webPagePreview->title.draw(target, cache); - pos.y += contentByUrlResult.webPagePreview->title.getHeight(); + pos.y += webPagePreview->title.getHeight(); - contentByUrlResult.webPagePreview->description.setCharacterSize(characterSize); - contentByUrlResult.webPagePreview->description.setMaxWidth(previewWidth); - contentByUrlResult.webPagePreview->description.setPosition(pos); - contentByUrlResult.webPagePreview->description.setLineSpacing(0.0f); - contentByUrlResult.webPagePreview->description.setFillColor(color); - contentByUrlResult.webPagePreview->description.draw(target, cache); + webPagePreview->description.setCharacterSize(characterSize); + webPagePreview->description.setMaxWidth(previewWidth); + webPagePreview->description.setPosition(pos); + webPagePreview->description.setLineSpacing(0.0f); + webPagePreview->description.setFillColor(color); + webPagePreview->description.draw(target, cache); break; } } -- cgit v1.2.3