-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
158 lines (116 loc) · 4.83 KB
/
editor.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Editor = function() {
this.document = new Document();
this.gestureRecognizer = new GestureRecognizer();
this.canvas = document.querySelector("canvas");
this.render = new Render(this.document, this.canvas);
this.cursorService = new CursorService();
this.toolManager = new ToolManager();
this.toolManager.addTool("LineTool", function() { return new LineTool(this.document); }.bind(this));
this.toolManager.addTool("RectTool", function() { return new RectTool(this.document); }.bind(this));
this.toolManager.addTool("RotateTool", function() { return new RotateTool(this.document); }.bind(this));
this.toolManager.addTool("SelectTool", function() { return new SelectTool(this.document, this.cursorService); }.bind(this), true);
this.bindEvents();
}
Editor.prototype.activateTool = function(toolname) {
this.toolManager.activateTool(toolname);
}
Editor.prototype.getMousePos = function(evt) {
var rect = this.canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
Editor.prototype.bindEvents = function() {
this.gestureRecognizer.onMouseMove.addListener(this.toolManager.onMouseMove.bind(this.toolManager));
this.gestureRecognizer.onMouseDrag.addListener(this.toolManager.onMouseDrag.bind(this.toolManager));
this.gestureRecognizer.onMouseLeftDown.addListener(this.toolManager.onMouseLeftDown.bind(this.toolManager));
this.gestureRecognizer.onMouseRightDown.addListener(this.toolManager.onMouseRightDown.bind(this.toolManager));
this.gestureRecognizer.onMouseRelease.addListener(this.toolManager.onMouseRelease.bind(this.toolManager));
this.gestureRecognizer.onMouseDblClick.addListener(this.toolManager.onMouseDblClick.bind(this.toolManager));
// this.document.invalidate.addListener(function() {
// this.invalidate();
// }.bind(this));
this.canvas.addEventListener('mousemove', function(evt) {
var mousePos = this.getMousePos(evt);
//var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
// console.log(message);
this.gestureRecognizer.handleMouseMove(mousePos);
}.bind(this), false);
this.canvas.addEventListener('mousedown', function(evt) {
var mousePos = this.getMousePos(evt);
var message = 'Mouse clicked at: ' + mousePos.x + ',' + mousePos.y;
console.log(message);
console.log(evt.button);
this.gestureRecognizer.handleMouseDown(mousePos, evt.button === 2);
}.bind(this), false);
this.canvas.addEventListener('mouseup', function(evt) {
var mousePos = this.getMousePos(evt);
// var message = 'Mouse up at: ' + mousePos.x + ',' + mousePos.y;
// console.log(message);
this.gestureRecognizer.handleMouseRelease(mousePos);
}.bind(this), false);
this.canvas.addEventListener('dblclick', function(evt) {
var mousePos = this.getMousePos(evt);
var message = 'Mouse dblclick at: ' + mousePos.x + ',' + mousePos.y;
console.log(message);
this.gestureRecognizer.handleMouseDblClick(mousePos);
}.bind(this), false);
this.canvas.oncontextmenu = function (evt) {
evt.preventDefault();
};
}
Editor.prototype.drawGeometries = function(geometries) {
var context = this.canvas.getContext("2d");
geometries.forEach(function(geometry) {
if (geometry.strokeStyle)
context.strokeStyle = geometry.getStrokeStyle();
context.beginPath();
for (var i = 0; i < geometry.getPoints().length; i++) {
var point = geometry.getPoints()[i];
if (i == 0) {
context.moveTo(point.x, point.y);
}
else {
context.lineTo(point.x, point.y);
}
}
context.stroke();
/*if (geometry instanceof Line) {
context.beginPath();
context.moveTo(geometry.p1.x, geometry.p1.y);
context.lineTo(geometry.p2.x, geometry.p2.y);
context.stroke();
}
else if (geometry instanceof Rect) {
context.beginPath();
context.moveTo(geometry.p1.x, geometry.p1.y);
context.lineTo(geometry.p2.x, geometry.p1.y);
context.lineTo(geometry.p2.x, geometry.p2.y);
context.lineTo(geometry.p1.x, geometry.p2.y);
context.lineTo(geometry.p1.x, geometry.p1.y);
context.stroke();
}
else if (geometry instanceof Polygon) {
context.beginPath();
for (var i = 0; i < geometry.points.length; i++) {
var point = geometry.points[i];
if (i == 0) {
context.moveTo(point[0], point[1]);
}
else {
context.lineTo(point[0], point[1]);
}
}
context.stroke();
}*/
});
}
Editor.prototype.invalidate = function() {
var context = this.canvas.getContext("2d");
context.clearRect(0, 0, this.canvas.width, this.canvas.height);
context.strokeStyle = "black";
this.drawGeometries(this.document.getGeometries());
context.strokeStyle = "red";
this.drawGeometries(this.document.getEditionGeometries());
}