-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchemaDrawer.js
144 lines (121 loc) · 4.41 KB
/
SchemaDrawer.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
"use strict";
/********************************************************************************************/
function SchemaDrawer(model, scale, drawing_area)
/********************************************************************************************/
{
this.model = model;
this.drawing_area = drawing_area;
this.highlight = null;
this.temp_connection = null;
}
SchemaDrawer.prototype.setContext = function(ctx) {
this.ctx = ctx;
//this.setTransform();
};
SchemaDrawer.prototype.setScale = function(scale) {
this.scale = scale;
this.setTransform();
};
SchemaDrawer.prototype.setDrawingArea = function(drawing_area) {
this.drawing_area = drawing_area;
this.setTransform();
};
SchemaDrawer.prototype.clear = function() { // We can probably do without this now.
// Based on code on this page http://stackoverflow.com/questions/2142535/how-to-clear-the-canvas-for-redrawing
this.ctx.save();
this.ctx.setTransform(1, 0, 0, 1, 0, 0);
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
this.ctx.restore();
}
SchemaDrawer.prototype.invalidateRectangle = function(rectangle) {
if (!this.invalid_rectangle) {
this.invalid_rectangle = rectangle;
} else {
this.invalid_rectangle = SmallestCoveringBox(this.invalid_rectangle, rectangle);
}
};
SchemaDrawer.prototype.draw = function(exclude_item) {
/*if (this.temp_connection) {
this.invalidateRectangle(this.temp_connection.boundingBox().expand(0.1));
}*/
if (true || this.invalid_rectangle) {
// Set up the clip path.
/*this.ctx.save();
this.ctx.beginPath();
this.ctx.moveTo(this.invalid_rectangle.left, this.invalid_rectangle.top);
this.ctx.lineTo(this.invalid_rectangle.right, this.invalid_rectangle.top);
this.ctx.lineTo(this.invalid_rectangle.right, this.invalid_rectangle.bottom);
this.ctx.lineTo(this.invalid_rectangle.left, this.invalid_rectangle.bottom);
this.ctx.closePath();
this.ctx.clip();*/
// Delete the old rectangle.
//this.ctx.clearRect(this.invalid_rectangle.left, this.invalid_rectangle.top, this.invalid_rectangle.width(), this.invalid_rectangle.height());
// Draw everything that might have been deleted or damaged by the clearRect.
var all_needing_redrawn = this.model.allObjectsTouchingBox(this.invalid_rectangle, true, true);
for (var i = 0; i < all_needing_redrawn.length; i++) {
var list_item = all_needing_redrawn[i];
if (list_item !== exclude_item) {
list_item.drawWrapper(this.ctx);
}
}
if (this.highlight) {
this.drawHighlight(this.highlight);
}
if (this.temp_connection) {
this.temp_connection.drawWrapper(this.ctx);
}
// Drop the clip path and draw item in new position.
//this.ctx.restore();
if (this.selection_box) {
this.ctx.save();
this.ctx.lineWidth = 0.05;
this.ctx.strokeRect(this.selection_box.left, this.selection_box.top, this.selection_box.width(), this.selection_box.height());
this.ctx.restore();
}
this.invalid_rectangle = null;
}
};
SchemaDrawer.prototype.invalidateItem = function(item) {
this.invalidateRectangle(item.boundingBox());
var connections = item.allConnections();
for (var i = 0; i < connections.length; i++) {
this.invalidateRectangle(connections[i].boundingBox().expand(0.1));
}
}
SchemaDrawer.prototype.moveItem = function(item, new_position) {
this.invalidateItem(item);
item.setPosition(new_position);
this.invalidateItem(item);
};
SchemaDrawer.prototype.drawHighlight = function(point) {
this.ctx.save();
this.ctx.globalAlpha = 0.5;
this.ctx.strokeStyle = "red";
this.ctx.lineWidth = 0.1;
this.ctx.beginPath();
this.ctx.arc(point.x, point.y, 0.1, 0, 2 * Math.PI);
this.ctx.stroke()
this.ctx.restore();
};
SchemaDrawer.prototype.addHighlight = function(point) {
this.highlight = point;
this.invalidateRectangle(BoxFromPointAndSize(point.minus(new Point(0.2, 0.2)), {width: 0.4, height: 0.4}));
};
SchemaDrawer.prototype.removeHighlight = function(point) {
this.highlight = null;
this.invalidateRectangle(BoxFromPointAndSize(point.minus(new Point(0.2, 0.2)), {width: 0.4, height: 0.4}));
};
SchemaDrawer.prototype.addTempConnection = function(connection) {
this.temp_connection = connection;
};
SchemaDrawer.prototype.removeTempConnection = function() {
this.invalidateRectangle(this.temp_connection.boundingBox().expand(0.1));
this.temp_connection = null;
};
SchemaDrawer.prototype.setSelectionBox = function(box) {
if (box) {
this.selection_box = box;
} else if (this.selection_box) {
delete this.selection_box;
}
};