aboutsummaryrefslogtreecommitdiff
path: root/src/Text.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Text.cpp')
-rw-r--r--src/Text.cpp191
1 files changed, 104 insertions, 87 deletions
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 <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Window/Clipboard.hpp>
+#include <dchat/Gif.hpp>
+#include <dchat/WebPagePreview.hpp>
+#include <dchat/Process.hpp>
#include <cmath>
namespace dchat
@@ -718,75 +721,79 @@ namespace dchat
return static_cast<int>(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<char>*)&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<char>*)&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<SfmlStaticImage*>(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<char>*)&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<SfmlGif*>(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<SfmlStaticImage*>(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<char>*)&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<SfmlGif*>(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<SfmlStaticImage*>(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<SfmlWebPagePreview*>(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;
}
}