2022-10-24 00:02:35 +00:00
|
|
|
import 'dart:ui';
|
2022-10-30 20:57:13 +00:00
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
import 'package:flutter/gestures.dart';
|
2022-10-24 00:02:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-11-12 18:35:16 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2022-10-24 00:02:35 +00:00
|
|
|
|
2022-11-12 18:35:16 +00:00
|
|
|
import '../context/file_change_notifier.dart';
|
2022-10-30 20:57:13 +00:00
|
|
|
import '../savesystem/note_file.dart';
|
2022-11-03 15:23:42 +00:00
|
|
|
import '../widgets/drawing_page_top_actions.dart';
|
2022-10-29 20:55:38 +00:00
|
|
|
import '../widgets/tool_bar.dart';
|
2022-11-03 15:43:50 +00:00
|
|
|
import 'document_types.dart';
|
2022-10-30 20:57:13 +00:00
|
|
|
import 'my_painter.dart';
|
|
|
|
import 'paint_controller.dart';
|
|
|
|
import 'screen_document_mapping.dart';
|
2022-10-24 00:02:35 +00:00
|
|
|
|
2022-10-29 15:39:25 +00:00
|
|
|
/// Handles input events and draws canvas element
|
2022-10-24 10:27:38 +00:00
|
|
|
class DrawingPage extends StatefulWidget {
|
2022-10-29 18:06:20 +00:00
|
|
|
// path to the .dbnote file
|
2022-11-03 15:43:50 +00:00
|
|
|
final NoteMetaData meta;
|
2022-10-29 18:06:20 +00:00
|
|
|
|
2022-11-03 15:43:50 +00:00
|
|
|
const DrawingPage({Key? key, required this.meta}) : super(key: key);
|
2022-10-24 00:02:35 +00:00
|
|
|
|
|
|
|
@override
|
2022-10-24 10:27:38 +00:00
|
|
|
State<DrawingPage> createState() => _DrawingPageState();
|
2022-10-24 00:02:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 10:27:38 +00:00
|
|
|
class _DrawingPageState extends State<DrawingPage> {
|
2022-10-26 22:15:13 +00:00
|
|
|
double zoom = .75;
|
|
|
|
double basezoom = 1.0;
|
|
|
|
Offset offset = const Offset(.0, .0);
|
2022-10-24 10:27:38 +00:00
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
late PaintController controller;
|
2022-11-04 22:09:50 +00:00
|
|
|
late NoteFile noteFile = NoteFile(widget.meta.relativePath);
|
2022-11-12 18:35:16 +00:00
|
|
|
late TextEditingController titleController =
|
|
|
|
TextEditingController(text: widget.meta.name);
|
2022-10-27 22:55:37 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
controller = PaintController(noteFile);
|
2022-11-03 11:13:36 +00:00
|
|
|
debugPrint('initializing strokes from file');
|
2022-10-29 18:06:20 +00:00
|
|
|
noteFile.init().then((value) => controller.loadStrokesFromFile());
|
|
|
|
|
2022-10-27 22:55:37 +00:00
|
|
|
// todo might be weird behaviour if used with short side
|
|
|
|
final screenWidth =
|
|
|
|
(window.physicalSize.longestSide / window.devicePixelRatio);
|
2022-10-29 15:39:25 +00:00
|
|
|
_calcNewPageOffset(const Offset(.0, .0), screenWidth - 45);
|
2022-10-27 22:55:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
@override
|
2022-11-12 18:35:16 +00:00
|
|
|
void dispose() async {
|
2022-10-29 18:06:20 +00:00
|
|
|
super.dispose();
|
2022-11-12 18:35:16 +00:00
|
|
|
titleController.dispose();
|
2022-10-29 18:06:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 00:02:35 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-11-12 18:35:16 +00:00
|
|
|
return WillPopScope(
|
|
|
|
onWillPop: () async {
|
|
|
|
await noteFile.close().then((value) =>
|
|
|
|
Provider.of<FileChangeNotifier>(context, listen: false)
|
|
|
|
.loadAllNotes());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: TextField(
|
|
|
|
onChanged: (value) {
|
|
|
|
noteFile.rename('$value.dbnote');
|
|
|
|
},
|
|
|
|
controller: titleController,
|
|
|
|
style: const TextStyle(color: Colors.white, fontSize: 20),
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: [
|
|
|
|
DrawingPageTopActions(
|
|
|
|
controller: controller, noteMetaData: widget.meta)
|
|
|
|
]),
|
|
|
|
body: Row(
|
|
|
|
children: [
|
|
|
|
ToolBar(
|
|
|
|
onPenChange: (pen) {
|
|
|
|
controller.changePen(pen);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Expanded(child: RepaintBoundary(child: _buildCanvas())),
|
|
|
|
],
|
|
|
|
),
|
2022-10-29 10:33:47 +00:00
|
|
|
),
|
2022-10-24 00:02:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-27 22:55:37 +00:00
|
|
|
// calculate new page offset from mousepointer delta
|
|
|
|
void _calcNewPageOffset(Offset delta, double canvasWidth) {
|
|
|
|
if (zoom > 1.0) {
|
|
|
|
Offset newOffset = offset + delta;
|
|
|
|
// don't allow navigating out of page if zoomed in
|
|
|
|
if (newOffset.dx > .0) {
|
|
|
|
setState(() {
|
|
|
|
offset = Offset(.0, newOffset.dy);
|
|
|
|
});
|
|
|
|
} else if (newOffset.dx < (-canvasWidth * zoom) + canvasWidth) {
|
|
|
|
setState(() {
|
|
|
|
offset = Offset((-canvasWidth * zoom) + canvasWidth, newOffset.dy);
|
|
|
|
});
|
2022-10-30 20:57:13 +00:00
|
|
|
debugPrint(offset.toString());
|
2022-10-27 22:55:37 +00:00
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
offset = offset + delta;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
// keep page x centered if zoomed out
|
|
|
|
offset = Offset(
|
|
|
|
(canvasWidth - (canvasWidth * zoom)) / 2, offset.dy + delta.dy);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onPointerMove(PointerMoveEvent event, Size size) {
|
|
|
|
Offset pos = event.localPosition;
|
|
|
|
final scale = calcPageDependentScale(zoom, a4Page, size);
|
|
|
|
pos = translateScreenToDocumentPoint(pos, scale, offset);
|
|
|
|
|
|
|
|
if (!a4Page.contains(pos)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
controller.pointMoveEvent(pos, event);
|
2022-10-28 15:09:14 +00:00
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
if (event.kind == PointerDeviceKind.touch ||
|
|
|
|
(event.kind == PointerDeviceKind.mouse &&
|
|
|
|
event.buttons == kSecondaryMouseButton)) {
|
2022-10-27 22:55:37 +00:00
|
|
|
_calcNewPageOffset(event.delta, size.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 00:02:35 +00:00
|
|
|
Widget _buildCanvas() {
|
2022-10-27 22:55:37 +00:00
|
|
|
final size = MediaQuery.of(context).size;
|
2022-10-29 10:33:47 +00:00
|
|
|
final canvasSize = Size(size.width - 45, size.height);
|
|
|
|
|
2022-10-28 21:59:12 +00:00
|
|
|
return Listener(
|
|
|
|
behavior: HitTestBehavior.opaque,
|
2022-10-29 15:39:25 +00:00
|
|
|
onPointerMove: (e) => _onPointerMove(e, canvasSize),
|
2022-11-14 12:46:07 +00:00
|
|
|
onPointerSignal: (pointerSignal) {
|
|
|
|
debugPrint('got pointer signal: $pointerSignal');
|
|
|
|
|
|
|
|
if (pointerSignal is PointerScrollEvent) {
|
|
|
|
final delta = pointerSignal.scrollDelta;
|
|
|
|
_calcNewPageOffset(-delta, size.width);
|
|
|
|
}
|
|
|
|
},
|
2022-10-29 19:39:08 +00:00
|
|
|
onPointerDown: (d) {
|
|
|
|
Offset pos = d.localPosition;
|
2022-10-29 15:39:25 +00:00
|
|
|
final scale = calcPageDependentScale(zoom, a4Page, canvasSize);
|
|
|
|
pos = translateScreenToDocumentPoint(pos, scale, offset);
|
2022-10-29 19:39:08 +00:00
|
|
|
controller.pointDownEvent(pos, d);
|
2022-10-28 21:59:12 +00:00
|
|
|
},
|
2022-10-29 19:39:08 +00:00
|
|
|
onPointerUp: (e) {
|
|
|
|
controller.pointUpEvent(e);
|
2022-10-28 21:59:12 +00:00
|
|
|
},
|
|
|
|
child: GestureDetector(
|
|
|
|
onScaleUpdate: (details) {
|
|
|
|
if (details.scale == 1.0) return;
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
zoom = (basezoom * details.scale).clamp(0.25, 5.0);
|
|
|
|
});
|
2022-10-24 00:02:35 +00:00
|
|
|
},
|
2022-10-28 21:59:12 +00:00
|
|
|
onScaleEnd: (details) {
|
|
|
|
basezoom = zoom;
|
|
|
|
},
|
|
|
|
child: CustomPaint(
|
2022-10-29 10:33:47 +00:00
|
|
|
painter: MyPainter(
|
|
|
|
offset: offset,
|
|
|
|
zoom: zoom,
|
2022-10-29 15:39:25 +00:00
|
|
|
canvasSize: canvasSize,
|
|
|
|
controller: controller),
|
2022-10-29 10:33:47 +00:00
|
|
|
size: canvasSize,
|
2022-10-24 00:02:35 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|