Beispiel #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Graphics g = panel1.CreateGraphics();

            controller            = new BlockController(g, this.Font, panel1.Size, () => MakeTextBox());
            controller.Modified  += new ControllerModified(controller_Modified);
            document              = new Document("Kitsune");
            document.TitleChange += new TitleChangeEvent(document_TitleChange);
            document.SetTitle();
            RegisterMethods();
            controller.InitPalette(); // after methods are registered

            vm       = new VM.VM();
            compiler = new Compiler(vm, controller.GetBlockSpace());

            runForm = panel2;
            //runForm.Location = this.Location;
            //runForm.Size = this.Size;
            runGraphics = runForm.CreateGraphics();
            stage       = new Stage.Stage(runGraphics, this.Font, runForm.ClientSize);
            kitsune     = new Sprite(BitmapExtensions.LoadBmp("kitsune2.bmp"),
                                     runForm.ClientRectangle.Center().Offseted(-15, -15),
                                     true,
                                     -90);


            runForm.Paint       += new PaintEventHandler(runForm_Paint);
            runForm.DoubleClick += new EventHandler(runForm_DoubleClick);
            stage.AddSprite(kitsune);
            stage.RedrawAll();
            PrepareVM(vm, stage);
            PrepareCompiler(compiler);


            // I really should use a testing framework
            // testSplitFuncArgs();
            // testCBlockView(new Point(10, 10));
            // testReporterBlockView(new Point(250, 10));
            // testBlockStackView(new Point(450, 10));
        }
Beispiel #2
0
        private void PrepareVM(VM.VM vm, Stage.Stage stage)
        {
            vm.RegisterPrimitve("fd", delegate(object[] args)
            {
                stage.Fwd(kitsune, (double)args[0]);
                return(null);
            });

            vm.RegisterPrimitve("rt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.rt((float)(double)args[0]));
                return(null);
            });

            vm.RegisterPrimitve("lt", delegate(object[] args)
            {
                stage.WithSprite(kitsune, (s) => s.lt((float)(double)args[0]));
                return(null);
            });

            vm.RegisterPrimitve("gotoxy", delegate(object[] args)
            {
                float x = (float)(double)args[0];
                float y = (float)(double)args[1];
                stage.WithSprite(kitsune, (s) => s.Move(new PointF(x, y)));
                return(null);
            });

            vm.RegisterPrimitve("random", delegate(object[] args)
            {
                int low  = (int)(double)args[0];
                int hi   = (int)(double)args[1];
                double r = random.Next(low, hi);
                return(r);
            });

            vm.RegisterPrimitve("+", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a + b);
            });

            vm.RegisterPrimitve("-", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a - b);
            });

            vm.RegisterPrimitve("*", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a * b);
            });

            vm.RegisterPrimitve("/", delegate(object[] args)
            {
                double a = (double)args[0];
                double b = (double)args[1];
                return(a / b);
            });

            vm.RegisterPrimitve("<", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return((a as IComparable).CompareTo(b) < 0);
            });

            vm.RegisterPrimitve("=", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return(a.Equals(b));
            });

            vm.RegisterPrimitve(">", delegate(object[] args)
            {
                object a = args[0];
                object b = args[1];
                if (a.GetType() != b.GetType())
                {
                    a = a.ToString();
                    b = b.ToString();
                }
                return((a as IComparable).CompareTo(b) > 0);
            });

            vm.RegisterPrimitve("and", delegate(object[] args)
            {
                bool a = (bool)args[0];
                bool b = (bool)args[1];

                return(a && b);
            });

            vm.RegisterPrimitve("or", delegate(object[] args)
            {
                bool a = (bool)args[0];
                bool b = (bool)args[1];

                return(a || b);
            });

            vm.RegisterPrimitve("not", delegate(object[] args)
            {
                bool a = (bool)args[0];

                return(!a);
            });

            vm.RegisterPrimitve("say", delegate(object[] args)
            {
                stage.Say(kitsune, args[0].ToString());
                return(null);
            });

            vm.RegisterPrimitve("sin", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Sin(angle);
                return(result);
            });

            vm.RegisterPrimitve("cos", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Cos(angle);
                return(result);
            });

            vm.RegisterPrimitve("tan", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Tan(angle);
                return(result);
            });

            vm.RegisterPrimitve("asin", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Asin(angle);
                return(result);
            });

            vm.RegisterPrimitve("acos", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Acos(angle);
                return(result);
            });

            vm.RegisterPrimitve("atan", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Atan(angle);
                return(result);
            });


            vm.RegisterPrimitve("sqrt", delegate(object[] args)
            {
                double angle  = (double)args[0];
                angle         = angle * Math.PI / 180.0;
                double result = Math.Sqrt(angle);
                return(result);
            });

            vm.RegisterPrimitve("doNothing", delegate(object[] args)
            {
                return(null);
            });
        }