From 348886b5bbde5556cd871c0b4913b1e9f152d72b Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Thu, 3 Nov 2022 11:22:38 +0100 Subject: [PATCH] fix direction dependent thichness --- lib/canvas/paint_controller.dart | 34 +++++++++++++++++---------- lib/context/file_change_notifier.dart | 2 +- lib/savesystem/note_file.dart | 11 ++++++--- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/canvas/paint_controller.dart b/lib/canvas/paint_controller.dart index f17472f..3fb4964 100644 --- a/lib/canvas/paint_controller.dart +++ b/lib/canvas/paint_controller.dart @@ -31,20 +31,26 @@ class PaintController extends ChangeNotifier { } double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) { - double dx = pt2.point.dx - pt1.point.dx; - double dy = pt2.point.dy - pt1.point.dy; + final delta = pt2.point - pt1.point; + final normalizedDelta = + delta / sqrt(delta.dx * delta.dx + delta.dy * delta.dy); - // todo those deltas to small to get an accurate direction! - - double alpha = atan(dx / dy); - // alpha has range from 0 - 2pi - // we want 0.5 -1; - - alpha /= (2 * pi * 2); + double alpha = asin(normalizedDelta.dy); + // range [-pi,pi] + alpha += (3 * pi / 4); + // range [0,inf] + alpha = alpha % (2 * pi); + // range [0,2pi] + alpha -= pi; + // range [-pi,pi] + alpha = alpha.abs(); + // range [0,pi] + alpha /= pi; + // range [0,1] alpha += .5; + // range [.5,1.5] - basetickness * alpha; - return basetickness; + return basetickness * alpha; } void pointDownEvent(Offset offset, PointerDownEvent e) async { @@ -121,9 +127,11 @@ class PaintController extends ChangeNotifier { if (pts.last.point == offset) return; double newWidth = _calcTiltedWidth(5.0, event.tilt); - if (strokes.last.points.length > 1) { + if (pts.length > 1) { newWidth = _calcAngleDependentWidth( - pts.last, pts[pts.length - 2], newWidth); + pts.last, + pts[pts.length - (pts.length > 5 ? 5 : pts.length - 1)], + newWidth); } Point p = Point(offset, newWidth); diff --git a/lib/context/file_change_notifier.dart b/lib/context/file_change_notifier.dart index 9ac292c..84c5aba 100644 --- a/lib/context/file_change_notifier.dart +++ b/lib/context/file_change_notifier.dart @@ -26,7 +26,7 @@ class FileChangeNotifier extends ChangeNotifier { return NoteTileData(name, filename, lastmodified); }).toList(); dta.sort( - (a, b) => a.lastModified.isAfter(b.lastModified) ? -1 : 1, + (a, b) => a.lastModified.compareTo(b.lastModified), ); notifyListeners(); tiledata = dta; diff --git a/lib/savesystem/note_file.dart b/lib/savesystem/note_file.dart index bd041f4..f87501a 100644 --- a/lib/savesystem/note_file.dart +++ b/lib/savesystem/note_file.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:flutter/widgets.dart'; import 'package:sqflite/sqflite.dart'; import 'path.dart'; @@ -19,7 +20,7 @@ class NoteFile { _db = await openDatabase( path, onCreate: (db, version) async { - Batch batch = db.batch(); + final batch = db.batch(); batch.execute('DROP TABLE IF EXISTS strokes;'); batch.execute('DROP TABLE IF EXISTS points;'); @@ -43,7 +44,11 @@ class NoteFile { Future close() async { // shrink the db file size - await _db.execute('VACUUM'); - await _db.close(); + if (_db.isOpen) { + await _db.execute('VACUUM'); + await _db.close(); + } else { + debugPrint('db file unexpectedly closed before shrinking'); + } } }