2025-08-02 20:10:33 +03:00
|
|
|
#include "ImageEditTool.hpp"
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../../DrawingProgram.hpp"
|
|
|
|
|
#include "../../../CanvasComponents/ImageCanvasComponent.hpp"
|
|
|
|
|
#include "../../../World.hpp"
|
|
|
|
|
#include "../../../Toolbar.hpp"
|
|
|
|
|
#include "../../../MainProgram.hpp"
|
|
|
|
|
#include "../../../ResourceManager.hpp"
|
2026-01-06 12:36:15 +03:00
|
|
|
#include "../EditTool.hpp"
|
2025-08-02 20:10:33 +03:00
|
|
|
#include "DrawingProgramEditToolBase.hpp"
|
2026-01-06 12:36:15 +03:00
|
|
|
#include "Eigen/Geometry"
|
2025-07-06 20:43:53 +03:00
|
|
|
#include <fstream>
|
2025-10-11 13:02:07 +03:00
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
|
#include <EmscriptenHelpers/emscripten_browser_file.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
ImageEditTool::ImageEditTool(DrawingProgram& initDrawP):
|
|
|
|
|
DrawingProgramEditToolBase(initDrawP)
|
2025-07-06 20:43:53 +03:00
|
|
|
{}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
void ImageEditTool::edit_start(EditTool& editTool, CanvasComponentContainer::ObjInfo* comp, std::any& prevData) {
|
2026-01-06 12:36:15 +03:00
|
|
|
auto& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
|
|
|
|
|
static Vector2f staticZero = {0.0f, 0.0f};
|
|
|
|
|
static Vector2f staticOne = {1.0f, 1.0f};
|
|
|
|
|
|
|
|
|
|
Affine2f transformMat = Translation2f(a.d.p1) * AlignedScaling2f(a.d.p2 - a.d.p1) * Affine2f::Identity();
|
|
|
|
|
|
|
|
|
|
prevData = a.d;
|
|
|
|
|
editTool.add_point_handle({&a.d.cropP1, &staticZero, &a.d.cropP2, 0.0f, transformMat});
|
|
|
|
|
editTool.add_point_handle({&a.d.cropP2, &a.d.cropP1, &staticOne, 0.0f, transformMat});
|
|
|
|
|
a.d.editing = true;
|
|
|
|
|
comp->obj->commit_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
void ImageEditTool::commit_edit_updates(CanvasComponentContainer::ObjInfo* comp, std::any& prevData) {
|
2026-01-06 12:36:15 +03:00
|
|
|
auto& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
|
|
|
|
|
a.d.editing = false;
|
|
|
|
|
comp->obj->commit_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
bool ImageEditTool::edit_update(CanvasComponentContainer::ObjInfo* comp) {
|
2025-07-06 20:43:53 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
bool ImageEditTool::edit_gui(CanvasComponentContainer::ObjInfo* comp) {
|
2025-12-15 15:36:42 +03:00
|
|
|
ImageCanvasComponent& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
|
2025-07-06 20:43:53 +03:00
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
t.gui.push_id("edit tool image");
|
|
|
|
|
t.gui.text_label_centered("File Properties");
|
2025-12-16 12:02:08 +03:00
|
|
|
auto resourceData = drawP.world.netObjMan.get_obj_temporary_ref_from_id<ResourceData>(a.d.imageID);
|
2025-10-11 13:02:07 +03:00
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
if(resourceData) {
|
|
|
|
|
t.gui.text_label("Name: " + resourceData->name);
|
|
|
|
|
if(t.gui.text_button_wide("file download", "Download file")) {
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
|
emscripten_browser_file::download(
|
|
|
|
|
resourceData->name,
|
|
|
|
|
"application/octet-binary",
|
|
|
|
|
*resourceData->data
|
|
|
|
|
);
|
|
|
|
|
#endif
|
|
|
|
|
t.open_file_selector("Download File", {{"Any File", "*"}}, [resourceData](const std::filesystem::path& p, const auto& e) {
|
2026-03-07 13:00:10 +03:00
|
|
|
SDL_SaveFile(p.c_str(), resourceData->data->c_str(), resourceData->data->size());
|
2025-07-06 20:43:53 +03:00
|
|
|
}, resourceData->name, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
t.gui.text_label_centered("Loading resource...");
|
|
|
|
|
t.gui.pop_id();
|
|
|
|
|
return false;
|
|
|
|
|
}
|