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) {
var paint = Paint()
..color = Colors.blue
..strokeCap = StrokeCap.square;
..strokeCap = StrokeCap.square
..isAntiAlias = true;
// clipping canvas to reqired size
canvas.clipRect(Offset.zero & size);
@ -59,8 +60,24 @@ class MyPainter extends CustomPainter {
Offset pt2 = stroke.points[i + 1].point;
pt2 = _translatept(pt2, size);
canvas.drawLine(
pt1, pt2, paint..strokeWidth = stroke.points[i].thickness * zoom);
final zoomedthickness = 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;
int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0;
final color = activePen == Pen.pen
? const Color(0xFF444444)
: Colors.yellow.withOpacity(.3);
strokes.add(Stroke.fromPoints(
[Point(offset, _calcTiltedWidth(3.0, e.tilt))],
strokeid,
activePen == Pen.pen
? const Color(0xFF444444)
: Colors.yellow.withOpacity(.5)));
file.addStroke(strokeid);
[Point(offset, _calcTiltedWidth(3.0, e.tilt))], strokeid, color));
file.addStroke(strokeid, color);
notifyListeners();
}
}
void pointUpEvent(PointerUpEvent e) {
if (activePen == Pen.eraser) return;
// pointerupevent doesn't deliver correct event button
if (_allowedToDraw(e.kind, 1)) {
final lastStroke = strokes.last;
if (lastStroke.points.length <= 1) {
@ -122,20 +123,24 @@ class PaintController extends ChangeNotifier {
}
break;
case Pen.pen:
case Pen.highlighter:
final pts = strokes.last.points;
// avoid duplicates
if (pts.last.point == offset) return;
double newWidth = _calcTiltedWidth(5.0, event.tilt);
double newWidth = _calcTiltedWidth(4.0, event.tilt);
if (pts.length > 1) {
newWidth = _calcAngleDependentWidth(pts.last,
pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth);
}
Point p = Point(offset, newWidth);
strokes.last.addPoint(p);
// // todo do a batch commit per stroke
// file.addPoint(strokes.last.id, p);
strokes.last.addPoint(Point(offset, newWidth));
break;
case Pen.highlighter:
final pts = strokes.last.points;
// avoid duplicates
if (pts.last.point == offset) return;
strokes.last.addPoint(Point(offset, 15.0));
break;
case Pen.selector:
// TODO: Handle this case.

View File

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

View File

@ -5,9 +5,13 @@ import 'note_file.dart';
extension LineLoading on NoteFile {
Future<List<Stroke>> loadStrokes() async {
final query = await db().query('points',
orderBy: 'strokeid',
columns: ['x', 'y', 'thickness', 'strokeid', 'id']);
const sql = '''
SELECT x, y, thickness, strokeid, points.id, s.color
from points
JOIN strokes s on s.id = strokeid
ORDER BY strokeid
''';
final query = await db().rawQuery(sql);
int strokeid = -1;
List<Stroke> strokes = [];
@ -15,7 +19,7 @@ extension LineLoading on NoteFile {
final int csid = i['strokeid'] as int;
if (csid != strokeid) {
strokeid = csid;
strokes.add(Stroke(strokeid, Colors.green));
strokes.add(Stroke(strokeid, Color(i['color'] as int)));
}
final Point p = Point(
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
Future<void> addStroke(int newStrokeId) async {
Future<void> addStroke(int newStrokeId, Color color) async {
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 {
await db().insert('points', {
'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 {
final batch = db().batch();
for (final p in pts) {