Skip to content
This repository was archived by the owner on May 15, 2022. It is now read-only.

Shape Export to Objective C

claus edited this page Aug 13, 2010 · 21 revisions

With as3swf you can automatically generate Objective-C source code (Core Graphics, Quartz) from SWF Shapes, to reuse those shapes in your iPhone projects. The CoreGraphics exporter generates ready-to-use Objective-C header (.h) and class (.m) files.


Example

The following example is a simple AIR application that loads a SWF and exports all contained shapes to Objective-C source code. Source files are written to a folder “as3swf” on the Desktop, named “Shape[x]View.h” and “Shape[x]View.m” (where [x] is the ShapeID).

package 
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.filesystem.File;
  import flash.filesystem.FileMode;
  import flash.filesystem.FileStream;
  import flash.net.URLLoader;
  import flash.net.URLLoaderDataFormat;
  import flash.net.URLRequest;
  import flash.utils.ByteArray;
  
  import com.codeazur.as3swf.SWF;
  import com.codeazur.as3swf.tags.ITag;
  import com.codeazur.as3swf.tags.TagDefineShape;
  import com.codeazur.as3swf.exporters.CoreGraphicsShapeExportDocumentHandler;

  public class Test extends Sprite
  {
    public function Test()
    {
      var request:URLRequest = new URLRequest("any.swf");
      var loader:URLLoader = new URLLoader();
      loader.dataFormat = URLLoaderDataFormat.BINARY;
      loader.addEventListener(Event.COMPLETE, completeHandler);
      loader.load(request);
    }

    private function completeHandler(e:Event):void {
      var swf:SWF = new SWF(URLLoader(e.target).data as ByteArray);
      for (var i:uint = 0; i < swf.tags.length; i++) {
        var tag:ITag = swf.tags[i];
        if (tag is TagDefineShape) {
          var defineShape:TagDefineShape = tag as TagDefineShape;
          var docHandler:CoreGraphicsShapeExportDocumentHandler = 
            new CoreGraphicsShapeExportDocumentHandler(
              "Shape" + defineShape.shapeId + "View",
              "Claus Wahlers",
              "côdeazur brasil"
            );
          defineShape.export(docHandler);
          writeSource(docHandler);
        }
      }
    }

    private function writeSource(docHandler:CoreGraphicsShapeExportDocumentHandler):void {
      var fileName:String = "as3swf" + File.separator + docHandler.className;
      var h:File = File.desktopDirectory.resolvePath(fileName + ".h");
      var m:File = File.desktopDirectory.resolvePath(fileName + ".m");
      var stream:FileStream = new FileStream();
      stream.open(h, FileMode.WRITE);
      stream.writeUTFBytes(docHandler.h);
      stream.close();
      stream.open(m, FileMode.WRITE);
      stream.writeUTFBytes(docHandler.m);
      stream.close();
    }
  }
}

A Simple Demo

Here’s a simple demo of a SWF whose shapes get exported to Objective-C:

This SWF contains only one shape (the côdeazur logo):

[SWF] Version: 10, FileLength: 1791, FileLengthCompressed: 1021, FrameSize: (320,203), FrameRate: 60, FrameCount: 1, Tags: 8
  Tags:
    [69:FileAttributes] AS3: true, HasMetadata: true, UseDirectBlit: false, UseGPU: false, UseNetwork: false
    [77:Metadata] 
    [09:SetBackgroundColor] Color: ffffffff
    [86:DefineSceneAndFrameLabelData] 
      Scenes:
        [0] Offset: 0, Name: Scene 1
    [22:DefineShape2] ID: 1, Bounds: (622,5776,620,3452)
      FillStyles:
        [1] [SWFFillStyle] Type: 0 (solid), Color: ff000000
      ShapeRecords:
        [SWFShapeRecordStyleChange] MoveTo: 2644,764, FillStyle0: 1
        [SWFShapeRecordCurvedEdge] ControlDelta: -288,-138, AnchorDelta: -324,0
        [SWFShapeRecordCurvedEdge] ControlDelta: -585,0, AnchorDelta: -411,414
        [SWFShapeRecordCurvedEdge] ControlDelta: -414,411, AnchorDelta: 0,585
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,585, AnchorDelta: 414,414
        [SWFShapeRecordCurvedEdge] ControlDelta: 411,411, AnchorDelta: 585,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 324,0, AnchorDelta: 288,-138
        [SWFShapeRecordCurvedEdge] ControlDelta: 279,-135, AnchorDelta: 198,-240
        [SWFShapeRecordCurvedEdge] ControlDelta: -192,-261, AnchorDelta: -66,-312
        [SWFShapeRecordCurvedEdge] ControlDelta: -99,252, AnchorDelta: -222,156
        [SWFShapeRecordCurvedEdge] ControlDelta: -231,159, AnchorDelta: -279,0
        [SWFShapeRecordCurvedEdge] ControlDelta: -369,0, AnchorDelta: -261,-261
        [SWFShapeRecordCurvedEdge] ControlDelta: -261,-261, AnchorDelta: 0,-369
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-369, AnchorDelta: 261,-261
        [SWFShapeRecordCurvedEdge] ControlDelta: 261,-261, AnchorDelta: 369,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 279,0, AnchorDelta: 231,159
        [SWFShapeRecordCurvedEdge] ControlDelta: 222,156, AnchorDelta: 99,252
        [SWFShapeRecordCurvedEdge] ControlDelta: 66,-312, AnchorDelta: 192,-261
        [SWFShapeRecordCurvedEdge] ControlDelta: -198,-240, AnchorDelta: -279,-135
        [SWFShapeRecordStyleChange] MoveTo: 0,0, FillStyle0: 0, FillStyle1: 0, LineStyle: 0
          New FillStyles:
            [1] [SWFFillStyle] Type: 0 (solid), Color: ff86b9e1
        [SWFShapeRecordStyleChange] MoveTo: 2374,1694, FillStyle0: 1
        [SWFShapeRecordCurvedEdge] ControlDelta: 144,141, AnchorDelta: 0,201
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,201, AnchorDelta: -144,141
        [SWFShapeRecordCurvedEdge] ControlDelta: -141,144, AnchorDelta: -201,0
        [SWFShapeRecordCurvedEdge] ControlDelta: -201,0, AnchorDelta: -141,-144
        [SWFShapeRecordCurvedEdge] ControlDelta: -144,-141, AnchorDelta: 0,-201
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-201, AnchorDelta: 144,-141
        [SWFShapeRecordCurvedEdge] ControlDelta: 141,-144, AnchorDelta: 201,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 201,0, AnchorDelta: 141,144
        [SWFShapeRecordStyleChange] MoveTo: 2575,1493
        [SWFShapeRecordCurvedEdge] ControlDelta: -222,-225, AnchorDelta: -321,0
        [SWFShapeRecordCurvedEdge] ControlDelta: -321,0, AnchorDelta: -225,225
        [SWFShapeRecordCurvedEdge] ControlDelta: -225,225, AnchorDelta: 0,318
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,318, AnchorDelta: 225,225
        [SWFShapeRecordCurvedEdge] ControlDelta: 225,225, AnchorDelta: 321,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 321,0, AnchorDelta: 222,-225
        [SWFShapeRecordCurvedEdge] ControlDelta: 225,-222, AnchorDelta: 0,-321
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-318, AnchorDelta: -225,-225
        [SWFShapeRecordStyleChange] MoveTo: 0,0, FillStyle0: 0, FillStyle1: 0, LineStyle: 0
          New FillStyles:
            [1] [SWFFillStyle] Type: 0 (solid), Color: ff000000
        [SWFShapeRecordStyleChange] MoveTo: 5776,2030, FillStyle0: 1
        [SWFShapeRecordCurvedEdge] ControlDelta: -3,-582, AnchorDelta: -414,-414
        [SWFShapeRecordCurvedEdge] ControlDelta: -414,-414, AnchorDelta: -585,0
        [SWFShapeRecordCurvedEdge] ControlDelta: -588,0, AnchorDelta: -414,414
        [SWFShapeRecordCurvedEdge] ControlDelta: -414,414, AnchorDelta: 0,588
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,585, AnchorDelta: 414,414
        [SWFShapeRecordCurvedEdge] ControlDelta: 411,414, AnchorDelta: 585,3
        [SWFShapeRecordStraightEdge] Horizontal: 783
        [SWFShapeRecordStraightEdge] Vertical: -522
        [SWFShapeRecordStraightEdge] Horizontal: -780
        [SWFShapeRecordCurvedEdge] ControlDelta: -369,-3, AnchorDelta: -261,-261
        [SWFShapeRecordCurvedEdge] ControlDelta: -261,-261, AnchorDelta: 0,-369
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-369, AnchorDelta: 264,-261
        [SWFShapeRecordCurvedEdge] ControlDelta: 261,-264, AnchorDelta: 369,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 369,0, AnchorDelta: 261,261
        [SWFShapeRecordCurvedEdge] ControlDelta: 261,261, AnchorDelta: 3,369
        [SWFShapeRecordStraightEdge] General: -3,1419
        [SWFShapeRecordStraightEdge] Horizontal: 525
        [SWFShapeRecordStraightEdge] Vertical: -1422
        [SWFShapeRecordStyleChange] MoveTo: 0,0, FillStyle0: 0, FillStyle1: 0, LineStyle: 0
          New FillStyles:
            [1] [SWFFillStyle] Type: 0 (solid), Color: ff86b9e1
        [SWFShapeRecordStyleChange] MoveTo: 4360,1259, FillStyle0: 1
        [SWFShapeRecordCurvedEdge] ControlDelta: -321,0, AnchorDelta: -228,228
        [SWFShapeRecordCurvedEdge] ControlDelta: -228,228, AnchorDelta: 0,321
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,321, AnchorDelta: 225,228
        [SWFShapeRecordCurvedEdge] ControlDelta: 228,228, AnchorDelta: 321,0
        [SWFShapeRecordStraightEdge] Horizontal: 780
        [SWFShapeRecordStraightEdge] Vertical: -780
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-321, AnchorDelta: -228,-228
        [SWFShapeRecordCurvedEdge] ControlDelta: -228,-225, AnchorDelta: -321,0
        [SWFShapeRecordStyleChange] MoveTo: 4852,2039
        [SWFShapeRecordStraightEdge] General: -3,12
        [SWFShapeRecordStraightEdge] Vertical: 477
        [SWFShapeRecordStraightEdge] Horizontal: -492
        [SWFShapeRecordCurvedEdge] ControlDelta: -204,-3, AnchorDelta: -141,-144
        [SWFShapeRecordCurvedEdge] ControlDelta: -144,-144, AnchorDelta: 0,-201
        [SWFShapeRecordCurvedEdge] ControlDelta: 0,-201, AnchorDelta: 144,-147
        [SWFShapeRecordCurvedEdge] ControlDelta: 147,-144, AnchorDelta: 201,0
        [SWFShapeRecordCurvedEdge] ControlDelta: 201,0, AnchorDelta: 144,144
        [SWFShapeRecordCurvedEdge] ControlDelta: 144,141, AnchorDelta: 3,204
        [SWFShapeRecordStraightEdge] Vertical: 6
        [SWFShapeRecordEnd]
    [26:PlaceObject2] Depth: 4, CharacterID: 1, Matrix: (1,1,0,0,0,0)
    [01:ShowFrame] 
    [00:End]

Exported to Objective-C we get the following source files:


The final result, integrated in a simple iPhone project:

Clone this wiki locally