public VexObject Convert(SwfCompilationUnit swf) { this.swf = swf; this.v = new VexObject(swf.Name); Log = new StringBuilder(); RootTimeline = new Timeline(v.NextId()); RootTimeline.Name = "_root"; this.curTimeline = RootTimeline; curTimeline.FrameCount = swf.Header.FrameCount; curTimeline.Duration = GetDuration(swf.Header.FrameCount); v.Root = curTimeline; this.curDepthChart = rootDepthChart; this.ParseHeader(); foreach(ISwfTag tag in swf.Tags) { this.ParseTag(tag); } WriteEndTimes(); return v; }
public void CalculateBounds() { if (definition is Vex.Timeline) { Vex.Timeline vt = (Vex.Timeline)definition; Vex.Rectangle bounds = Vex.Rectangle.Empty; float top = int.MaxValue; float left = int.MaxValue; uint[] instances = vt.GetInstanceIds(); foreach (uint id in instances) { DesignInstance inst = MainForm.CurrentInstanceManager[id]; if (inst != null && inst.Instance.GetTransformAtTime(0) != null) { if (bounds.IsEmpty) { bounds = inst.StrokeBounds; } else { bounds = inst.StrokeBounds.Union(bounds);// Rectangle.Union(bounds, inst.Bounds); } left = Math.Min(left, inst.Location.X); top = Math.Min(top, inst.Location.Y); } } this.Definition.StrokeBounds = bounds; } }
//public uint Duration; //public uint FrameCount = 0; //public List<Label> Labels = new List<Label>(); public DrawTimeline(Timeline tl) { this.Id = tl.Id; this.Name = tl.Name; this.StrokeBounds = tl.StrokeBounds; this.Instances = tl.Instances; }
public void RenderInto(Vex.IDefinition def, uint frame, Graphics graphics) { this.g = graphics; if (def is Vex.Timeline) { Vex.Timeline tl = (Vex.Timeline)def; Vex.Timeline namedSymbol = GetNamedSymbol(tl); if (namedSymbol != null)// && HasSymbols(namedSymbol)) { RenderFrame(namedSymbol, frame); } } else if (def is Vex.Symbol) { Vex.Symbol sy = (Vex.Symbol)def; RenderFrame(sy, 0); } else if (def is Vex.Text) { Vex.Text tx = (Vex.Text)def; if (tx.TextRuns.Count > 0 && !tx.TextRuns[0].isEditable) { RenderFrame(tx, 0); } } else if (def is Vex.Image) { Bitmap sourceBmp = stage.BitmapCache.GetBitmap(def); DrawImage(sourceBmp); } }
private void DrawTimeline(Vex.Timeline tl) { for (int i = 0; i < tl.InstanceCount; i++) { Vex.IDefinition idef = stage.Library[tl.InstanceAt(i).DefinitionId].Definition; if (idef is Vex.Symbol) { DrawSymbol((Vex.Symbol)idef); } else if (idef is Vex.Timeline) { DrawTimeline((Vex.Timeline)idef); } } }
public bool HasSymbols(Vex.Timeline tl) { bool result = false; for (int i = 0; i < tl.InstanceCount; i++) { Vex.IDefinition idef = stage.Library[tl.InstanceAt(i).DefinitionId].Definition; if (idef is Vex.Symbol) { result = true; break; } } return(result); }
// filtered meaning remove annotations like box2d bounding boxes private void DrawFilteredTimeline(Vex.Timeline tl, uint frame) { for (int i = 0; i < tl.InstanceCount; i++) { Vex.Instance inst = (Vex.Instance)tl.InstanceAt(i); if (inst.GetTransformAtTime(frame) != null) { Vex.IDefinition idef = stage.Library[inst.DefinitionId].Definition; if (idef is Vex.Symbol || idef is Vex.Timeline) { GraphicsState gs = g.Save(); using (Matrix nm = inst.GetTransformAtTime(0).Matrix.GetDrawing2DMatrix()) { nm.Multiply(g.Transform, MatrixOrder.Append); g.Transform = nm; if (idef is Vex.Symbol) { DrawFilteredSymbol((Vex.Symbol)idef); } else { DrawFilteredTimeline((Vex.Timeline)idef, frame); } } g.Restore(gs); } else if (idef is Vex.Text) { DrawText((Vex.Text)idef, inst.Transformations[0].Matrix); } else if (idef is Vex.Image) { Matrix pm = g.Transform.Clone(); Matrix nm = inst.GetTransformAtTime(0).Matrix.GetDrawing2DMatrix(); nm.Multiply(pm, MatrixOrder.Append); g.Transform = nm; DrawImage(stage.BitmapCache.GetBitmap(idef)); g.Transform = pm; } } } }
private Vex.Timeline GetNamedSymbol(Vex.Timeline tl) { Vex.Timeline result = null; if (tl.Name != "") { result = tl; } else { for (int i = 0; i < tl.InstanceCount; i++) { Vex.IDefinition idef = stage.Library[tl.InstanceAt(i).DefinitionId].Definition; if (idef is Vex.Timeline && ((Vex.Timeline)idef).Name != "") { result = (Vex.Timeline)idef; } } } return(result); }
private Timeline GetNamedSymbol(VexObject v, Timeline tl) { Timeline result = null; if (tl.Name != "") { result = tl; } else { for (int i = 0; i < tl.Instances.Count; i++) { uint defId = tl.Instances[i].DefinitionId; if (v.Definitions[defId] is Timeline && ((Timeline)v.Definitions[defId]).Name != "") { result = (Timeline)(v.Definitions[defId]); } } } return result; }
private void DrawTimeline(VexObject v, Timeline tl) { for (int i = 0; i < tl.Instances.Count; i++) { IDefinition def = v.Definitions[tl.Instances[i].DefinitionId]; if (def is Symbol) { DrawSymbol((Symbol)def); } else if (def is Timeline) { DrawTimeline(v, (Timeline)def); } } }
private void DrawFilteredTimeline(VexObject v, Timeline tl) { for (int i = 0; i < tl.Instances.Count; i++) { Instance inst = (Instance)tl.Instances[i]; IDefinition def = v.Definitions[inst.DefinitionId]; if (def is Symbol) { DrawFilteredSymbol((Symbol)def); } else if (def is Timeline) { DrawFilteredTimeline(v, (Timeline)def); } else if (def is Text) { DrawText((Text)def, inst.Transformations[0].Matrix); } } }
public bool HasSymbols(VexObject v, Timeline tl) { bool result = false; for (int i = 0; i < tl.Instances.Count; i++) { if (v.Definitions[tl.Instances[i].DefinitionId] is Symbol) { result = true; break; } } return result; }
public LibraryItem[] AddSwf(string path) { List <LibraryItem> result = new List <LibraryItem>(); if (File.Exists(path)) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { BinaryReader br = new BinaryReader(fs); string name = Path.GetFileNameWithoutExtension(path); SwfReader r = new SwfReader(br.ReadBytes((int)fs.Length)); SwfCompilationUnit scu = new SwfCompilationUnit(r); if (scu.IsValid) { SwfToVex s2v = new SwfToVex(); Vex.VexObject v = s2v.Convert(scu); DateTime creationTime = File.GetLastWriteTimeUtc(path); string swfName = Path.GetFileName(path); string wf = MainForm.CurrentStage.WorkingFolderFull; bool inWorkingFolder = path.IndexOf(wf) == 0; string wp = inWorkingFolder ? path.Substring(wf.Length) : swfName; string workingPath = wp.Substring(0, wp.Length - swfName.Length); TreeNode existingNode = symbolTree.GetNode(workingPath, swfName); bool isUpdatingSwf = (existingNode != null); LibraryItem li; Dictionary <uint, uint> translator = new Dictionary <uint, uint>(); List <uint> recycledInstIds = new List <uint>(); foreach (uint key in v.Definitions.Keys) { Vex.IDefinition loadedDef = v.Definitions[key]; if (scu.SymbolClasses.ContainsKey(key)) { loadedDef.Name = scu.SymbolClasses[key]; } uint loadedId = loadedDef.Id; bool isTimeline = loadedDef is Vex.Timeline; LibraryItem existingLi = null; if (isUpdatingSwf) { loadedDef.HasSaveableChanges = true; existingLi = isTimeline ? MainForm.CurrentLibrary.GetLibraryItem(workingPath + swfName, loadedDef.Name) : MainForm.CurrentLibrary.GetByOriginalSourceId(loadedDef.Id); } if (existingLi != null) { loadedDef.Id = existingLi.DefinitionId; translator[loadedId] = loadedDef.Id; existingLi.Date = creationTime; if (isTimeline) { Vex.Timeline orgTl = (Vex.Timeline)existingLi.Definition; Vex.IInstance[] instances = orgTl.Instances.ToArray(); for (int i = 0; i < instances.Length; i++) { recycledInstIds.Add(instances[i].InstanceHash); orgTl.RemoveInstance(instances[i]); MainForm.CurrentStage.InstanceManager.RemoveInstance(instances[i].InstanceHash); } } MainForm.CurrentStage.vexObject.Definitions[loadedDef.Id] = loadedDef; existingLi.Definition = loadedDef; } else { li = MainForm.CurrentStage.CreateLibraryItem(loadedDef, false); li.Date = creationTime; li.LibraryPath = workingPath + swfName; li.OriginalSourceId = loadedId; translator[loadedId] = li.Definition.Id; if (isTimeline) { result.Add(li); AddItemToLibrary(li); } } if (isTimeline) { Vex.Timeline tl = (Vex.Timeline)loadedDef; for (int i = 0; i < tl.InstanceCount; i++) { Vex.IInstance inst = tl.InstanceAt(i); if (recycledInstIds.Count > 0) { inst.InstanceHash = recycledInstIds[0]; recycledInstIds.RemoveAt(0); } inst.DefinitionId = translator[inst.DefinitionId]; inst.ParentDefinitionId = translator[inst.ParentDefinitionId]; MainForm.CurrentStage.CreateInstance((Vex.Instance)inst); } } } } } } return(result.ToArray()); }
private void ParseDefineSpriteTag(DefineSpriteTag tag) { uint rootFrame = curFrame; curFrame = 0; curTimeline = new Timeline(v.NextId()); curTimeline.FrameCount = tag.FrameCount; curTimeline.Id = tag.SpriteId; curTimeline.Duration = GetDuration(tag.FrameCount); v.Definitions.Add(curTimeline.Id, curTimeline); curDepthChart = new SortedDictionary<uint, Instance>(); foreach (ISwfTag t in tag.ControlTags) { this.ParseTag(t); } WriteEndTimes(); curTimeline.StrokeBounds = ParseSpriteBounds(tag); curTimeline = RootTimeline; curDepthChart = rootDepthChart; curFrame = rootFrame; }
public List <Bitmap> RenderDefinitionFrames(Vex.IDefinition def, uint startFrame, uint endFrame, Matrix m) { List <Bitmap> bmpFrames = new List <Bitmap>(); if (def is Vex.Timeline) { Vex.Timeline tl = (Vex.Timeline)def; Vex.Timeline namedSymbol = GetNamedSymbol(tl); if (namedSymbol != null)// && HasSymbols(namedSymbol)) { for (uint i = startFrame; i < endFrame; i++) { m.Translate(-namedSymbol.StrokeBounds.Point.X, -namedSymbol.StrokeBounds.Point.Y); Bitmap myBitmap = CreateRenderBitmap(namedSymbol.StrokeBounds, m); RenderFrame(namedSymbol, i); bmpFrames.Add(myBitmap); } } } else if (def is Vex.Symbol) { Vex.Symbol sy = (Vex.Symbol)def; m.Translate(-sy.StrokeBounds.Point.X, -sy.StrokeBounds.Point.Y); Bitmap myBitmap = CreateRenderBitmap(sy.StrokeBounds, m); RenderFrame(sy, 0); bmpFrames.Add(myBitmap); } else if (def is Vex.Text) { Vex.Text tx = (Vex.Text)def; if (tx.TextRuns.Count > 0 && !tx.TextRuns[0].isEditable) { Bitmap myBitmap = CreateRenderBitmap(tx.StrokeBounds, m); RenderFrame(tx, 0); bmpFrames.Add(myBitmap); } } else if (def is Vex.Image) { Bitmap myBitmap; m.Translate(-def.StrokeBounds.Point.X, -def.StrokeBounds.Point.Y); if (m.IsScaledOrSheared()) { myBitmap = CreateRenderBitmap(def.StrokeBounds, m); Bitmap sourceBmp = stage.BitmapCache.GetBitmap(def); using (Graphics gr = Graphics.FromImage(myBitmap)) { gr.DrawImage(sourceBmp, 0, 0, myBitmap.Width, myBitmap.Height); } } else { myBitmap = stage.BitmapCache.GetBitmap(def); } bmpFrames.Add(myBitmap); } if (g != null) { g.Dispose(); } return(bmpFrames); }
public override void WriteTimelineDefiniton(Timeline timeline, bool isRoot) { if (isRoot) { //Instance rootInst = new Instance(); //rootInst.DefinitionId = timeline.Id; //rootInst.Depth = 0; //rootInst.EndTime = timeline.Duration; //rootInst.Name = "_root"; //rootInst.InstanceID = 0; //timelineStack.Push(rootInst); xw.OpenRootTag(); } else { xw.OpenTimelineTag(instancePrefix + instName);//timelinePrefix + timeline.Id); Instance inst = timelineStack.Peek(); if (inst.Transformations.Count > 0 && inst.Transformations[0].Matrix == Matrix.Empty) { xw.WriteAttributeString("Canvas.Left", "0"); xw.WriteAttributeString("Canvas.Top", "0"); } else { xw.WriteAttributeString("Canvas.Left", (-timeline.StrokeBounds.Point.X).ToString()); xw.WriteAttributeString("Canvas.Top", (-timeline.StrokeBounds.Point.Y).ToString()); } } timeline.Instances.Sort(); WriteStoryboards(timeline.Instances, isRoot); WriteInstances(timeline.Instances, isRoot); xw.CloseFrameTag(); }
public abstract void WriteTimelineDefiniton(Timeline timeline, bool isRoot);
public override void WriteTimelineDefiniton(Timeline timeline, bool isRoot) { if (isRoot) { xw.OpenRootTag(); } else { string tlName = timelinePrefix + instName; xw.OpenTimelineTag(tlName);//timelinePrefix + timeline.Id); Instance inst = timelineStack.Peek(); if (inst.Transformations.Count > 0 && inst.Transformations[0].Matrix == Matrix.Empty) { xw.WriteAttributeString("Canvas.Left", "0"); xw.WriteAttributeString("Canvas.Top", "0"); } else { xw.WriteAttributeString("Canvas.Left", (-timeline.StrokeBounds.Point.X).ToString()); xw.WriteAttributeString("Canvas.Top", (-timeline.StrokeBounds.Point.Y).ToString()); } WriteTransformsDefs(timeline, inst, tlName); WriteStoryboard(timeline, inst, tlName); } timeline.Instances.Sort(); NumberInstances(timeline.Instances, isRoot); WriteInstances(timeline.Instances, isRoot); xw.CloseFrameTag(); }
private void ParseTimeline(Timeline t) { List<IInstance> insts = t.Instances; foreach (IInstance instance in insts) { if (!(instance is Instance)) { continue; } Instance inst = (Instance)instance; if (inst.Name == null) { inst.Name = "$inst_" + instAutoNumber++; } IDefinition def = curVo.Definitions[inst.DefinitionId]; DefinitionKind dk = (DefinitionKind)def.UserData; if ((dk & DefinitionKind.Ignore) != 0) { continue; } if ((dk & DefinitionKind.JointMarker) != 0) { V2DJointKind jointKind = Enum.Parse(V2DJointKind, def.Name, true); ParseJoint(jointKind, inst); } if ((dk & DefinitionKind.ShapeMarker) != 0) { } if ((dk & DefinitionKind.TextField) != 0) { Text txt = (Text)def; } if ((dk & DefinitionKind.Timeline) != 0) { V2DDefinition vd = CreateDefinition(inst, def); V2DInstance vi = CreateInstance(inst, vd); parentStack.Push(vi); ParseTimeline((Timeline)def); parentStack.Pop(); } if ((dk & DefinitionKind.Symbol) != 0) { V2DInstance vi = CreateInstance(inst, def); } if ((dk & DefinitionKind.Vex2D) != 0) { // todo: this is just adding images and/or instances Body2D b2d = CreateDefinition(inst, def); b2d.AddShapes(curVo, def, inst); } } GenerateJointData(); }
private void ParseTimeline(Timeline t) { List<IInstance> insts = t.Instances; foreach (IInstance instance in insts) { if (!(instance is Instance)) { continue; } Instance inst = (Instance)instance; if (!curVo.Definitions.ContainsKey(inst.DefinitionId)) { throw new KeyNotFoundException("Defs have no key: " + inst.DefinitionId + " named: " + inst.Name); } if (inst.Name == null) { inst.Name = "$" + instAutoNumber++ + "$"; } IDefinition def = curVo.Definitions[inst.DefinitionId]; EnsureDefinition(inst, def); } GenerateJointData(parentStack.Peek().Definition); }
private static void LoadInstances(string folderPath, VexObject vo) { List <string> dataPaths = new List <string>(); List <string> types = new List <string>(); uint instanceIdCounter = 1; string libFileName = folderPath + Path.DirectorySeparatorChar + "Instances.xml"; FileStream fs = new FileStream(libFileName, FileMode.Open); XmlTextReader r = new XmlTextReader(fs); r.WhitespaceHandling = WhitespaceHandling.None; r.ReadStartElement("Instances"); do { if (r.IsStartElement()) { switch (r.Name) { case "InstanceIdCounter": if (r.Read()) { instanceIdCounter = uint.Parse(r.Value.Trim(), NumberStyles.Any); r.Read(); } break; case "InstanceItems": while (r.Read()) { if (r.IsStartElement() && r.Name == "Item") { dataPaths.Add(r.GetAttribute("DataPath")); types.Add(r.GetAttribute("Type")); } } break; default: r.Read(); break; } } }while (r.Read()); r.Close(); fs.Close(); // read actual DesignInstance Items for (int i = 0; i < dataPaths.Count; i++) { string dp = folderPath + Path.DirectorySeparatorChar + dataPaths[i]; string type = types[i]; IInstance inst = LoadFromPath(type, dp); if (vo.Definitions.ContainsKey(inst.ParentDefinitionId)) { Timeline parent = (Timeline)vo.Definitions[inst.ParentDefinitionId]; parent.AddInstance(inst); } } }