Brush tool works with events
This commit is contained in:
parent
4814780bec
commit
e39c94ff19
8 changed files with 155 additions and 130 deletions
|
|
@ -31,6 +31,102 @@ void BrushTool::erase_component(CanvasComponentContainer::ObjInfo* erasedComp) {
|
|||
objInfoBeingEdited = nullptr;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool BrushTool::extensive_point_checking(const BrushStrokeCanvasComponent& brushStroke, const Vector2f& newPoint) {
|
||||
auto& points = *brushStroke.d.points;
|
||||
if(points.size() >= 1 && (newPoint - points[points.size() - 1].pos).norm() < MINIMUM_DISTANCE_TO_NEXT_POINT)
|
||||
|
|
@ -54,111 +150,8 @@ bool BrushTool::extensive_point_checking_back(const BrushStrokeCanvasComponent&
|
|||
}
|
||||
|
||||
void BrushTool::tool_update() {
|
||||
auto& toolConfig = drawP.world.main.toolConfig;
|
||||
|
||||
if(drawP.controls.cursorHoveringOverCanvas)
|
||||
drawP.world.main.input.hideCursor = true;
|
||||
|
||||
if(!objInfoBeingEdited)
|
||||
penSmoothingData.clear();
|
||||
|
||||
if(drawP.world.main.input.pen.isDown && drawP.controls.leftClickHeld && drawP.world.main.toolbar.tabletOptions.pressureAffectsBrushWidth) {
|
||||
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(
|
||||
drawP.world.main.input.pen.pressure,
|
||||
std::chrono::steady_clock::now()
|
||||
);
|
||||
float averagePressure = 0.0f;
|
||||
for(auto& [width, t] : penSmoothingData)
|
||||
averagePressure += width;
|
||||
averagePressure /= penSmoothingData.size();
|
||||
penWidth = averagePressure;
|
||||
if(penWidth != 0.0f) {
|
||||
float brushMinSize = drawP.world.main.toolbar.tabletOptions.brushMinimumSize;
|
||||
penWidth = brushMinSize + penWidth * (1.0f - brushMinSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
penWidth = 1.0f;
|
||||
|
||||
bool isPenDown = false;
|
||||
if(drawP.world.main.input.pen.isDown && drawP.controls.leftClickHeld)
|
||||
isPenDown = penWidth != 0.0;
|
||||
else if(drawP.controls.leftClickHeld)
|
||||
isPenDown = true;
|
||||
|
||||
if(isPenDown) {
|
||||
if(!objInfoBeingEdited) {
|
||||
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;
|
||||
|
||||
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;
|
||||
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 {
|
||||
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.get_mouse_pos(drawP.world);
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if(objInfoBeingEdited)
|
||||
commit_stroke();
|
||||
}
|
||||
|
||||
void BrushTool::commit_stroke() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue