2025-07-06 20:43:53 +03:00
|
|
|
#include "BrushTool.hpp"
|
|
|
|
|
#include <Helpers/ConvertVec.hpp>
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../../GUIStuff/GUIManager.hpp"
|
|
|
|
|
#include "../DrawingProgram.hpp"
|
|
|
|
|
#include "../../MainProgram.hpp"
|
2025-08-02 20:10:33 +03:00
|
|
|
#include "DrawingProgramToolBase.hpp"
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../../CanvasComponents/BrushStrokeCanvasComponent.hpp"
|
2025-12-14 18:57:40 +03:00
|
|
|
#include "Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp"
|
|
|
|
|
#include "Helpers/NetworkingObjects/NetObjWeakPtr.hpp"
|
2025-12-26 10:40:58 +03:00
|
|
|
#include "../../CanvasComponents/CanvasComponentContainer.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
#include <chrono>
|
|
|
|
|
|
2026-03-29 20:23:38 +03:00
|
|
|
#include "../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
|
|
|
|
|
#include "../../GUIStuff/ElementHelpers/CheckBoxHelpers.hpp"
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
#define VEL_SMOOTH_MIN 0.6
|
|
|
|
|
#define VEL_SMOOTH_MAX 1.0
|
|
|
|
|
#define MINIMUM_DISTANCE_TO_NEXT_POINT 0.002f
|
|
|
|
|
|
|
|
|
|
BrushTool::BrushTool(DrawingProgram& initDrawP):
|
2025-08-02 20:10:33 +03:00
|
|
|
DrawingProgramToolBase(initDrawP)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
DrawingProgramToolType BrushTool::get_type() {
|
|
|
|
|
return DrawingProgramToolType::BRUSH;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-02 20:39:21 +03:00
|
|
|
void BrushTool::switch_tool(DrawingProgramToolType newTool) {
|
2025-07-06 20:43:53 +03:00
|
|
|
commit_stroke();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
void BrushTool::erase_component(CanvasComponentContainer::ObjInfo* erasedComp) {
|
|
|
|
|
if(objInfoBeingEdited == erasedComp)
|
2025-12-15 15:36:42 +03:00
|
|
|
objInfoBeingEdited = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 22:31:48 +03:00
|
|
|
void BrushTool::input_mouse_button_on_canvas_callback(const InputManager::MouseButtonCallbackArgs& button) {
|
|
|
|
|
if(button.button == InputManager::MouseButton::LEFT) {
|
|
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
|
|
|
|
if(button.down && drawP.layerMan.is_a_layer_being_edited() && !objInfoBeingEdited) {
|
|
|
|
|
if(drawP.world.main.input.pen.isDown) {
|
|
|
|
|
penWidth = drawP.world.main.input.pen.pressure;
|
|
|
|
|
if(penWidth != 0.0f) {
|
|
|
|
|
float brushMinSize = drawP.world.main.toolbar.tabletOptions.brushMinimumSize;
|
|
|
|
|
penWidth = brushMinSize + penWidth * (1.0f - brushMinSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
penWidth = 1.0f;
|
|
|
|
|
|
|
|
|
|
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
|
|
|
|
|
if(!relativeWidthResult.first.has_value()) {
|
|
|
|
|
drawP.world.main.toolConfig.print_relative_width_fail_message(relativeWidthResult.second);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float width = relativeWidthResult.first.value() * penWidth;
|
|
|
|
|
|
|
|
|
|
CanvasComponentContainer* newBrushStrokeContainer = new CanvasComponentContainer(drawP.world.netObjMan, CanvasComponentType::BRUSHSTROKE);
|
|
|
|
|
BrushStrokeCanvasComponent& newBrushStroke = static_cast<BrushStrokeCanvasComponent&>(newBrushStrokeContainer->get_comp());
|
|
|
|
|
|
|
|
|
|
BrushStrokeCanvasComponentPoint p;
|
|
|
|
|
p.pos = button.pos;
|
|
|
|
|
p.width = width;
|
|
|
|
|
prevPointUnaltered = p.pos;
|
|
|
|
|
newBrushStroke.d.points->emplace_back(p);
|
|
|
|
|
newBrushStroke.d.color = toolConfig.globalConf.foregroundColor;
|
|
|
|
|
newBrushStroke.d.hasRoundCaps = toolConfig.brush.hasRoundCaps;
|
|
|
|
|
newBrushStrokeContainer->coords = drawP.world.drawData.cam.c;
|
|
|
|
|
objInfoBeingEdited = drawP.layerMan.add_component_to_layer_being_edited(newBrushStrokeContainer);
|
|
|
|
|
addedTemporaryPoint = false;
|
|
|
|
|
}
|
|
|
|
|
else if(!button.down && objInfoBeingEdited)
|
|
|
|
|
commit_stroke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BrushTool::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
|
|
|
|
|
if(objInfoBeingEdited) {
|
|
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
|
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
|
|
|
|
BrushStrokeCanvasComponent& brushStroke = static_cast<BrushStrokeCanvasComponent&>(containerPtr->get_comp());
|
|
|
|
|
auto& brushPoints = *brushStroke.d.points;
|
|
|
|
|
float width = toolConfig.get_relative_width_stroke_size(drawP, containerPtr->coords.inverseScale).first.value() * penWidth;
|
|
|
|
|
|
|
|
|
|
BrushStrokeCanvasComponentPoint p;
|
|
|
|
|
p.pos = containerPtr->coords.from_cam_space_to_this(drawP.world, motion.pos);
|
|
|
|
|
p.width = width;
|
|
|
|
|
|
|
|
|
|
if(!addedTemporaryPoint) {
|
|
|
|
|
if(extensive_point_checking(brushStroke, p.pos)) {
|
|
|
|
|
brushPoints.emplace_back(p);
|
|
|
|
|
addedTemporaryPoint = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
brushPoints.back().width = std::max(brushPoints.back().width, p.width);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(addedTemporaryPoint) {
|
|
|
|
|
const BrushStrokeCanvasComponentPoint& prevP = brushPoints[brushPoints.size() - 2];
|
|
|
|
|
float distToPrev = (p.pos - prevP.pos).norm();
|
|
|
|
|
if(extensive_point_checking_back(brushStroke, p.pos)) {
|
|
|
|
|
brushPoints.back().pos = p.pos;
|
|
|
|
|
brushPoints.back().width = std::max(brushPoints.back().width, p.width);
|
|
|
|
|
brushPoints[brushPoints.size() - 2].width = std::max(brushPoints[brushPoints.size() - 2].width, p.width);
|
|
|
|
|
}
|
|
|
|
|
if((!drawingMinimumRelativeToSize && distToPrev >= 10.0) || (drawingMinimumRelativeToSize && distToPrev >= width * BrushStrokeCanvasComponent::DRAW_MINIMUM_LIMIT)) {
|
|
|
|
|
brushPoints.back() = p;
|
|
|
|
|
addedTemporaryPoint = false;
|
|
|
|
|
|
|
|
|
|
if(midwayInterpolation) {
|
|
|
|
|
if(brushPoints.size() != 2) // Don't interpolate the first point
|
|
|
|
|
brushPoints[brushPoints.size() - 2].pos = (prevPointUnaltered + p.pos) * 0.5;
|
|
|
|
|
prevPointUnaltered = p.pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
containerPtr->send_comp_update(drawP, false);
|
|
|
|
|
containerPtr->commit_update(drawP);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BrushTool::input_pen_axis_callback(const InputManager::PenAxisCallbackArgs& axis) {
|
|
|
|
|
if(axis.axis == SDL_PEN_AXIS_PRESSURE) {
|
|
|
|
|
penWidth = axis.value;
|
|
|
|
|
if(penWidth != 0.0f) {
|
|
|
|
|
float brushMinSize = drawP.world.main.toolbar.tabletOptions.brushMinimumSize;
|
|
|
|
|
penWidth = brushMinSize + penWidth * (1.0f - brushMinSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
bool BrushTool::extensive_point_checking(const BrushStrokeCanvasComponent& brushStroke, const Vector2f& newPoint) {
|
2026-01-27 16:55:09 +03:00
|
|
|
auto& points = *brushStroke.d.points;
|
2025-07-06 20:43:53 +03:00
|
|
|
if(points.size() >= 1 && (newPoint - points[points.size() - 1].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
if(points.size() >= 2 && (newPoint - points[points.size() - 2].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
if(points.size() >= 3 && (newPoint - points[points.size() - 3].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
bool BrushTool::extensive_point_checking_back(const BrushStrokeCanvasComponent& brushStroke, const Vector2f& newPoint) {
|
2026-01-27 16:55:09 +03:00
|
|
|
auto& points = *brushStroke.d.points;
|
2025-07-06 20:43:53 +03:00
|
|
|
if(points.size() >= 2 && (newPoint - points[points.size() - 2].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
if(points.size() >= 3 && (newPoint - points[points.size() - 3].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
if(points.size() >= 4 && (newPoint - points[points.size() - 4].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BrushTool::tool_update() {
|
|
|
|
|
if(drawP.controls.cursorHoveringOverCanvas)
|
|
|
|
|
drawP.world.main.input.hideCursor = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
void BrushTool::commit_stroke() {
|
2025-12-15 16:55:09 +03:00
|
|
|
if(objInfoBeingEdited) {
|
|
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
|
|
|
|
containerPtr->commit_update(drawP);
|
|
|
|
|
containerPtr->send_comp_update(drawP, true);
|
2026-01-04 16:50:47 +03:00
|
|
|
drawP.layerMan.add_undo_place_component(objInfoBeingEdited);
|
2025-12-15 16:55:09 +03:00
|
|
|
objInfoBeingEdited = nullptr;
|
|
|
|
|
}
|
2025-12-14 18:57:40 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
void BrushTool::gui_toolbox() {
|
2026-03-29 20:23:38 +03:00
|
|
|
using namespace GUIStuff;
|
|
|
|
|
using namespace ElementHelpers;
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
t.gui.push_id("brush tool");
|
2026-03-29 20:23:38 +03:00
|
|
|
text_label_centered(t.gui, "Brush");
|
|
|
|
|
checkbox_boolean_field(t.gui, "hasroundcaps", "Round Caps", &drawP.world.main.toolConfig.brush.hasRoundCaps);
|
2026-01-29 20:33:22 +03:00
|
|
|
drawP.world.main.toolConfig.relative_width_gui(drawP, "Size");
|
2025-07-06 20:43:53 +03:00
|
|
|
t.gui.pop_id();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 20:23:38 +03:00
|
|
|
void BrushTool::right_click_popup_gui(Vector2f popupPos) {
|
2025-12-01 17:33:14 +03:00
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
t.paint_popup(popupPos);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-27 16:53:22 +03:00
|
|
|
bool BrushTool::prevent_undo_or_redo() {
|
2025-12-30 19:46:08 +03:00
|
|
|
return objInfoBeingEdited;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BrushTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
2026-03-05 12:36:34 +03:00
|
|
|
if(!drawP.world.main.input.isTouchDevice && drawData.main->world->drawProg.controls.cursorHoveringOverCanvas) {
|
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()) {
|
|
|
|
|
float width = relativeWidthResult.first.value();
|
|
|
|
|
if(objInfoBeingEdited)
|
|
|
|
|
width *= penWidth * 0.5f;
|
|
|
|
|
else
|
|
|
|
|
width *= 0.5f;
|
2026-03-20 16:24:52 +03:00
|
|
|
width += 1.0f;
|
2026-01-29 20:33:22 +03:00
|
|
|
Vector2f pos = drawData.main->input.mouse.pos;
|
2026-03-20 16:24:52 +03:00
|
|
|
SkPaint linePaint;
|
2026-03-20 20:53:59 +03:00
|
|
|
linePaint.setAntiAlias(drawData.skiaAA);
|
2026-03-20 16:24:52 +03:00
|
|
|
linePaint.setColor4f({1.0f, 1.0f, 1.0f, 1.0f});
|
|
|
|
|
linePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
linePaint.setStrokeCap(SkPaint::kRound_Cap);
|
|
|
|
|
linePaint.setStrokeWidth(0.0f);
|
|
|
|
|
linePaint.setBlender(CanvasTheme::get_visible_blend_mode());
|
|
|
|
|
SkPath circ = SkPath::Circle(pos.x(), pos.y(), width);
|
|
|
|
|
canvas->drawPath(circ, linePaint);
|
2026-01-29 20:33:22 +03:00
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|