-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawEngine.dart
96 lines (85 loc) · 2.49 KB
/
DrawEngine.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#import('dart:html');
#import('dart:core');
#import('DrawUtils.dart');
class DrawGraph{
List<DrawNode> Nodes;
int currentSelected = 0;
bool readyToLine = false;
DrawNode currentInitialNode;
DrawNode currentFinalNode;
DrawGraph(){
this.Nodes = new List<DrawNode>();
}
void ResetLine(){
this.currentSelected = 0;
this.readyToLine = false;
}
bool HasClick(int x, int y){
for(int i=0; i<this.Nodes.length; i++){
if (this.Nodes[i].isCoorInside(x, y)){
if (this.currentSelected==0) {
this.currentSelected = 1;
this.currentInitialNode = this.Nodes[i];
}
else if(this.currentSelected == 1){
this.currentSelected = 0;
this.currentFinalNode = this.Nodes[i];
this.readyToLine = true;
if (this.currentFinalNode.val == this.currentInitialNode.val){
this.ResetLine();
return true;
}
}
return true;
}
}
return false;
}
}
class DrawNode{
int x;
int y;
int w=40;
int h=40;
int he;
String val;
bool d;
bool selected=false;
bool isCoorInside (int cx, int cy){
if (((cx)>=this.x-20) && ((cx)<=(this.x+this.w-20)) &&
((cy)>=this.y-20) && ((cy)<=(this.y+this.h-20))
){
return true;
}
else
return false;
}
DrawNode(int x, int y, String val , int he){
this.x = x;
this.y = y;
this.val = val;
this.he = he;
}
}
void main() {
CanvasElement canvas = document.query("#canvas");
CanvasRenderingContext2D ctx = canvas.getContext("2d");
DrawGraph GraphN = new DrawGraph();
var alpth = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","v","w","x","y","z"];
int it = 0;
canvas.on.click.add(function (e){
if (!GraphN.HasClick(e.offsetX, e.offsetY))
{
GraphN.ResetLine();
DrawNode newNode = new DrawNode(e.offsetX,e.offsetY,alpth[it],0);
GraphN.Nodes.add(newNode);
DrawNode LastNode = GraphN.Nodes.last();
DrawUtils.DrawNode(ctx, LastNode.x, LastNode.y, LastNode.val,"${LastNode.he}");
it++;
}
else if (GraphN.readyToLine){
DrawUtils.DrawLine(ctx, GraphN.currentInitialNode.x, GraphN.currentInitialNode.y, GraphN.currentFinalNode.x , GraphN.currentFinalNode.y);
GraphN.ResetLine();
}
});
}