startDrawing method

void startDrawing(
  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 startDrawing(
  Offset point, {
  List<KLineEntity>? kLineData,
  double? scaleX,
  double? scrollX,
  double Function(double)? getX,
  double Function(double)? getY,
  double Function(double)? getPriceFromY, // 新增:从Y坐标反推价格的函数
  Rect? chartRect,
}) {
  debugPrint('DrawingToolManager.startDrawing: $_currentToolType at $point');
  if (_currentToolType == null || !_modeManager.isDrawingModeEnabled) return;

  _finishCurrentDrawing();

  // 应用磁铁吸附
  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,
    );
  }

  final id = _generateId();
  final properties = {
    'color': _currentColor,
    'strokeWidth': _currentStrokeWidth,
  };

  switch (_currentToolType!) {
    case DrawingToolType.trendLine:
      _currentDrawingTool = TrendLineTool(
        id: id,
        startPoint: adjustedPoint,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建趋势线工具: startPoint=$adjustedPoint, isComplete=${_currentDrawingTool!.isComplete}');
      break;
    case DrawingToolType.trendAngle:
      _currentDrawingTool = TrendAngleTool(
        id: id,
        startPoint: adjustedPoint,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建趋势角度工具: startPoint=$adjustedPoint, isComplete=${_currentDrawingTool!.isComplete}');
      break;
    case DrawingToolType.arrow:
      _currentDrawingTool = ArrowTool(
        id: id,
        startPoint: adjustedPoint,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建箭头工具: startPoint=$adjustedPoint, isComplete=${_currentDrawingTool!.isComplete}');
      break;
    case DrawingToolType.verticalLine:
      _currentDrawingTool = VerticalLineTool(
        id: id,
        xPosition: adjustedPoint.dx,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建垂直线工具: xPosition=${adjustedPoint.dx}, isComplete=${_currentDrawingTool!.isComplete}');
      // 垂直线工具改为预览模式,不立即完成
      _currentDrawingTool!.state = DrawingToolState.drawing;
      _updateDrawingPosition(adjustedPoint);
      break;
    case DrawingToolType.horizontalLine:
      _currentDrawingTool = HorizontalLineTool(
        id: id,
        yPosition: adjustedPoint.dy,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建水平线工具: yPosition=${adjustedPoint.dy}, isComplete=${_currentDrawingTool!.isComplete}');
      // 水平线工具改为预览模式,不立即完成
      _currentDrawingTool!.state = DrawingToolState.drawing;
      _updateDrawingPosition(adjustedPoint);
      break;
    case DrawingToolType.horizontalRay:
      // 计算真实价格值
      double realPrice = adjustedPoint.dy;
      if (getPriceFromY != null) {
        // 使用反向转换函数从屏幕Y坐标计算真实价格
        realPrice = getPriceFromY(adjustedPoint.dy);
        debugPrint('水平射线屏幕Y坐标: ${adjustedPoint.dy},真实价格值: $realPrice');
      } else {
        debugPrint('警告:没有价格转换函数,使用屏幕Y坐标作为价格');
      }

      _currentDrawingTool = HorizontalRayTool(
        id: _generateId(),
        yPosition: adjustedPoint.dy,
        centerX: adjustedPoint.dx,
        priceValue: realPrice, // 使用计算出的真实价格值
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      // 水平射线改为预览模式,不立即完成
      _currentDrawingTool!.state = DrawingToolState.drawing;
      _updateDrawingPosition(adjustedPoint);
      break;
    case DrawingToolType.ray:
      _currentDrawingTool = RayTool(
        id: id,
        startPoint: adjustedPoint,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      debugPrint(
          '创建射线工具: startPoint=$adjustedPoint, isComplete=${_currentDrawingTool!.isComplete}');
      // 射线工具改为预览模式,等待确定方向
      _currentDrawingTool!.state = DrawingToolState.drawing;
      _updateDrawingPosition(adjustedPoint);
      break;
    case DrawingToolType.crossLine:
      _currentDrawingTool = CrossLineTool(
        id: id,
        centerPoint: adjustedPoint,
        color: _currentColor,
        strokeWidth: _currentStrokeWidth,
      );
      // 十字线工具改为预览模式,不立即完成
      _currentDrawingTool!.state = DrawingToolState.drawing;
      _updateDrawingPosition(adjustedPoint);
      break;
  }

  if (_currentDrawingTool != null) {
    // 只有在工具状态不是drawing的情况下才设置为drawing
    // 因为单点工具已经在创建时设置了正确的状态
    if (_currentDrawingTool!.state == DrawingToolState.none) {
      _currentDrawingTool!.state = DrawingToolState.drawing;
    }
    _updateDrawingPosition(adjustedPoint);
    _notifyToolsChanged();
    debugPrint(
        '工具创建完成: state=${_currentDrawingTool!.state}, isComplete=${_currentDrawingTool!.isComplete}');
  }
}