コード例 #1
0
ファイル: Program.cs プロジェクト: scnerd/Relativity
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            string CurrentText = "";
            EventManager Manager = new EventManager(
                (k, t) => { CurrentText += (char)k; Console.Write((char)k); return true; },
                () => CurrentText,
                (s) => { Console.Clear(); CurrentText = (string)s; Console.Write(CurrentText); },
                100);
            stopwatch.Start();
            Manager.Start();
            char Typed;

            Action Process = () =>
                {
                    Typed = (char)0;
                    if (Console.KeyAvailable)
                    {
                        Typed = Console.ReadKey(true).KeyChar;
                        Manager.AddEvent(Typed);
                    }
                    Manager.ProcessEvents();
                };

            while (stopwatch.Elapsed.Seconds < 10) Process();
            //Console.In.Close();

            //while (stopwatch.Elapsed.Seconds < 11) Process();
            //Manager.JumpToTime(9000);

            //while (stopwatch.Elapsed.Seconds < 12) Process();
            //Manager.JumpToTime(8000);

            //while (stopwatch.Elapsed.Seconds < 13) Process();
            //Manager.JumpToTime(7000);

            //while (stopwatch.Elapsed.Seconds < 15) Process();
            //Manager.JumpToTime(6000);

            //while (stopwatch.Elapsed.Seconds < 17) Process();
            //Manager.JumpToTime(5000);

            //while (stopwatch.Elapsed.Seconds < 19) Process();
            //Manager.JumpToTime(4000);

            //while (stopwatch.Elapsed.Seconds < 22) Process();
            //Manager.JumpToTime(3000);

            //while (stopwatch.Elapsed.Seconds < 25) Process();
            //Manager.JumpToTime(2000);

            //while (stopwatch.Elapsed.Seconds < 30) Process();
            //Manager.JumpToTime(1000);

            //while (stopwatch.Elapsed.Seconds < 35) Process();
            Manager.JumpToTime(0000);

            while (true) Process();
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: scnerd/Relativity
        private void pictureBox1_Click(object sender, EventArgs clickargs)
        {
            if (IsRunning)
                return;
            IsRunning = true;
            int Velocity = 200; //Pixels per second

            Point PreviousPosition, CurrentPosition = new Point(pictureBox1.Width / 2, pictureBox1.Height * 3 / 4);
            Direction PreviousDir = Direction.Right;
            State PreviousState = State.Stop;
            long PreviousCmdTime = 0;

            PreviousPosition = CurrentPosition;

            EventManager Manager = new EventManager(
                (c, t) =>
                {
                    if (PreviousState == State.Start && ((Command)c).St == State.Start)
                        return false;
                    if (((Command)c).St == State.Stop && ((Command)c).Dir != PreviousDir)
                        return false;
                    if (((Command)c).St == State.Stop)
                        PreviousPosition.X = CurrentPosition.X =
                            PreviousPosition.X + (int)(Velocity * (t - PreviousCmdTime) / 1000 * (PreviousDir == Direction.Left ? -1 : 1));

                    PreviousDir = ((Command)c).Dir;
                    PreviousState = ((Command)c).St;
                    PreviousCmdTime = t;

                    return true;
                },
                () =>
                    new object[] { PreviousPosition, PreviousDir, PreviousState, PreviousCmdTime },
                (o) =>
                {
                    PreviousPosition = (Point)((object[])o)[0];
                    PreviousDir = (Direction)((object[])o)[1];
                    PreviousState = (State)((object[])o)[2];
                    PreviousCmdTime = (long)((object[])o)[3];
                },
                2000);

            splitContainer1.KeyDown += new KeyEventHandler((o, e) =>
            {
                if (PreviousState == State.Stop)
                {
                    if (e.KeyCode == Keys.Left)
                        Manager.AddEvent(new Command(Direction.Left, State.Start));
                    else if (e.KeyCode == Keys.Right)
                        Manager.AddEvent(new Command(Direction.Right, State.Start));
                }
            });

            splitContainer1.KeyUp += new KeyEventHandler((o, e) =>
            {
                if (e.KeyCode == Keys.Left)
                    Manager.AddEvent(new Command(Direction.Left, State.Stop));
                else if (e.KeyCode == Keys.Right)
                    Manager.AddEvent(new Command(Direction.Right, State.Stop));
            });

            Manager.Start();
            Bitmap img = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(img);
            long MaxTime = 0;
            while (!this.IsDisposed)
            {
                Manager.ProcessEvents();

                if (PreviousState == State.Start)
                    CurrentPosition.X = PreviousPosition.X +
                        (int)(Velocity * (Manager.CurrentTime() - PreviousCmdTime) / 1000 * (PreviousDir == Direction.Left ? -1 : 1));

                g.Clear(Color.White);
                g.FillEllipse(new SolidBrush(Color.Red),
                    CurrentPosition.X - 20,
                    CurrentPosition.Y - 20,
                    40,
                    40);

                pictureBox1.Image = img;

                if (HasBeenScrolled)
                {
                    Manager.JumpToTime((int)(MaxTime * trackBar1.Value / trackBar1.Maximum));
                    HasBeenScrolled = false;
                }
                else
                {
                    long CurTime = Manager.CurrentTime();
                    MaxTime = Math.Max(MaxTime, CurTime);
                    trackBar1.Value = (int)(CurTime * trackBar1.Maximum / MaxTime);
                }

                this.Refresh();
                Application.DoEvents();
                splitContainer1.Focus();
                System.Threading.Thread.Sleep(30);
            }
        }