updateDrawing method

void updateDrawing(
  1. Offset point, {
  2. List<KLineEntity>? kLineData,
  3. double? scaleX,
  4. double? scrollX,
  5. double getX(
    1. double
    )?,
  6. double getY(
    1. double
    )?,
  7. double getPriceFromY(
    1. double
    )?,
  8. Rect? chartRect,
})

Implementation

void updateDrawing(
  Offset point, {
  List<KLineEntity>? kLineData,
  double? scaleX,
  double? scrollX,
  double Function(double)? getX,
  double Function(double)? getY,
  double Function(double)? getPriceFromY, // 新增:从Y坐标反推价格的函数
  Rect? chartRect,
}) {
  if (_currentDrawingTool == null) return;

  // 应用磁铁吸附
  Offset adjustedPoint = point;
  if (_modeManager.isMagnetMode &&
      kLineData != null &&
      scaleX != null &&
      scrollX != null &&
      getX != null &&
      getY != null &&
      chartRect != null) {
    adjustedPoint = _modeManager.magnetSnap(
      point,
      kLineData,
      scaleX,
      scrollX,
      getX,
      getY,
      chartRect,
    );
  }

  switch (_currentDrawingTool!.type) {
    case DrawingToolType.trendLine:
      final tool = _currentDrawingTool as TrendLineTool;
      tool.endPoint = adjustedPoint;
      break;
    case DrawingToolType.trendAngle:
      final tool = _currentDrawingTool as TrendAngleTool;
      tool.endPoint = adjustedPoint;
      // 计算角度
      if (tool.startPoint != null && tool.endPoint != null) {
        final dx = tool.endPoint!.dx - tool.startPoint!.dx;
        final dy = tool.endPoint!.dy - tool.startPoint!.dy;
        tool.angle = (atan2(dy, dx) * 180 / pi).abs();
      }
      break;
    case DrawingToolType.arrow:
      final tool = _currentDrawingTool as ArrowTool;
      tool.endPoint = adjustedPoint;
      break;
    case DrawingToolType.verticalLine:
      final tool = _currentDrawingTool as VerticalLineTool;
      tool.xPosition = adjustedPoint.dx;
      break;
    case DrawingToolType.horizontalLine:
      final tool = _currentDrawingTool as HorizontalLineTool;
      tool.yPosition = adjustedPoint.dy;
      break;
    case DrawingToolType.horizontalRay:
      final tool = _currentDrawingTool as HorizontalRayTool;
      tool.yPosition = adjustedPoint.dy;
      tool.centerX = adjustedPoint.dx;
      // 更新真实价格值
      if (getPriceFromY != null) {
        tool.priceValue = getPriceFromY(adjustedPoint.dy);
        debugPrint('更新水平射线价格: ${tool.priceValue}');
      } else {
        tool.priceValue = adjustedPoint.dy; // 后备方案
      }
      break;
    case DrawingToolType.ray:
      final tool = _currentDrawingTool as RayTool;
      tool.directionPoint = adjustedPoint;
      break;
    case DrawingToolType.crossLine:
      final tool = _currentDrawingTool as CrossLineTool;
      tool.centerPoint = adjustedPoint;
      break;
  }

  _updateDrawingPosition(adjustedPoint);
  _notifyToolsChanged();
}