|
| 1 | +// Copyright (c) 2023- All flutter_graph_view authors. All rights reserved. |
| 2 | +// |
| 3 | +// This source code is licensed under Apache 2.0 License. |
| 4 | + |
| 5 | +import 'dart:ui'; |
| 6 | + |
| 7 | +import 'package:flutter_graph_view/core/util.dart'; |
| 8 | +import 'package:flutter_graph_view/flutter_graph_view.dart'; |
| 9 | + |
| 10 | +/// Used to customize the edge UI. |
| 11 | +/// |
| 12 | +/// 用于自定义边的UI |
| 13 | +abstract class EdgeShape { |
| 14 | + /// render the edge shape to canvas by data. |
| 15 | + /// |
| 16 | + /// 通过边数据将自定义的图形绘制到画布中。 |
| 17 | + render(Edge edge, Canvas canvas, Paint paint, List<Paint> paintLayers); |
| 18 | + |
| 19 | + /// compute the width of the shape from data, used for overlap and mouse event judgment. |
| 20 | + /// |
| 21 | + /// 通过数据来计算形状的宽,用于重叠与鼠标事件判断。 |
| 22 | + double width(Edge edge) { |
| 23 | + return len(edge); |
| 24 | + } |
| 25 | + |
| 26 | + /// compute the height of the shape from data, used for overlap and mouse event judgment. |
| 27 | + /// |
| 28 | + /// 通过数据计算形状的高,用于重叠与鼠标事件判断。 |
| 29 | + double height(Edge edge); |
| 30 | + |
| 31 | + /// compute the size of the shape from data, used for overlap and mouse event judgment. |
| 32 | + /// |
| 33 | + /// 通过数据计算形状的尺寸,用于重叠与鼠标事件判断。 |
| 34 | + Vector2 size(Edge edge) { |
| 35 | + return Vector2(width(edge), height(edge)); |
| 36 | + } |
| 37 | + |
| 38 | + /// update the paint from data. |
| 39 | + /// |
| 40 | + /// 根据数据更新画笔属性。 |
| 41 | + void setPaint(Edge edge); |
| 42 | + |
| 43 | + /// When some elements are activated and do not contain the current element. |
| 44 | + /// |
| 45 | + /// 当一些元素被激活且不包含当前元素 |
| 46 | + bool isWeaken(Edge edge) { |
| 47 | + var graph = edge.cpn!.gameRef.graph; |
| 48 | + return graph.hoverVertex != null && |
| 49 | + !graph.hoverVertex!.neighborEdges.contains(edge); |
| 50 | + } |
| 51 | + |
| 52 | + /// Compute the line length by two vertex. |
| 53 | + /// |
| 54 | + /// 通过两个节点的坐标,计算线的长度。 |
| 55 | + double len(Edge edge) => |
| 56 | + edge.end == null || edge.start.cpn == null || edge.end!.cpn == null |
| 57 | + ? 10 |
| 58 | + : Util.distance(edge.start.cpn!.position, edge.end!.cpn!.position); |
| 59 | + |
| 60 | + /// Compute the shape angle by two vertex. |
| 61 | + /// |
| 62 | + /// 通过两个节点的坐标,获取线的旋转角度 |
| 63 | + double angle(Edge edge) { |
| 64 | + Offset offset = Offset( |
| 65 | + (edge.end == null |
| 66 | + ? edge.start.cpn!.position.x |
| 67 | + : edge.end!.cpn!.position.x) - |
| 68 | + (edge.start.cpn!.position.x), |
| 69 | + (edge.end == null |
| 70 | + ? edge.start.cpn!.position.y |
| 71 | + : edge.end!.cpn!.position.y) - |
| 72 | + (edge.start.cpn!.position.y), |
| 73 | + ); |
| 74 | + |
| 75 | + return offset.direction; |
| 76 | + } |
| 77 | +} |
0 commit comments