2024-11-15 17:42:52 +08:00
|
|
|
import 'dart:developer' as dev;
|
|
|
|
// import 'dart:math';
|
2024-11-06 17:23:29 +08:00
|
|
|
|
2024-08-23 17:54:57 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
2024-08-26 18:25:21 +08:00
|
|
|
import 'package:scence_map/controllers/controller.dart';
|
2024-11-06 17:23:29 +08:00
|
|
|
import 'package:scence_map/controllers/plum_controller.dart';
|
2024-08-23 17:54:57 +08:00
|
|
|
|
2024-11-15 17:42:52 +08:00
|
|
|
import 'view.dart';
|
2024-08-23 17:54:57 +08:00
|
|
|
|
|
|
|
ScenceMapController mapController = Get.find<ScenceMapController>();
|
|
|
|
|
|
|
|
class DrawDirection extends CustomPainter {
|
|
|
|
PlumDataController controller;
|
|
|
|
Offset pos;
|
|
|
|
bool isDarkMode;
|
|
|
|
bool isInit;
|
|
|
|
DrawDirection(this.controller, this.pos, this.isDarkMode, this.isInit);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void paint(Canvas canvas, Size size) {
|
|
|
|
controller.canvasSize = size;
|
|
|
|
final paint = Paint();
|
|
|
|
Path path = Path();
|
|
|
|
|
|
|
|
paint
|
|
|
|
..strokeWidth = 2
|
|
|
|
..style = PaintingStyle.stroke
|
|
|
|
..color = isDarkMode ? Colors.white : Colors.black;
|
|
|
|
if (controller.linePointOffset.value == Offset.zero ||
|
2024-11-15 17:42:52 +08:00
|
|
|
controller.centerXY.value == Offset.zero ||
|
|
|
|
controller.checkValue.value == "checkPile") {
|
2024-08-23 17:54:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset linePointOffset = mapController.xy2Screen(
|
|
|
|
controller.linePointXY.value.dx, controller.linePointXY.value.dy);
|
|
|
|
Offset centerOffset = controller.centerOffset.value;
|
|
|
|
Offset centerXY = controller.centerXY.value;
|
|
|
|
if (isInit) {
|
|
|
|
linePointOffset = linePointOffset + pos;
|
|
|
|
centerOffset = controller.centerOffset.value + pos;
|
|
|
|
}
|
2024-11-15 17:42:52 +08:00
|
|
|
dev.log("-----矩阵中心$centerOffset,$centerXY");
|
2024-08-23 17:54:57 +08:00
|
|
|
|
|
|
|
path.moveTo(centerOffset.dx, centerOffset.dy);
|
|
|
|
path.lineTo(linePointOffset.dx, linePointOffset.dy);
|
|
|
|
canvas.drawPath(path, paint);
|
|
|
|
paint.style = PaintingStyle.fill;
|
2024-11-15 17:42:52 +08:00
|
|
|
// double deLength = 100;
|
|
|
|
|
|
|
|
// double angle = atan2((linePointOffset.dy - centerOffset.dy),
|
|
|
|
// (linePointOffset.dx - centerOffset.dx));
|
|
|
|
// // dev.log("-----角度$angle,${controller.angle.value}");
|
|
|
|
// canvas.drawCircle(linePointOffset, 10, paint);
|
|
|
|
// var dx1 = (deLength * cos(angle + pi / 2)) + centerOffset.dx;
|
|
|
|
// var dy1 = (deLength) * sin(angle + pi / 2) + centerOffset.dy;
|
|
|
|
|
|
|
|
// canvas.drawCircle(Offset(dx1, dy1), 10, paint);
|
|
|
|
|
|
|
|
// dx1 = (deLength * cos(angle + pi)) + centerOffset.dx;
|
|
|
|
// dy1 = (deLength) * sin(angle + pi) + centerOffset.dy;
|
|
|
|
|
|
|
|
// canvas.drawCircle(Offset(dx1, dy1), 10, paint);
|
|
|
|
|
|
|
|
// dx1 = (deLength * cos(angle + pi / 2 * 3)) + centerOffset.dx;
|
|
|
|
// dy1 = (deLength) * sin(angle + pi / 2 * 3) + centerOffset.dy;
|
|
|
|
|
|
|
|
// canvas.drawCircle(Offset(dx1, dy1), 10, paint);
|
|
|
|
|
2024-08-23 17:54:57 +08:00
|
|
|
paint
|
|
|
|
// ..color = Colors.grey.withOpacity(.4)
|
|
|
|
.style = PaintingStyle.stroke;
|
|
|
|
|
|
|
|
if (controller.isUp.value && controller.angle.value != 0) {
|
2024-11-15 17:42:52 +08:00
|
|
|
List<Offset> rect = getRect(controller.distancesOffset);
|
|
|
|
List<Offset> outRect = getOutRect(rect, centerOffset, linePointOffset);
|
|
|
|
double unit = controller.space / mapController.pixel2MeterRatio;
|
2024-08-23 17:54:57 +08:00
|
|
|
|
2024-11-15 17:42:52 +08:00
|
|
|
paint.color = Colors.grey;
|
|
|
|
|
|
|
|
canvas.drawLine(rect[0], rect[1], paint); // 绘制上边
|
|
|
|
canvas.drawLine(rect[1], rect[2], paint); // 绘制右边
|
|
|
|
canvas.drawLine(rect[2], rect[3], paint); // 绘制下边
|
|
|
|
canvas.drawLine(rect[3], rect[0], paint); // 绘制左边
|
2024-08-23 17:54:57 +08:00
|
|
|
|
2024-11-15 17:42:52 +08:00
|
|
|
// 沿着路径绘制圆圈
|
|
|
|
double distance = (outRect[2] - outRect[0]).distance;
|
|
|
|
int horizonalCircles = (distance / unit).ceil();
|
|
|
|
taskcontroller.currentask.value.list.length = 0;
|
2024-08-23 17:54:57 +08:00
|
|
|
List<Offset> circlePoints = [];
|
2024-11-15 17:42:52 +08:00
|
|
|
paint.color = Colors.yellow;
|
|
|
|
//
|
|
|
|
double minY = rect[0].dy;
|
|
|
|
double maxY = rect[2].dy;
|
|
|
|
double minX = rect[0].dx;
|
|
|
|
double maxX = rect[2].dx;
|
|
|
|
bool centerEven = false;
|
|
|
|
for (int j = -1; j <= horizonalCircles; j++) {
|
|
|
|
double t = j * unit / distance;
|
2024-08-23 17:54:57 +08:00
|
|
|
Offset point = Offset.lerp(centerOffset, linePointOffset, t)!;
|
|
|
|
circlePoints.add(point);
|
2024-11-15 17:42:52 +08:00
|
|
|
if (j > 1) {
|
|
|
|
Offset point1 = Offset.lerp(centerOffset, linePointOffset, -t)!;
|
|
|
|
if (point1.dx >= (minX - unit) &&
|
|
|
|
point1.dx <= (maxX + unit) &&
|
|
|
|
point1.dy >= (minY - unit) &&
|
|
|
|
point1.dy <= (maxY + unit)) {
|
|
|
|
circlePoints.add(point1);
|
|
|
|
if (point1 - centerOffset < Offset(unit, unit)) {
|
|
|
|
centerEven = circlePoints.length % 2 == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
}
|
2024-08-23 17:54:57 +08:00
|
|
|
}
|
2024-11-15 17:42:52 +08:00
|
|
|
circlePoints.sort((a, b) => a.dx.compareTo(b.dx));
|
2024-08-23 17:54:57 +08:00
|
|
|
Offset direction =
|
|
|
|
(linePointOffset - centerOffset).scale(1 / distance, 1 / distance);
|
|
|
|
Offset perpendicular = Offset(-direction.dy, direction.dx);
|
2024-11-15 17:42:52 +08:00
|
|
|
controller.plumList.length = 0;
|
2024-08-23 17:54:57 +08:00
|
|
|
// 绘制上下方的圆圈,形成正方形矩阵
|
2024-11-15 17:42:52 +08:00
|
|
|
|
|
|
|
for (int p = 0; p <= circlePoints.length - 1; p++) {
|
|
|
|
Offset point = circlePoints[p];
|
|
|
|
|
|
|
|
for (int j = 0; j <= horizonalCircles; j++) {
|
2024-08-23 17:54:57 +08:00
|
|
|
Offset upPoint = point + perpendicular * (j * unit);
|
|
|
|
Offset downPoint = point - perpendicular * (j * unit);
|
2024-11-15 17:42:52 +08:00
|
|
|
bool isUpPointInRect = upPoint.dx >= minX &&
|
|
|
|
upPoint.dx <= maxX &&
|
|
|
|
upPoint.dy >= minY &&
|
|
|
|
upPoint.dy <= maxY;
|
|
|
|
bool isDownPointInRect = downPoint.dx >= minX &&
|
|
|
|
downPoint.dx <= maxX &&
|
|
|
|
downPoint.dy >= minY &&
|
|
|
|
downPoint.dy <= maxY;
|
|
|
|
if (controller.isPlum.value) {
|
|
|
|
if ((j % 2 == p % 2) == centerEven) {
|
|
|
|
paint.style = PaintingStyle.fill;
|
|
|
|
} else {
|
|
|
|
paint.style = PaintingStyle.stroke;
|
|
|
|
}
|
|
|
|
if (isDownPointInRect && j > 0) {
|
|
|
|
canvas.drawCircle(
|
|
|
|
downPoint, .3 / mapController.pixel2MeterRatio, paint);
|
|
|
|
if ((j % 2 == p % 2) == centerEven) {
|
|
|
|
Offset xy1 = mapController.screen2xy0(downPoint);
|
|
|
|
controller.plumList.add(xy1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUpPointInRect) {
|
|
|
|
canvas.drawCircle(
|
|
|
|
upPoint, .3 / mapController.pixel2MeterRatio, paint);
|
|
|
|
if ((j % 2 == p % 2) != centerEven) {
|
|
|
|
Offset xy = mapController.screen2xy0(upPoint);
|
|
|
|
controller.plumList.add(xy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUpPointInRect) {
|
|
|
|
// TextPainter textPainter = TextPainter(
|
|
|
|
// text: TextSpan(
|
|
|
|
// text: '$p,$j',
|
|
|
|
// style: TextStyle(color: Colors.blue, fontSize: 16)),
|
|
|
|
// textDirection: TextDirection.ltr,
|
|
|
|
// );
|
|
|
|
// textPainter.layout();
|
|
|
|
// textPainter.paint(canvas, upPoint);
|
|
|
|
canvas.drawCircle(
|
|
|
|
upPoint, .3 / mapController.pixel2MeterRatio, paint);
|
|
|
|
Offset xy = mapController.screen2xy0(upPoint);
|
|
|
|
controller.plumList.add(xy);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDownPointInRect && j > 0) {
|
|
|
|
// TextPainter textPainter = TextPainter(
|
|
|
|
// text: TextSpan(
|
|
|
|
// text: '$p,$j',
|
|
|
|
// style: TextStyle(color: Colors.blue, fontSize: 16)),
|
|
|
|
// textDirection: TextDirection.ltr,
|
|
|
|
// );
|
|
|
|
// textPainter.layout();
|
|
|
|
// textPainter.paint(canvas, downPoint);
|
|
|
|
canvas.drawCircle(
|
|
|
|
downPoint, .3 / mapController.pixel2MeterRatio, paint);
|
|
|
|
Offset xy1 = mapController.screen2xy0(downPoint);
|
|
|
|
controller.plumList.add(xy1);
|
|
|
|
}
|
2024-08-23 17:54:57 +08:00
|
|
|
}
|
|
|
|
}
|
2024-11-15 17:42:52 +08:00
|
|
|
dev.log("controller.plumList:${controller.plumList.length}");
|
|
|
|
}
|
|
|
|
}
|
2024-08-23 17:54:57 +08:00
|
|
|
|
2024-11-15 17:42:52 +08:00
|
|
|
getRect(List<Offset> list) {
|
|
|
|
List<Offset> rect = [];
|
|
|
|
double minx = double.infinity;
|
|
|
|
double maxx = double.negativeInfinity;
|
|
|
|
double miny = double.infinity;
|
|
|
|
double maxy = double.negativeInfinity;
|
|
|
|
for (var point in list) {
|
|
|
|
minx = point.dx < minx ? point.dx : minx;
|
|
|
|
maxx = point.dx > maxx ? point.dx : maxx;
|
|
|
|
miny = point.dy < miny ? point.dy : miny;
|
|
|
|
maxy = point.dy > maxy ? point.dy : maxy;
|
2024-08-23 17:54:57 +08:00
|
|
|
}
|
2024-11-15 17:42:52 +08:00
|
|
|
minx += controller.transOff.dx;
|
|
|
|
maxx += controller.transOff.dx;
|
|
|
|
miny += controller.transOff.dy;
|
|
|
|
maxy += controller.transOff.dy;
|
|
|
|
// 根据最小最大坐标生成矩形的四个顶点
|
|
|
|
rect.add(Offset(minx, miny)); // 左下角
|
|
|
|
rect.add(Offset(maxx, miny)); // 右下角
|
|
|
|
rect.add(Offset(maxx, maxy)); // 右上角
|
|
|
|
rect.add(Offset(minx, maxy)); // 左上角
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
getOutRect(List<Offset> rect, Offset centerOffset, Offset linePointOffset) {
|
|
|
|
// double height = rect[2].dy - rect[0].dy;
|
|
|
|
// double width = rect[1].dx - rect[0].dx;
|
|
|
|
double newHeight = (centerOffset.dy - linePointOffset.dy) * 2;
|
|
|
|
double newWidth = (centerOffset.dx - linePointOffset.dx) * 2;
|
|
|
|
// if (newHeight < height) {
|
|
|
|
// newHeight = height;
|
|
|
|
// }
|
|
|
|
// if (newWidth < width) {
|
|
|
|
// newWidth = width;
|
|
|
|
// }
|
|
|
|
Offset topLeft =
|
|
|
|
Offset(centerOffset.dx - newWidth / 2, centerOffset.dy - newHeight / 2);
|
|
|
|
Offset topRight =
|
|
|
|
Offset(centerOffset.dx + newWidth / 2, centerOffset.dy - newHeight / 2);
|
|
|
|
Offset bottomLeft =
|
|
|
|
Offset(centerOffset.dx - newWidth / 2, centerOffset.dy + newHeight / 2);
|
|
|
|
Offset bottomRight =
|
|
|
|
Offset(centerOffset.dx + newWidth / 2, centerOffset.dy + newHeight / 2);
|
|
|
|
return [
|
|
|
|
bottomLeft,
|
|
|
|
bottomRight,
|
|
|
|
topRight,
|
|
|
|
topLeft,
|
|
|
|
];
|
2024-08-23 17:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
|
|
|
|
}
|