forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCArmatureCanvasRenderCmd.js
190 lines (162 loc) · 7.46 KB
/
CCArmatureCanvasRenderCmd.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
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
(function(){
ccs.Armature.RenderCmd = {
_updateAnchorPointInPoint: function(){
var node = this._node;
var contentSize = node._contentSize, anchorPoint = node._anchorPoint, offsetPoint = node._offsetPoint;
this._anchorPointInPoints.x = contentSize.width * anchorPoint.x - offsetPoint.x;
this._anchorPointInPoints.y = contentSize.height * anchorPoint.y - offsetPoint.y;
this._realAnchorPointInPoints.x = contentSize.width * anchorPoint.x;
this._realAnchorPointInPoints.y = contentSize.height * anchorPoint.y;
this.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
},
getAnchorPointInPoints: function(){
return cc.p(this._realAnchorPointInPoints);
}
};
})();
(function(){
ccs.Armature.CanvasRenderCmd = function(renderableObject){
cc.Node.CanvasRenderCmd.call(this, renderableObject);
this._needDraw = true;
this._realAnchorPointInPoints = new cc.Point(0,0);
this._startRenderCmd = new cc.CustomRenderCmd(this, this._startCmdCallback);
this._RestoreRenderCmd = new cc.CustomRenderCmd(this, this._RestoreCmdCallback);
};
var proto = ccs.Armature.CanvasRenderCmd.prototype = Object.create(cc.Node.CanvasRenderCmd.prototype);
cc.inject(ccs.Armature.RenderCmd, proto);
proto.constructor = ccs.Armature.CanvasRenderCmd;
proto._startCmdCallback = function(ctx, scaleX, scaleY){
var node = this._node, parent = node._parent;
this.transform(parent ? parent._renderCmd : null);
var wrapper = ctx || cc._renderContext;
wrapper.save();
//set to armature mode
wrapper._switchToArmatureMode(true, this._worldTransform, scaleX, scaleY);
};
proto.transform = function(parentCmd, recursive){
ccs.Node.CanvasRenderCmd.prototype.transform.call(this, parentCmd, recursive);
var locChildren = this._node._children;
for (var i = 0, len = locChildren.length; i< len; i++) {
var selBone = locChildren[i];
if (selBone && selBone.getDisplayRenderNode) {
var selNode = selBone.getDisplayRenderNode();
if (selNode && selNode._renderCmd){
var cmd = selNode._renderCmd;
cmd.transform(null); //must be null, use transform in armature mode
//update displayNode's color and opacity, because skin didn't call visit()
var parentColor = selBone._renderCmd._displayedColor, parentOpacity = selBone._renderCmd._displayedOpacity;
var flags = cc.Node._dirtyFlags, locFlag = cmd._dirtyFlag;
var colorDirty = locFlag & flags.colorDirty,
opacityDirty = locFlag & flags.opacityDirty;
if(colorDirty)
cmd._updateDisplayColor(parentColor);
if(opacityDirty)
cmd._updateDisplayOpacity(parentOpacity);
}
}
}
};
proto._RestoreCmdCallback = function(wrapper){
this._cacheDirty = false;
//wrapper.restore();
wrapper._switchToArmatureMode(false);
wrapper.restore();
};
proto.initShaderCache = function(){};
proto.setShaderProgram = function(){};
proto.updateChildPosition = function(dis, bone){
dis.visit();
// cc.renderer.pushRenderCommand(dis._renderCmd);
};
proto.rendering = function(ctx, scaleX, scaleY){
var node = this._node;
var locChildren = node._children;
var alphaPremultiplied = cc.BlendFunc.ALPHA_PREMULTIPLIED, alphaNonPremultipled = cc.BlendFunc.ALPHA_NON_PREMULTIPLIED;
for (var i = 0, len = locChildren.length; i< len; i++) {
var selBone = locChildren[i];
if (selBone && selBone.getDisplayRenderNode) {
var selNode = selBone.getDisplayRenderNode();
if (null === selNode)
continue;
switch (selBone.getDisplayRenderNodeType()) {
case ccs.DISPLAY_TYPE_SPRITE:
if(selNode instanceof ccs.Skin)
this.updateChildPosition(selNode, selBone, alphaPremultiplied, alphaNonPremultipled);
break;
case ccs.DISPLAY_TYPE_ARMATURE:
selNode._renderCmd.rendering(ctx, scaleX, scaleY);
break;
default:
selNode.visit(this);
break;
}
} else if(selBone instanceof cc.Node) {
this._visitNormalChild(selBone);
//selBone.visit(this);
}
}
};
proto._visitNormalChild = function(childNode){
if(childNode == null)
return;
var cmd = childNode._renderCmd;
// quick return if not visible
if (!childNode._visible)
return;
cmd._curLevel = this._curLevel + 1;
//visit for canvas
var i, children = childNode._children, child;
cmd._syncStatus(this);
//because armature use transform, not setTransform
cmd.transform(null);
var len = children.length;
if (len > 0) {
childNode.sortAllChildren();
// draw children zOrder < 0
for (i = 0; i < len; i++) {
child = children[i];
if (child._localZOrder < 0)
child._renderCmd.visit(cmd);
else
break;
}
cc.renderer.pushRenderCommand(cmd);
for (; i < len; i++)
children[i]._renderCmd.visit(cmd);
} else {
cc.renderer.pushRenderCommand(cmd);
}
this._dirtyFlag = 0;
};
proto.visit = function(parentCmd){
var node = this._node;
// quick return if not visible. children won't be drawn.
if (!node._visible)
return;
this.updateStatus(parentCmd);
node.sortAllChildren();
cc.renderer.pushRenderCommand(this._startRenderCmd);
this.rendering();
cc.renderer.pushRenderCommand(this._RestoreRenderCmd);
this._cacheDirty = false;
};
})();