diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-05-20 11:15:15 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-05-20 11:15:18 +0200 |
commit | 4c392178dac1de9a299beb78989c4e0f3fecade9 (patch) | |
tree | 552b7a7dfa58e8193705934059e28461815bb951 /src/Text.cpp | |
parent | 34e1d3d9d40f9b9139b801de99292a563c3c9a96 (diff) |
Add image preview and url/image open in browser
Diffstat (limited to 'src/Text.cpp')
-rw-r--r-- | src/Text.cpp | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/src/Text.cpp b/src/Text.cpp index 9688ad1..3aa1574 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -3,6 +3,8 @@ #include "../include/Gif.hpp" #include "../include/WebPagePreview.hpp" #include "../include/ColorScheme.hpp" +#include "../include/ImagePreview.hpp" +#include "../include/StringUtils.hpp" #include <SFML/Graphics/RectangleShape.hpp> #include <cmath> #include <process.hpp> @@ -439,7 +441,6 @@ namespace dchat { // If there was a space in the text and text width is too long, then we need to word wrap at space index instead, // which means we need to change the position of all vertices after the space to the current vertex - //printf("last spacing word wrap index: %zu\n", lastSpacingWordWrapIndex); if(lastSpacingWordWrapIndex != -1) { for(size_t j = lastSpacingWordWrapIndex; j < i; ++j) @@ -698,8 +699,81 @@ namespace dchat return result; } - void Text::processEvent(const sf::Event &event) + void Text::onMouseClick(const sf::Event::MouseButtonEvent &event, Cache &cache) { + if(event.button != sf::Mouse::Button::Left) return; + float vspace = font->getLineSpacing(characterSize); + + for(TextElement &textElement : textElements) + { + if(textElement.type == TextElement::Type::URL) + { + 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<char>*)&u8Str; + const ContentByUrlResult contentByUrlResult = cache.getContentByUrl(utf8Str); + if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) + { + switch(contentByUrlResult.cachedType) + { + case ContentByUrlResult::CachedType::TEXTURE: + { + 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; + } + 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) + { + ImagePreview::preview(contentByUrlResult.gif, utf8Str); + return; + } + 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) + { + // 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; + } + } + } + } + } + } + + void Text::processEvent(const sf::Event &event, Cache &cache) + { + if(event.type == sf::Event::MouseButtonReleased) + { + onMouseClick(event.mouseButton, cache); + } + if(!editable) return; bool caretAtEnd = textElements.size() == 0 || textElements[0].text.size == 0 || caretIndex == textElements[0].text.size; @@ -798,7 +872,7 @@ namespace dchat } } - void Text::draw(sf::RenderTarget &target, Cache &cache) + bool Text::draw(sf::RenderTarget &target, Cache &cache) { if(dirtyText) { @@ -831,7 +905,7 @@ 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) return; + if(pos.y + getHeight() <= 0.0f || pos.y >= target.getSize().y) return false; /* if(editable) { @@ -972,7 +1046,7 @@ namespace dchat } } - if(!editable) return; + if(!editable) return true; //float rows = floor(totalHeight / (vspace + lineSpacing)); const float caretRow = getRowByPosition(caretPosition); @@ -981,5 +1055,6 @@ namespace dchat caretRect.setFillColor(sf::Color::White); caretRect.setPosition(sf::Vector2f(floor(pos.x + caretPosition.x), floor(pos.y + caretRow * (vspace + lineSpacing)))); target.draw(caretRect); + return true; } } |