~aleteoryx/muditaos

f8e05f0b21fadccb307c07bea64f8282c4cd44eb — Wojtek Rzepecki 4 years ago 39a760b
[EGD-2949] Fix fallback image

Added fallback image in case
of missing image asset
M module-gui/gui/core/ImageManager.cpp => module-gui/gui/core/ImageManager.cpp +57 -24
@@ 1,34 1,25 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 * ImageManager.cpp
 *
 *  Created on: 18 maj 2019
 *      Author: robert
 */

#include <set>
#include <string>

#include <cstdio>
#include <cstring>
#include <filesystem>

#include "ImageManager.hpp"
#include "utf8/UTF8.hpp"
#include <log.hpp>
// module-gui
#include "ImageMap.hpp"
#include "VecMap.hpp"
#include "PixMap.hpp"
#include <Utils.hpp>
#include "DrawCommand.hpp"
#include "Renderer.hpp"
#include <log.hpp>
#include <set>
#include <string>
#include <filesystem>
#include <list>

namespace gui
{

    ImageManager::ImageManager()
    {}
    {
        addFallbackImage();
    }

    ImageManager::~ImageManager()
    {


@@ 167,6 158,49 @@ namespace gui
        return vecMap;
    }

    void ImageManager::addFallbackImage()
    {
        const std::string fallbackImageName{"FallbackImage"};

        auto *fallbackImage = createFallbackImage();
        fallbackImageId     = imageMaps.size();
        fallbackImage->setID(fallbackImageId);
        fallbackImage->setName(fallbackImageName);
        imageMaps.push_back(fallbackImage);
    }

    ImageMap *ImageManager::createFallbackImage()
    {
        // Creation of square with crossed lines as fallback image
        constexpr auto squareWidth = 15;

        std::list<std::unique_ptr<gui::DrawCommand>> commands;
        auto rectangle    = std::make_unique<DrawRectangle>();
        rectangle->origin = {0, 0};
        rectangle->width  = squareWidth;
        rectangle->height = squareWidth;
        rectangle->areaX  = 0;
        rectangle->areaY  = 0;
        rectangle->areaW  = squareWidth;
        rectangle->areaH  = squareWidth;
        commands.emplace_back(std::move(rectangle));

        auto line1   = std::make_unique<DrawLine>();
        line1->start = {0, 0};
        line1->end   = {squareWidth, squareWidth};
        commands.emplace_back(std::move(line1));

        auto line2   = std::make_unique<DrawLine>();
        line2->start = {squareWidth - 1, 0};
        line2->end   = {0, squareWidth - 1};
        commands.emplace_back(std::move(line2));

        auto renderContext = std::make_unique<Context>(squareWidth, squareWidth);
        Renderer().render(renderContext.get(), commands);

        return new PixMap(squareWidth, squareWidth, renderContext->getData());
    }

    std::vector<std::string> ImageManager::getImageMapList(std::string ext)
    {



@@ 204,8 238,8 @@ namespace gui
    ImageMap *ImageManager::getImageMap(uint32_t id)
    {
        if (id >= imageMaps.size()) {
            LOG_FATAL("Unable to find an image by id: %" PRIu32, id);
            throw ImageNotFound{"Image could not be found."};
            LOG_ERROR("Unable to find an image by id: %" PRIu32, id);
            return imageMaps[fallbackImageId];
        }
        return imageMaps[id];
    }


@@ 216,9 250,8 @@ namespace gui
                return i;
            }
        }

        LOG_FATAL("Unable to find an image: %s", name.c_str());
        throw ImageNotFound{"Image could not be found."};
        LOG_ERROR("Unable to find an image: %s , using deafult fallback image instead.", name.c_str());
        return fallbackImageId;
    }

} /* namespace gui */

M module-gui/gui/core/ImageManager.hpp => module-gui/gui/core/ImageManager.hpp +6 -10
@@ 1,25 1,20 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "ImageMap.hpp"
#include <vector>
#include <string>
#include <cstdint>
#include <cstring>

#include "ImageMap.hpp"

namespace gui
{
    class ImageNotFound : public std::runtime_error
    {
      public:
        using std::runtime_error::runtime_error;
    };

    class ImageManager
    {
      private:
        ImageMap *createFallbackImage();
        std::uint32_t fallbackImageId{0};

      protected:
        std::string mapFolder;
        std::vector<ImageMap *> imageMaps;


@@ 28,6 23,7 @@ namespace gui

        ImageMap *loadPixMap(std::string filename);
        ImageMap *loadVecMap(std::string filename);
        void addFallbackImage();
        void loadImageMaps(std::string baseDirectory);

        ImageManager();

M module-gui/test/test-catch/test-gui-image.cpp => module-gui/test/test-catch/test-gui-image.cpp +10 -2
@@ 35,12 35,20 @@ TEST_CASE("Setting an incorrect image name")
{
    constexpr auto imageName = "ABC";
    gui::Image image{};
    REQUIRE_THROWS(image.set(imageName));
    image.set(imageName);

    std::list<gui::Command> commands;
    image.buildDrawListImplementation(commands);
    REQUIRE(!commands.empty());
}

TEST_CASE("Setting an incorrect image id")
{
    constexpr auto imageId = 1000;
    gui::Image image{};
    REQUIRE_THROWS(image.set(imageId));
    image.set(imageId);

    std::list<gui::Command> commands;
    image.buildDrawListImplementation(commands);
    REQUIRE(!commands.empty());
}