infinipaint/src/DrawingProgram/Tools/EditTools/ImageEditTool.cpp
2026-06-03 16:27:49 +03:00

111 lines
4.4 KiB
C++

/*
* InfiniPaint
* Copyright (C) 2025-2026 Yousef Khadadeh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ImageEditTool.hpp"
#include "../../DrawingProgram.hpp"
#include "../../../CanvasComponents/ImageCanvasComponent.hpp"
#include "../../../World.hpp"
#include "../../../Toolbar.hpp"
#include "../../../MainProgram.hpp"
#include "../../../ResourceManager.hpp"
#include "../EditTool.hpp"
#include "DrawingProgramEditToolBase.hpp"
#include "Eigen/Geometry"
#include "../../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
#include "../../../GUIStuff/ElementHelpers/ButtonHelpers.hpp"
#ifdef __EMSCRIPTEN__
#include <EmscriptenHelpers/emscripten_browser_file.h>
#endif
ImageEditTool::ImageEditTool(DrawingProgram& initDrawP, CanvasComponentContainer::ObjInfo* initComp):
DrawingProgramEditToolBase(initDrawP, initComp)
{}
void ImageEditTool::edit_start(EditTool& editTool, std::any& prevData) {
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;
constexpr float MINIMUM_DISTANCE_BETWEEN_IMAGE_CROP_POINTS = 0.02f;
editTool.add_point_handle({&a.d.cropP1, &staticZero, &a.d.cropP2, 0.0f, MINIMUM_DISTANCE_BETWEEN_IMAGE_CROP_POINTS, transformMat});
editTool.add_point_handle({&a.d.cropP2, &a.d.cropP1, &staticOne, MINIMUM_DISTANCE_BETWEEN_IMAGE_CROP_POINTS, 0.0f, transformMat});
a.d.editing = true;
commitUpdate = true;
}
void ImageEditTool::commit_edit_updates(std::any& prevData) {
auto& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
a.d.editing = false;
}
void ImageEditTool::edit_update() {
}
void ImageEditTool::edit_gui(Toolbar& t) {
using namespace GUIStuff;
using namespace ElementHelpers;
ImageCanvasComponent& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
auto& gui = drawP.world.main.g.gui;
gui.new_id("edit tool image", [&] {
text_label_centered(gui, "File Properties");
auto resourceData = drawP.world.netObjMan.get_obj_temporary_ref_from_id<ResourceData>(a.d.imageID);
if(resourceData) {
text_label(gui, "Name: " + resourceData->name);
text_button(gui, "file download", "Download file", {
.wide = true,
.onClick = [&, resourceData] {
#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) {
SDL_SaveFile(p.string().c_str(), resourceData->data->c_str(), resourceData->data->size());
}, resourceData->name, true);
}
});
}
else
text_label_centered(gui, "Loading resource...");
});
}
void ImageEditTool::gui_phone_toolbox(PhoneDrawingProgramScreen& t) {
using namespace GUIStuff;
using namespace ElementHelpers;
ImageCanvasComponent& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
auto& gui = drawP.world.main.g.gui;
gui.new_id("edit tool image", [&] {
text_label_centered(gui, "File Properties");
auto resourceData = drawP.world.netObjMan.get_obj_temporary_ref_from_id<ResourceData>(a.d.imageID);
if(resourceData)
text_label(gui, "Name: " + resourceData->name);
else
text_label_centered(gui, "Loading resource...");
});
}