forked from robterrell/cocos2d-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchNode.js
254 lines (201 loc) · 7.17 KB
/
BatchNode.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'use strict'
var util = require('util'),
events = require('events'),
geo = require('geometry'),
ccp = geo.ccp,
TextureAtlas = require('../TextureAtlas').TextureAtlas,
RenderTexture = require('./RenderTexture').RenderTexture,
Node = require('./Node').Node
/**
* @class
* Draws all children to an in-memory canvas and only redraws when something changes
*
* @memberOf cocos.nodes
* @extends cocos.nodes.Node
*
* @opt {geometry.Size} size The size of the in-memory canvas used for drawing to
* @opt {Boolean} [partialDraw=false] Draw only the area visible on screen. Small maps may be slower in some browsers if this is true.
*/
function BatchNode (opts) {
BatchNode.superclass.constructor.call(this, opts)
var size = opts.size || geo.sizeMake(1, 1)
this.partialDraw = opts.partialDraw
events.addPropertyListener(this, 'contentSize', 'change', this._resizeCanvas.bind(this))
this._dirtyRects = []
this.contentRect = geo.rectMake(0, 0, size.width, size.height)
this.renderTexture = new RenderTexture(size)
this.renderTexture.sprite.isRelativeAnchorPoint = false
this.addChild({child: this.renderTexture})
}
BatchNode.inherit(Node, /** @lends cocos.nodes.BatchNode# */ {
partialDraw: false,
contentRect: null,
renderTexture: null,
dirty: true,
/**
* Region to redraw
* @type geometry.Rect
*/
dirtyRegion: null,
dynamicResize: false,
/** @private
* Areas that need redrawing
*
* Not implemented
*/
_dirtyRects: null,
addChild: function (opts) {
BatchNode.superclass.addChild.call(this, opts)
var child = opts.child,
z = opts.z
if (child == this.renderTexture) {
return
}
// TODO handle texture resize
// Watch for changes in child
events.addListener(child, 'drawdirty', function (oldBox) {
if (oldBox) {
this.addDirtyRegion(oldBox)
}
this.addDirtyRegion(child.boundingBox)
}.bind(this))
this.addDirtyRegion(child.boundingBox)
},
removeChild: function (opts) {
BatchNode.superclass.removeChild.call(this, opts)
// TODO remove isTransformDirty and visible property listeners
this.dirty = true
},
addDirtyRegion: function (rect) {
// Increase rect slightly to compensate for subpixel artifacts
rect = new geo.Rect(Math.floor(rect.origin.x) - 1, Math.floor(rect.origin.y) - 1,
Math.ceil(rect.size.width) + 2 ,Math.ceil(rect.size.height) + 2)
var region = this.dirtyRegion
if (!region) {
region = rect
} else {
region = geo.rectUnion(region, rect)
}
this.dirtyRegion = region
this.dirty = true
},
_resizeCanvas: function (oldSize) {
var size = this.contentSize
if (geo.sizeEqualToSize(size, oldSize)) {
return; // No change
}
this.renderTexture.contentSize = size
this.dirty = true
},
update: function () {
},
visit: function (context) {
if (!this.visible) {
return
}
context.save()
this.transform(context)
var rect = this.dirtyRegion
// Only redraw if something changed
if (this.dirty) {
if (rect) {
if (this.partialDraw) {
// Clip region to visible area
var s = require('../Director').Director.sharedDirector.winSize,
p = this.position
var r = new geo.Rect(
0, 0,
s.width, s.height
)
r = geo.rectApplyAffineTransform(r, this.worldToNodeTransform())
rect = geo.rectIntersection(r, rect)
}
this.renderTexture.clear(rect)
this.renderTexture.context.save()
this.renderTexture.context.beginPath()
this.renderTexture.context.rect(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
this.renderTexture.context.clip()
this.renderTexture.context.closePath()
} else {
this.renderTexture.clear()
}
for (var i = 0, childLen = this.children.length; i < childLen; i++) {
var c = this.children[i]
if (c == this.renderTexture) {
continue
}
// Draw children inside rect
if (!rect || geo.rectOverlapsRect(c.boundingBox, rect)) {
c.visit(this.renderTexture.context, rect)
}
}
if (SHOW_REDRAW_REGIONS) {
if (rect) {
this.renderTexture.context.beginPath()
this.renderTexture.context.rect(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
this.renderTexture.context.fillStyle = "rgba(0, 0, 255, 0.5)"
this.renderTexture.context.fill()
this.renderTexture.context.closePath()
}
}
if (rect) {
this.renderTexture.context.restore()
}
this.dirty = false
this.dirtyRegion = null
}
this.renderTexture.visit(context)
context.restore()
},
draw: function (ctx) {
},
onEnter: function () {
BatchNode.superclass.onEnter.call(this)
if (this.partialDraw) {
events.addPropertyListener(this.parent, 'isTransformDirty', 'change', function () {
var box = this.visibleRect
this.addDirtyRegion(box)
}.bind(this))
}
}
})
/**
* @class
* A BatchNode that accepts only Sprite using the same texture
*
* @memberOf cocos.nodes
* @extends cocos.nodes.BatchNode
*
* @opt {String} file (Optional) Path to image to use as sprite atlas
* @opt {Texture2D} texture (Optional) Texture to use as sprite atlas
* @opt {cocos.TextureAtlas} textureAtlas (Optional) TextureAtlas to use as sprite atlas
*/
function SpriteBatchNode (opts) {
SpriteBatchNode.superclass.constructor.call(this, opts)
var file = opts.file,
textureAtlas = opts.textureAtlas,
texture = opts.texture
if (file || texture) {
textureAtlas = new TextureAtlas({file: file, texture: texture})
}
this.textureAtlas = textureAtlas
// FIXME This listener needs to be added/remove onEnter/onExit to avoid memory leaks
events.addPropertyListener(this, 'opacity', 'change', function () {
for (var i = 0, len = this.children.length; i < len; i++) {
var child = this.children[i]
child.opacity = this.opacity
}
}.bind(this))
}
SpriteBatchNode.inherit(BatchNode, /** @lends cocos.nodes.SpriteBatchNode# */ {
textureAtlas: null,
/**
* @type cocos.Texture2D
*/
get texture () {
return this.textureAtlas ? this.textureAtlas.texture : null
}
})
exports.BatchNode = BatchNode
exports.SpriteBatchNode = SpriteBatchNode
// vim:et:st=4:fdm=marker:fdl=0:fdc=1