2026-05-11 23:06:39 +03:00
|
|
|
/*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-01-08 11:13:49 +03:00
|
|
|
#include "LineDrawTool.hpp"
|
|
|
|
|
#include "../DrawingProgram.hpp"
|
|
|
|
|
#include "../../MainProgram.hpp"
|
|
|
|
|
#include "../../DrawData.hpp"
|
|
|
|
|
#include "DrawingProgramToolBase.hpp"
|
|
|
|
|
#include "../../CanvasComponents/BrushStrokeCanvasComponent.hpp"
|
|
|
|
|
#include "../../CanvasComponents/CanvasComponentContainer.hpp"
|
|
|
|
|
#include "Helpers/MathExtras.hpp"
|
|
|
|
|
|
2026-03-30 12:32:41 +03:00
|
|
|
#include "../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
|
2026-03-30 17:05:24 +03:00
|
|
|
#include "../../GUIStuff/ElementHelpers/CheckBoxHelpers.hpp"
|
2026-03-30 12:32:41 +03:00
|
|
|
|
2026-01-08 11:13:49 +03:00
|
|
|
LineDrawTool::LineDrawTool(DrawingProgram& initDrawP):
|
|
|
|
|
DrawingProgramToolBase(initDrawP)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
DrawingProgramToolType LineDrawTool::get_type() {
|
|
|
|
|
return DrawingProgramToolType::LINE;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 12:36:00 +03:00
|
|
|
void LineDrawTool::gui_toolbox(Toolbar& t) {
|
2026-03-30 12:32:41 +03:00
|
|
|
using namespace GUIStuff;
|
|
|
|
|
using namespace ElementHelpers;
|
2026-03-31 13:17:58 +03:00
|
|
|
auto& gui = drawP.world.main.g.gui;
|
2026-01-26 19:08:36 +03:00
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
2026-03-31 13:17:58 +03:00
|
|
|
gui.new_id("rect draw tool", [&] {
|
|
|
|
|
text_label_centered(gui, "Draw Line");
|
2026-03-30 18:51:14 +03:00
|
|
|
toolConfig.relative_width_gui(drawP, "Size");
|
2026-03-31 13:17:58 +03:00
|
|
|
checkbox_boolean_field(gui, "hasroundcaps", "Round Caps", &toolConfig.lineDraw.hasRoundCaps);
|
2026-03-30 18:51:14 +03:00
|
|
|
});
|
2026-01-08 11:13:49 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-30 12:09:03 +03:00
|
|
|
void LineDrawTool::gui_phone_toolbox(PhoneDrawingProgramScreen& t) {
|
|
|
|
|
using namespace GUIStuff;
|
|
|
|
|
using namespace ElementHelpers;
|
|
|
|
|
auto& gui = drawP.world.main.g.gui;
|
|
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
|
|
|
|
gui.new_id("rect draw tool", [&] {
|
|
|
|
|
toolConfig.relative_width_gui(drawP, "Size");
|
|
|
|
|
checkbox_boolean_field(gui, "hasroundcaps", "Round Caps", &toolConfig.lineDraw.hasRoundCaps);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 16:47:09 +03:00
|
|
|
void LineDrawTool::input_mouse_button_on_canvas_callback(const InputManager::MouseButtonCallbackArgs& button) {
|
2026-03-02 23:09:48 +03:00
|
|
|
if(button.button == InputManager::MouseButton::LEFT) {
|
2026-03-02 16:47:09 +03:00
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
2026-03-31 18:41:19 +03:00
|
|
|
if(button.down && drawP.layerMan.is_a_layer_being_edited() && !objInfoBeingEdited && !drawP.world.main.g.gui.cursor_obstructed()) {
|
2026-01-29 20:33:22 +03:00
|
|
|
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
|
|
|
|
|
if(!relativeWidthResult.first.has_value()) {
|
2026-03-03 12:06:48 +03:00
|
|
|
drawP.world.main.toolConfig.print_relative_width_fail_message(relativeWidthResult.second);
|
2026-01-29 20:33:22 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float width = relativeWidthResult.first.value();
|
|
|
|
|
|
2026-01-08 11:13:49 +03:00
|
|
|
CanvasComponentContainer* newBrushStrokeContainer = new CanvasComponentContainer(drawP.world.netObjMan, CanvasComponentType::BRUSHSTROKE);
|
|
|
|
|
BrushStrokeCanvasComponent& newBrushStroke = static_cast<BrushStrokeCanvasComponent&>(newBrushStrokeContainer->get_comp());
|
|
|
|
|
|
|
|
|
|
BrushStrokeCanvasComponentPoint p;
|
|
|
|
|
p.pos = drawP.world.main.input.mouse.pos;
|
2026-01-29 20:33:22 +03:00
|
|
|
p.width = width;
|
2026-01-27 16:55:09 +03:00
|
|
|
newBrushStroke.d.points->emplace_back(p);
|
2026-01-08 11:13:49 +03:00
|
|
|
p.pos = ensure_points_have_distance(p.pos, p.pos, 1.0f);
|
2026-01-27 16:55:09 +03:00
|
|
|
newBrushStroke.d.points->emplace_back(p);
|
|
|
|
|
newBrushStroke.d.color = toolConfig.globalConf.foregroundColor;
|
|
|
|
|
newBrushStroke.d.hasRoundCaps = toolConfig.lineDraw.hasRoundCaps;
|
2026-01-08 11:13:49 +03:00
|
|
|
newBrushStrokeContainer->coords = drawP.world.drawData.cam.c;
|
|
|
|
|
objInfoBeingEdited = drawP.layerMan.add_component_to_layer_being_edited(newBrushStrokeContainer);
|
|
|
|
|
}
|
2026-03-02 16:47:09 +03:00
|
|
|
else if(!button.down && objInfoBeingEdited)
|
|
|
|
|
commit();
|
2026-01-08 11:13:49 +03:00
|
|
|
}
|
2026-03-02 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineDrawTool::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
|
|
|
|
|
if(objInfoBeingEdited) {
|
2026-01-08 11:47:32 +03:00
|
|
|
constexpr float SNAP_DIVISION_COUNT = 12.0f;
|
2026-03-02 16:47:09 +03:00
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
|
|
|
|
BrushStrokeCanvasComponent& brushStroke = static_cast<BrushStrokeCanvasComponent&>(containerPtr->get_comp());
|
2026-03-03 10:18:47 +03:00
|
|
|
Vector2f newPos = containerPtr->coords.from_cam_space_to_this(drawP.world, motion.pos);
|
2026-03-02 16:47:09 +03:00
|
|
|
Vector2f oldPos = brushStroke.d.points->front().pos;
|
|
|
|
|
if(drawP.world.main.input.key(InputManager::KEY_GENERIC_LSHIFT).held) {
|
|
|
|
|
Vector2f diff = (newPos - oldPos);
|
|
|
|
|
float diffLength = diff.norm();
|
|
|
|
|
diff.normalize();
|
|
|
|
|
float angle = (std::atan2(diff.y(), diff.x()) / std::numbers::pi) * SNAP_DIVISION_COUNT;
|
|
|
|
|
angle = std::round(angle);
|
|
|
|
|
angle = (angle * std::numbers::pi) / SNAP_DIVISION_COUNT;
|
|
|
|
|
newPos = oldPos + diffLength * Vector2f{cos(angle), sin(angle)};
|
2026-01-08 11:13:49 +03:00
|
|
|
}
|
2026-03-02 16:47:09 +03:00
|
|
|
brushStroke.d.points->back().pos = ensure_points_have_distance(oldPos, newPos, 1.0f);
|
2026-05-18 18:35:40 +03:00
|
|
|
commitUpdate = true;
|
2026-01-08 11:13:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 16:47:09 +03:00
|
|
|
void LineDrawTool::erase_component(CanvasComponentContainer::ObjInfo* erasedComp) {
|
2026-05-18 18:35:40 +03:00
|
|
|
if(objInfoBeingEdited == erasedComp) {
|
2026-03-02 16:47:09 +03:00
|
|
|
objInfoBeingEdited = nullptr;
|
2026-05-18 18:35:40 +03:00
|
|
|
commitUpdate = false;
|
|
|
|
|
}
|
2026-03-02 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 12:36:00 +03:00
|
|
|
void LineDrawTool::right_click_popup_gui(Toolbar& t, Vector2f popupPos) {
|
2026-03-02 16:47:09 +03:00
|
|
|
t.paint_popup(popupPos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineDrawTool::switch_tool(DrawingProgramToolType newTool) {
|
|
|
|
|
commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineDrawTool::tool_update() {
|
2026-05-18 18:35:40 +03:00
|
|
|
if(commitUpdate && objInfoBeingEdited) {
|
|
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
|
|
|
|
containerPtr->commit_update(drawP);
|
|
|
|
|
containerPtr->send_comp_update(drawP, false);
|
|
|
|
|
commitUpdate = false;
|
|
|
|
|
}
|
2026-03-02 16:47:09 +03:00
|
|
|
}
|
|
|
|
|
|
2026-01-08 11:13:49 +03:00
|
|
|
bool LineDrawTool::prevent_undo_or_redo() {
|
|
|
|
|
return objInfoBeingEdited;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineDrawTool::commit() {
|
|
|
|
|
if(objInfoBeingEdited) {
|
|
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
|
|
|
|
containerPtr->commit_update(drawP);
|
|
|
|
|
containerPtr->send_comp_update(drawP, true);
|
2026-05-18 18:35:40 +03:00
|
|
|
commitUpdate = false;
|
2026-04-03 14:18:04 +03:00
|
|
|
if(containerPtr->get_world_bounds().has_value())
|
|
|
|
|
drawP.layerMan.add_undo_place_component(objInfoBeingEdited);
|
|
|
|
|
else {
|
|
|
|
|
auto& components = containerPtr->parentLayer->get_layer().components;
|
|
|
|
|
components->erase(components, containerPtr->objInfo);
|
|
|
|
|
}
|
2026-01-08 11:13:49 +03:00
|
|
|
objInfoBeingEdited = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineDrawTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
|
|
|
|
}
|