-
Notifications
You must be signed in to change notification settings - Fork 79
Create cursor_tool object and provider. #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haske6155
wants to merge
4
commits into
Catrobat:develop
Choose a base branch
from
haske6155:PAINTROID-790
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:paintroid/core/providers/state/canvas_state_provider.dart'; | ||
import 'package:paintroid/core/tools/implementation/cursor_tool.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
import 'package:paintroid/core/commands/command_factory/command_factory_provider.dart'; | ||
import 'package:paintroid/core/commands/command_manager/command_manager_provider.dart'; | ||
import 'package:paintroid/core/commands/graphic_factory/graphic_factory_provider.dart'; | ||
import 'package:paintroid/core/enums/tool_types.dart'; | ||
|
||
part 'cursor_tool_provider.g.dart'; | ||
|
||
@riverpod | ||
class CursorToolProvider extends _$CursorToolProvider { | ||
@override | ||
CursorTool build() { | ||
final canvasCenter = ref.read(canvasStateProvider).size.center(Offset.zero); | ||
return CursorTool( | ||
commandManager: ref.watch(commandManagerProvider), | ||
commandFactory: ref.watch(commandFactoryProvider), | ||
graphicFactory: ref.watch(graphicFactoryProvider), | ||
canvasCenter: canvasCenter, | ||
type: ToolType.CURSOR, | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
lib/core/providers/object/tools/cursor_tool_provider.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:paintroid/core/tools/implementation/brush_tool.dart'; | ||
|
||
class CursorTool extends BrushTool { | ||
static const double _tapTolerance = 10.0; | ||
static const double _circleRadius = 40.0; | ||
static const double _crossLength = 90.0; | ||
static const double _crossThickness = 10.0; | ||
static const double _strokeWidth = 10.0; | ||
|
||
final Offset canvasCenter; | ||
Offset lastPoint = Offset.zero; | ||
bool isActive = false; | ||
|
||
bool _isCurrentlyDrawing = false; | ||
Offset? _initialTouchPoint; | ||
Offset? _initialCursorPosition; | ||
bool _hasDragged = false; | ||
|
||
CursorTool({ | ||
required super.commandFactory, | ||
required super.commandManager, | ||
required super.graphicFactory, | ||
required this.canvasCenter, | ||
required super.type, | ||
}) { | ||
lastPoint = canvasCenter; | ||
} | ||
|
||
void toggleActive() => isActive = !isActive; | ||
|
||
void setCursorPosition(Offset position) => lastPoint = position; | ||
|
||
@override | ||
void onDown(Offset point, Paint paint) { | ||
_initializeTouch(point); | ||
|
||
if (isActive) { | ||
super.onDown(lastPoint, paint); | ||
_isCurrentlyDrawing = true; | ||
} | ||
} | ||
|
||
@override | ||
void onDrag(Offset point, Paint paint) { | ||
_updateDragState(point); | ||
_updateCursorPosition(point); | ||
|
||
if (isActive && _isCurrentlyDrawing) { | ||
super.onDrag(lastPoint, paint); | ||
} | ||
} | ||
|
||
@override | ||
void onUp(Offset point, Paint paint) { | ||
_updateCursorPosition(point); | ||
|
||
if (_isTap()) { | ||
_handleTap(); | ||
return; | ||
} | ||
|
||
if (isActive && _isCurrentlyDrawing) { | ||
super.onUp(lastPoint, paint); | ||
} | ||
|
||
_finalizeDraw(); | ||
} | ||
|
||
void drawCursorIcon(Canvas canvas, Paint paint) { | ||
final cursorColor = isActive ? Colors.red : Colors.black; | ||
final circlePaint = _createCirclePaint(cursorColor); | ||
final crossPaint = _createCrossPaint(cursorColor); | ||
|
||
canvas.drawCircle(lastPoint, _circleRadius, circlePaint); | ||
_drawCrossLines(canvas, crossPaint); | ||
} | ||
|
||
void _initializeTouch(Offset point) { | ||
_initialTouchPoint = point; | ||
_initialCursorPosition = lastPoint; | ||
_hasDragged = false; | ||
_isCurrentlyDrawing = false; | ||
} | ||
|
||
void _updateDragState(Offset point) { | ||
if (_initialTouchPoint != null) { | ||
final distance = (point - _initialTouchPoint!).distance; | ||
if (distance > _tapTolerance) _hasDragged = true; | ||
} | ||
} | ||
|
||
void _updateCursorPosition(Offset point) { | ||
if (_initialTouchPoint != null && _initialCursorPosition != null) { | ||
final delta = point - _initialTouchPoint!; | ||
lastPoint = _initialCursorPosition! + delta; | ||
} | ||
} | ||
|
||
bool _isTap() => !_hasDragged; | ||
|
||
void _handleTap() { | ||
toggleActive(); | ||
if (!isActive && _isCurrentlyDrawing) { | ||
_isCurrentlyDrawing = false; | ||
_resetTracking(); | ||
} | ||
} | ||
|
||
void _finalizeDraw() { | ||
_isCurrentlyDrawing = false; | ||
_resetTracking(); | ||
} | ||
|
||
void _resetTracking() { | ||
_initialTouchPoint = null; | ||
_initialCursorPosition = null; | ||
_hasDragged = false; | ||
} | ||
|
||
Paint _createCirclePaint(Color color) => Paint() | ||
..color = color | ||
..style = PaintingStyle.stroke | ||
..strokeWidth = _strokeWidth | ||
..isAntiAlias = true; | ||
|
||
Paint _createCrossPaint(Color color) => Paint() | ||
..color = color | ||
..style = PaintingStyle.stroke | ||
..strokeWidth = _crossThickness | ||
..strokeCap = StrokeCap.round | ||
..isAntiAlias = true; | ||
|
||
void _drawCrossLines(Canvas canvas, Paint paint) { | ||
final crossStart = _circleRadius; | ||
final crossEnd = crossStart + _crossLength; | ||
|
||
final lines = [ | ||
( | ||
Offset(lastPoint.dx, lastPoint.dy - crossEnd), | ||
Offset(lastPoint.dx, lastPoint.dy - crossStart) | ||
), | ||
( | ||
Offset(lastPoint.dx, lastPoint.dy + crossStart), | ||
Offset(lastPoint.dx, lastPoint.dy + crossEnd) | ||
), | ||
( | ||
Offset(lastPoint.dx - crossEnd, lastPoint.dy), | ||
Offset(lastPoint.dx - crossStart, lastPoint.dy) | ||
), | ||
( | ||
Offset(lastPoint.dx + crossStart, lastPoint.dy), | ||
Offset(lastPoint.dx + crossEnd, lastPoint.dy) | ||
), | ||
]; | ||
|
||
for (final (start, end) in lines) { | ||
canvas.drawLine(start, end, paint); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When selecting the cursor tool, the paint stroke width isn’t updated. You may need to call
ref.read(paintProvider.notifier).updateStrokeWidth(...)
with the cursor’s stroke width to ensure drawing uses the correct thickness.Copilot uses AI. Check for mistakes.