fix direction dependent thichness

This commit is contained in:
lukas-heiligenbrunner 2022-11-03 11:22:38 +01:00
parent 019cd2dad0
commit 348886b5bb
3 changed files with 30 additions and 17 deletions

View File

@ -31,20 +31,26 @@ class PaintController extends ChangeNotifier {
} }
double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) { double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) {
double dx = pt2.point.dx - pt1.point.dx; final delta = pt2.point - pt1.point;
double dy = pt2.point.dy - pt1.point.dy; final normalizedDelta =
delta / sqrt(delta.dx * delta.dx + delta.dy * delta.dy);
// todo those deltas to small to get an accurate direction! double alpha = asin(normalizedDelta.dy);
// range [-pi,pi]
double alpha = atan(dx / dy); alpha += (3 * pi / 4);
// alpha has range from 0 - 2pi // range [0,inf]
// we want 0.5 -1; alpha = alpha % (2 * pi);
// range [0,2pi]
alpha /= (2 * pi * 2); alpha -= pi;
// range [-pi,pi]
alpha = alpha.abs();
// range [0,pi]
alpha /= pi;
// range [0,1]
alpha += .5; alpha += .5;
// range [.5,1.5]
basetickness * alpha; return basetickness * alpha;
return basetickness;
} }
void pointDownEvent(Offset offset, PointerDownEvent e) async { void pointDownEvent(Offset offset, PointerDownEvent e) async {
@ -121,9 +127,11 @@ class PaintController extends ChangeNotifier {
if (pts.last.point == offset) return; if (pts.last.point == offset) return;
double newWidth = _calcTiltedWidth(5.0, event.tilt); double newWidth = _calcTiltedWidth(5.0, event.tilt);
if (strokes.last.points.length > 1) { if (pts.length > 1) {
newWidth = _calcAngleDependentWidth( 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); Point p = Point(offset, newWidth);

View File

@ -26,7 +26,7 @@ class FileChangeNotifier extends ChangeNotifier {
return NoteTileData(name, filename, lastmodified); return NoteTileData(name, filename, lastmodified);
}).toList(); }).toList();
dta.sort( dta.sort(
(a, b) => a.lastModified.isAfter(b.lastModified) ? -1 : 1, (a, b) => a.lastModified.compareTo(b.lastModified),
); );
notifyListeners(); notifyListeners();
tiledata = dta; tiledata = dta;

View File

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
import 'path.dart'; import 'path.dart';
@ -19,7 +20,7 @@ class NoteFile {
_db = await openDatabase( _db = await openDatabase(
path, path,
onCreate: (db, version) async { 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 strokes;');
batch.execute('DROP TABLE IF EXISTS points;'); batch.execute('DROP TABLE IF EXISTS points;');
@ -43,7 +44,11 @@ class NoteFile {
Future<void> close() async { Future<void> close() async {
// shrink the db file size // shrink the db file size
if (_db.isOpen) {
await _db.execute('VACUUM'); await _db.execute('VACUUM');
await _db.close(); await _db.close();
} else {
debugPrint('db file unexpectedly closed before shrinking');
}
} }
} }