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>
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2026-01-26 19:08:36 +03:00
|
|
|
auto& toolConfig = drawP.world.main.toolConfig;
|
|
|
|
|
|
2025-07-06 20:43:53 +03:00
|
|
|
if(drawP.controls.cursorHoveringOverCanvas)
|
|
|
|
|
drawP.world.main.input.hideCursor = true;
|
|
|
|
|
|
2025-12-30 19:46:08 +03:00
|
|
|
if(!objInfoBeingEdited)
|
2025-12-14 18:57:40 +03:00
|
|
|
penSmoothingData.clear();
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
if(drawP.world.main.input.pen.isDown && drawP.controls.leftClickHeld && drawP.world.main.toolbar.tabletOptions.pressureAffectsBrushWidth) {
|
2025-12-14 18:57:40 +03:00
|
|
|
while(!penSmoothingData.empty() && (std::chrono::steady_clock::now() - penSmoothingData.front().t) > std::chrono::milliseconds(static_cast<int>(drawP.world.main.toolbar.tabletOptions.smoothingSamplingTime * 1000.0f)))
|
|
|
|
|
penSmoothingData.pop_front();
|
|
|
|
|
penSmoothingData.emplace_back(
|
2025-07-06 20:43:53 +03:00
|
|
|
drawP.world.main.input.pen.pressure,
|
|
|
|
|
std::chrono::steady_clock::now()
|
|
|
|
|
);
|
|
|
|
|
float averagePressure = 0.0f;
|
2025-12-14 18:57:40 +03:00
|
|
|
for(auto& [width, t] : penSmoothingData)
|
2025-07-06 20:43:53 +03:00
|
|
|
averagePressure += width;
|
2025-12-14 18:57:40 +03:00
|
|
|
averagePressure /= penSmoothingData.size();
|
|
|
|
|
penWidth = averagePressure;
|
|
|
|
|
if(penWidth != 0.0f) {
|
2025-11-30 16:19:36 +03:00
|
|
|
float brushMinSize = drawP.world.main.toolbar.tabletOptions.brushMinimumSize;
|
2025-12-14 18:57:40 +03:00
|
|
|
penWidth = brushMinSize + penWidth * (1.0f - brushMinSize);
|
2025-11-30 16:19:36 +03:00
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
else
|
2025-12-14 18:57:40 +03:00
|
|
|
penWidth = 1.0f;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
bool isPenDown = false;
|
|
|
|
|
if(drawP.world.main.input.pen.isDown && drawP.controls.leftClickHeld)
|
2025-12-14 18:57:40 +03:00
|
|
|
isPenDown = penWidth != 0.0;
|
2025-07-06 20:43:53 +03:00
|
|
|
else if(drawP.controls.leftClickHeld)
|
|
|
|
|
isPenDown = true;
|
|
|
|
|
|
|
|
|
|
if(isPenDown) {
|
2025-12-30 19:46:08 +03:00
|
|
|
if(!objInfoBeingEdited) {
|
2026-01-29 20:33:22 +03:00
|
|
|
if(drawP.controls.leftClick && drawP.layerMan.is_a_layer_being_edited()) {
|
|
|
|
|
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
|
|
|
|
|
if(!relativeWidthResult.first.has_value()) {
|
|
|
|
|
if(drawP.controls.leftClick)
|
|
|
|
|
drawP.world.main.toolConfig.print_relative_width_fail_message(relativeWidthResult.second);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
float width = relativeWidthResult.first.value() * penWidth;
|
|
|
|
|
|
2025-12-26 10:10:19 +03:00
|
|
|
CanvasComponentContainer* newBrushStrokeContainer = new CanvasComponentContainer(drawP.world.netObjMan, CanvasComponentType::BRUSHSTROKE);
|
|
|
|
|
BrushStrokeCanvasComponent& newBrushStroke = static_cast<BrushStrokeCanvasComponent&>(newBrushStrokeContainer->get_comp());
|
2025-12-14 18:57:40 +03:00
|
|
|
|
2025-12-26 10:10:19 +03:00
|
|
|
BrushStrokeCanvasComponentPoint p;
|
|
|
|
|
p.pos = drawP.world.main.input.mouse.pos;
|
2026-01-29 20:33:22 +03:00
|
|
|
p.width = width;
|
2025-12-26 10:10:19 +03:00
|
|
|
prevPointUnaltered = p.pos;
|
2026-01-27 16:55:09 +03:00
|
|
|
newBrushStroke.d.points->emplace_back(p);
|
|
|
|
|
newBrushStroke.d.color = toolConfig.globalConf.foregroundColor;
|
|
|
|
|
newBrushStroke.d.hasRoundCaps = toolConfig.brush.hasRoundCaps;
|
2025-12-26 10:10:19 +03:00
|
|
|
newBrushStrokeContainer->coords = drawP.world.drawData.cam.c;
|
|
|
|
|
objInfoBeingEdited = drawP.layerMan.add_component_to_layer_being_edited(newBrushStrokeContainer);
|
|
|
|
|
addedTemporaryPoint = false;
|
|
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
else {
|
2025-12-15 15:36:42 +03:00
|
|
|
NetworkingObjects::NetObjOwnerPtr<CanvasComponentContainer>& containerPtr = objInfoBeingEdited->obj;
|
2025-12-14 18:57:40 +03:00
|
|
|
BrushStrokeCanvasComponent& brushStroke = static_cast<BrushStrokeCanvasComponent&>(containerPtr->get_comp());
|
2026-01-27 16:55:09 +03:00
|
|
|
auto& brushPoints = *brushStroke.d.points;
|
2026-01-29 20:33:22 +03:00
|
|
|
float width = toolConfig.get_relative_width_stroke_size(drawP, containerPtr->coords.inverseScale).first.value() * penWidth;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
BrushStrokeCanvasComponentPoint p;
|
|
|
|
|
p.pos = containerPtr->coords.get_mouse_pos(drawP.world);
|
2026-01-29 20:33:22 +03:00
|
|
|
p.width = width;
|
2025-07-06 20:43:53 +03:00
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
if(!addedTemporaryPoint) {
|
|
|
|
|
if(extensive_point_checking(brushStroke, p.pos)) {
|
2026-01-27 16:55:09 +03:00
|
|
|
brushPoints.emplace_back(p);
|
2025-12-14 18:57:40 +03:00
|
|
|
addedTemporaryPoint = true;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
else
|
2026-01-27 16:55:09 +03:00
|
|
|
brushPoints.back().width = std::max(brushPoints.back().width, p.width);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 18:57:40 +03:00
|
|
|
if(addedTemporaryPoint) {
|
2026-01-27 16:55:09 +03:00
|
|
|
const BrushStrokeCanvasComponentPoint& prevP = brushPoints[brushPoints.size() - 2];
|
2025-07-06 20:43:53 +03:00
|
|
|
float distToPrev = (p.pos - prevP.pos).norm();
|
2025-12-14 18:57:40 +03:00
|
|
|
if(extensive_point_checking_back(brushStroke, p.pos)) {
|
2026-01-27 16:55:09 +03:00
|
|
|
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);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
2026-01-29 20:33:22 +03:00
|
|
|
if((!drawingMinimumRelativeToSize && distToPrev >= 10.0) || (drawingMinimumRelativeToSize && distToPrev >= width * BrushStrokeCanvasComponent::DRAW_MINIMUM_LIMIT)) {
|
2026-01-27 16:55:09 +03:00
|
|
|
brushPoints.back() = p;
|
2025-12-14 18:57:40 +03:00
|
|
|
addedTemporaryPoint = false;
|
|
|
|
|
|
|
|
|
|
if(midwayInterpolation) {
|
2026-01-27 16:55:09 +03:00
|
|
|
if(brushPoints.size() != 2) // Don't interpolate the first point
|
|
|
|
|
brushPoints[brushPoints.size() - 2].pos = (prevPointUnaltered + p.pos) * 0.5;
|
2025-12-14 18:57:40 +03:00
|
|
|
prevPointUnaltered = p.pos;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-15 15:36:42 +03:00
|
|
|
|
|
|
|
|
containerPtr->send_comp_update(drawP, false);
|
|
|
|
|
containerPtr->commit_update(drawP);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-30 19:46:08 +03:00
|
|
|
else if(objInfoBeingEdited)
|
2025-07-06 20:43:53 +03:00
|
|
|
commit_stroke();
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
|
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
t.gui.push_id("brush tool");
|
|
|
|
|
t.gui.text_label_centered("Brush");
|
2026-01-26 19:08:36 +03:00
|
|
|
t.gui.checkbox_field("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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 17:33:14 +03:00
|
|
|
bool BrushTool::right_click_popup_gui(Vector2f popupPos) {
|
|
|
|
|
Toolbar& t = drawP.world.main.toolbar;
|
|
|
|
|
t.paint_popup(popupPos);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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-02-27 16:16:51 +03:00
|
|
|
if(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()) {
|
|
|
|
|
SkPaint linePaint;
|
|
|
|
|
linePaint.setColor4f(drawP.world.canvasTheme.get_tool_front_color());
|
|
|
|
|
linePaint.setStroke(true);
|
|
|
|
|
linePaint.setStrokeWidth(0.0f);
|
|
|
|
|
float width = relativeWidthResult.first.value();
|
|
|
|
|
if(objInfoBeingEdited)
|
|
|
|
|
width *= penWidth * 0.5f;
|
|
|
|
|
else
|
|
|
|
|
width *= 0.5f;
|
|
|
|
|
Vector2f pos = drawData.main->input.mouse.pos;
|
|
|
|
|
canvas->drawCircle(pos.x(), pos.y(), width, linePaint);
|
|
|
|
|
}
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|