1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 /**
 27  * @ignore
 28  */
 29 ccs.displayFactory = {
 30     addDisplay: function (bone, decoDisplay, displayData) {
 31         switch (displayData.displayType) {
 32             case ccs.DISPLAY_TYPE_SPRITE:
 33                 this.addSpriteDisplay(bone, decoDisplay, displayData);
 34                 break;
 35             case ccs.DISPLAY_TYPE_PARTICLE:
 36                 this.addParticleDisplay(bone, decoDisplay, displayData);
 37                 break;
 38             case ccs.DISPLAY_TYPE_ARMATURE:
 39                 this.addArmatureDisplay(bone, decoDisplay, displayData);
 40                 break;
 41             default:
 42                 break;
 43         }
 44     },
 45 
 46     createDisplay: function (bone, decoDisplay) {
 47         switch (decoDisplay.getDisplayData().displayType) {
 48             case ccs.DISPLAY_TYPE_SPRITE:
 49                 this.createSpriteDisplay(bone, decoDisplay);
 50                 break;
 51             case ccs.DISPLAY_TYPE_PARTICLE:
 52                 this.createParticleDisplay(bone, decoDisplay);
 53                 break;
 54             case ccs.DISPLAY_TYPE_ARMATURE:
 55                 this.createArmatureDisplay(bone, decoDisplay);
 56                 break;
 57             default:
 58                 break;
 59         }
 60     },
 61 
 62     _helpTransform: {a:1, b:0, c:0, d:1, tx:0, ty:0},
 63     updateDisplay: function (bone,dt, dirty) {
 64         var display = bone.getDisplayRenderNode();
 65         if(!display)
 66             return;
 67 
 68         switch (bone.getDisplayRenderNodeType()) {
 69             case ccs.DISPLAY_TYPE_SPRITE:
 70                 if (dirty)
 71                     display.updateArmatureTransform();
 72                 break;
 73             case ccs.DISPLAY_TYPE_PARTICLE:
 74                 this.updateParticleDisplay(bone, display, dt);
 75                 break;
 76             case ccs.DISPLAY_TYPE_ARMATURE:
 77                 this.updateArmatureDisplay(bone, display, dt);
 78                 break;
 79             default:
 80                 var transform = bone.getNodeToArmatureTransform();
 81                 display.setAdditionalTransform(transform);
 82                 break;
 83         }
 84         if (ccs.ENABLE_PHYSICS_CHIPMUNK_DETECT || ccs.ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX) {
 85             if (dirty) {
 86                 var decoDisplay = bone.getDisplayManager().getCurrentDecorativeDisplay();
 87                 var detector = decoDisplay.getColliderDetector();
 88                 if (detector) {
 89                     var node = decoDisplay.getDisplay();
 90                     var displayTransform = node.nodeToParentTransform();
 91                     var helpTransform = this._helpTransform;
 92                     helpTransform.a = displayTransform.a;
 93                     helpTransform.b = displayTransform.b;
 94                     helpTransform.c = displayTransform.c;
 95                     helpTransform.d = displayTransform.d;
 96                     helpTransform.tx = displayTransform.tx;
 97                     helpTransform.ty = displayTransform.ty;
 98                     var anchorPoint = cc.pointApplyAffineTransform(node.getAnchorPointInPoints(), helpTransform);
 99                     helpTransform.tx = anchorPoint.x;
100                     helpTransform.ty = anchorPoint.y;
101                     var t = cc.affineTransformConcat(helpTransform, bone.getArmature().nodeToParentTransform());
102                     detector.updateTransform(t);
103                 }
104             }
105         }
106     },
107 
108     addSpriteDisplay: function (bone, decoDisplay, displayData) {
109         var sdp = new ccs.SpriteDisplayData();
110         sdp.copy(displayData);
111         decoDisplay.setDisplayData(sdp);
112         this.createSpriteDisplay(bone, decoDisplay);
113     },
114 
115     createSpriteDisplay: function (bone, decoDisplay) {
116         var skin = null;
117         var displayData = decoDisplay.getDisplayData();
118         //! remove .xxx
119         var textureName = displayData.displayName;
120         var startPos = textureName.lastIndexOf(".");
121         if (startPos != -1)
122             textureName = textureName.substring(0, startPos);
123         //! create display
124         if (textureName == "")
125             skin = ccs.Skin.create();
126         else
127             skin = ccs.Skin.createWithSpriteFrameName(textureName + ".png");
128 
129         decoDisplay.setDisplay(skin);
130 
131         if(skin == null)
132             return;
133 
134         skin.setBone(bone);
135         this.initSpriteDisplay(bone, decoDisplay, displayData.displayName, skin);
136 
137         var armature = bone.getArmature();
138         if (armature) {
139             if (armature.getArmatureData().dataVersion >= ccs.CONST_VERSION_COMBINED)
140                 skin.setSkinData(displayData.skinData);
141             else
142                 skin.setSkinData(bone.boneData);
143         }
144     },
145 
146     initSpriteDisplay: function (bone, decoDisplay, displayName, skin) {
147         //! remove .xxx
148         var textureName = displayName;
149         var startPos = textureName.lastIndexOf(".");
150 
151         if (startPos != -1)
152             textureName = textureName.substring(0, startPos);
153 
154         var textureData = ccs.armatureDataManager.getTextureData(textureName);
155         if (textureData) {
156             //! Init display anchorPoint, every Texture have a anchor point
157             skin.setAnchorPoint(cc.p(textureData.pivotX, textureData.pivotY));
158         }
159 
160         if (ccs.ENABLE_PHYSICS_CHIPMUNK_DETECT || ccs.ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX) {
161             if (textureData && textureData.contourDataList.length > 0) {
162                 //! create ContourSprite
163                 var colliderDetector = ccs.ColliderDetector.create(bone);
164                 colliderDetector.addContourDataList(textureData.contourDataList);
165                 decoDisplay.setColliderDetector(colliderDetector);
166             }
167         }
168     },
169 
170     addArmatureDisplay: function (bone, decoDisplay, displayData) {
171         var adp = new ccs.ArmatureDisplayData();
172         adp.copy(displayData);
173         decoDisplay.setDisplayData(adp);
174 
175         this.createArmatureDisplay(bone, decoDisplay);
176     },
177 
178     createArmatureDisplay: function (bone, decoDisplay) {
179         var displayData = decoDisplay.getDisplayData();
180         var armature = ccs.Armature.create(displayData.displayName, bone);
181         decoDisplay.setDisplay(armature);
182     },
183 
184     updateArmatureDisplay: function (bone, armature, dt) {
185         if (armature) {
186             armature.sortAllChildren();
187             armature.update(dt);
188         }
189     },
190 
191     addParticleDisplay: function (bone, decoDisplay, displayData) {
192         var adp = new ccs.ParticleDisplayData();
193         adp.copy(displayData);
194         decoDisplay.setDisplayData(adp);
195         this.createParticleDisplay(bone, decoDisplay);
196     },
197 
198     createParticleDisplay: function (bone, decoDisplay) {
199         var displayData = decoDisplay.getDisplayData();
200         var system = cc.ParticleSystem.create(displayData.displayName);
201 
202         system.removeFromParent();
203         system.cleanup();
204 
205         var armature = bone.getArmature();
206         if (armature)
207             system.setParent(bone.getArmature());
208 
209         decoDisplay.setDisplay(system);
210     },
211 
212     updateParticleDisplay: function (bone, particleSystem, dt) {
213         var node = new ccs.BaseData();
214         ccs.TransformHelp.matrixToNode(bone.nodeToArmatureTransform(), node);
215         particleSystem.setPosition(node.x, node.y);
216         particleSystem.setScaleX(node.scaleX);
217         particleSystem.setScaleY(node.scaleY);
218         particleSystem.update(dt);
219     }
220 };