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 = ccs.DisplayFactory || ccs.Class.extend({});
 30 ccs.DisplayFactory.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 ccs.DisplayFactory.createDisplay = function (bone, decoDisplay) {
 46     switch (decoDisplay.getDisplayData().displayType) {
 47         case ccs.DISPLAY_TYPE_SPRITE:
 48             this.createSpriteDisplay(bone, decoDisplay);
 49             break;
 50         case ccs.DISPLAY_TYPE_PARTICLE:
 51             this.createParticleDisplay(bone, decoDisplay);
 52             break;
 53         case ccs.DISPLAY_TYPE_ARMATURE:
 54             this.createArmatureDisplay(bone, decoDisplay);
 55             break;
 56         default:
 57             break;
 58     }
 59 };
 60 ccs.DisplayFactory._helpTransform =  {a:1, b:0, c:0, d:1, tx:0, ty:0};
 61 ccs.DisplayFactory.updateDisplay = function (bone,dt, dirty) {
 62     var display = bone.getDisplayRenderNode();
 63     if(!display)
 64         return;
 65 
 66     switch (bone.getDisplayRenderNodeType()) {
 67         case ccs.DISPLAY_TYPE_SPRITE:
 68             if (dirty){
 69                 display.updateArmatureTransform();
 70             }
 71             break;
 72         case ccs.DISPLAY_TYPE_PARTICLE:
 73             this.updateParticleDisplay(bone, display, dt);
 74             break;
 75         case ccs.DISPLAY_TYPE_ARMATURE:
 76             this.updateArmatureDisplay(bone, display, dt);
 77             break;
 78         default:
 79             var transform = bone.getNodeToArmatureTransform();
 80             display.setAdditionalTransform(transform);
 81             break;
 82     }
 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 =  node.getAnchorPointInPoints();
 99                 anchorPoint = cc.PointApplyAffineTransform(anchorPoint, helpTransform);
100                 helpTransform.tx = anchorPoint.x;
101                 helpTransform.ty = anchorPoint.y;
102                 var t = cc.AffineTransformConcat(helpTransform, bone.getArmature().nodeToParentTransform());
103                 detector.updateTransform(t);
104             }
105         }
106     }
107 
108 };
109 ccs.DisplayFactory.addSpriteDisplay = function (bone, decoDisplay, displayData) {
110     var sdp = new ccs.SpriteDisplayData();
111     sdp.copy(displayData);
112     decoDisplay.setDisplayData(sdp);
113     this.createSpriteDisplay(bone, decoDisplay);
114 };
115 
116 ccs.DisplayFactory.createSpriteDisplay = function (bone, decoDisplay) {
117     var skin = null;
118     var displayData = decoDisplay.getDisplayData();
119     //! remove .xxx
120     var textureName = displayData.displayName;
121     var startPos = textureName.lastIndexOf(".");
122     if (startPos != -1) {
123         textureName = textureName.substring(0, startPos);
124     }
125     //! create display
126     if (textureName == "") {
127         skin = ccs.Skin.create();
128     }
129     else {
130         skin = ccs.Skin.createWithSpriteFrameName(textureName + ".png");
131     }
132     decoDisplay.setDisplay(skin);
133     skin.setBone(bone);
134     this.initSpriteDisplay(bone, decoDisplay, displayData.displayName, skin);
135     var armature = bone.getArmature();
136     if (armature) {
137         if (armature.getArmatureData().dataVersion >= ccs.CONST_VERSION_COMBINED)
138             skin.setSkinData(displayData.skinData);
139         else
140             skin.setSkinData(bone.getBoneData());
141     }
142 
143 
144 };
145 
146 ccs.DisplayFactory.initSpriteDisplay = function(bone, decoDisplay, displayName, skin){
147     var textureName = displayName;
148     var startPos = textureName.lastIndexOf(".");
149     if (startPos != -1) {
150         textureName = textureName.substring(0, startPos);
151     }
152     var textureData = ccs.armatureDataManager.getTextureData(textureName);
153     if (textureData) {
154         //! Init display anchorPoint, every Texture have a anchor point
155         skin.setAnchorPoint(textureData.pivotX, textureData.pivotY);
156     }
157     if (ccs.ENABLE_PHYSICS_CHIPMUNK_DETECT || ccs.ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX) {
158         if (textureData && textureData.contourDataList.length > 0)        {
159             var colliderDetector = ccs.ColliderDetector.create(bone);
160             colliderDetector.addContourDataList(textureData.contourDataList);
161             decoDisplay.setColliderDetector(colliderDetector);
162         }
163     }
164 };
165 
166 ccs.DisplayFactory.addArmatureDisplay = function (bone, decoDisplay, displayData) {
167     var adp = new ccs.ArmatureDisplayData();
168     adp.copy(displayData);
169     decoDisplay.setDisplayData(adp);
170 
171     this.createArmatureDisplay(bone, decoDisplay);
172 };
173 ccs.DisplayFactory.createArmatureDisplay = function (bone, decoDisplay) {
174     var displayData = decoDisplay.getDisplayData();
175     var armature = ccs.Armature.create(displayData.displayName, bone);
176     decoDisplay.setDisplay(armature);
177 };
178 ccs.DisplayFactory.updateArmatureDisplay = function (bone, armature, dt) {
179     if (armature) {
180         armature.sortAllChildren();
181         armature.update(dt);
182     }
183 };
184 
185 ccs.DisplayFactory.addParticleDisplay = function (bone, decoDisplay, displayData) {
186     var adp = new ccs.ParticleDisplayData();
187     adp.copy(displayData);
188     decoDisplay.setDisplayData(adp);
189     this.createParticleDisplay(bone, decoDisplay);
190 };
191 ccs.DisplayFactory.createParticleDisplay = function (bone, decoDisplay) {
192     var displayData = decoDisplay.getDisplayData();
193     var system = cc.ParticleSystem.create(displayData.displayName);
194 
195     system.removeFromParent();
196     system.cleanup();
197 
198     var armature = bone.getArmature();
199     if (armature)    {
200         system.setParent(bone.getArmature());
201     }
202 
203     decoDisplay.setDisplay(system);
204 };
205 ccs.DisplayFactory.updateParticleDisplay = function (bone, particleSystem, dt) {
206     var node = new ccs.BaseData();
207     ccs.TransformHelp.matrixToNode(bone.nodeToArmatureTransform(), node);
208     particleSystem.setPosition(node.x, node.y);
209     particleSystem.setScaleX(node.scaleX);
210     particleSystem.setScaleY(node.scaleY);
211     particleSystem.update(dt);
212 };
213