Add image import service boundary
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace pp::app {
|
||||
|
||||
enum class DocumentImageImportAction {
|
||||
@@ -17,13 +19,31 @@ struct DocumentImageImportPlan {
|
||||
bool enters_transform_mode = false;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentImageImportPlan> plan_document_image_import(
|
||||
class DocumentImageImportServices {
|
||||
public:
|
||||
virtual ~DocumentImageImportServices() = default;
|
||||
|
||||
virtual void import_equirectangular(std::string_view path) = 0;
|
||||
virtual void enter_transform_import(std::string_view path) = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status validate_document_image_import_dimensions(
|
||||
int width,
|
||||
int height) noexcept
|
||||
{
|
||||
if (width <= 0 || height <= 0) {
|
||||
return pp::foundation::Result<DocumentImageImportPlan>::failure(
|
||||
pp::foundation::Status::invalid_argument("image dimensions must be positive"));
|
||||
return pp::foundation::Status::invalid_argument("image dimensions must be positive");
|
||||
}
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Result<DocumentImageImportPlan> plan_document_image_import(
|
||||
int width,
|
||||
int height) noexcept
|
||||
{
|
||||
const auto dimensions = validate_document_image_import_dimensions(width, height);
|
||||
if (!dimensions.ok()) {
|
||||
return pp::foundation::Result<DocumentImageImportPlan>::failure(dimensions);
|
||||
}
|
||||
|
||||
const auto wide_equirect = static_cast<long long>(width) == static_cast<long long>(height) * 2LL;
|
||||
@@ -40,4 +60,29 @@ struct DocumentImageImportPlan {
|
||||
return pp::foundation::Result<DocumentImageImportPlan>::success(plan);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline pp::foundation::Status execute_document_image_import_plan(
|
||||
const DocumentImageImportPlan& plan,
|
||||
std::string_view path,
|
||||
DocumentImageImportServices& services)
|
||||
{
|
||||
const auto dimensions = validate_document_image_import_dimensions(plan.width, plan.height);
|
||||
if (!dimensions.ok()) {
|
||||
return dimensions;
|
||||
}
|
||||
if (path.empty()) {
|
||||
return pp::foundation::Status::invalid_argument("image import path must not be empty");
|
||||
}
|
||||
|
||||
switch (plan.action) {
|
||||
case DocumentImageImportAction::import_equirectangular:
|
||||
services.import_equirectangular(path);
|
||||
return pp::foundation::Status::success();
|
||||
case DocumentImageImportAction::place_transform:
|
||||
services.enter_transform_import(path);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
return pp::foundation::Status::invalid_argument("unknown image import action");
|
||||
}
|
||||
|
||||
} // namespace pp::app
|
||||
|
||||
@@ -143,17 +143,44 @@ public:
|
||||
if (!import_plan)
|
||||
return;
|
||||
|
||||
if (import_plan.value().imports_equirectangular)
|
||||
{
|
||||
Canvas::I->import_equirectangular(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto m = static_cast<CanvasModeTransform*>(app_ptr->canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
|
||||
m->m_action = CanvasModeTransform::ActionType::Import;
|
||||
m->m_source_image = std::move(img);
|
||||
Canvas::set_mode(kCanvasMode::Import);
|
||||
}
|
||||
class LegacyDocumentImageImportServices final : public pp::app::DocumentImageImportServices {
|
||||
public:
|
||||
LegacyDocumentImageImportServices(App& app, Image& image) noexcept
|
||||
: app_(app)
|
||||
, image_(image)
|
||||
{
|
||||
}
|
||||
|
||||
void import_equirectangular(std::string_view import_path) override
|
||||
{
|
||||
if (Canvas::I)
|
||||
Canvas::I->import_equirectangular(std::string(import_path));
|
||||
}
|
||||
|
||||
void enter_transform_import(std::string_view) override
|
||||
{
|
||||
if (!app_.canvas || !app_.canvas->m_canvas)
|
||||
return;
|
||||
|
||||
auto* mode = static_cast<CanvasModeTransform*>(
|
||||
app_.canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
|
||||
mode->m_action = CanvasModeTransform::ActionType::Import;
|
||||
mode->m_source_image = std::move(image_);
|
||||
Canvas::set_mode(kCanvasMode::Import);
|
||||
}
|
||||
|
||||
private:
|
||||
App& app_;
|
||||
Image& image_;
|
||||
};
|
||||
|
||||
LegacyDocumentImageImportServices services(*app_ptr, img);
|
||||
const auto status = pp::app::execute_document_image_import_plan(
|
||||
import_plan.value(),
|
||||
path,
|
||||
services);
|
||||
if (!status.ok())
|
||||
LOG("Image import failed: %s", status.message);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user