init view abstractions
This commit is contained in:
committed by
Kueffner, Maximilian
parent
6370bfdff6
commit
790c55c0bb
@@ -40,6 +40,7 @@ add_subdirectory(lib)
|
||||
set(SRC
|
||||
src/AppGLFW.cpp
|
||||
src/views/PixelariumImageView.cpp
|
||||
src/viewmodels/ImageViewFactory.cpp
|
||||
src/main.cpp
|
||||
${imgui_DIR}/imgui.cpp
|
||||
${imgui_DIR}/imgui_demo.cpp
|
||||
|
||||
@@ -14,8 +14,12 @@ class PixelariumImage
|
||||
|
||||
// get back the defaults
|
||||
PixelariumImage() = default;
|
||||
// we cannot copy an Image since this conflicts with the _img field
|
||||
PixelariumImage(const PixelariumImage& other) = delete;
|
||||
PixelariumImage(const PixelariumImage& other)
|
||||
{
|
||||
// be ware!!
|
||||
// we make a copy of the image data here!
|
||||
img_ = std::make_unique<cv::Mat>(*other.img_);
|
||||
};
|
||||
PixelariumImage(PixelariumImage&& other) noexcept
|
||||
: img_(std::move(other.img_)) {}
|
||||
// requires a copy ctor which we don't have
|
||||
|
||||
@@ -28,6 +28,7 @@ class IResourcePool
|
||||
virtual bool ModifyResource(size_t id, std::unique_ptr<R> res) = 0;
|
||||
virtual bool DeleteResource(size_t id) = 0;
|
||||
virtual void EnumerateResources(const std::function<void(size_t, const R&)>& func) = 0;
|
||||
virtual size_t GetTotalSize() const = 0;
|
||||
};
|
||||
|
||||
// Now with the =GetResource= method, I do not want to transfer ownership to the caller of that method. The ownership
|
||||
@@ -52,6 +53,7 @@ class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
|
||||
|
||||
void EnumerateResources(const std::function<void(size_t, const imaging::PixelariumImage&)>& func) override;
|
||||
|
||||
size_t GetTotalSize() const override { return resources_.size();}
|
||||
private:
|
||||
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
|
||||
};
|
||||
|
||||
+13
-5
@@ -117,9 +117,19 @@ int pixelarium::ui::AppGLFW::Run()
|
||||
this->MenuBar();
|
||||
if (demop_) ImGui::ShowDemoWindow(&this->demop_);
|
||||
|
||||
if (this->image_view_)
|
||||
// if (this->image_view_)
|
||||
// {
|
||||
// this->image_view_->ShowImage();
|
||||
// }
|
||||
|
||||
if (ImGui::BeginListBox("ListBox"))
|
||||
{
|
||||
this->image_view_->ShowImage();
|
||||
pool_.EnumerateResources([](size_t id, const imaging::PixelariumImage& img) -> void
|
||||
{
|
||||
ImGui::Selectable(std::format("Image {}", id).c_str());
|
||||
});
|
||||
|
||||
ImGui::EndListBox();
|
||||
}
|
||||
|
||||
// Rendering
|
||||
@@ -198,8 +208,6 @@ void pixelarium::ui::AppGLFW::LoadImageProt()
|
||||
{
|
||||
this->logger_.Debug(std::format("{}: Creating image {}", __FUNCTION__, p));
|
||||
|
||||
auto img = std::make_shared<PixelariumImage>(p);
|
||||
this->image_view_ = std::make_shared<PixelariumImageView>(img);
|
||||
this->image_view_->ToggleView(true);
|
||||
image_view_model_->AddImage(std::move(std::make_unique<PixelariumImage>(p)));
|
||||
}
|
||||
}
|
||||
|
||||
+8
-19
@@ -11,33 +11,21 @@
|
||||
#include "rendering/CvMatRender.hpp"
|
||||
#include "resources/resource.hpp"
|
||||
#include "utilities/ILog.hpp"
|
||||
#include "viewmodels/ImageViewFactory.hpp"
|
||||
#include "views/PixelariumImageView.hpp"
|
||||
|
||||
namespace pixelarium::ui
|
||||
{
|
||||
// static bool dim_changed_p(const ImVec2& ref_rect, const ImVec2& new_rect);
|
||||
|
||||
// static ImVec2 aspect_const_dimensions(const pixelarium::imaging::PixelariumImage& img, const ImVec2& curr_dim);
|
||||
|
||||
class AppGLFW
|
||||
{
|
||||
public:
|
||||
explicit AppGLFW(std::unique_ptr<utils::log::ILog>& log) : logger_(*log)
|
||||
AppGLFW(std::unique_ptr<utils::log::ILog>& log, std::unique_ptr<pixelarium::resources::ImageResourcePool>& pool)
|
||||
: logger_(*log), pool_(*pool)
|
||||
{
|
||||
logger_.Debug(std::format("{}: Initiating a new window", __FUNCTION__).c_str());
|
||||
|
||||
if (pool_)
|
||||
{
|
||||
logger_.Debug(std::format("{}: We have an image resource pool!", __FUNCTION__).c_str());
|
||||
}
|
||||
image_view_model_ = std::make_unique<ImageViewFactory>(pool_);
|
||||
|
||||
this->InitMainWindow();
|
||||
}
|
||||
AppGLFW(std::unique_ptr<utils::log::ILog>& log, std::unique_ptr<pixelarium::resources::ImageResourcePool>& pool)
|
||||
: AppGLFW(log)
|
||||
{
|
||||
pool_ = pool.get();
|
||||
};
|
||||
int Run();
|
||||
|
||||
private:
|
||||
@@ -47,12 +35,13 @@ class AppGLFW
|
||||
|
||||
private:
|
||||
utils::log::ILog& logger_;
|
||||
resources::ImageResourcePool* pool_;
|
||||
resources::ImageResourcePool& pool_;
|
||||
GLFWwindow* window = nullptr;
|
||||
ImGuiWindowFlags window_flags_ = 0;
|
||||
// std::shared_ptr<pixelarium::imaging::PixelariumImage> img_;
|
||||
std::shared_ptr<pixelarium::ui::PixelariumImageView> image_view_;
|
||||
pixelarium::render::CvMatRender render_;
|
||||
// std::shared_ptr<pixelarium::ui::PixelariumImageView> image_view_;
|
||||
std::unique_ptr<ImageViewFactory> image_view_model_;
|
||||
// pixelarium::render::CvMatRender render_;
|
||||
bool imagep_{false};
|
||||
bool demop_{false};
|
||||
int log_level_{0};
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "ImageViewFactory.hpp"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
using namespace pixelarium::ui;
|
||||
|
||||
std::unique_ptr<PixelariumImageView> ImageViewFactory::RenderImage(size_t image_id)
|
||||
{
|
||||
auto img{this->image_pool_.GetResource(image_id)};
|
||||
|
||||
if (!img.has_value()) return nullptr;
|
||||
|
||||
// beware: here we copy the actual image resource over to the new image
|
||||
return std::make_unique<PixelariumImageView>(std::make_shared<Image>(*img.value()));
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "PixelariumImage.hpp"
|
||||
#include "resources/resource.hpp"
|
||||
#include "views/PixelariumImageView.hpp"
|
||||
namespace pixelarium::ui
|
||||
{
|
||||
class ImageViewFactory
|
||||
{
|
||||
using Image = imaging::PixelariumImage;
|
||||
using Pool = resources::ImageResourcePool;
|
||||
|
||||
public:
|
||||
explicit ImageViewFactory(Pool& pool) : image_pool_(pool) {}
|
||||
|
||||
[[nodiscard("Image Id is ignored")]]
|
||||
size_t AddImage(std::unique_ptr<imaging::PixelariumImage> res) const noexcept
|
||||
{
|
||||
return image_pool_.SetResource(std::move(res));
|
||||
}
|
||||
|
||||
std::unique_ptr<PixelariumImageView> RenderImage(size_t id);
|
||||
|
||||
private:
|
||||
Pool& image_pool_;
|
||||
};
|
||||
} // namespace pixelarium::ui
|
||||
Reference in New Issue
Block a user