private void dealfile(StreamReader reader, ref int element) { string row; string[] tmp = null; SBelement tmpe; string Position = ""; row = reader.ReadLine(); while (!reader.EndOfStream) { if (row.Trim() == "") { row = reader.ReadLine(); continue; } if (row.StartsWith("//") || row.Length == 0) { row = reader.ReadLine(); continue; } if (row.StartsWith("[")) { Position = row.Substring(1, row.Length - 2); row = reader.ReadLine(); continue; } switch (Position) { case "Variables": { SBvar tmpvar = new SBvar(); tmpvar.name = row.Split(new char[] { '=' }, 2)[0]; tmpvar.replace = row.Split(new char[] { '=' }, 2)[1]; tmpvar.name.Substring(1, tmpvar.name.Length - 1); Variables.Add(tmpvar); row = reader.ReadLine(); break; } case "Events": //do variables change first foreach (SBvar tmpvar in Variables) { if (row.Contains(tmpvar.name)) { row.Replace(tmpvar.name, tmpvar.replace); } } if (row.StartsWith("Sample") || row.StartsWith("5,")) { //Sample,time,layer,"filepath",volume tmp = row.Split(new char[] { ',' }); tmpe = new SBelement(); tmpe.Type = ElementType.Sample; tmpe.Layers = (ElementLayer)(System.Enum.Parse(typeof(ElementLayer), tmp[2])); tmpe.Path = tmp[3].Substring(1, tmp[3].Length - 2); Elements.Add(tmpe); element++; SBEvent tmpev = new SBEvent(); tmpev.startT = Convert.ToInt32(tmp[1]); tmpev.Type = EventType.Play; if (tmp.Length < 5) { tmpev.volume = 100; } else { tmpev.volume = Convert.ToInt32(tmp[4]); } row = reader.ReadLine(); // elements[element].events.Add(tmpev); } else if (row.StartsWith("Animation") || row.StartsWith("6,")) { //Animation,"layer","origin","filepath",x,y,frameCount,frameDelay,looptype tmp = row.Split(new char[] { ',' }); tmpe = new SBelement(); tmpe.Type = ElementType.Animation; tmpe.Layers = (ElementLayer)(System.Enum.Parse(typeof(ElementLayer), tmp[1])); if (tmpe.Layers == ElementLayer.Fail) { row = reader.ReadLine(); continue; } tmpe.Origin = (ElementOrigin)(System.Enum.Parse(typeof(ElementOrigin), tmp[2])); tmpe.Path = tmp[3].Substring(1, tmp[3].Length - 2); tmpe.x = Convert.ToInt32(tmp[4]); tmpe.y = Convert.ToInt32(tmp[5]); tmpe.FrameCount = Convert.ToInt32(tmp[6]); tmpe.Framedelay = (int)(Convert.ToDouble(tmp[7])); tmpe.Looptype = (ElementLoopType)(System.Enum.Parse(typeof(ElementLoopType), tmp[8])); Elements.Add(tmpe); element++; row = Dealevents(element, reader); if (Elements[element].F.Count != 0 && Elements[element].F[0].Value == 0) { } else { Elements[element].F.Insert(0, new TActionNode(Elements[element].Starttime - 1, 255f, 4)); } Elements[element].F.Add(new TActionNode(Elements[element].Lasttime + 1, 0f, 4)); } else if (row.StartsWith("Sprite") || row.StartsWith("4,")) { //Sprite,"layer","origin","filepath",x,y tmp = row.Split(new char[] { ',' }); tmpe = new SBelement(); tmpe.Type = ElementType.Sprite; tmpe.Layers = (ElementLayer)(System.Enum.Parse(typeof(ElementLayer), tmp[1])); if (tmpe.Layers == ElementLayer.Fail) { row = reader.ReadLine(); continue; } tmpe.Origin = (ElementOrigin)(System.Enum.Parse(typeof(ElementOrigin), tmp[2])); tmpe.Path = tmp[3].Substring(1, tmp[3].Length - 2); tmpe.x = Convert.ToInt32(tmp[4]); tmpe.y = Convert.ToInt32(tmp[5]); if (File.Exists(Path.Combine(location, tmpe.Path))) { Elements.Add(tmpe); element++; row = Dealevents(element, reader); if (Elements[element].F.Count != 0 && Elements[element].F[0].Value == 0) { } else { Elements[element].F.Insert(0, new TActionNode(Elements[element].Starttime - 1, 255f, 4)); } Elements[element].F.Add(new TActionNode(Elements[element].Lasttime + 1, 0f, 4)); } else { row = reader.ReadLine(); } } else if (row.StartsWith("0,")) { tmp = row.Split(new char[] { ',' }); tmpe = new SBelement(); tmpe.Type = ElementType.Sprite; tmpe.Layers = ElementLayer.Background; tmpe.Origin = ElementOrigin.TopLeft; tmpe.Path = tmp[2].Substring(1, tmp[2].Length - 2); if (File.Exists(Path.Combine(location, tmpe.Path))) { Bitmap tmpbm = new Bitmap(Path.Combine(location, tmpe.Path)); float BGScale = 640f / tmpbm.Width < 480f / tmpbm.Height ? 640f / tmpbm.Width : 480f / tmpbm.Height; Elements.Add(tmpe); element++; row = Dealevents(element, reader); Elements[element].F.Insert(0, new TActionNode(0, 255f, 4)); Elements[element].SX.Insert(0, new TActionNode(0, BGScale, 4)); Elements[element].SY.Insert(0, new TActionNode(0, BGScale, 4)); } else { row = reader.ReadLine(); } } else { row = reader.ReadLine(); } break; default: row = reader.ReadLine(); break; } } }
public TGraphic(Device graphicDevice, SBelement Element, string Location, int layerdelta) : base() { switch (Element.Type) { case ElementType.Sprite: { if (File.Exists(Path.Combine(Location, Element.Path))) { using (FileStream s = new FileStream(Path.Combine(Location, Element.Path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { this.texturearray = new Texture2D[1] { Texture2D.FromFile(graphicDevice, s) }; } } else { this.texturearray = new Texture2D[1] { new Texture2D(graphicDevice, 1, 1, 0, 0, SurfaceFormat.Bgr32) }; } this.frameCount = 1; this.currentFrameIndex = 0; this.msLastFrame = 0; this.mSPerFrame = 16; this.position = new Vector2(Element.x, Element.y); this.origin = Getorigin(this.texturearray[0], Element.Origin); break; } case ElementType.Animation: { string prefix = Path.Combine(Location, Element.Path); string ext = prefix.Substring(prefix.LastIndexOf(".") + 1); prefix = prefix.Substring(0, prefix.LastIndexOf(".")); texturearray = new Texture2D[Element.FrameCount]; for (int i = 0; i < Element.FrameCount; i++) { if (File.Exists(prefix + i.ToString() + "." + ext)) { using (FileStream s = new FileStream(prefix + i.ToString() + "." + ext, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { this.texturearray[i] = Texture2D.FromFile(graphicDevice, s); } } else { this.texturearray[i] = new Texture2D(graphicDevice, 1, 1, 0, 0, SurfaceFormat.Bgr32); } } this.frameCount = Element.FrameCount; this.mSPerFrame = Element.Framedelay; this.Loop = Element.Looptype; this.currentFrameIndex = 0; this.msLastFrame = 0; this.position = new Vector2(Element.x, Element.y); //this.texture = texturearray[0]; this.origin = Getorigin(this.texturearray[0], Element.Origin); break; } default: throw (new FormatException("Failed to resolve .osb file")); } this.Origin = Element.Origin; this.color = Color.White; this.alpha = 0; this.parameter = 0; this.rotate = 0f; if (Element.Layers == ElementLayer.Background) { this.layer = 0.9f - layerdelta * 0.000001f; } else { this.layer = 0.5f - layerdelta * 0.000001f; } this.scale = new Vector2(1f, 1f); // this.InitSpriteAction(); }
private void Dealfile(StreamReader reader, ref int element) { var position = ""; var row = reader.ReadLine(); while (!reader.EndOfStream) { if (row == null) { throw new Exception(); } if (row.Trim() == "") { row = reader.ReadLine(); continue; } if (row.StartsWith("//") || row.Length == 0) { row = reader.ReadLine(); continue; } if (row.StartsWith("[")) { position = row.Substring(1, row.Length - 2); row = reader.ReadLine(); continue; } switch (position) { case "Variables": { var name = row.Split(new[] { '=' }, 2)[0]; var replace = row.Split(new[] { '=' }, 2)[1]; name = name.Substring(1, name.Length - 1); _variables.Add(name, replace); row = reader.ReadLine(); break; } case "Events": //do variables change first foreach (var replace in _variables) { if (row.Contains(replace.Key)) { row = row.Replace(replace.Key, replace.Value); } } if (row.StartsWith("Sample") || row.StartsWith("5,")) { //Sample,time,layer,"filepath",volume //var tmp = row.Split(new[] { ',' }); //var path = tmp[3].Substring(1, tmp[3].Length - 2); //element++; /*var tmpe = new SBelement * { * Type = ElementType.Sample, * Layers = (ElementLayer)(Enum.Parse(typeof(ElementLayer), tmp[2])), * }; * var tmpev = new SBEvent * { * startT = Convert.ToInt32(tmp[1]), * Type = EventType.Play, * volume = tmp.Length < 5 ? 100 : Convert.ToInt32(tmp[4]) * }; * if (Elements.ContainsKey(path)) * { * //tmpe = Elements[path]; * } * //else * //{ * //Elements.Add(path, tmpe); * //}*/ row = reader.ReadLine(); // elements[element].events.Add(tmpev); } else if (row.StartsWith("Animation") || row.StartsWith("6,")) { //Animation,"layer","origin","filepath",x,y,frameCount,frameDelay,looptype var tmp = row.Split(new[] { ',' }); var path = tmp[3].Substring(1, tmp[3].Length - 2); var tmpe = new SBelement { Type = ElementType.Animation, Layers = (ElementLayer)(Enum.Parse(typeof(ElementLayer), tmp[1])), Origin = (ElementOrigin)(Enum.Parse(typeof(ElementOrigin), tmp[2])), X0 = Convert.ToInt32(tmp[4]), Y0 = Convert.ToInt32(tmp[5]), Z = element, FrameCount = Convert.ToInt32(tmp[6]), Framedelay = (int)(Convert.ToDouble(tmp[7])), Looptype = (ElementLoopType)(Enum.Parse(typeof(ElementLoopType), tmp[8])) }; if (tmpe.Layers == ElementLayer.Fail) { row = reader.ReadLine(); continue; } if (!Elements.ContainsKey(path)) { Elements.Add(path, new SBFile()); } row = Dealevents(ref tmpe, reader); if (tmpe.F.Count != 0 && tmpe.F[0].Value == 0) { } else { tmpe.F.Insert(0, new TActionNode(tmpe.Starttime - 1, 255f, 4)); } tmpe.F.Add(new TActionNode(tmpe.Lasttime + 1, 0f, 4)); Elements[path].Element.Add(tmpe); element++; } else if (row.StartsWith("Sprite") || row.StartsWith("4,")) { //Sprite,"layer","origin","filepath",x,y var tmp = row.Split(new[] { ',' }); var path = tmp[3].Substring(1, tmp[3].Length - 2); var tmpe = new SBelement { Type = ElementType.Sprite, Layers = (ElementLayer)(Enum.Parse(typeof(ElementLayer), tmp[1])), Origin = (ElementOrigin)(Enum.Parse(typeof(ElementOrigin), tmp[2])), X0 = Convert.ToInt32(tmp[4]), Y0 = Convert.ToInt32(tmp[5]), Z = element }; if (tmpe.Layers == ElementLayer.Fail) { row = reader.ReadLine(); continue; } if (File.Exists(Path.Combine(_location, path))) { if (!Elements.ContainsKey(path)) { Elements.Add(path, new SBFile()); } row = Dealevents(ref tmpe, reader); if (tmpe.F.Count != 0 && tmpe.F[0].Value == 0) { } else { tmpe.F.Insert(0, new TActionNode(tmpe.Starttime - 1, 255f, 4)); } tmpe.F.Add(new TActionNode(tmpe.Lasttime + 1, 0f, 4)); Elements[path].Element.Add(tmpe); element++; } else { row = reader.ReadLine(); } } else if (row.StartsWith("0,")) { var tmp = row.Split(new[] { ',' }); var path = tmp[2].Substring(1, tmp[2].Length - 2); var tmpe = new SBelement { Type = ElementType.Sprite, Layers = ElementLayer.Background, Origin = ElementOrigin.TopLeft, }; if (File.Exists(Path.Combine(_location, path))) { if (!Elements.ContainsKey(path)) { Elements.Add(path, new SBFile()); } var tmpbm = new Bitmap(Path.Combine(_location, path)); var bgScale = Math.Min(640f / tmpbm.Width, 480f / tmpbm.Height); row = Dealevents(ref tmpe, reader); tmpe.F.Insert(0, new TActionNode(0, 255f, 4)); tmpe.SX.Insert(0, new TActionNode(0, bgScale, 4)); tmpe.SY.Insert(0, new TActionNode(0, bgScale, 4)); Elements[path].Element.Add(tmpe); element++; } else { row = reader.ReadLine(); } } else { row = reader.ReadLine(); } break; default: row = reader.ReadLine(); break; } } }
private void Dealevent(string str, ref SBelement element, int delta) { var tmp = str.Split(new[] { ',' }); var type = (EventType)Enum.Parse(typeof(EventType), tmp[0].Trim()); var easing = Convert.ToInt32(tmp[1]); var startT = Convert.ToInt32(tmp[2]) + delta; int endT; //②_M,0,1000,1000,320,240,320,240-->_M,0,1000,,320,240,320,240(开始结束时间相同) if (tmp[3] == "") { endT = startT; } else { endT = Convert.ToInt32(tmp[3]) + delta; } if (type == EventType.P) { switch (tmp[4]) { case "H": element.P.Add(new TActionNode(startT, 1, 3)); element.P.Add(new TActionNode(endT, 1, 4)); break; case "V": element.P.Add(new TActionNode(startT, 2, 3)); element.P.Add(new TActionNode(endT, 2, 4)); break; case "A": element.P.Add(new TActionNode(startT, 4, 3)); element.P.Add(new TActionNode(endT, 4, 4)); break; } return; } int point = 4; int time = startT; delta = endT - startT; if (element.Starttime > time) { element.Starttime = time; } if (element.Lasttime < endT) { element.Lasttime = endT; } while (point < tmp.Length) { switch (type) { case EventType.F: //③_M,0,1000,,320,240,320,240-->_M,0,1000,,320,240 (开始结束值相同) if (point != tmp.Length - 1) { element.F.Add(new TActionNode(time, Convert.ToSingle(tmp[point]) * 255, easing)); point++; time += delta; } else { element.F.Add(new TActionNode(time, Convert.ToSingle(tmp[point]) * 255, 4)); if (time < endT) { element.F.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]) * 255, 4)); } return; } break; case EventType.MX: if (point != tmp.Length - 1) { element.X.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), easing)); point++; time += delta; } else { element.X.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), 4)); if (time < endT) { element.X.Add(new TActionNode(endT, Convert.ToInt32(tmp[point]), 4)); } return; } break; case EventType.MY: if (point != tmp.Length - 1) { element.Y.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), easing)); point++; time += delta; } else { element.Y.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), 4)); if (time < endT) { element.Y.Add(new TActionNode(endT, Convert.ToInt32(tmp[point]), 4)); } return; } break; case EventType.M: if (point != tmp.Length - 2) { element.X.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), easing)); point++; element.Y.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), easing)); point++; time += delta; } else { element.X.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), 4)); if (time < endT) { element.X.Add(new TActionNode(endT, Convert.ToInt32(tmp[point]), 4)); } point++; element.Y.Add(new TActionNode(time, Convert.ToInt32(tmp[point]), 4)); if (time < endT) { element.Y.Add(new TActionNode(endT, Convert.ToInt32(tmp[point]), 4)); } return; } break; case EventType.S: if (point != tmp.Length - 1) { element.SX.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), easing)); element.SY.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), easing)); point++; time += delta; } else { element.SX.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), 4)); element.SY.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), 4)); if (time < endT) { element.SX.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]), 4)); element.SY.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]), 4)); } return; } break; case EventType.V: if (point != tmp.Length - 2) { element.SX.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), easing)); point++; element.SY.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), easing)); point++; time += delta; } else { element.SX.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), 4)); if (time < endT) { element.SX.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]), 4)); } point++; element.SY.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), 4)); if (time < endT) { element.SY.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]), 4)); } return; } break; case EventType.R: if (point != tmp.Length - 1) { element.R.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), easing)); point++; time += delta; } else { element.R.Add(new TActionNode(time, Convert.ToSingle(tmp[point]), 4)); if (time < endT) { element.R.Add(new TActionNode(endT, Convert.ToSingle(tmp[point]), 4)); } return; } break; case EventType.C: if (point != tmp.Length - 3) { element.C.Add(new TActionNode( time, Color(Convert.ToInt32(tmp[point]), Convert.ToInt32(tmp[point + 1]), Convert.ToInt32(tmp[point + 2])), easing)); point += 3; time += delta; } else { element.C.Add(new TActionNode( time, Color(Convert.ToInt32(tmp[point]), Convert.ToInt32(tmp[point + 1]), Convert.ToInt32(tmp[point + 2])), 4)); if (time < endT) { element.C.Add(new TActionNode( endT, Color(Convert.ToInt32(tmp[point]), Convert.ToInt32(tmp[point + 1]), Convert.ToInt32(tmp[point + 2])), 4)); } return; } break; //throw (new FormatException("Failed to read .osb file")); } //_event,easing,starttime,endtime,val1,val2,val3,...,valN if (element.Lasttime < time) { element.Lasttime = time; } } }
private string Dealevents(ref SBelement element, StreamReader reader) { var row = reader.ReadLine(); while (!reader.EndOfStream) { // ReSharper disable PossibleNullReferenceException if (row.Trim() == "") { row = reader.ReadLine(); continue; } if (row.StartsWith("//") || row.Length == 0) { row = reader.ReadLine(); continue; } if (row.StartsWith("[")) { return(row); } if (!row.StartsWith(" ")) { return(row); } //do variables change first foreach (var replace in _variables) { if (row.Contains(replace.Key)) { row = row.Replace(replace.Key, replace.Value); } } if (row.StartsWith(" L")) { //④对于L的处理:直接复制_L,time difference,loopcount //row = reader.ReadLine(); Picknext(ref row); var delta = Convert.ToInt32(Picknext(ref row)); var loopcount = Convert.ToInt32(Picknext(ref row)); row = reader.ReadLine(); //delta: 循环开始的时间和此系列SB事件第一次生效的最初时间之间的时间差, 单位是毫秒 while (!reader.EndOfStream && row.StartsWith(" ")) { for (var i = 1; i <= loopcount; i++) { Dealevent((row.Substring(1)), ref element, i * delta); } row = reader.ReadLine(); } return(row); } if (row.StartsWith(" T")) { row = reader.ReadLine(); while (!reader.EndOfStream && row.StartsWith(" ")) { //For i As Integer = 0 To tmpe.startT // dealevent(raw(currentrow).Substring(1), element, i * tmpe.easing, currentrow) // currentrow -= 1 //Next row = reader.ReadLine(); } return(row); } Dealevent(row, ref element, 0); row = reader.ReadLine(); } return(""); }
public TGraphic(SBelement element, Texture2D[] file) { switch (element.Type) { case ElementType.Sprite: { Texturearray = file; FrameCount = 1; CurrentFrameIndex = 0; _msLastFrame = 0; _mSPerFrame = 16; Position = new Vector2(element.X0, element.Y0); Origin = Getorigin(Texturearray[0], element.Origin); break; } case ElementType.Animation: { Texturearray = file; FrameCount = element.FrameCount; _mSPerFrame = element.Framedelay; _loop = element.Looptype; CurrentFrameIndex = 0; _msLastFrame = 0; Position = new Vector2(element.X0, element.Y0); //this.texture = texturearray[0]; Origin = Getorigin(Texturearray[0], element.Origin); break; } default: throw (new FormatException("Failed to resolve .osb file")); } _origin = element.Origin; Color = Color.White; Alpha = 0; Parameter = 0; Rotate = 0f; if (element.Layers == ElementLayer.Background) { Layer = 0.9f - element.Z*0.000001f; } else { Layer = 0.5f - element.Z*0.000001f; } Scale = new Vector2(1f, 1f); // this.InitSpriteAction(); }