2022-10-29 15:39:25 +00:00
|
|
|
import 'dart:math';
|
|
|
|
import 'dart:ui';
|
2022-10-30 20:57:13 +00:00
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
import 'package:flutter/gestures.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2022-10-29 15:39:25 +00:00
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
import '../savesystem/line_loader.dart';
|
2022-10-29 18:06:20 +00:00
|
|
|
import '../savesystem/note_file.dart';
|
2022-10-29 15:39:25 +00:00
|
|
|
import 'document_types.dart';
|
|
|
|
import 'my_painter.dart';
|
|
|
|
|
|
|
|
enum Pen { eraser, pen, highlighter, selector }
|
|
|
|
|
|
|
|
class PaintController extends ChangeNotifier {
|
|
|
|
Pen activePen = Pen.pen;
|
|
|
|
List<Stroke> strokes = [];
|
|
|
|
final bool _allowDrawWithFinger = false;
|
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
PaintController(this.file);
|
|
|
|
|
|
|
|
final NoteFile file;
|
|
|
|
|
2022-10-29 15:39:25 +00:00
|
|
|
void changePen(Pen pen) {
|
|
|
|
activePen = pen;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
double _calcTiltedWidth(double baseWidth, double tilt) {
|
|
|
|
if (tilt == .0) return baseWidth;
|
|
|
|
return baseWidth * tilt;
|
|
|
|
}
|
|
|
|
|
|
|
|
double _calcAngleDependentWidth(Point pt1, Point pt2, double basetickness) {
|
2022-11-03 10:22:38 +00:00
|
|
|
final delta = pt2.point - pt1.point;
|
|
|
|
final normalizedDelta =
|
|
|
|
delta / sqrt(delta.dx * delta.dx + delta.dy * delta.dy);
|
|
|
|
|
|
|
|
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]
|
2022-10-29 15:39:25 +00:00
|
|
|
alpha += .5;
|
2022-11-03 10:22:38 +00:00
|
|
|
// range [.5,1.5]
|
2022-10-29 15:39:25 +00:00
|
|
|
|
2022-11-03 10:22:38 +00:00
|
|
|
return basetickness * alpha;
|
2022-10-29 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
void pointDownEvent(Offset offset, PointerDownEvent e) async {
|
2022-11-03 11:13:36 +00:00
|
|
|
if (_allowedToDraw(e.kind, e.buttons)) {
|
2022-10-29 15:39:25 +00:00
|
|
|
// todo line drawn on edge where line left page
|
|
|
|
if (!a4Page.contains(offset)) return;
|
|
|
|
|
|
|
|
// todo handle other pens
|
2022-10-29 19:39:08 +00:00
|
|
|
if (activePen == Pen.eraser || activePen == Pen.selector) return;
|
2022-10-29 15:39:25 +00:00
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0;
|
2022-11-03 14:12:40 +00:00
|
|
|
final color = activePen == Pen.pen
|
|
|
|
? const Color(0xFF444444)
|
|
|
|
: Colors.yellow.withOpacity(.3);
|
2022-10-29 18:06:20 +00:00
|
|
|
strokes.add(Stroke.fromPoints(
|
2022-11-03 14:12:40 +00:00
|
|
|
[Point(offset, _calcTiltedWidth(3.0, e.tilt))], strokeid, color));
|
|
|
|
file.addStroke(strokeid, color);
|
2022-10-29 15:39:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
void pointUpEvent(PointerUpEvent e) {
|
2022-10-29 15:39:25 +00:00
|
|
|
if (activePen == Pen.eraser) return;
|
2022-11-03 14:12:40 +00:00
|
|
|
|
|
|
|
// pointerupevent doesn't deliver correct event button
|
2022-11-03 11:13:36 +00:00
|
|
|
if (_allowedToDraw(e.kind, 1)) {
|
2022-10-29 18:06:20 +00:00
|
|
|
final lastStroke = strokes.last;
|
|
|
|
if (lastStroke.points.length <= 1) {
|
2022-10-29 15:39:25 +00:00
|
|
|
// if the line consists only of one point (point) add endpoint as the same to allow drawing a line
|
2022-10-29 18:06:20 +00:00
|
|
|
lastStroke.points.add(lastStroke.points.last);
|
|
|
|
file.addPoint(lastStroke.id, lastStroke.points.last);
|
2022-10-29 15:39:25 +00:00
|
|
|
notifyListeners();
|
2022-11-03 11:13:36 +00:00
|
|
|
} else {
|
|
|
|
debugPrint('adding points to db');
|
|
|
|
file.addPoints(lastStroke.id, lastStroke.points);
|
2022-10-29 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
/// check if pointer event is allowed to draw points
|
2022-11-03 11:13:36 +00:00
|
|
|
bool _allowedToDraw(PointerDeviceKind kind, int button) {
|
|
|
|
return (_allowDrawWithFinger && kind == PointerDeviceKind.touch) ||
|
|
|
|
kind == PointerDeviceKind.stylus ||
|
|
|
|
(kind == PointerDeviceKind.mouse && button == kPrimaryMouseButton);
|
2022-10-29 19:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pointMoveEvent(Offset offset, PointerMoveEvent event) {
|
2022-10-29 15:39:25 +00:00
|
|
|
if (!a4Page.contains(offset)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:13:36 +00:00
|
|
|
if (_allowedToDraw(event.kind, event.buttons)) {
|
2022-10-29 15:39:25 +00:00
|
|
|
switch (activePen) {
|
|
|
|
case Pen.eraser:
|
|
|
|
// todo dynamic eraser size
|
|
|
|
final eraserrect = Rect.fromCircle(center: offset, radius: 3);
|
|
|
|
for (final stroke in strokes) {
|
|
|
|
// check if delete action was within bounding rect of stroke
|
|
|
|
if (stroke.getBoundingRect().contains(offset)) {
|
|
|
|
// check if eraser hit an point within its range
|
|
|
|
for (final pt in stroke.points) {
|
|
|
|
if (eraserrect.contains(pt.point)) {
|
2022-10-29 18:06:20 +00:00
|
|
|
file.removeStroke(stroke.id);
|
2022-10-29 15:39:25 +00:00
|
|
|
strokes.remove(stroke);
|
|
|
|
notifyListeners();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Pen.pen:
|
|
|
|
final pts = strokes.last.points;
|
2022-11-03 14:12:40 +00:00
|
|
|
// avoid duplicates
|
2022-10-29 15:39:25 +00:00
|
|
|
if (pts.last.point == offset) return;
|
|
|
|
|
2022-11-03 14:12:40 +00:00
|
|
|
double newWidth = _calcTiltedWidth(4.0, event.tilt);
|
2022-11-03 10:22:38 +00:00
|
|
|
if (pts.length > 1) {
|
2022-11-03 11:13:36 +00:00
|
|
|
newWidth = _calcAngleDependentWidth(pts.last,
|
|
|
|
pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth);
|
2022-10-29 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:12:40 +00:00
|
|
|
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));
|
2022-10-29 15:39:25 +00:00
|
|
|
break;
|
|
|
|
case Pen.selector:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
2022-10-29 18:06:20 +00:00
|
|
|
|
|
|
|
Future<void> loadStrokesFromFile() async {
|
|
|
|
strokes = await file.loadStrokes();
|
2022-11-03 11:13:36 +00:00
|
|
|
debugPrint('finished loading strokes from file');
|
2022-10-29 18:06:20 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-10-29 15:39:25 +00:00
|
|
|
}
|