IncrementalDisplayable.js
4.31 KB
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
/**
* Displayable for incremental rendering. It will be rendered in a separate layer
* IncrementalDisplay have too main methods. `clearDisplayables` and `addDisplayables`
* addDisplayables will render the added displayables incremetally.
*
* It use a not clearFlag to tell the painter don't clear the layer if it's the first element.
*/
import { inherits } from '../core/util';
import Displayble from './Displayable';
import BoundingRect from '../core/BoundingRect';
// TODO Style override ?
function IncrementalDisplayble(opts) {
Displayble.call(this, opts);
this._displayables = [];
this._temporaryDisplayables = [];
this._cursor = 0;
this.notClear = true;
}
IncrementalDisplayble.prototype.incremental = true;
IncrementalDisplayble.prototype.clearDisplaybles = function () {
this._displayables = [];
this._temporaryDisplayables = [];
this._cursor = 0;
this.dirty();
this.notClear = false;
};
IncrementalDisplayble.prototype.addDisplayable = function (displayable, notPersistent) {
if (notPersistent) {
this._temporaryDisplayables.push(displayable);
}
else {
this._displayables.push(displayable);
}
this.dirty();
};
IncrementalDisplayble.prototype.addDisplayables = function (displayables, notPersistent) {
notPersistent = notPersistent || false;
for (var i = 0; i < displayables.length; i++) {
this.addDisplayable(displayables[i], notPersistent);
}
};
IncrementalDisplayble.prototype.eachPendingDisplayable = function (cb) {
for (var i = this._cursor; i < this._displayables.length; i++) {
cb && cb(this._displayables[i]);
}
for (var i = 0; i < this._temporaryDisplayables.length; i++) {
cb && cb(this._temporaryDisplayables[i]);
}
};
IncrementalDisplayble.prototype.update = function () {
this.updateTransform();
for (var i = this._cursor; i < this._displayables.length; i++) {
var displayable = this._displayables[i];
// PENDING
displayable.parent = this;
displayable.update();
displayable.parent = null;
}
for (var i = 0; i < this._temporaryDisplayables.length; i++) {
var displayable = this._temporaryDisplayables[i];
// PENDING
displayable.parent = this;
displayable.update();
displayable.parent = null;
}
};
IncrementalDisplayble.prototype.brush = function (ctx, prevEl) {
// Render persistant displayables.
for (var i = this._cursor; i < this._displayables.length; i++) {
var displayable = this._displayables[i];
displayable.beforeBrush && displayable.beforeBrush(ctx);
displayable.brush(ctx, i === this._cursor ? null : this._displayables[i - 1]);
displayable.afterBrush && displayable.afterBrush(ctx);
}
this._cursor = i;
// Render temporary displayables.
for (var i = 0; i < this._temporaryDisplayables.length; i++) {
var displayable = this._temporaryDisplayables[i];
displayable.beforeBrush && displayable.beforeBrush(ctx);
displayable.brush(ctx, i === 0 ? null : this._temporaryDisplayables[i - 1]);
displayable.afterBrush && displayable.afterBrush(ctx);
}
this._temporaryDisplayables = [];
this.notClear = true;
};
var m = [];
IncrementalDisplayble.prototype.getBoundingRect = function () {
if (!this._rect) {
var rect = new BoundingRect(Infinity, Infinity, -Infinity, -Infinity);
for (var i = 0; i < this._displayables.length; i++) {
var displayable = this._displayables[i];
var childRect = displayable.getBoundingRect().clone();
if (displayable.needLocalTransform()) {
childRect.applyTransform(displayable.getLocalTransform(m));
}
rect.union(childRect);
}
this._rect = rect;
}
return this._rect;
};
IncrementalDisplayble.prototype.contain = function (x, y) {
var localPos = this.transformCoordToLocal(x, y);
var rect = this.getBoundingRect();
if (rect.contain(localPos[0], localPos[1])) {
for (var i = 0; i < this._displayables.length; i++) {
var displayable = this._displayables[i];
if (displayable.contain(x, y)) {
return true;
}
}
}
return false;
};
inherits(IncrementalDisplayble, Displayble);
export default IncrementalDisplayble;