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             display.setAdditionalTransform(bone.nodeToArmatureTransform());
 80             break;
 81     }
 82 
 83     if (ccs.ENABLE_PHYSICS_CHIPMUNK_DETECT || ccs.ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX) {
 84         if (dirty) {
 85             var decoDisplay = bone.getDisplayManager().getCurrentDecorativeDisplay();
 86             var detector = decoDisplay.getColliderDetector();
 87             if (detector) {
 88                 var node = decoDisplay.getDisplay();
 89                 var displayTransform = node.nodeToParentTransform();
 90                 var helpTransform = this._helpTransform;
 91                 helpTransform.a = displayTransform.a;
 92                 helpTransform.b = displayTransform.b;
 93                 helpTransform.c = displayTransform.c;
 94                 helpTransform.d = displayTransform.d;
 95                 helpTransform.tx = displayTransform.tx;
 96                 helpTransform.ty = displayTransform.ty;
 97                 var anchorPoint =  node.getAnchorPointInPoints();
 98                 anchorPoint = cc.PointApplyAffineTransform(anchorPoint, 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 ccs.DisplayFactory.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 ccs.DisplayFactory.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     }
124     //! create display
125     if (textureName == "") {
126         skin = ccs.Skin.create();
127     }
128     else {
129         skin = ccs.Skin.createWithSpriteFrameName(textureName + ".png");
130     }
131     decoDisplay.setDisplay(skin);
132     skin.setBone(bone);
133     this.initSpriteDisplay(bone, decoDisplay, displayData.displayName, skin);
134     var armature = bone.getArmature();
135     if (armature) {
136         if (armature.getArmatureData().dataVersion >= ccs.CONST_VERSION_COMBINED)
137             skin.setSkinData(displayData.skinData);
138         else
139             skin.setSkinData(bone.getBoneData());
140     }
141 
142 
143 };
144 
145 ccs.DisplayFactory.initSpriteDisplay = function(bone, decoDisplay, displayName, skin){
146     var textureName = displayName;
147     var startPos = textureName.lastIndexOf(".");
148     if (startPos != -1) {
149         textureName = textureName.substring(0, startPos);
150     }
151     var textureData = ccs.armatureDataManager.getTextureData(textureName);
152     if (textureData) {
153         //! Init display anchorPoint, every Texture have a anchor point
154         skin.setAnchorPoint(textureData.pivotX, textureData.pivotY);
155     }
156     if (ccs.ENABLE_PHYSICS_CHIPMUNK_DETECT || ccs.ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX) {
157         if (textureData && textureData.contourDataList.length > 0)        {
158             var colliderDetector = ccs.ColliderDetector.create(bone);
159             colliderDetector.addContourDataList(textureData.contourDataList);
160             decoDisplay.setColliderDetector(colliderDetector);
161         }
162     }
163 };
164 
165 ccs.DisplayFactory.addArmatureDisplay = function (bone, decoDisplay, displayData) {
166     var adp = new ccs.ArmatureDisplayData();
167     adp.copy(displayData);
168     decoDisplay.setDisplayData(adp);
169 
170     this.createArmatureDisplay(bone, decoDisplay);
171 };
172 ccs.DisplayFactory.createArmatureDisplay = function (bone, decoDisplay) {
173     var displayData = decoDisplay.getDisplayData();
174     var armature = ccs.Armature.create(displayData.displayName, bone);
175     decoDisplay.setDisplay(armature);
176 };
177 ccs.DisplayFactory.updateArmatureDisplay = function (bone, armature, dt) {
178     if (armature) {
179         armature.sortAllChildren();
180         armature.update(dt);
181     }
182 };
183 
184 ccs.DisplayFactory.addParticleDisplay = function (bone, decoDisplay, displayData) {
185     var adp = new ccs.ParticleDisplayData();
186     adp.copy(displayData);
187     decoDisplay.setDisplayData(adp);
188     this.createParticleDisplay(bone, decoDisplay);
189 };
190 ccs.DisplayFactory.createParticleDisplay = function (bone, decoDisplay) {
191     var displayData = decoDisplay.getDisplayData();
192     var system = cc.ParticleSystem.create(displayData.displayName);
193     var armature = bone.getArmature();
194     if (armature)    {
195         system.setParent(bone.getArmature());
196     }
197     decoDisplay.setDisplay(system);
198 };
199 ccs.DisplayFactory.updateParticleDisplay = function (bone, particleSystem, dt) {
200     var node = new ccs.BaseData();
201     ccs.TransformHelp.matrixToNode(bone.nodeToArmatureTransform(), node);
202     particleSystem.setPosition(node.x, node.y);
203     particleSystem.setScaleX(node.scaleX);
204     particleSystem.setScaleY(node.scaleY);
205     particleSystem.update(dt);
206 };
207