Пример #1
0
        public static Track LoadTrackTRK(string track, string savename)
        {
            var ret = new Track();

            ret.Name = track;
            var addedlines = new Dictionary <int, StandardLine>();
            var extensions = new List <Extensionentry>();
            var location   = Program.CurrentDirectory + "Tracks" + Path.DirectorySeparatorChar + track;

            if (savename != null)
            {
                location += Path.DirectorySeparatorChar + savename + ".trk";
            }
            else
            {
                location += ".trk";
            }
            using (var file =
                       File.Open(location, FileMode.Open))
            {
                var br    = new BinaryReader(file);
                int magic = br.ReadInt32();
                if (magic == ('T' | 'R' << 8 | 'K' << 16 | 0xF2 << 24))
                {
                    byte     version  = br.ReadByte();
                    string[] features = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt16())).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    if (version != 1)
                    {
                        throw new Exception("Unsupported version");
                    }
                    bool redmultipier     = false;
                    bool scenerywidth     = false;
                    bool supports61       = false;
                    bool songinfo         = false;
                    bool ignorabletrigger = false;
                    for (int i = 0; i < features.Length; i++)
                    {
                        switch (features[i])
                        {
                        case "REDMULTIPLIER":
                            redmultipier = true;
                            break;

                        case "SCENERYWIDTH":
                            scenerywidth = true;
                            break;

                        case "6.1":
                            supports61 = true;
                            break;

                        case "SONGINFO":
                            songinfo = true;
                            break;

                        case "IGNORABLE_TRIGGER":
                            ignorabletrigger = true;
                            break;

                        case "ZEROSTART":
                            ret.ZeroStart = true;
                            break;

                        default:
                            throw new Exception("Unsupported feature");
                        }
                    }
                    if (supports61)
                    {
                        ret.SetVersion(6.1m);
                    }
                    else
                    {
                        ret.SetVersion(6.2m);
                    }
                    if (songinfo)
                    {
                        var song = br.ReadString();
                        try
                        {
                            var strings = song.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            var fn      = Program.CurrentDirectory + "Songs" +
                                          Path.DirectorySeparatorChar +
                                          strings[0];
                            if (File.Exists(fn))
                            {
                                if (AudioPlayback.LoadFile(ref fn))
                                {
                                    game.CurrentSong = new Song(Path.GetFileName(fn), float.Parse(strings[1]));
                                    game.EnableSong  = true;
                                }
                                else
                                {
                                    Program.NonFatalError("An unknown error occured trying to load the song file");
                                }
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }
                    ret.Start = new Vector2d(br.ReadDouble(), br.ReadDouble());
                    var lines = br.ReadInt32();
                    for (var i = 0; i < lines; i++)
                    {
                        Line        l;
                        byte        ltype      = br.ReadByte();
                        var         lt         = (LineType)(ltype & 0x1F);//we get 5 bits
                        var         inv        = (ltype >> 7) != 0;
                        var         lim        = (ltype >> 5) & 0x3;
                        var         ID         = -1;
                        var         prvID      = -1;
                        var         nxtID      = -1;
                        var         multiplier = 1;
                        var         linewidth  = 1f;
                        LineTrigger tr         = ignorabletrigger ? new LineTrigger() : null;
                        if (redmultipier)
                        {
                            if (lt == LineType.Red)
                            {
                                multiplier = br.ReadByte();
                            }
                        }
                        if (lt == LineType.Blue || lt == LineType.Red)
                        {
                            if (ignorabletrigger)
                            {
                                bool zoomtrigger = br.ReadBoolean();
                                if (zoomtrigger)
                                {
                                    tr.Zoomtrigger = true;
                                    var target = br.ReadSingle();
                                    var frames = br.ReadInt16();
                                    tr.ZoomFrames = frames;
                                    tr.ZoomTarget = target;
                                }
                                else
                                {
                                    tr = null;
                                }
                            }
                            ID = br.ReadInt32();
                            if (lim != 0)
                            {
                                prvID = br.ReadInt32();
                                nxtID = br.ReadInt32();
                            }
                        }
                        if (lt == LineType.Scenery)
                        {
                            if (scenerywidth)
                            {
                                float b = br.ReadByte();
                                linewidth = b / 10f;
                            }
                        }
                        var x1 = br.ReadDouble();
                        var y1 = br.ReadDouble();
                        var x2 = br.ReadDouble();
                        var y2 = br.ReadDouble();
                        switch (lt)
                        {
                        case LineType.Blue:
                            var bl = new StandardLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                            bl.ID = ID;
                            bl.SetExtension(lim);
                            l = bl;
                            if (prvID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = bl, Linkid = prvID, Next = false
                                });
                            }
                            if (nxtID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = bl, Linkid = nxtID, Next = true
                                });
                            }
                            bl.Trigger = tr;
                            break;

                        case LineType.Red:
                            var rl = new RedLine(new Vector2d(x1, y1), new Vector2d(x2, y2), inv);
                            rl.ID = ID;
                            rl.SetExtension(lim);
                            if (redmultipier)
                            {
                                rl.Multiplier = multiplier;
                            }
                            l = rl;
                            if (prvID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = rl, Linkid = prvID, Next = false
                                });
                            }
                            if (nxtID != -1)
                            {
                                extensions.Add(new Extensionentry {
                                    Line = rl, Linkid = nxtID, Next = true
                                });
                            }
                            rl.Trigger = tr;
                            break;

                        case LineType.Scenery:
                            l = new SceneryLine(new Vector2d(x1, y1), new Vector2d(x2, y2))
                            {
                                Width = linewidth
                            };

                            break;

                        default:
                            throw new Exception("Invalid line type");
                        }
                        if (l is StandardLine)
                        {
                            if (!addedlines.ContainsKey(l.ID))
                            {
                                addedlines[ID] = (StandardLine)l;
                                ret.AddLines(l);
                            }
                        }
                        else
                        {
                            ret.AddLines(l);
                        }
                    }
                }
            }
            foreach (var v in extensions)
            {
                if (v.Next)
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        //if (sl.Extension == StandardLine.ExtensionDirection.Right || sl.Extension == StandardLine.ExtensionDirection.Both)
                        {
                            v.Line.Next = sl;
                            sl.Prev     = v.Line;
                        }
                    }
                }
                else //prev
                {
                    StandardLine sl;
                    if (addedlines.TryGetValue(v.Linkid, out sl))
                    {
                        //if (sl.Extension == StandardLine.ExtensionDirection.Left || sl.Extension == StandardLine.ExtensionDirection.Both)
                        {
                            v.Line.Prev = sl;
                            sl.Next     = v.Line;
                        }
                    }
                }
            }
            ret.ResetUndo();
            ret.ResetChanges();
            return(ret);
        }
Пример #2
0
        public void Select(StandardLine line, Vector2d position)
        {
            if (selectionwindow != null)
            {
                if (selectionwindow.UserData != line)
                {
                    selectionwindow.Close();
                    selectionwindow = null;
                }
            }
            if (selectionwindow == null)
            {
                selectionwindow = new WindowControl(game.Canvas, "Line Settings", false);
                selectionwindow.MakeModal(true);
                selectionwindow.UserData      = line;
                selectionwindow.DeleteOnClose = true;
                selectionwindow.DisableResizing();
                selectionwindow.Height = 170;
                selectionwindow.Width  = 150;

                ControlBase container1 = new ControlBase(selectionwindow);
                container1.Dock = Gwen.Pos.Fill;
                if (line.GetLineType() != LineType.Scenery)
                {
                    LabeledCheckBox btn = new LabeledCheckBox(container1);
                    btn.Dock          = Gwen.Pos.Top;
                    btn.Text          = "Inverse";
                    btn.IsChecked     = line.inv;
                    btn.CheckChanged += (o, e) =>
                    {
                        var caller = (LabeledCheckBox)o;
                        line.inv = caller.IsChecked;
                        line.CalculateConstants();
                        game.Track.TrackUpdated();
                        game.Invalidate();
                    };
                    LineTrigger tr = (LineTrigger)line.Trigger ?? new LineTrigger();

                    var gb = new PropertyTree(container1);
                    gb.Height = 110;
                    gb.Dock   = Gwen.Pos.Top;

                    PropertyTree table = new PropertyTree(gb);

                    table.Name   = "triggers";
                    table.Dock   = Gwen.Pos.Fill;
                    table.Height = 100;

                    var row     = table.Add("Zoom Trigger");
                    var enabled = row.Add("Enabled", new Gwen.Controls.Property.Check(table));
                    enabled.Value         = tr.Zoomtrigger ? "1" : "0";
                    enabled.ValueChanged += (o, e) =>
                    {
                        if (enabled.Value == "1")
                        {
                            tr.Zoomtrigger = true;
                            tr.ZoomTarget  = float.Parse(((PropertyRow)container1.FindChildByName("Zoom", true)).Value);
                            tr.ZoomFrames  = int.Parse(((PropertyRow)container1.FindChildByName("ZoomFrames", true)).Value);
                            line.Trigger   = tr;
                        }
                        else
                        {
                            tr.Zoomtrigger = false;
                            if (!tr.Enabled)
                            {
                                line.Trigger = null;
                            }
                        }
                        game.Track.LineChanged(line);
                    };
                    var prop = row.Add("Zoom");
                    prop.Name          = "Zoom";
                    prop.Value         = (enabled.Value == "1" ? tr.ZoomTarget : 1).ToString();
                    prop.ValueChanged += (o, e) =>
                    {
                        var   caller = (PropertyRow)o;
                        float val    = 0;
                        if (float.TryParse(caller.Value, out val) && val >= 0.1 && val <= 24)
                        {
                            caller.LabelColor = System.Drawing.Color.Black;
                            tr.ZoomTarget     = val;
                        }
                        else
                        {
                            caller.LabelColor = System.Drawing.Color.Red;
                        }
                    };
                    prop               = row.Add("Frames");
                    prop.Name          = "ZoomFrames";
                    prop.Value         = (enabled.Value == "1" ? tr.ZoomFrames : 40).ToString();
                    prop.ValueChanged += (o, e) =>
                    {
                        var caller = (PropertyRow)o;
                        int val    = 0;
                        if (int.TryParse(caller.Value, out val) && val >= 1 && val < 10000)
                        {
                            caller.LabelColor = System.Drawing.Color.Black;
                            tr.ZoomFrames     = val;
                        }
                        else
                        {
                            caller.LabelColor = System.Drawing.Color.Red;
                        }
                    };
                }

                if (line.GetLineType() == LineType.Red)
                {
                    selectionwindow.Height += 30;
                    NoDecimalNUD nud  = new NoDecimalNUD(container1);
                    var          marg = nud.Margin;
                    marg.Top          = 5;
                    marg.Left         = marg.Right = 1;
                    marg.Left         = 70;
                    marg.Bottom       = 10;
                    nud.Margin        = marg;
                    nud.Dock          = Gwen.Pos.Top;
                    nud.Min           = 1;
                    nud.Max           = 3;
                    nud.Value         = (line as RedLine).Multiplier;
                    nud.ValueChanged += nud_redlinemultiplier_ValueChanged;
                    nud.UserData      = line;
                    Label l = new Label(container1);
                    l.Y    = 137;
                    l.Text = "Multiplier";
                    selectionwindow.Height += 25;


                    nud         = new NoDecimalNUD(container1);
                    marg        = nud.Margin;
                    marg.Top    = 5;
                    marg.Left   = marg.Right = 1;
                    marg.Left   = 70;
                    marg.Bottom = 10;
                    nud.Margin  = marg;
                    nud.Dock    = Gwen.Pos.Top;
                    var         lines    = game.Track.GetLinesInRect(new FloatRect((Vector2)line.Position, new Vector2(1, 1)), false);
                    List <Line> redlines = new List <Line>();
                    foreach (var red in lines)
                    {
                        if (red.Position == line.Position && red.Position2 == line.Position2)
                        {
                            redlines.Add(red);
                        }
                    }
                    nud.Min = 1;
                    nud.Max = 50;
                    redlines.Sort(new Track.Linecomparer());
                    nud.Value         = redlines.Count;
                    nud.ValueChanged += (o, e) =>
                    {
                        var diff = nud.Value - redlines.Count;
                        if (diff < 0)
                        {
                            for (int i = 0; i > diff; i--)
                            {
                                game.Track.RemoveLine(redlines[(int)((redlines.Count - 1))]);
                                redlines.RemoveAt(redlines.Count - 1);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < diff; i++)
                            {
                                var red = new RedLine(line.Position, line.Position2, line.inv)
                                {
                                    Multiplier = ((RedLine)line).Multiplier
                                };
                                game.Track.AddLine(red);
                                redlines.Add(red);
                            }
                        }
                        game.Track.TrackUpdated();
                    };
                    nud.UserData = line;
                    l            = new Label(container1);
                    l.Y          = 137 + 35;
                    l.Text       = "Multilines";
                }
                selectionwindow.IsHiddenChanged += Selectionwindow_IsHiddenChanged;
                selectionwindow.Show();
                selectionwindow.X = (int)position.X;
                selectionwindow.Y = (int)position.Y;
                game.Cursor       = MouseCursor.Default;
            }
        }