Rewrite collision detection
This commit is contained in:
parent
7b472fe18d
commit
60ab8f3c73
35 changed files with 292 additions and 258 deletions
|
|
@ -22,6 +22,7 @@
|
|||
#include "../../DrawData.hpp"
|
||||
#include <include/core/SkBlendMode.h>
|
||||
#include <include/core/SkPathBuilder.h>
|
||||
#include "../../CanvasComponents/BrushComponentCode.hpp"
|
||||
|
||||
#include "../../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"
|
||||
#include "../../GUIStuff/ElementHelpers/RadioButtonHelpers.hpp"
|
||||
|
|
@ -74,7 +75,7 @@ void EraserTool::input_mouse_button_on_canvas_callback(const InputManager::Mouse
|
|||
if(button.button == InputManager::MouseButton::LEFT) {
|
||||
if(button.down && !isErasing && !drawP.world.main.g.gui.cursor_obstructed()) {
|
||||
isErasing = true;
|
||||
erase_between_points(button.pos, button.pos);
|
||||
start = end = button.pos;
|
||||
}
|
||||
else if(!button.down && isErasing)
|
||||
switch_tool(get_type());
|
||||
|
|
@ -82,29 +83,17 @@ void EraserTool::input_mouse_button_on_canvas_callback(const InputManager::Mouse
|
|||
}
|
||||
|
||||
void EraserTool::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
|
||||
if(isErasing)
|
||||
erase_between_points(motion.pos, motion.pos - motion.move);
|
||||
end = motion.pos;
|
||||
}
|
||||
|
||||
void EraserTool::erase_between_points(const Vector2f& start, const Vector2f& end) {
|
||||
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);
|
||||
isErasing = false;
|
||||
return;
|
||||
}
|
||||
float width = relativeWidthResult.first.value() * 0.5f;
|
||||
|
||||
SCollision::ColliderCollection<float> cC;
|
||||
SCollision::generate_wide_line(cC, start, end, width * 2.0f, true);
|
||||
auto cCWorld = drawP.world.drawData.cam.c.collider_to_world<SCollision::ColliderCollection<WorldScalar>, SCollision::ColliderCollection<float>>(cC);
|
||||
|
||||
drawP.drawCache.traverse_bvh_run_function(cCWorld.bounds, [&](const auto& bvhNode) {
|
||||
void EraserTool::erase_between_points() {
|
||||
auto cCWorldBounds = drawP.world.drawData.cam.c.collider_to_world<SCollision::AABB<WorldScalar>, SCollision::AABB<float>>(erasePath.getBounds());
|
||||
drawP.drawCache.traverse_bvh_run_function(cCWorldBounds, [&](const auto& bvhNode) {
|
||||
if(bvhNode &&
|
||||
SCollision::collide(cC, drawP.world.drawData.cam.c.to_space(bvhNode->bounds.min)) &&
|
||||
SCollision::collide(cC, drawP.world.drawData.cam.c.to_space(bvhNode->bounds.max)) &&
|
||||
SCollision::collide(cC, drawP.world.drawData.cam.c.to_space(bvhNode->bounds.top_right())) &&
|
||||
SCollision::collide(cC, drawP.world.drawData.cam.c.to_space(bvhNode->bounds.bottom_left()))) {
|
||||
erasePath.contains(convert_vec2<SkPoint>(drawP.world.drawData.cam.c.to_space(bvhNode->bounds.min))) &&
|
||||
erasePath.contains(convert_vec2<SkPoint>(drawP.world.drawData.cam.c.to_space(bvhNode->bounds.max))) &&
|
||||
erasePath.contains(convert_vec2<SkPoint>(drawP.world.drawData.cam.c.to_space(bvhNode->bounds.top_right()))) &&
|
||||
erasePath.contains(convert_vec2<SkPoint>(drawP.world.drawData.cam.c.to_space(bvhNode->bounds.bottom_left())))) {
|
||||
drawP.drawCache.invalidate_cache_at_aabb(bvhNode->bounds);
|
||||
drawP.drawCache.traverse_bvh_run_function_starting_at_node_no_collision_check(bvhNode, [&](const auto& bvhNodeChild) {
|
||||
drawP.drawCache.node_loop_erase_if_components(bvhNodeChild, [&](auto c) {
|
||||
|
|
@ -119,7 +108,7 @@ void EraserTool::erase_between_points(const Vector2f& start, const Vector2f& end
|
|||
return false;
|
||||
}
|
||||
drawP.drawCache.node_loop_erase_if_components(bvhNode, [&](auto c) {
|
||||
if(drawP.layerMan.component_passes_layer_selector(c, drawP.controls.layerSelector) && c->obj->collides_with(drawP.world.drawData.cam.c, cCWorld, cC)) {
|
||||
if(drawP.layerMan.component_passes_layer_selector(c, drawP.controls.layerSelector) && c->obj->collides_with(drawP.world.drawData.cam.c, erasePath)) {
|
||||
erasedComponents.emplace(c);
|
||||
drawP.drawCache.invalidate_cache_at_optional_aabb(c->obj->get_world_bounds());
|
||||
return true;
|
||||
|
|
@ -142,6 +131,31 @@ void EraserTool::switch_tool(DrawingProgramToolType newTool) {
|
|||
void EraserTool::tool_update() {
|
||||
if(!drawP.world.main.g.gui.cursor_obstructed())
|
||||
drawP.world.main.input.hideCursor = true;
|
||||
|
||||
auto relativeWidthResult = drawP.world.main.toolConfig.get_relative_width_stroke_size(drawP, drawP.world.drawData.cam.c.inverseScale);
|
||||
if(!relativeWidthResult.first.has_value()) {
|
||||
isErasing = false;
|
||||
erasePath = eraseBorderPath = SkPath();
|
||||
return;
|
||||
}
|
||||
|
||||
float width = relativeWidthResult.first.value() * 0.5f;
|
||||
using namespace BrushComponentCode;
|
||||
std::vector<BrushPoint> brushPoints;
|
||||
brushPoints.emplace_back(start, width);
|
||||
if(start != end)
|
||||
brushPoints.emplace_back(end, width);
|
||||
erasePath = brush_stroke_to_skpath(brushPoints, true);
|
||||
brushPoints.clear();
|
||||
brushPoints.emplace_back(start, width + 1.0f);
|
||||
if(start != end)
|
||||
brushPoints.emplace_back(end, width + 1.0f);
|
||||
eraseBorderPath = brush_stroke_to_skpath(brushPoints, true);
|
||||
|
||||
if(isErasing)
|
||||
erase_between_points();
|
||||
|
||||
start = end;
|
||||
}
|
||||
|
||||
bool EraserTool::prevent_undo_or_redo() {
|
||||
|
|
@ -149,34 +163,14 @@ bool EraserTool::prevent_undo_or_redo() {
|
|||
}
|
||||
|
||||
void EraserTool::draw(SkCanvas* canvas, const DrawData& drawData) {
|
||||
if(!drawP.world.main.input.isTouchDevice && !drawData.main->g.gui.cursor_obstructed()) {
|
||||
if(!lastPosOpt.has_value())
|
||||
lastPosOpt = drawData.main->input.mouse.pos;
|
||||
auto& lastPos = lastPosOpt.value();
|
||||
if(!drawP.world.main.input.isTouchDevice && !drawData.main->g.gui.cursor_obstructed() && !erasePath.isEmpty() && !eraseBorderPath.isEmpty()) {
|
||||
SkPaint linePaint;
|
||||
linePaint.setAntiAlias(drawData.skiaAA);
|
||||
linePaint.setColor4f({0.0f, 0.0f, 0.0f, 0.4f});
|
||||
linePaint.setStyle(SkPaint::kFill_Style);
|
||||
canvas->drawPath(erasePath, linePaint);
|
||||
|
||||
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() * 0.5f;
|
||||
SkPaint linePaint;
|
||||
linePaint.setAntiAlias(drawData.skiaAA);
|
||||
linePaint.setColor4f({0.0f, 0.0f, 0.0f, 0.4f});
|
||||
linePaint.setStyle(SkPaint::kStroke_Style);
|
||||
linePaint.setStrokeCap(SkPaint::kRound_Cap);
|
||||
linePaint.setStrokeWidth(width * 2.0f + 1.0f);
|
||||
SkPathBuilder erasePath;
|
||||
erasePath.moveTo(convert_vec2<SkPoint>(lastPos));
|
||||
erasePath.lineTo(convert_vec2<SkPoint>(drawData.main->input.mouse.pos));
|
||||
|
||||
SkPath erasePathDetach = erasePath.detach();
|
||||
canvas->drawPath(erasePathDetach, linePaint);
|
||||
|
||||
linePaint.setStrokeWidth(width * 2.0f);
|
||||
linePaint.setColor4f({1.0f, 1.0f, 1.0f, 0.4f});
|
||||
canvas->drawPath(erasePathDetach, linePaint);
|
||||
}
|
||||
|
||||
lastPos = drawData.main->input.mouse.pos;
|
||||
linePaint.setColor4f({1.0f, 1.0f, 1.0f, 0.4f});
|
||||
canvas->drawPath(eraseBorderPath, linePaint);
|
||||
}
|
||||
else
|
||||
lastPosOpt = std::nullopt;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue