2025-07-06 20:43:53 +03:00
|
|
|
#include "EditTool.hpp"
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include "DrawingProgram.hpp"
|
|
|
|
|
#include "../MainProgram.hpp"
|
|
|
|
|
#include "../DrawData.hpp"
|
|
|
|
|
#include "Helpers/MathExtras.hpp"
|
|
|
|
|
#include "Helpers/SCollision.hpp"
|
|
|
|
|
#include "../SharedTypes.hpp"
|
|
|
|
|
#include <cereal/types/vector.hpp>
|
|
|
|
|
#include <memory>
|
2025-08-02 20:10:33 +03:00
|
|
|
|
|
|
|
|
#include "TextBoxEditTool.hpp"
|
|
|
|
|
#include "RectDrawEditTool.hpp"
|
|
|
|
|
#include "ImageEditTool.hpp"
|
|
|
|
|
#include "EllipseDrawEditTool.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
EditTool::EditTool(DrawingProgram& initDrawP):
|
2025-08-02 20:10:33 +03:00
|
|
|
DrawingProgramToolBase(initDrawP)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
DrawingProgramToolType EditTool::get_type() {
|
|
|
|
|
return DrawingProgramToolType::EDIT;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EditTool::gui_toolbox() {
|
|
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
if(controls.isEditing) {
|
|
|
|
|
std::shared_ptr<DrawComponent> a = controls.compToEdit.lock();
|
|
|
|
|
if(a) {
|
2025-08-02 20:10:33 +03:00
|
|
|
bool editHappened = controls.compEditTool->edit_gui(a);
|
|
|
|
|
if(editHappened) {
|
|
|
|
|
a->commit_update(drawP);
|
|
|
|
|
a->client_send_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
t.gui.push_id("edit tool");
|
|
|
|
|
t.gui.text_label_centered("Edit");
|
|
|
|
|
t.gui.pop_id();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EditTool::add_point_handle(const HandleData& handle) {
|
|
|
|
|
controls.pointHandles.emplace_back(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:39:21 +03:00
|
|
|
void EditTool::switch_tool(DrawingProgramToolType newTool) {
|
2025-07-06 20:43:53 +03:00
|
|
|
auto a = controls.compToEdit.lock();
|
2025-07-23 17:21:56 +03:00
|
|
|
if(a && a->collabListInfo.lock()) {
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool->commit_edit_updates(a, controls.prevData);
|
2025-07-31 09:34:17 +03:00
|
|
|
a->commit_update(drawP);
|
|
|
|
|
a->client_send_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
controls.compToEdit.reset();
|
|
|
|
|
controls.pointHandles.clear();
|
|
|
|
|
controls.pointDragging = nullptr;
|
|
|
|
|
controls.isEditing = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EditTool::edit_start(const std::shared_ptr<DrawComponent>& comp) {
|
|
|
|
|
controls.isEditing = true;
|
|
|
|
|
switch(comp->get_type()) {
|
|
|
|
|
case DRAWCOMPONENT_TEXTBOX: {
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool = std::make_unique<TextBoxEditTool>(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case DRAWCOMPONENT_ELLIPSE: {
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool = std::make_unique<EllipseDrawEditTool>(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case DRAWCOMPONENT_RECTANGLE: {
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool = std::make_unique<RectDrawEditTool>(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case DRAWCOMPONENT_IMAGE: {
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool = std::make_unique<ImageEditTool>(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
controls.isEditing = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(controls.isEditing) {
|
|
|
|
|
controls.compToEdit = comp;
|
2025-08-02 20:10:33 +03:00
|
|
|
controls.compEditTool->edit_start(*this, comp, controls.prevData);
|
2025-07-06 20:43:53 +03:00
|
|
|
comp->lastUpdateTime = std::chrono::steady_clock::now();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EditTool::tool_update() {
|
|
|
|
|
if(!controls.isEditing) {
|
2025-07-24 16:35:48 +03:00
|
|
|
SCollision::AABB<WorldScalar> mouseAABB{drawP.world.get_mouse_world_pos() - WorldVec{0.5f, 0.5f}, drawP.world.get_mouse_world_pos() + WorldVec{0.5f, 0.5f}};
|
|
|
|
|
SCollision::ColliderCollection<WorldScalar> cMouseAABB;
|
|
|
|
|
cMouseAABB.aabb.emplace_back(mouseAABB);
|
|
|
|
|
cMouseAABB.recalculate_bounds();
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
if(drawP.controls.leftClick) {
|
2025-07-24 16:35:48 +03:00
|
|
|
CollabListType::ObjectInfoPtr lastSelectedObj;
|
|
|
|
|
uint64_t lastPos = 0;
|
|
|
|
|
|
|
|
|
|
drawP.compCache.traverse_bvh_run_function(mouseAABB, [&](const std::shared_ptr<DrawingProgramCacheBVHNode>& bvhNode, const std::vector<CollabListType::ObjectInfoPtr>& components) {
|
|
|
|
|
for(auto& c : components) {
|
2025-07-30 11:45:18 +03:00
|
|
|
if(c->pos >= lastPos && c->obj->collides_with_world_coords(drawP.world.drawData.cam.c, cMouseAABB)) {
|
2025-07-24 16:35:48 +03:00
|
|
|
lastSelectedObj = c;
|
|
|
|
|
lastPos = c->pos;
|
|
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
2025-07-24 16:35:48 +03:00
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(lastSelectedObj)
|
|
|
|
|
edit_start(lastSelectedObj->obj);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2025-07-24 16:35:48 +03:00
|
|
|
SCollision::Circle<float> mouseCircle{drawP.world.main.input.mouse.pos, 1.0f};
|
|
|
|
|
SCollision::ColliderCollection<float> cMouseCircle;
|
|
|
|
|
cMouseCircle.circle.emplace_back(mouseCircle);
|
|
|
|
|
cMouseCircle.recalculate_bounds();
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
std::shared_ptr<DrawComponent> a = controls.compToEdit.lock();
|
|
|
|
|
if(a) {
|
|
|
|
|
bool isMovingPoint = false;
|
|
|
|
|
bool clickedAway = false;
|
|
|
|
|
if(!controls.pointDragging && drawP.controls.leftClick) {
|
|
|
|
|
for(HandleData& h : controls.pointHandles) {
|
2025-07-24 16:35:48 +03:00
|
|
|
if(SCollision::collide(mouseCircle, SCollision::Circle<float>(drawP.world.drawData.cam.c.to_space(a->coords.from_space(*h.p)), DRAG_POINT_RADIUS))) {
|
2025-07-06 20:43:53 +03:00
|
|
|
controls.pointDragging = &h;
|
|
|
|
|
isMovingPoint = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 14:25:28 +03:00
|
|
|
if(!isMovingPoint && !a->collides_with_cam_coords(drawP.world.drawData.cam.c, cMouseCircle))
|
2025-07-06 20:43:53 +03:00
|
|
|
clickedAway = true;
|
|
|
|
|
}
|
|
|
|
|
else if(controls.pointDragging) {
|
|
|
|
|
if(drawP.controls.leftClickHeld) {
|
|
|
|
|
Vector2f newPos = a->coords.get_mouse_pos(drawP.world);
|
|
|
|
|
if(newPos != *controls.pointDragging->p) {
|
|
|
|
|
if(controls.pointDragging->min)
|
|
|
|
|
newPos = cwise_vec_max(*controls.pointDragging->min, newPos);
|
|
|
|
|
if(controls.pointDragging->max)
|
|
|
|
|
newPos = cwise_vec_min(*controls.pointDragging->max, newPos);
|
|
|
|
|
*controls.pointDragging->p = newPos;
|
2025-07-31 09:34:17 +03:00
|
|
|
a->commit_update(drawP);
|
|
|
|
|
a->client_send_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
isMovingPoint = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
controls.pointDragging = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a->lastUpdateTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
2025-08-02 20:10:33 +03:00
|
|
|
bool shouldNotReset = controls.compEditTool->edit_update(a);
|
2025-07-06 20:43:53 +03:00
|
|
|
if(!shouldNotReset || clickedAway)
|
2025-08-02 20:39:21 +03:00
|
|
|
switch_tool(get_type());
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
else
|
2025-08-02 20:39:21 +03:00
|
|
|
switch_tool(get_type());
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 16:53:22 +03:00
|
|
|
bool EditTool::prevent_undo_or_redo() {
|
|
|
|
|
return controls.isEditing;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
void EditTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
|
|
|
|
if(controls.isEditing) {
|
|
|
|
|
std::shared_ptr<DrawComponent> a = controls.compToEdit.lock();
|
|
|
|
|
if(a) {
|
|
|
|
|
for(HandleData& h : controls.pointHandles)
|
|
|
|
|
drawP.draw_drag_circle(canvas, drawData.cam.c.to_space((a->coords.from_space(*h.p))), {0.1f, 0.9f, 0.9f, 1.0f}, drawData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|