-
-
Notifications
You must be signed in to change notification settings - Fork 337
feat: new area eraser #1761
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
Draft
Imagination-King
wants to merge
4
commits into
saber-notes:main
Choose a base branch
from
Imagination-King:eraserSplit
base: main
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.
Draft
feat: new area eraser #1761
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a606187
New eraser functionality added: area eraser that erases parts of line…
Imagination-King 53d93fb
Added axis-dependant spacing for eraser modal.
Imagination-King a1c4191
Merge branch 'main' into eraserSplit
Imagination-King fbdc889
Last merge broke some tests
Imagination-King 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,289 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_svg/flutter_svg.dart'; | ||
| import 'package:saber/data/extensions/axis_extensions.dart'; | ||
| import 'package:saber/data/prefs.dart'; | ||
| import 'package:saber/data/tools/eraser.dart'; | ||
| import 'package:saber/i18n/strings.g.dart'; | ||
|
|
||
| class EraserModal extends StatefulWidget { | ||
| const EraserModal({super.key, required this.getTool}); | ||
|
|
||
| final Eraser Function() getTool; | ||
|
|
||
| @override | ||
| State<EraserModal> createState() => _EraserModalState(); | ||
| } | ||
|
|
||
| class _EraserModalState extends State<EraserModal> { | ||
| @override | ||
| Widget build(BuildContext context) { | ||
| final eraser = widget.getTool(); | ||
| final axis = stows.editorToolbarAlignment.value.axis.opposite; | ||
|
|
||
| return Padding( | ||
| padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6), | ||
| child: Flex( | ||
| direction: axis, | ||
| mainAxisSize: MainAxisSize.min, | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| crossAxisAlignment: CrossAxisAlignment.center, | ||
| children: [ | ||
| _EraserSizePicker( | ||
| eraser: eraser, | ||
| axis: axis, | ||
| onDrag: (percent) { | ||
| setState(() { | ||
| percent = percent.clamp(0.0, 1.0); | ||
| final stepsFromMin = | ||
| (percent * eraser.sizeStepsBetweenMinAndMax).round(); | ||
| final newSize = eraser.sizeMin + stepsFromMin * eraser.sizeStep; | ||
| eraser.size = newSize; | ||
| }); | ||
| }, | ||
| ), | ||
| SizedBox( | ||
| width: axis == Axis.horizontal ? 8 : 0, | ||
| height: axis == Axis.vertical ? 8 : 0, | ||
| ), | ||
| Flex( | ||
| direction: axis, | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: [ | ||
| _EraserModeButton( | ||
| selected: !eraser.areaEraser, | ||
| tooltip: t.editor.eraserOptions.strokeEraser, | ||
| onPressed: () { | ||
| setState(() { | ||
| eraser.areaEraser = false; | ||
| }); | ||
| }, | ||
| icon: SvgPicture.asset( | ||
| 'assets/images/eraser-stroke.svg', | ||
| width: 32, | ||
| height: 32, | ||
| theme: SvgTheme( | ||
| currentColor: !eraser.areaEraser | ||
| ? ColorScheme.of(context).secondary | ||
| : ColorScheme.of(context).onSurface, | ||
| ), | ||
| ), | ||
| ), | ||
| SizedBox( | ||
| width: axis == Axis.horizontal ? 8 : 0, | ||
| height: axis == Axis.vertical ? 8 : 0, | ||
| ), | ||
| _EraserModeButton( | ||
| selected: eraser.areaEraser, | ||
| tooltip: t.editor.eraserOptions.areaEraser, | ||
| onPressed: () { | ||
| setState(() { | ||
| eraser.areaEraser = true; | ||
| }); | ||
| }, | ||
| icon: SvgPicture.asset( | ||
| 'assets/images/eraser-area.svg', | ||
| width: 32, | ||
| height: 32, | ||
| theme: SvgTheme( | ||
| currentColor: eraser.areaEraser | ||
| ? ColorScheme.of(context).secondary | ||
| : ColorScheme.of(context).onSurface, | ||
| ), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _EraserSizePicker extends StatelessWidget { | ||
| const _EraserSizePicker({ | ||
| required this.axis, | ||
| required this.eraser, | ||
| required this.onDrag, | ||
| }); | ||
|
|
||
| final Axis axis; | ||
| final Eraser eraser; | ||
| final ValueChanged<double> onDrag; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final colorScheme = ColorScheme.of(context); | ||
| return Flex( | ||
| direction: axis, | ||
| mainAxisSize: MainAxisSize.min, | ||
| children: [ | ||
| Column( | ||
| children: [ | ||
| Text( | ||
| t.editor.penOptions.size, | ||
| style: TextStyle( | ||
| color: colorScheme.onSurface.withValues(alpha: 0.8), | ||
| fontSize: 10, | ||
| height: 1, | ||
| ), | ||
| ), | ||
| Text(eraser.size.round().toString()), | ||
| ], | ||
| ), | ||
| const SizedBox(width: 8), | ||
| Padding( | ||
| padding: const EdgeInsets.symmetric(vertical: 8), | ||
| child: _EraserSizeSlider(axis: axis, eraser: eraser, onDrag: onDrag), | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _EraserSizeSlider extends StatelessWidget { | ||
| const _EraserSizeSlider({ | ||
| required this.axis, | ||
| required this.eraser, | ||
| required this.onDrag, | ||
| }); | ||
|
|
||
| final Axis axis; | ||
| final Eraser eraser; | ||
| final ValueChanged<double> onDrag; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final colorScheme = ColorScheme.of(context); | ||
| return GestureDetector( | ||
| // Map drag gestures to a normalized 0..1 percent value used by the | ||
| // parent. For horizontal layout we use the local dx; for vertical we | ||
| // use local dy. The fixed divisor (150) approximates the slider pixel | ||
| // range used by the CustomPaint size. | ||
| onHorizontalDragStart: axis == Axis.horizontal | ||
| ? (details) => onDrag(details.localPosition.dx / 150) | ||
| : null, | ||
| onHorizontalDragUpdate: axis == Axis.horizontal | ||
| ? (details) => onDrag(details.localPosition.dx / 150) | ||
| : null, | ||
| onVerticalDragStart: axis == Axis.vertical | ||
| ? (details) => onDrag(details.localPosition.dy / 150) | ||
| : null, | ||
| onVerticalDragUpdate: axis == Axis.vertical | ||
| ? (details) => onDrag(details.localPosition.dy / 150) | ||
| : null, | ||
| child: RotatedBox( | ||
| quarterTurns: axis == Axis.horizontal ? 0 : 1, | ||
| child: CustomPaint( | ||
| size: const Size(150, 25), | ||
| painter: _EraserSizeSliderPainter( | ||
| axis: axis, | ||
| minSize: eraser.sizeMin, | ||
| maxSize: eraser.sizeMax, | ||
| currentSize: eraser.size, | ||
| trackColor: colorScheme.onSurface.withValues(alpha: 0.2), | ||
| thumbColor: colorScheme.primary, | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _EraserModeButton extends StatelessWidget { | ||
| const _EraserModeButton({ | ||
| required this.selected, | ||
| required this.icon, | ||
| required this.tooltip, | ||
| required this.onPressed, | ||
| }); | ||
|
|
||
| final bool selected; | ||
| final Widget icon; | ||
| final String tooltip; | ||
| final VoidCallback onPressed; | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final colorScheme = ColorScheme.of(context); | ||
| final iconColor = selected ? colorScheme.secondary : colorScheme.onSurface; | ||
|
|
||
| return IconButton( | ||
| onPressed: onPressed, | ||
| style: TextButton.styleFrom( | ||
| foregroundColor: iconColor, | ||
| backgroundColor: selected | ||
| ? colorScheme.secondary.withAlpha((0.1 * 255).round()) | ||
| : Colors.transparent, | ||
| shape: const CircleBorder(), | ||
| ), | ||
| tooltip: tooltip, | ||
| icon: icon, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class _EraserSizeSliderPainter extends CustomPainter { | ||
| _EraserSizeSliderPainter({ | ||
| required this.axis, | ||
| required this.minSize, | ||
| required this.maxSize, | ||
| required this.currentSize, | ||
| required this.trackColor, | ||
| required this.thumbColor, | ||
| }); | ||
|
|
||
| final Axis axis; | ||
| final double minSize; | ||
| final double maxSize; | ||
| final double currentSize; | ||
| final Color trackColor; | ||
| final Color thumbColor; | ||
|
|
||
| @override | ||
| void paint(Canvas canvas, Size size) { | ||
| final leftHeight = size.height * minSize / maxSize; | ||
| final topLeft = Offset(0, (size.height - leftHeight) / 2); | ||
| final bottomLeft = Offset(0, topLeft.dy + leftHeight); | ||
| final topRight = Offset(size.width, 0); | ||
| final bottomRight = Offset(size.width, size.height); | ||
|
|
||
| canvas.drawPath( | ||
| Path() | ||
| ..moveTo(topLeft.dx, topLeft.dy) | ||
| ..lineTo(bottomLeft.dx, bottomLeft.dy) | ||
| ..lineTo(bottomRight.dx, bottomRight.dy) | ||
| ..lineTo(topRight.dx, topRight.dy) | ||
| ..close(), | ||
| Paint() | ||
| ..color = trackColor | ||
| ..style = PaintingStyle.fill, | ||
| ); | ||
|
|
||
| final ratio = (currentSize - minSize) / (maxSize - minSize); | ||
| final thumbHeight = size.height * ratio; | ||
| final thumbRight = size.width * ratio; | ||
| final thumbTop = (size.height - thumbHeight) / 2; | ||
| canvas.drawPath( | ||
| Path() | ||
| ..moveTo(topLeft.dx, topLeft.dy) | ||
| ..lineTo(bottomLeft.dx, bottomLeft.dy) | ||
| ..lineTo(thumbRight, thumbTop + thumbHeight) | ||
| ..lineTo(thumbRight, thumbTop) | ||
| ..close(), | ||
| Paint() | ||
| ..color = thumbColor | ||
| ..style = PaintingStyle.fill, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| bool shouldRepaint(covariant CustomPainter oldDelegate) { | ||
| return oldDelegate is! _EraserSizeSliderPainter || | ||
| oldDelegate.axis != axis || | ||
| oldDelegate.minSize != minSize || | ||
| oldDelegate.maxSize != maxSize || | ||
| oldDelegate.currentSize != currentSize || | ||
| oldDelegate.trackColor != trackColor || | ||
| oldDelegate.thumbColor != thumbColor; | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.