Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: imageSourceBase64 #584

Merged
merged 16 commits into from
Aug 22, 2023
3 changes: 1 addition & 2 deletions src/framework/graphics/texturemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class TextureManager
void preload(const std::string& fileName, bool smooth = true) { getTexture(fileName, smooth); }
TexturePtr getTexture(const std::string& fileName, bool smooth = true);
const TexturePtr& getEmptyTexture() { return m_emptyTexture; }

private:
TexturePtr loadTexture(std::stringstream& file);

private:
stdext::map<std::string, TexturePtr> m_textures;
std::vector<AnimatedTexturePtr> m_animatedTextures;
TexturePtr m_emptyTexture;
Expand Down
2 changes: 1 addition & 1 deletion src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,4 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<SoundChannel>("isEnabled", &SoundChannel::isEnabled);
g_lua.bindClassMemberFunction<SoundChannel>("getId", &SoundChannel::getId);
#endif
}
}
2 changes: 1 addition & 1 deletion src/framework/ui/uiwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class UIWidget : public LuaObject
EdgeGroup<int> m_imageBorder;

public:
void setImageSource(const std::string_view source);
void setImageSource(const std::string_view source, bool base64);
void setImageClip(const Rect& clipRect) { m_imageClipRect = clipRect; updateImageCache(); }
void setImageOffsetX(int x) { m_imageRect.setX(x); updateImageCache(); }
void setImageOffsetY(int y) { m_imageRect.setY(y); updateImageCache(); }
Expand Down
20 changes: 16 additions & 4 deletions src/framework/ui/uiwidgetimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
#include <framework/core/eventdispatcher.h>
#include <framework/graphics/painter.h>
#include <framework/graphics/animatedtexture.h>
#include <framework/graphics/image.h>
#include <framework/graphics/texture.h>
#include <framework/graphics/texturemanager.h>
#include "uiwidget.h"
#include "framework/graphics/drawpoolmanager.h"
#include <framework/util/crypt.h>

void UIWidget::initImage() {}

void UIWidget::parseImageStyle(const OTMLNodePtr& styleNode)
{
for (const auto& node : styleNode->children()) {
if (node->tag() == "image-source")
setImageSource(stdext::resolve_path(node->value(), node->source()));
setImageSource(stdext::resolve_path(node->value(), node->source()), false);
else if (node->tag() == "image-source-base64")
setImageSource(stdext::resolve_path(node->value(), node->source()), true);
else if (node->tag() == "image-offset-x")
setImageOffsetX(node->value<int>());
else if (node->tag() == "image-offset-y")
Expand Down Expand Up @@ -177,7 +181,7 @@ void UIWidget::drawImage(const Rect& screenCoords)
}
}

void UIWidget::setImageSource(const std::string_view source)
void UIWidget::setImageSource(const std::string_view source, bool base64)
{
updateImageCache();

Expand All @@ -187,7 +191,15 @@ void UIWidget::setImageSource(const std::string_view source)
return;
}

m_imageTexture = g_textures.getTexture(m_imageSource = source, isImageSmooth());
if (base64) {
std::stringstream stream;
const auto& decoded = g_crypt.base64Decode(source);
stream.write(decoded.c_str(), decoded.size());
m_imageTexture = g_textures.loadTexture(stream);
} else {
m_imageTexture = g_textures.getTexture(m_imageSource = source, isImageSmooth());
}

if (!m_imageTexture)
return;

Expand All @@ -211,4 +223,4 @@ void UIWidget::setImageSource(const std::string_view source)

setSize(size);
}
}
}
4 changes: 2 additions & 2 deletions src/framework/util/crypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ std::string Crypt::base64Encode(const std::string& decoded_string)
return ret;
}

std::string Crypt::base64Decode(const std::string& encoded_string)
std::string Crypt::base64Decode(const std::string_view& encoded_string)
{
int len = encoded_string.size();
int i = 0;
Expand Down Expand Up @@ -375,4 +375,4 @@ std::string Crypt::crc32(const std::string& decoded_string, bool upperCase)
else
std::transform(result.begin(), result.end(), result.begin(), tolower);
return result;
}
}
2 changes: 1 addition & 1 deletion src/framework/util/crypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Crypt
~Crypt();

std::string base64Encode(const std::string& decoded_string);
std::string base64Decode(const std::string& encoded_string);
std::string base64Decode(const std::string_view& encoded_string);
std::string xorCrypt(const std::string& buffer, const std::string& key);
std::string encrypt(const std::string& decrypted_string) { return _encrypt(decrypted_string, true); }
std::string decrypt(const std::string& encrypted_string) { return _decrypt(encrypted_string, true); }
Expand Down