Refactor gallery function to separate class (#14)

* refactor gallery function to separate class

* typo and version adaptions

* clang-format
This commit is contained in:
m-aXimilian
2025-10-11 01:29:33 +02:00
committed by Maximilian Kueffner
parent d6a08f7db3
commit a760535b92
9 changed files with 153 additions and 105 deletions
-4
View File
@@ -84,14 +84,10 @@ void pixelarium::application::AppGLFW::InitMainWindow()
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform
// Windows
// io.ConfigViewportsNoAutoMerge = true;
// io.ConfigViewportsNoTaskBarIcon = true;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
+2
View File
@@ -6,6 +6,8 @@ set(APPLIBSRC
AppGLFW.cpp
DefaultApp.hpp
DefaultApp.cpp
PixelariumGallery.hpp
PixelariumGallery.cpp
${imgui_DIR}/imgui.cpp
${imgui_DIR}/imgui_demo.cpp
${imgui_DIR}/imgui_draw.cpp
+7 -86
View File
@@ -2,25 +2,24 @@
#include <cstddef>
#include <format>
#include <memory>
#include "PixelariumImageFactory.hpp"
#include "app_resources_default.h"
#include "imgui.h"
#include "portable-file-dialogs.h"
#include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
using namespace pixelarium::imaging;
using namespace pixelarium::application;
void pixelarium::ui::DefaultApp::MenuBarOptionsColumn1()
void DefaultApp::MenuBarOptionsColumn1()
{
ImGui::MenuItem(SHOWIMGUIDEMOS, NULL, &this->demop_);
ImGui::MenuItem(SHOWIMAGEGALLERY, NULL, &this->image_listp_);
}
void pixelarium::ui::DefaultApp::MenuBarOptionsColumn2()
void DefaultApp::MenuBarOptionsColumn2()
{
if (ImGui::BeginMenu(FILEMENUNAME))
{
@@ -33,93 +32,15 @@ void pixelarium::ui::DefaultApp::MenuBarOptionsColumn2()
}
}
void pixelarium::ui::DefaultApp::Run()
void DefaultApp::Run()
{
if (demop_) ImGui::ShowDemoWindow(&this->demop_);
if (image_listp_) this->ImageGalleryRender();
if (image_listp_) this->gallery.RenderGallery();
this->RenderImages();
this->gallery.RenderImages();
}
void pixelarium::ui::DefaultApp::RenderImages()
{
this->render_manager_->Enumerate(
[&](resources::ResourceKey key, render::RenderImageStateWrapper& render_state)
{
render_state.view->ShowImage();
if (!*render_state.view->GetStatus())
{
this->render_manager_->MarkForDeletion(key);
}
});
}
void pixelarium::ui::DefaultApp::ImageGalleryRender()
{
ImGui::SetNextWindowSize(ImVec2(300, 550));
ImGui::Begin(SHOWIMAGEGALLERY, &this->image_listp_);
// this updates the render collection
// essentially deleting render views that were
// marked for deletion
this->render_manager_->UpdateCollection();
static size_t selected_index{0};
int highlight_index{-1};
if (ImGui::BeginListBox("Image List", ImVec2(200, 400)))
{
pool_.EnumerateResources(
[&](size_t id, size_t idx, const imaging::IPixelariumImage& img) -> void
{
const bool is_selected = selected_index == idx;
if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected))
{
selected_index = idx;
this->selected_image_ = id;
}
if (highlight_index && ImGui::IsItemHovered()) highlight_index = idx;
if (is_selected) ImGui::SetItemDefaultFocus();
});
ImGui::EndListBox();
}
ImGui::Checkbox(AUTOSHOWSELECTED, &this->auto_show_selectd_image_);
ImGui::SameLine(); // put the button next to the checkbox
// note that the button will only show when the checkbox is toggled off
// this is intended behavior as the selected image will render automatically
// when the checkbox is toggled on
if (this->auto_show_selectd_image_ || ImGui::Button(OPENIMAGE))
{
// Try add the selected index to the manager
this->render_manager_->Add(this->selected_image_);
}
if (ImGui::Button(LOADIMAGE))
{
this->LoadImage();
}
if (ImGui::Button(REMOVEIMAGE))
{
this->render_manager_->MarkForDeletion(this->selected_image_);
this->pool_.DeleteResource(this->selected_image_);
}
ImGui::SameLine();
if (ImGui::Button(CLEARALL))
{
this->render_manager_->Clear();
this->pool_.Clear();
}
ImGui::End(); // end gallery window
}
void pixelarium::ui::DefaultApp::LoadImage()
void DefaultApp::LoadImage()
{
auto res{pfd::open_file("Load Inputs", pfd::path::home(), {"All Files", "*"}, pfd::opt::multiselect).result()};
for (auto& p : res)
+7 -13
View File
@@ -1,27 +1,25 @@
#pragma once
#include <cstddef>
#include <memory>
#include "AppGLFW.hpp"
#include "app/PixelariumGallery.hpp"
#include "imgui.h"
#include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::ui
namespace pixelarium::application
{
/// @brief Default implementation of AppGLFW.
/// This can either be used as is, as an example or as a base class
/// providing some defaults for a more custom implementation.
class DefaultApp : public application::AppGLFW
class DefaultApp : public AppGLFW
{
public:
DefaultApp(const utils::log::ILog& log, pixelarium::resources::ImageResourcePool& pool)
: application::AppGLFW(log),
pool_(pool),
render_manager_(std::make_unique<render::RenderImageManager>(pool, log))
: application::AppGLFW(log), pool_(pool), gallery(log, pool)
{
gallery.SetLoadFunction([&]() -> void { this->LoadImage(); });
}
protected:
@@ -31,16 +29,12 @@ class DefaultApp : public application::AppGLFW
private:
void LoadImage();
void ImageGalleryRender();
void RenderImages();
private:
resources::ImageResourcePool& pool_;
std::unique_ptr<render::RenderImageManager> render_manager_;
bool image_listp_{true};
bool auto_show_selectd_image_{true};
bool demop_{false};
ImVec2 curr_dim_;
size_t selected_image_{0};
application::PixelariumImageGallery gallery;
};
} // namespace pixelarium::ui
} // namespace pixelarium::application
+86
View File
@@ -0,0 +1,86 @@
#include "PixelariumGallery.hpp"
#include <format>
#include "app_resources_default.h"
#include "imgui.h"
using namespace pixelarium::application;
void PixelariumImageGallery::RenderGallery()
{
ImGui::SetNextWindowSize(ImVec2(300, 550));
ImGui::Begin(SHOWIMAGEGALLERY, &this->image_listp_);
// this updates the render collection
// essentially deleting render views that were
// marked for deletion
this->render_manager_->UpdateCollection();
static size_t selected_index{0};
int highlight_index{-1};
if (ImGui::BeginListBox("Image List", ImVec2(200, 400)))
{
pool_.EnumerateResources(
[&](size_t id, size_t idx, const imaging::IPixelariumImage& img) -> void
{
const bool is_selected = selected_index == idx;
if (ImGui::Selectable(std::format("{}", img.Name()).c_str(), is_selected))
{
selected_index = idx;
this->selected_image_ = id;
}
if (highlight_index && ImGui::IsItemHovered()) highlight_index = idx;
if (is_selected) ImGui::SetItemDefaultFocus();
});
ImGui::EndListBox();
}
ImGui::Checkbox(AUTOSHOWSELECTED, &this->auto_show_selectd_image_);
ImGui::SameLine(); // put the button next to the checkbox
// note that the button will only show when the checkbox is toggled off
// this is intended behavior as the selected image will render automatically
// when the checkbox is toggled on
if (this->auto_show_selectd_image_ || ImGui::Button(OPENIMAGE))
{
// Try add the selected index to the manager
this->render_manager_->Add(this->selected_image_);
}
if (ImGui::Button(LOADIMAGE))
{
this->load_image_();
}
if (ImGui::Button(REMOVEIMAGE))
{
this->render_manager_->MarkForDeletion(this->selected_image_);
this->pool_.DeleteResource(this->selected_image_);
}
ImGui::SameLine();
if (ImGui::Button(CLEARALL))
{
this->render_manager_->Clear();
this->pool_.Clear();
}
ImGui::End(); // end gallery window
}
void PixelariumImageGallery::RenderImages()
{
this->render_manager_->Enumerate(
[&](resources::ResourceKey key, render::RenderImageStateWrapper& render_state)
{
render_state.view->ShowImage();
if (!*render_state.view->GetStatus())
{
this->render_manager_->MarkForDeletion(key);
}
});
}
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include "rendering/RenderImageManager.hpp"
#include "resources/resource.hpp"
#include "utilities/ILog.hpp"
namespace pixelarium::application
{
template <typename P>
concept GalleryT = requires(P& r) { static_cast<resources::IResourcePool<P>&>(r); };
template <typename GalleryT>
class IPixelariumGallery
{
public:
virtual ~IPixelariumGallery() = default;
virtual void RenderGallery() = 0;
};
class PixelariumImageGallery : IPixelariumGallery<resources::ImageResourcePool>
{
using Pool = resources::ImageResourcePool;
using Log = utils::log::ILog;
public:
PixelariumImageGallery(const Log& log, resources::ImageResourcePool& pool)
: pool_{pool}, log_{log}, render_manager_(std::make_unique<render::RenderImageManager>(pool, log))
{
}
void RenderGallery() override;
void RenderImages();
void Add(resources::ResourceKey key) { this->render_manager_->Add(key); }
void SetLoadFunction(const std::function<void()>& fun) { this->load_image_ = fun; };
private:
std::function<void()> load_image_{};
Pool& pool_;
const Log& log_;
std::unique_ptr<render::RenderImageManager> render_manager_;
bool image_listp_{true};
bool auto_show_selectd_image_{true};
size_t selected_image_{0};
};
} // namespace pixelarium::application