remove obtrusive static image component and add svg caching

This commit is contained in:
Sophia Hadash 2021-09-27 00:41:53 +02:00
parent 39f67cfcdf
commit 593b6d94ee
6 changed files with 40 additions and 50 deletions

View file

@ -16,52 +16,41 @@
// Available slot definitions.
const std::vector<std::string> BadgesComponent::mSlots = {SLOT_FAVORITE, SLOT_COMPLETED, SLOT_KIDS, SLOT_BROKEN,
SLOT_ALTERNATIVE_EMULATOR};
std::map<std::string, std::string> BadgesComponent::mBadgeIcons = std::map<std::string, std::string>();
std::map<std::string, ImageComponent> BadgesComponent::mImageComponents = std::map<std::string, ImageComponent>();
std::vector<BadgesComponent *> BadgesComponent::mInstances = {};
BadgesComponent::BadgesComponent(Window *window)
: FlexboxComponent(window) {
if (mBadgeIcons.empty()) {
mBadgeIcons[SLOT_FAVORITE] = ":/graphics/badge_favorite.svg";
mBadgeIcons[SLOT_COMPLETED] = ":/graphics/badge_completed.svg";
mBadgeIcons[SLOT_KIDS] = ":/graphics/badge_kidgame.svg";
mBadgeIcons[SLOT_BROKEN] = ":/graphics/badge_broken.svg";
mBadgeIcons[SLOT_ALTERNATIVE_EMULATOR] = ":/graphics/badge_altemu.svg";
}
mBadgeIcons = std::map<std::string, std::string>();
mBadgeIcons[SLOT_FAVORITE] = ":/graphics/badge_favorite.svg";
mBadgeIcons[SLOT_COMPLETED] = ":/graphics/badge_completed.svg";
mBadgeIcons[SLOT_KIDS] = ":/graphics/badge_kidgame.svg";
mBadgeIcons[SLOT_BROKEN] = ":/graphics/badge_broken.svg";
mBadgeIcons[SLOT_ALTERNATIVE_EMULATOR] = ":/graphics/badge_altemu.svg";
// Create the child ImageComponent for every badge.
if (mImageComponents.empty()) {
ImageComponent mImageFavorite = ImageComponent(window);
mImageFavorite.setImage(mBadgeIcons[SLOT_FAVORITE], false, true);
mImageComponents.insert({SLOT_FAVORITE, mImageFavorite});
ImageComponent mImageCompleted = ImageComponent(window);
mImageCompleted.setImage(mBadgeIcons[SLOT_COMPLETED], false, true);
mImageComponents.insert({SLOT_COMPLETED, mImageCompleted});
ImageComponent mImageKids = ImageComponent(window);
mImageKids.setImage(mBadgeIcons[SLOT_KIDS], false, true);
mImageComponents.insert({SLOT_KIDS, mImageKids});
ImageComponent mImageBroken = ImageComponent(window);
mImageBroken.setImage(mBadgeIcons[SLOT_BROKEN], false, true);
mImageComponents.insert({SLOT_BROKEN, mImageBroken});
ImageComponent mImageAltEmu = ImageComponent(window);
mImageAltEmu.setImage(mBadgeIcons[SLOT_ALTERNATIVE_EMULATOR], false, true);
mImageComponents.insert({SLOT_ALTERNATIVE_EMULATOR, mImageAltEmu});
}
mInstances.push_back(this);
mImageComponents = std::map<std::string, ImageComponent>();
ImageComponent mImageFavorite = ImageComponent(window);
mImageFavorite.setImage(mBadgeIcons[SLOT_FAVORITE], false, true, true);
mImageComponents.insert({SLOT_FAVORITE, mImageFavorite});
ImageComponent mImageCompleted = ImageComponent(window);
mImageCompleted.setImage(mBadgeIcons[SLOT_COMPLETED], false, true, true);
mImageComponents.insert({SLOT_COMPLETED, mImageCompleted});
ImageComponent mImageKids = ImageComponent(window);
mImageKids.setImage(mBadgeIcons[SLOT_KIDS], false, true, true);
mImageComponents.insert({SLOT_KIDS, mImageKids});
ImageComponent mImageBroken = ImageComponent(window);
mImageBroken.setImage(mBadgeIcons[SLOT_BROKEN], false, true, true);
mImageComponents.insert({SLOT_BROKEN, mImageBroken});
ImageComponent mImageAltEmu = ImageComponent(window);
mImageAltEmu.setImage(mBadgeIcons[SLOT_ALTERNATIVE_EMULATOR], false, true, true);
mImageComponents.insert({SLOT_ALTERNATIVE_EMULATOR, mImageAltEmu});
}
BadgesComponent::~BadgesComponent() noexcept {
for (GuiComponent *c: mChildren)
c->clearChildren();
clearChildren();
mInstances.erase(std::remove(mInstances.begin(), mInstances.end(), this), mInstances.end());
if (mInstances.empty()) {
mBadgeIcons.clear();
mImageComponents.clear();
}
mBadgeIcons.clear();
mImageComponents.clear();
}
@ -107,7 +96,7 @@ void BadgesComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
for (auto& slot : mSlots) {
if (properties & PATH && elem->has(slot) && mBadgeIcons[slot] != elem->get<std::string>(slot)) {
mBadgeIcons[slot] = elem->get<std::string>(slot);
mImageComponents.find(slot)->second.setImage(mBadgeIcons[slot], false, true);
mImageComponents.find(slot)->second.setImage(mBadgeIcons[slot], false, true, true);
imgChanged = true;
}
}

View file

@ -43,9 +43,8 @@ public:
private:
static const std::vector<std::string> mSlots;
static std::map<std::string, std::string> mBadgeIcons;
static std::map<std::string, ImageComponent> mImageComponents;
static std::vector<BadgesComponent *> mInstances;
std::map<std::string, std::string> mBadgeIcons;
std::map<std::string, ImageComponent> mImageComponents;
};
#endif // ES_APP_COMPONENTS_BADGES_COMPONENT_H

View file

@ -126,8 +126,7 @@ void ImageComponent::resize()
onSizeChanged();
}
void ImageComponent::setImage(std::string path, bool tile, bool linearMagnify)
{
void ImageComponent::setImage(std::string path, bool tile, bool linearMagnify, bool cacheImage) {
// Always load bundled graphic resources statically, unless mForceLoad has been set.
// This eliminates annoying texture pop-in problems that would otherwise occur.
if (!mForceLoad && (path[0] == ':') && (path[1] == '/')) {
@ -139,10 +138,10 @@ void ImageComponent::setImage(std::string path, bool tile, bool linearMagnify)
mTexture.reset();
else
mTexture =
TextureResource::get(mDefaultPath, tile, mForceLoad, mDynamic, linearMagnify);
TextureResource::get(mDefaultPath, tile, mForceLoad, mDynamic, linearMagnify, 1.0f, cacheImage);
}
else {
mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic, linearMagnify);
mTexture = TextureResource::get(path, tile, mForceLoad, mDynamic, linearMagnify, 1.0f, cacheImage);
}
resize();

View file

@ -24,7 +24,7 @@ public:
// Loads the image at the given filepath. Will tile if tile is true (retrieves texture
// as tiling, creates vertices accordingly).
void setImage(std::string path, bool tile = false, bool linearMagnify = false);
void setImage(std::string path, bool tile = false, bool linearMagnify = false, bool cacheSVG = false);
// Loads an image from memory.
void setImage(const char *data, size_t length, bool tile = false);

View file

@ -143,12 +143,13 @@ bool TextureResource::bind()
}
}
std::shared_ptr<TextureResource> TextureResource::get(const std::string& path,
std::shared_ptr<TextureResource> TextureResource::get(const std::string &path,
bool tile,
bool forceLoad,
bool dynamic,
bool linearMagnify,
float scaleDuringLoad)
float scaleDuringLoad,
bool cacheImage)
{
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
@ -176,7 +177,7 @@ std::shared_ptr<TextureResource> TextureResource::get(const std::string& path,
std::shared_ptr<TextureData> data = sTextureDataManager.get(tex.get());
// Is it an SVG?
if (key.first.substr(key.first.size() - 4, std::string::npos) != ".svg") {
if (key.first.substr(key.first.size() - 4, std::string::npos) != ".svg" || cacheImage) {
// Probably not. Add it to our map. We don't add SVGs because 2 SVGs might be
// rasterized at different sizes.
sTextureMap[key] = std::weak_ptr<TextureResource>(tex);

View file

@ -25,13 +25,15 @@ class TextureData;
class TextureResource : public IReloadable
{
public:
static std::shared_ptr<TextureResource> get(const std::string& path,
static std::shared_ptr<TextureResource> get(const std::string &path,
bool tile = false,
bool forceLoad = false,
bool dynamic = true,
bool linearMagnify = false,
float scaleDuringLoad = 1.0f);
void initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height);
float scaleDuringLoad = 1.0f,
bool cacheImage = false);
void initFromPixels(const unsigned char *dataRGBA, size_t width, size_t height);
virtual void initFromMemory(const char* data, size_t length);
static void manualUnload(std::string path, bool tile);