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);
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, true);
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 17:33:14 +03:00
|
|
|
bool EditTool::right_click_popup_gui(Vector2f popupPos) {
|
2025-12-02 09:53:03 +03:00
|
|
|
if(controls.isEditing)
|
|
|
|
|
return controls.compEditTool->right_click_popup_gui(controls.compToEdit.lock(), popupPos);
|
|
|
|
|
else
|
|
|
|
|
return drawP.selection_action_menu(popupPos);
|
2025-12-01 17:33:14 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
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);
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, true);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
controls.compToEdit.reset();
|
|
|
|
|
controls.pointHandles.clear();
|
|
|
|
|
controls.pointDragging = nullptr;
|
|
|
|
|
controls.isEditing = false;
|
2025-09-29 11:39:00 +03:00
|
|
|
|
2025-10-03 13:16:38 +03:00
|
|
|
if(!drawP.is_selection_allowing_tool(newTool))
|
2025-09-29 11:39:00 +03:00
|
|
|
drawP.selection.deselect_all();
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 11:45:46 +03:00
|
|
|
bool EditTool::is_editable(const std::shared_ptr<DrawComponent>& comp) {
|
|
|
|
|
return comp->get_type() != DRAWCOMPONENT_BRUSHSTROKE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
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-09-29 11:39:00 +03:00
|
|
|
SCollision::AABB<float> camMouseAABB{drawP.world.main.input.mouse.pos - Vector2f{0.5f, 0.5f}, drawP.world.main.input.mouse.pos + Vector2f{0.5f, 0.5f}};
|
|
|
|
|
SCollision::ColliderCollection<float> camCMouseAABB;
|
|
|
|
|
camCMouseAABB.aabb.emplace_back(camMouseAABB);
|
|
|
|
|
camCMouseAABB.recalculate_bounds();
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
if(drawP.controls.leftClick) {
|
2025-09-29 11:45:46 +03:00
|
|
|
bool modifySelection = !drawP.selection.is_being_transformed();
|
2025-09-29 11:39:00 +03:00
|
|
|
if(drawP.world.main.input.mouse.leftClicks >= 2 && !drawP.world.main.input.key(InputManager::KEY_GENERIC_LSHIFT).held && !drawP.world.main.input.key(InputManager::KEY_GENERIC_LALT).held) {
|
|
|
|
|
CollabListType::ObjectInfoPtr selectedObjectToEdit = drawP.selection.get_front_object_colliding_with(camCMouseAABB);
|
2025-07-24 16:35:48 +03:00
|
|
|
|
2025-09-29 11:45:46 +03:00
|
|
|
if(selectedObjectToEdit && is_editable(selectedObjectToEdit->obj)) {
|
2025-09-29 11:39:00 +03:00
|
|
|
drawP.selection.deselect_all();
|
|
|
|
|
edit_start(selectedObjectToEdit->obj);
|
2025-09-29 11:45:46 +03:00
|
|
|
modifySelection = false;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
2025-09-29 11:39:00 +03:00
|
|
|
}
|
2025-09-29 11:45:46 +03:00
|
|
|
if(modifySelection) {
|
2025-09-29 11:39:00 +03:00
|
|
|
if(drawP.world.main.input.key(InputManager::KEY_GENERIC_LSHIFT).held)
|
|
|
|
|
drawP.selection.add_from_cam_coord_collider_to_selection(camCMouseAABB, true);
|
|
|
|
|
else if(drawP.world.main.input.key(InputManager::KEY_GENERIC_LALT).held)
|
|
|
|
|
drawP.selection.remove_from_cam_coord_collider_to_selection(camCMouseAABB, true);
|
|
|
|
|
else {
|
|
|
|
|
drawP.selection.deselect_all();
|
|
|
|
|
drawP.selection.add_from_cam_coord_collider_to_selection(camCMouseAABB, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
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-11-23 15:02:02 +03:00
|
|
|
if(SCollision::collide(mouseCircle, SCollision::Circle<float>(drawP.world.drawData.cam.c.to_space(a->coords.from_space(*h.p)), drawP.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)
|
2025-08-03 13:04:07 +03:00
|
|
|
newPos = cwise_vec_max((*controls.pointDragging->min + Vector2f{controls.pointDragging->minimumDistanceBetweenBoundsAndPoint, controls.pointDragging->minimumDistanceBetweenBoundsAndPoint}).eval(), newPos);
|
2025-07-06 20:43:53 +03:00
|
|
|
if(controls.pointDragging->max)
|
2025-08-03 13:04:07 +03:00
|
|
|
newPos = cwise_vec_min((*controls.pointDragging->max - Vector2f{controls.pointDragging->minimumDistanceBetweenBoundsAndPoint, controls.pointDragging->minimumDistanceBetweenBoundsAndPoint}).eval(), newPos);
|
2025-07-06 20:43:53 +03:00
|
|
|
*controls.pointDragging->p = newPos;
|
2025-07-31 09:34:17 +03:00
|
|
|
a->commit_update(drawP);
|
2025-10-01 08:59:44 +03:00
|
|
|
a->client_send_update(drawP, false);
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|