Commits

Eugene Veretennikov committed 51d287e

SkeletonTest port

Comments (0)

Files changed (3)

projectSkeletonTest.nmml

 
     <meta title="spinehx - SkeletonTest" package="org.bitbucket.nitrobin.spinehx.SkeletonTest" version="1.0.0" company="spinehx" />
     <app main="spinehx.SkeletonTest" path="out" file="spinehx-SkeletonTest" />
-    <window width="320" height="480" fps="200" unless="mobile"/>
+    <window width="640" height="480" fps="200" unless="mobile"/>
     <window fps="60" if="mobile"/>
     <source path="src" />
     <source path="src-test" />

src/spinehx/SkeletonRenderer.hx

         sprites = new ObjectMap<RegionAttachment, Sprite>();
     }
 
+    public function clearBuffers () {
+        for (s in sprites)s.visible = false;
+    }
+
+    // TODO fix flipx
     public function draw () {
         graphics.clear();
 		var drawOrder:Array<Slot> = skeleton.drawOrder;
 				regionAttachment.updateVertices(slot);
 				var vertices:Array<Float> = regionAttachment.getVertices();
 
-                var sprite:Sprite = get(regionAttachment);
+                var wrapper:Sprite = get(regionAttachment);
+
+                var region:AtlasRegion = cast regionAttachment.getRegion();
                 var bone:Bone = slot.getBone();
+                var x:Float = regionAttachment.x - region.offsetX;
+                var y:Float = regionAttachment.y - region.offsetY;
+                wrapper.x = bone.worldX + x * bone.m00 + y * bone.m01;
+                wrapper.y = bone.worldY + x * bone.m10 + y * bone.m11;
+                wrapper.rotation = -(bone.worldRotation + regionAttachment.rotation);
+                wrapper.scaleX = bone.worldScaleX + regionAttachment.scaleX - 1;
+                wrapper.scaleY = bone.worldScaleY + regionAttachment.scaleY - 1;
 
-                var x1 = vertices[RegionAttachment.X1];
-                var y1 = vertices[RegionAttachment.Y1];
-                var x2 = vertices[RegionAttachment.X2];
-                var y2 = vertices[RegionAttachment.Y2];
-                // TODO optimize
-                sprite.x = x1;
-                sprite.y = y1;
-                sprite.rotation = Math.atan2(y2-y1, x2-x1) * 180 / Math.PI+90;
+                wrapper.visible = true;
             }
 		}
 	}
 
     public function get (regionAttachment:RegionAttachment):Sprite {
-        var sprite:Sprite = sprites.get(regionAttachment);
-        if(sprite == null){
-            var region:TextureRegion = regionAttachment.getRegion();
+        var wrapper:Sprite = sprites.get(regionAttachment);
+        if(wrapper == null){
+            var region:AtlasRegion = cast regionAttachment.getRegion();
             var texture:Texture = region.getTexture();
-            sprite = new Sprite();
-            var w:Int = region.getRegionWidth();
-            var h:Int = region.getRegionHeight();
-            var bd = new BitmapData(w, h);
-            bd.copyPixels(texture.bd,
-            new Rectangle(region.getRegionX(),region.getRegionY(),w, h), new Point(0,0));
-            var bmp = new Bitmap(bd);
-            bmp.y = -bd.height;
-            bmp.smoothing = true;
-            sprite.addChild(bmp);
 
-            addChild(sprite);
-            sprites.set(regionAttachment, sprite);
+            var bitmapData:BitmapData = texture.bd;
+            var regionData:BitmapData;
+            if (region.rotate) {
+                regionData = new BitmapData(region.getRegionHeight(), region.getRegionWidth());
+                regionData.copyPixels(bitmapData, //
+                new Rectangle(region.getRegionX(), region.getRegionY(), region.getRegionHeight(), region.getRegionWidth()), //
+                new Point());
+            } else {
+                regionData = new BitmapData(region.getRegionWidth(), region.getRegionHeight());
+                regionData.copyPixels(bitmapData, //
+                new Rectangle(region.getRegionX(), region.getRegionY(), region.getRegionWidth(), region.getRegionHeight()), //
+                new Point());
+            }
+
+            var bitmap:Bitmap = new Bitmap(regionData);
+            bitmap.smoothing = true;
+            bitmap.x = -regionAttachment.width / 2; // Registration point.
+            bitmap.y = -regionAttachment.height / 2;
+            if (region.rotate) {
+                bitmap.rotation = 90;
+                bitmap.x += region.getRegionWidth();
+            }
+
+            wrapper = new Sprite();
+            wrapper.addChild(bitmap);
+
+            sprites.set(regionAttachment, wrapper);
+            addChild(wrapper);
         }
-        return sprite;
+        return wrapper;
     }
     #end
 }

src/spinehx/attachments/RegionAttachment.hx

     public static inline var U4 = 18;
     public static inline var V4 = 19;
 
-   	private var region:TextureRegion;
-	private var x:Float = 0;
-    private var y:Float = 0;
-    private var scaleX:Float = 0;
-    private var scaleY:Float = 0;
-    private var rotation:Float = 0;
-    private var width:Float = 0;
-    private var height:Float = 0;
-	private var vertices:Array<Float>;
-	private var offset:Array<Float>;
+    public var region:TextureRegion;
+    public var x:Float = 0;
+    public var y:Float = 0;
+    public var scaleX:Float = 0;
+    public var scaleY:Float = 0;
+    public var rotation:Float = 0;
+    public var width:Float = 0;
+    public var height:Float = 0;
+    public var vertices:Array<Float>;
+    public var offset:Array<Float>;
 
 	public function new (name:String) {
 		super(name);