save correct color to db

use different pointer for highlighter
This commit is contained in:
lukas-heiligenbrunner 2022-11-03 15:12:40 +01:00
parent 9be2c69ff5
commit 4d52868854
4 changed files with 51 additions and 23 deletions

View File

@ -40,7 +40,8 @@ class MyPainter extends CustomPainter {
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
var paint = Paint() var paint = Paint()
..color = Colors.blue ..color = Colors.blue
..strokeCap = StrokeCap.square; ..strokeCap = StrokeCap.square
..isAntiAlias = true;
// clipping canvas to reqired size // clipping canvas to reqired size
canvas.clipRect(Offset.zero & size); canvas.clipRect(Offset.zero & size);
@ -59,8 +60,24 @@ class MyPainter extends CustomPainter {
Offset pt2 = stroke.points[i + 1].point; Offset pt2 = stroke.points[i + 1].point;
pt2 = _translatept(pt2, size); pt2 = _translatept(pt2, size);
canvas.drawLine( final zoomedthickness = stroke.points[i].thickness * zoom;
pt1, pt2, paint..strokeWidth = stroke.points[i].thickness * zoom);
// only temporary solution to differ from highlighter and pen
if (stroke.color.opacity != 1.0) {
final off = Offset(zoomedthickness / 2, zoomedthickness / 2);
canvas.drawPath(
Path()
..moveTo((pt1 - off).dx, (pt1 - off).dy)
..lineTo((pt1 + off).dx, (pt1 + off).dy)
..lineTo((pt2 + off).dx, (pt2 + off).dy)
..lineTo((pt2 - off).dx, (pt2 - off).dy)
..lineTo((pt1 - off).dx, (pt1 - off).dy),
paint
..style = PaintingStyle.fill
..strokeWidth = 0);
} else {
canvas.drawLine(pt1, pt2, paint..strokeWidth = zoomedthickness);
}
} }
} }
} }

View File

@ -62,19 +62,20 @@ class PaintController extends ChangeNotifier {
if (activePen == Pen.eraser || activePen == Pen.selector) return; if (activePen == Pen.eraser || activePen == Pen.selector) return;
int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0; int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0;
strokes.add(Stroke.fromPoints( final color = activePen == Pen.pen
[Point(offset, _calcTiltedWidth(3.0, e.tilt))],
strokeid,
activePen == Pen.pen
? const Color(0xFF444444) ? const Color(0xFF444444)
: Colors.yellow.withOpacity(.5))); : Colors.yellow.withOpacity(.3);
file.addStroke(strokeid); strokes.add(Stroke.fromPoints(
[Point(offset, _calcTiltedWidth(3.0, e.tilt))], strokeid, color));
file.addStroke(strokeid, color);
notifyListeners(); notifyListeners();
} }
} }
void pointUpEvent(PointerUpEvent e) { void pointUpEvent(PointerUpEvent e) {
if (activePen == Pen.eraser) return; if (activePen == Pen.eraser) return;
// pointerupevent doesn't deliver correct event button
if (_allowedToDraw(e.kind, 1)) { if (_allowedToDraw(e.kind, 1)) {
final lastStroke = strokes.last; final lastStroke = strokes.last;
if (lastStroke.points.length <= 1) { if (lastStroke.points.length <= 1) {
@ -122,20 +123,24 @@ class PaintController extends ChangeNotifier {
} }
break; break;
case Pen.pen: case Pen.pen:
case Pen.highlighter:
final pts = strokes.last.points; final pts = strokes.last.points;
// avoid duplicates
if (pts.last.point == offset) return; if (pts.last.point == offset) return;
double newWidth = _calcTiltedWidth(5.0, event.tilt); double newWidth = _calcTiltedWidth(4.0, event.tilt);
if (pts.length > 1) { if (pts.length > 1) {
newWidth = _calcAngleDependentWidth(pts.last, newWidth = _calcAngleDependentWidth(pts.last,
pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth); pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth);
} }
Point p = Point(offset, newWidth); strokes.last.addPoint(Point(offset, newWidth));
strokes.last.addPoint(p); break;
// // todo do a batch commit per stroke case Pen.highlighter:
// file.addPoint(strokes.last.id, p); final pts = strokes.last.points;
// avoid duplicates
if (pts.last.point == offset) return;
strokes.last.addPoint(Point(offset, 15.0));
break; break;
case Pen.selector: case Pen.selector:
// TODO: Handle this case. // TODO: Handle this case.

View File

@ -47,9 +47,9 @@ void exportPDF(List<Stroke> strokes, String name) async {
const PdfPageFormat a4 = PdfPageFormat(_a4width, _a4height); const PdfPageFormat a4 = PdfPageFormat(_a4width, _a4height);
pdf.addPage(pw.MultiPage( pdf.addPage(pw.Page(
pageFormat: a4, pageFormat: a4,
build: (context) => [_StrokePDFPaint(strokes)], build: (context) => _StrokePDFPaint(strokes),
)); ));
final path = await getSavePath(); final path = await getSavePath();

View File

@ -5,9 +5,13 @@ import 'note_file.dart';
extension LineLoading on NoteFile { extension LineLoading on NoteFile {
Future<List<Stroke>> loadStrokes() async { Future<List<Stroke>> loadStrokes() async {
final query = await db().query('points', const sql = '''
orderBy: 'strokeid', SELECT x, y, thickness, strokeid, points.id, s.color
columns: ['x', 'y', 'thickness', 'strokeid', 'id']); from points
JOIN strokes s on s.id = strokeid
ORDER BY strokeid
''';
final query = await db().rawQuery(sql);
int strokeid = -1; int strokeid = -1;
List<Stroke> strokes = []; List<Stroke> strokes = [];
@ -15,7 +19,7 @@ extension LineLoading on NoteFile {
final int csid = i['strokeid'] as int; final int csid = i['strokeid'] as int;
if (csid != strokeid) { if (csid != strokeid) {
strokeid = csid; strokeid = csid;
strokes.add(Stroke(strokeid, Colors.green)); strokes.add(Stroke(strokeid, Color(i['color'] as int)));
} }
final Point p = Point( final Point p = Point(
Offset(i['x'] as double, i['y'] as double), i['thickness'] as double); Offset(i['x'] as double, i['y'] as double), i['thickness'] as double);
@ -26,11 +30,12 @@ extension LineLoading on NoteFile {
} }
// create new stroke in file and return strokeid // create new stroke in file and return strokeid
Future<void> addStroke(int newStrokeId) async { Future<void> addStroke(int newStrokeId, Color color) async {
await db().insert( await db().insert(
'strokes', {'color': 0xffffff, 'elevation': 0, 'id': newStrokeId}); 'strokes', {'color': color.value, 'elevation': 0, 'id': newStrokeId});
} }
// add one point to a stroke in db
Future<void> addPoint(int strokeid, Point p) async { Future<void> addPoint(int strokeid, Point p) async {
await db().insert('points', { await db().insert('points', {
'x': p.point.dx, 'x': p.point.dx,
@ -40,6 +45,7 @@ extension LineLoading on NoteFile {
}); });
} }
// add list of points to a stroke in db
Future<void> addPoints(int strokeid, List<Point> pts) async { Future<void> addPoints(int strokeid, List<Point> pts) async {
final batch = db().batch(); final batch = db().batch();
for (final p in pts) { for (final p in pts) {