Click here to Skip to main content
15,896,201 members
Articles / Containers / Virtual Machine

Twiggery Scripting Language

Rate me:
Please Sign up or sign in to vote.
4.82/5 (14 votes)
12 Aug 2010LGPL313 min read 64.4K   1.1K   36  
Twiggery Scripting Language
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Runtime.InteropServices;
using num = System.Single;
using arguments = System.Collections.Generic.List<object>;

namespace Twiggery.Plugin.Graphics
{
    public class Graphics
    {
        public struct RECT
        {
            public int X1;
            public int Y1;
            public int X2;
            public int Y2;
        }

        private static int wndWidth = 0;
        private static int wndHeight = 0;
        private static Pen pen = new Pen(Color.Black);
        private static Form canvas = null;
        private static PictureBox pictureBox = null;
        private static Bitmap image0 = null;
        private static Bitmap image1 = null;
        private static System.Drawing.Graphics graphics0 = null;
        private static System.Drawing.Graphics graphics1 = null;
        private static Dictionary<int, Sprite> sprites = new Dictionary<int, Sprite>();
        private static int idp = 0;
        private static int keyCode = 0;
        private static int mouseClickedX = -1;
        private static int mouseClickedY = -1;
        private static int buttonId = 0;

        [DllImport("user32.dll")]
        static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);

        public static void getKeyCode(arguments args)
        {
            args.Add((num)keyCode);
            keyCode = 0;
        }

        private static void openCanvas(arguments args)
        {
            closeCanvas(null);

            wndHeight = (int)(num)args[0];
            wndWidth = (int)(num)args[1];
            string wndTitle = "Twiggery Graphics - Tony's Toy";
            if (args.Count == 3)
            {
                wndTitle = (string)args[2];
            }
            args.Clear();

            if (null == canvas)
            {
                canvas = new Form();
                canvas.Text = wndTitle;
                canvas.FormBorderStyle = FormBorderStyle.FixedSingle;
                canvas.StartPosition = FormStartPosition.CenterScreen;
                canvas.Size = new Size(wndWidth, wndHeight);
                canvas.MaximizeBox = false;
                canvas.BackColor = Color.White;
                canvas.KeyDown += delegate(object sender, KeyEventArgs e)
                {
                    keyCode = e.KeyValue;
                };
                canvas.FormClosing += delegate(object sender, FormClosingEventArgs e)
                {
                    closeCanvas(null);
                };
                canvas.Show();
                pictureBox = new PictureBox();
                pictureBox.Location = new Point(0, 0);
                pictureBox.Size = new Size(wndWidth, wndHeight);
                canvas.Controls.Add(pictureBox);
                pictureBox.MouseClick += delegate(object sender, MouseEventArgs e)
                {
                    mouseClickedX = e.Location.X;
                    mouseClickedY = e.Location.Y;
                };
                unsafe
                {
                    IntPtr hWnd = new IntPtr(canvas.Handle.ToPointer());
                    RECT rect;
                    GetClientRect(hWnd, out rect);
                    int dx = wndWidth - rect.X2;
                    int dy = wndHeight - rect.Y2;
                    canvas.Size = new Size(canvas.Width + dx, canvas.Height + dy);
                }

                image0 = new Bitmap(wndWidth, wndHeight);
                image1 = new Bitmap(wndWidth, wndHeight);
                graphics0 = System.Drawing.Graphics.FromImage(image0);
                graphics1 = System.Drawing.Graphics.FromImage(image1);
                pictureBox.Image = image0;
                graphics0.FillRectangle(Brushes.Black, 0, 0, wndWidth, wndHeight);
                graphics1.FillRectangle(Brushes.Black, 0, 0, wndWidth, wndHeight);
            }
            else
            {
                cleanSprites(null);
            }
        }
        
        private static void closeCanvas(arguments args)
        {
            if (null != canvas)
            {
                cleanSprites(null);

                if (null != args)
                {
                    canvas.Close();
                    canvas = null;
                    pictureBox = null;
                }

                graphics0 = null;
                graphics1 = null;
                image0 = null;
                image1 = null;
            }
        }

        private static void setBackColor(arguments args)
        {
            int _b = (int)(num)args[0];
            int _g = (int)(num)args[1];
            int _r = (int)(num)args[2];
            args.Clear();

            pen.Color = Color.FromArgb(_r, _g, _b);
        }

        public static void renderCanvas(arguments args)
        {
            // Start
            int dur = (int)(num)args[0];
            args.Clear();
            int start = DateTime.Now.Millisecond;

            // Clear scene
            System.Drawing.Graphics g = null;
            if (pictureBox.Image == image0)
            {
                g = graphics1;
            }
            else if (pictureBox.Image == image1)
            {
                g = graphics0;
            }
            g.FillRectangle(pen.Brush, 0, 0, wndWidth, wndHeight);
            // Do render
            foreach (Sprite s in sprites.Values)
            {
                s.Render(g);
            }
            // Delay
            int end = DateTime.Now.Millisecond;
            int ex = dur - (end - start);
            if (ex > 0)
            {
                Thread.Sleep(ex);
            }
            // Flush scene
            if (pictureBox.Image == image0)
            {
                pictureBox.Image = image1;
            }
            else if (pictureBox.Image == image1)
            {
                pictureBox.Image = image0;
            }

            pictureBox.Refresh();
            Application.DoEvents();

            if (canvas.Visible)
            {
                args.Add((num)1);
            }
            else
            {
                args.Add((num)0);
            }
        }

        public static void createSprite(arguments args)
        {
            if (null == canvas)
            {
                throw new Exception("Please create a canvas before create a sprite");
            }

            string fac = (string)args[args.Count - 1];
            Sprite s = null;
            switch (fac)
            {
                case "Circle":
                    s = new Circle(args);
                    break;
                case "Rect":
                    s = new Rect(args);
                    break;
                case "Text":
                    s = new Text(args);
                    break;
                case "Image":
                    s = new Image(args);
                    break;
                case "Button":
                    s = new Button(pictureBox.Controls, args, idp + 1);
                    ((Button)s).Ctrl.Click += delegate(object sender, EventArgs e)
                    {
                        buttonId = (int)((System.Windows.Forms.Button)sender).Tag;
                    };

                    break;
                case "TextBox":
                    s = new TextBox(pictureBox.Controls, args, idp + 1);
                    break;
                case "Label":
                    s = new Label(pictureBox.Controls, args, idp + 1);
                    break;
                default:
                    throw new Exception("Unknown sprite type " + fac);
            }
            args.Clear();

            sprites[++idp] = s;
            args.Add((num)idp);
        }

        private static void destroySprite(arguments args)
        {
            int id = (int)(num)args[0];
            args.Clear();

            if (sprites.ContainsKey(id))
            {
                sprites[id].Remove();
                sprites.Remove(id);
            }
        }

        private static void cleanSprites(arguments args)
        {
            idp = 0;
            keyCode = 0;
            mouseClickedX = -1;
            mouseClickedY = -1;
            buttonId = 0;
            foreach (Sprite sp in sprites.Values)
            {
                sp.Remove();
            }
            sprites.Clear();
        }

        private static void setSpritePosition(arguments args)
        {
            int y = (int)(num)args[0];
            int x = (int)(num)args[1];
            int id = (int)(num)args[2];
            args.Clear();

            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                s.SetPosition(x, y);
            }
        }

        private static void setSpriteColor(arguments args)
        {
            int _b = 0;
            int _g = 0;
            int _r = 0;
            int id = 0;

            int _v = 0;
            int _ch = 0;

            if (args.Count == 4)
            {
                _b = (int)(num)args[0];
                _g = (int)(num)args[1];
                _r = (int)(num)args[2];
                id = (int)(num)args[3];
            }
            else if (args.Count == 3)
            {
                _v = (int)(num)args[0];
                _ch = (int)(num)args[1];
                id = (int)(num)args[2];
            }

            args.Clear();

            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                if (0 == _ch)
                {
                    Color c = Color.FromArgb(_r, _g, _b);
                    s.SetColor(c);
                }
                else
                {
                    s.SetColor(_ch, _v);
                }
            }
        }

        private static void setSpriteSize(arguments args)
        {
            int id = (int)(num)args[args.Count - 1];
            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                s.SetSize(args);
            }
            args.Clear();
        }

        private static void setSpriteText(arguments args)
        {
            string text = string.Empty;
            for (int i = 0; i < args.Count - 1; i++)
            {
                text = args[i].ToString() + text;
            }
            int id = (int)(num)args[args.Count - 1];
            args.Clear();

            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                if (s is Text)
                {
                    (s as Text).SetText(text);
                }
                else if (s is ControlBase)
                {
                    (s as ControlBase).SetText(text);
                }
            }
        }

        private static void appendSpriteText(arguments args)
        {
            if (2 == args.Count)
            {
                object val = args[0];
                int id = (int)(num)args[1];

                if (sprites.ContainsKey(id))
                {
                    Sprite s = sprites[id];
                    if (s is ControlBase)
                    {
                        (s as ControlBase).AppendText(val);
                    }
                }
            }
            args.Clear();
        }

        public static void getSpriteTextAsNum(arguments args)
        {
            int id = (int)(num)args[0];
            num result = (num)0;
            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                if (s is ControlBase)
                {
                    result = (s as ControlBase).GetTextAsNum();
                }
            }
            args.Clear();

            args.Add(result);
        }

        private static void getSpriteVisible(arguments args)
        {
            int id = (int)(num)args[1];
            args.Clear();
            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                s.GetVisible(args);
            }
        }

        private static void setSpriteVisible(arguments args)
        {
            int id = (int)(num)args[1];
            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                s.SetVisible(args);
            }
            args.Clear();
        }

        private static void setSpriteAttrib(arguments args)
        {
            int id = (int)(num)args[args.Count - 1];
            if (sprites.ContainsKey(id))
            {
                Sprite s = sprites[id];
                s.SetAttribute(args);
            }
            args.Clear();
        }

        public static void getClickedControl(arguments args)
        {
            args.Add((num)buttonId);
            buttonId = 0;
        }

        public static void getMouseClicked(arguments args)
        {
            bool clicked = mouseClickedX != -1 && mouseClickedY != -1;
            args.Add((num)(clicked ? 1 : 0));
        }

        public static void getMouseClickedX(arguments args)
        {
            args.Add((num)mouseClickedX);
            mouseClickedX = -1;
        }

        public static void getMouseClickedY(arguments args)
        {
            args.Add((num)mouseClickedY);
            mouseClickedY = -1;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Architect
China China
Video game player & creator; Hardware geek & maker.

Comments and Discussions