From 790c55c0bb80e28ca3846e59a88122027d2a5348 Mon Sep 17 00:00:00 2001 From: m-aXimilian Date: Mon, 16 Jun 2025 13:19:28 +0200 Subject: [PATCH] init view abstractions --- CMakeLists.txt | 1 + lib/imaging/PixelariumImage.hpp | 8 ++++++-- lib/resources/resource.hpp | 2 ++ src/AppGLFW.cpp | 18 +++++++++++++----- src/AppGLFW.hpp | 27 ++++++++------------------- src/viewmodels/ImageViewFactory.cpp | 15 +++++++++++++++ src/viewmodels/ImageViewFactory.hpp | 27 +++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/viewmodels/ImageViewFactory.cpp create mode 100644 src/viewmodels/ImageViewFactory.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c62ada8..9dd6ce4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/lib/imaging/PixelariumImage.hpp b/lib/imaging/PixelariumImage.hpp index c5b98ce..2abad19 100644 --- a/lib/imaging/PixelariumImage.hpp +++ b/lib/imaging/PixelariumImage.hpp @@ -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(*other.img_); + }; PixelariumImage(PixelariumImage&& other) noexcept : img_(std::move(other.img_)) {} // requires a copy ctor which we don't have diff --git a/lib/resources/resource.hpp b/lib/resources/resource.hpp index 61e17a8..8c2514b 100644 --- a/lib/resources/resource.hpp +++ b/lib/resources/resource.hpp @@ -28,6 +28,7 @@ class IResourcePool virtual bool ModifyResource(size_t id, std::unique_ptr res) = 0; virtual bool DeleteResource(size_t id) = 0; virtual void EnumerateResources(const std::function& 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 void EnumerateResources(const std::function& func) override; + size_t GetTotalSize() const override { return resources_.size();} private: std::unordered_map> resources_; }; diff --git a/src/AppGLFW.cpp b/src/AppGLFW.cpp index b62ce89..61abe0b 100644 --- a/src/AppGLFW.cpp +++ b/src/AppGLFW.cpp @@ -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(p); - this->image_view_ = std::make_shared(img); - this->image_view_->ToggleView(true); + image_view_model_->AddImage(std::move(std::make_unique(p))); } } diff --git a/src/AppGLFW.hpp b/src/AppGLFW.hpp index 0eeff0d..54baf17 100644 --- a/src/AppGLFW.hpp +++ b/src/AppGLFW.hpp @@ -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& log) : logger_(*log) + AppGLFW(std::unique_ptr& log, std::unique_ptr& 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(pool_); this->InitMainWindow(); } - AppGLFW(std::unique_ptr& log, std::unique_ptr& 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 img_; - std::shared_ptr image_view_; - pixelarium::render::CvMatRender render_; + // std::shared_ptr image_view_; + std::unique_ptr image_view_model_; + // pixelarium::render::CvMatRender render_; bool imagep_{false}; bool demop_{false}; int log_level_{0}; diff --git a/src/viewmodels/ImageViewFactory.cpp b/src/viewmodels/ImageViewFactory.cpp new file mode 100644 index 0000000..633565b --- /dev/null +++ b/src/viewmodels/ImageViewFactory.cpp @@ -0,0 +1,15 @@ +#include "ImageViewFactory.hpp" +#include +#include + +using namespace pixelarium::ui; + +std::unique_ptr 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(std::make_shared(*img.value())); +} diff --git a/src/viewmodels/ImageViewFactory.hpp b/src/viewmodels/ImageViewFactory.hpp new file mode 100644 index 0000000..bfd6018 --- /dev/null +++ b/src/viewmodels/ImageViewFactory.hpp @@ -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 res) const noexcept + { + return image_pool_.SetResource(std::move(res)); + } + + std::unique_ptr RenderImage(size_t id); + + private: + Pool& image_pool_; +}; +} // namespace pixelarium::ui