infinipaint/src/DrawingProgram/Tools/EditTools/ImageEditTool.cpp

112 lines
4.4 KiB
C++
Raw Normal View History

/*
* 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/>.
*/
2025-08-02 20:10:33 +03:00
#include "ImageEditTool.hpp"
#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-10-11 13:02:07 +03:00
#include "../../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
#include "../../../GUIStuff/ElementHelpers/ButtonHelpers.hpp"
2025-07-06 20:43:53 +03:00
#ifdef __EMSCRIPTEN__
#include <EmscriptenHelpers/emscripten_browser_file.h>
#endif
ImageEditTool::ImageEditTool(DrawingProgram& initDrawP, CanvasComponentContainer::ObjInfo* initComp):
DrawingProgramEditToolBase(initDrawP, initComp)
2025-07-06 20:43:53 +03:00
{}
void ImageEditTool::edit_start(EditTool& editTool, 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;
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});
2026-01-06 12:36:15 +03:00
a.d.editing = true;
2026-05-18 18:35:40 +03:00
commitUpdate = true;
2025-07-06 20:43:53 +03:00
}
void ImageEditTool::commit_edit_updates(std::any& prevData) {
2026-01-06 12:36:15 +03:00
auto& a = static_cast<ImageCanvasComponent&>(comp->obj->get_comp());
a.d.editing = false;
2025-07-06 20:43:53 +03:00
}
2026-05-18 17:57:02 +03:00
void ImageEditTool::edit_update() {
2025-07-06 20:43:53 +03:00
}
2026-04-12 12:36:00 +03:00
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");
2026-03-30 18:51:14 +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
2026-03-30 18:51:14 +03:00
if(resourceData) {
text_label(gui, "Name: " + resourceData->name);
text_button(gui, "file download", "Download file", {
2026-03-30 18:51:14 +03:00
.wide = true,
2026-06-03 16:27:49 +03:00
.onClick = [&, resourceData] {
2026-03-30 18:51:14 +03:00
#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-04-11 11:17:26 +03:00
SDL_SaveFile(p.string().c_str(), resourceData->data->c_str(), resourceData->data->size());
2026-03-30 18:51:14 +03:00
}, resourceData->name, true);
}
});
}
else
text_label_centered(gui, "Loading resource...");
2026-03-30 18:51:14 +03:00
});
2025-07-06 20:43:53 +03:00
}
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...");
});
}