Skip to content

Commit

Permalink
Merge pull request #580 from MrGumpIT/develop
Browse files Browse the repository at this point in the history
change logic of eraser
  • Loading branch information
CodeDoctorDE authored Feb 2, 2024
2 parents a23ef76 + bc1bb28 commit 9c72e6b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions app/lib/handlers/eraser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ class EraserHandler extends Handler<EraserTool> {
bool _currentlyErasing = false;
Offset? _currentPos, _lastErased;
EraserHandler(super.data);

// Called when the user presses the pointer (e.g., a finger or the mouse) on the screen. It starts the erasing action.
@override
Future<void> onPointerDown(
PointerDownEvent event, EventContext context) async {
_changeElement(event.localPosition, context);
}

// Creates the cursors for the eraser. It shows an eraser cursor when the user is erasing.
@override
List<Renderer> createForegrounds(CurrentIndexCubit currentIndexCubit,
NoteData document, DocumentPage page, DocumentInfo info,
Expand All @@ -20,7 +21,7 @@ class EraserHandler extends Handler<EraserTool> {
EraserCursor(
ToolCursorData(EraserInfo.fromEraser(data), _currentPos!))
];

// Called when the user moves or hovers the pointer on the screen. They update the current position of the cursor and call the _changeElement method.
@override
void onPointerMove(PointerMoveEvent event, EventContext context) {
_changeElement(event.localPosition, context);
Expand All @@ -34,6 +35,7 @@ class EraserHandler extends Handler<EraserTool> {
context.refresh();
}

//method that handles the erasing logic. It determines whether an element should be erased based on its distance from the cursor and the stroke width of the eraser.
Future<void> _changeElement(Offset position, EventContext context) async {
final globalPos = context.getCameraTransform().localToGlobal(position);
final size = data.strokeWidth;
Expand All @@ -44,23 +46,28 @@ class EraserHandler extends Handler<EraserTool> {
if (!_currentlyErasing && shouldErase) {
_currentlyErasing = true;
_lastErased = globalPos;
// Raycast
final ray = await rayCast(globalPos, context.getDocumentBloc(),
context.getCameraTransform(), size);
final elements = ray.map((e) => e.element).whereType<PenElement>();
final modified = <int, List<PadElement>>{};
for (final element in elements) {
List<List<PathPoint>> paths = [[]];
bool broken = false;
for (final point in element.points) {
if ((point.toOffset() - globalPos).distance > size) {
if ((point.toOffset() - globalPos).distance >= (size * size)) {
paths.last.add(point);
continue;
} else if (paths.last.isNotEmpty) {
paths.add([]);
} else {
if (paths.last.isNotEmpty) {
paths.add([]);
broken = true;
}
}
}
if (paths.length <= 1) continue;
final index = page.content.indexOf(element);
if (broken) {
modified[index] = [];
}
modified[index] = paths
.where((element) => element.isNotEmpty)
.map((e) => element.copyWith(points: e))
Expand All @@ -74,11 +81,13 @@ class EraserHandler extends Handler<EraserTool> {
}
}

// Called when the user releases the pointer. It completes the erasing action.
@override
void onPointerUp(PointerUpEvent event, EventContext context) {
_changeElement(event.localPosition, context);
}

// Returns the mouse cursor to be used when the user interacts with the eraser tool.
@override
MouseCursor get cursor => SystemMouseCursors.precise;
}

0 comments on commit 9c72e6b

Please sign in to comment.