Example #1
0
        public void ReloadImages()
        {
            #region If not thread-safe
            if (this.InvokeRequired)
            {
                try
                {
                    this.Invoke(new MethodInvoker(() => { ReloadImages(); }));
                }
                catch { }
            }
            #endregion
            else
            {
                #region Update the Templates
                for (int i = 0; i < templates.Count; i++)
                {
                    ComponentTemplate t = templates[i];
                    if (t != null)
                    {
                        Button btn = t.button;
                        if (btn != null)
                        {
                            if (t.Image != null)
                            {
                                btn.BackgroundImage           = t.Image;
                                btn.FlatStyle                 = FlatStyle.Flat;
                                btn.FlatAppearance.BorderSize = 0;
                                btn.Size = t.Image.Size;
                                if (t.Image.Size.Width > splitContainer1.Panel1.Width)
                                {
                                    if (t.Image.Size.Width <= 200)
                                    {
                                        splitContainer1.SplitterDistance = t.Image.Size.Width + 6;
                                    }
                                    else
                                    {
                                        splitContainer1.SplitterDistance = 200;
                                    }
                                }
                                btn.Text = "";
                            }
                        }
                    }
                }
                #endregion
                #region Update the Components
                for (int i = 0; i < cButtons.Count; i++)
                {
                    CustomButton btn = cButtons[i];
                    if (btn != null)
                    {
                        if (btn.BackgroundImage != null)
                        {
                            btn.FlatStyle = FlatStyle.Flat;
                            btn.FlatAppearance.BorderSize = 0;
                            btn.Size = btn.BackgroundImage.Size;
                            btn.Text = "";
                        }
                    }
                }
                #endregion

                //Center Align
                FlowCenter();
                ResetFocus();
            }
        }
Example #2
0
        public UIDesigner(int id, int width, int height, UIComponent[] comps)
        {
            form = this;

            this.id     = id;
            this.width  = width;
            this.height = height;
            if (comps != null)
            {
                _components = comps.ToList();
            }

            InitializeComponent();

            //Add the form events
            form.Activated   += Form_Activated;
            form.FormClosing += Form_FormClosing;
            form.Load        += UIDesigner_Load;

            UIPanel.Size = new Size(width, height);

            #region RenderScript Core
            browser = new ChromiumWebBrowser("");

            form.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            browser.LoadingStateChanged += (object s, LoadingStateChangedEventArgs er) =>
            {
                if (!er.IsLoading && browser.Address != FAKE_ADDR)
                {
                    if (queueCallback.Count > 0)
                    {
                        queueCallback[0](Crop(new Bitmap(Base64ToImage(browser.Address.Replace("data:image/png;base64,", "")))), queueCallback.Count - 1 > 0 ? queueCallback.Count - 1 : 0);
                        if (queueCallback.Count > 0)
                        {
                            queueCallback.RemoveAt(0);
                        }
                        if (queueScript.Count > 0)
                        {
                            browser.LoadHtml(queueScript[0], FAKE_ADDR);
                            queueScript.RemoveAt(0);
                        }
                    }
                }
            };
            #endregion

            #region Load the templates
            steps = 0;

            //Load the templates
            if (templates == null)
            {
                templates = new List <ComponentTemplate>();
                if (Directory.Exists(Application.StartupPath + @"\Resources\Templates"))
                {
                    string[] files = Directory.GetFiles(Application.StartupPath + @"\Resources\Templates", "*.js");
                    loadingForm = new LoadingForm("Load in progress: ", "%");
                    foreach (string file in files)
                    {
                        steps++;
                        ComponentTemplate cT = new ComponentTemplate();
                        cT.Name = Path.GetFileNameWithoutExtension(file);
                        cT.Path = file;
                        cT.Data = File.ReadAllText(file);
                        RenderScript(cT.Data, (Image i, int count) => {
                            cT.Image = i;
                            LoadingForm.ProgressStep(form.steps, loadingForm);
                            if (count == 0)
                            {
                                loadingForm.SafeClose();
                                loadingForm = null;
                            }
                            ReloadImages();
                        });
                        templates.Add(cT);
                    }
                }
            }

            //Create Buttons
            for (int i = 0; i < templates.Count; i++)
            {
                ComponentTemplate t   = templates[i];
                Button            btn = new Button();
                btn.Size = new Size(80, 30);
                btn.Text = t.Image == null ? t.Name : "";
                if (t.Image != null)
                {
                    btn.BackgroundImage           = t.Image;
                    btn.FlatStyle                 = FlatStyle.Flat;
                    btn.FlatAppearance.BorderSize = 0;
                    btn.Text = "";
                    btn.Size = t.Image.Size;
                    if (t.Image.Size.Width > splitContainer1.Panel1.Width)
                    {
                        if (t.Image.Size.Width <= 200)
                        {
                            splitContainer1.SplitterDistance = t.Image.Size.Width + 6;
                        }
                        else
                        {
                            splitContainer1.SplitterDistance = 200;
                        }
                    }
                }
                btn.Parent    = flowLayoutPanel1;
                btn.BackColor = Color.Transparent;
                btn.Tag       = i.ToString();
                t.button      = btn;
                flowLayoutPanel1.Controls.Add(btn);
            }

            #endregion

            //Align
            FlowCenter();
            UICenter();
            ResetFocus();

            #region Load the Components
            if (_components.Count > 0 && loadingForm == null)
            {
                steps       = 0;
                loadingForm = new LoadingForm("Load in progress: ", "%");
            }
            //Load the Components
            for (int i = 0; i < _components.Count; i++)
            {
                UIComponent  u   = _components[i];
                CustomButton btn = new CustomButton();
                btn.Location = new Point(u.x, u.y);
                btn.Size     = new Size(80, 30);
                string name = UI.GetComponentNameFromScript(u.data);
                btn.Text      = name != null ? name : "Loading...";
                btn.Parent    = UIPanel;
                btn.BackColor = Color.Transparent;
                btn.Tag       = i.ToString();

                if (!string.IsNullOrWhiteSpace(u.data))
                {
                    string _width = UI.GetVariableFromScript(u.data, "width");
                    string _height = UI.GetVariableFromScript(u.data, "height");
                    string x = UI.GetVariableFromScript(u.data, "x");
                    string y = UI.GetVariableFromScript(u.data, "y");
                    int    w, h, _x, _y;
                    if (!string.IsNullOrWhiteSpace(_width) && !string.IsNullOrWhiteSpace(_height) &&
                        int.TryParse(_width, out w) && int.TryParse(_height, out h))
                    {
                        btn.Size = new Size(w, h);
                    }
                    if (!string.IsNullOrWhiteSpace(x) && !string.IsNullOrWhiteSpace(y) &&
                        int.TryParse(x, out _x) && int.TryParse(y, out _y))
                    {
                        btn.Location = new Point(_x, _y);
                    }
                }

                btn.Component = u;
                btn.BringToFront();
                #region Events
                btn.MouseDown += CustomButton_MouseDown;
                btn.MouseUp   += (object _sender, MouseEventArgs _e) =>
                {
                    CustomButton_UpdatePosition(btn);
                    btn.Moving = false;
                };
                btn.MouseMove   += CustomButton_MouseMove;
                btn.MouseLeave  += CustomButton_MouseLeave;
                btn.DoubleClick += CustomButton_DoubleClick;
                btn.GotFocus    += CustomButton_GotFocus;
                btn.LostFocus   += CustomButton_LostFocus;
                btn.MouseClick  += CustomButton_MouseClick;
                #endregion
                cButtons.Add(btn);
                UIPanel.Controls.Add(btn);
                form.steps++;
                RenderScript(u.data, (Image _i, int _count) => {
                    btn.BackgroundImage = _i;
                    LoadingForm.ProgressStep(form.steps, loadingForm);
                    if (_count == 0)
                    {
                        loadingForm.SafeClose();
                        loadingForm = null;
                    }
                    ReloadImages();
                });
            }

            #endregion

            #region Add the Templates' Events
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (typeof(Button) == c.GetType())
                {
                    c.MouseDown += (object sender, MouseEventArgs e) => {
                        CustomButton b = CloneButton((Button)c);
                        b.Parent = form;
                        b.BringToFront();

                        b.Focus();
                        b.Moving = true;
                        Point _mp = form.PointToClient(Cursor.Position);
                        b.Location = new Point(_mp.X - b.Size.Width / 2, _mp.Y - b.Size.Height / 2);

                        #region Events
                        b.MouseUp += (object _sender, MouseEventArgs _e) =>
                        {
                            if (b.Parent != UIPanel)
                            {
                                Point bL  = b.Parent.PointToScreen(b.Location);
                                Point UIL = UIPanel.Parent.PointToScreen(UIPanel.Location);
                                if (bL.X > UIL.X && bL.X < UIL.X + UIPanel.Size.Width &&
                                    bL.Y > UIL.Y && bL.Y < UIL.Y + UIPanel.Size.Height)
                                {
                                    b.Parent   = UIPanel;
                                    b.Location = UIPanel.PointToClient(bL);
                                    cButtons.Add(b);
                                    if (b.Component == null)
                                    {
                                        UIComponent comp = new UIComponent();
                                        comp.x    = b.Location.X;
                                        comp.y    = b.Location.Y;
                                        comp.data = templates[int.Parse(b.Tag.ToString())].Data;
                                        comp.id   = _components.Count;
                                        _components.Add(comp);
                                        b.Component = comp;

                                        string path = GameConfig.path + @"\Codes\UIs\ui" + form.id + @"\components\component" + comp.id + ".js";
                                        Directory.CreateDirectory(Path.GetDirectoryName(path));
                                        using (StreamWriter w = new StreamWriter(path))
                                        {
                                            w.Write(comp.data);
                                        }
                                    }
                                }
                                else
                                {
                                    b.Parent.Controls.Remove(b);
                                    b.Dispose();
                                }
                            }
                            CustomButton_UpdatePosition(b);
                            b.Moving = false;
                        };
                        b.MouseDown   += CustomButton_MouseDown;
                        b.MouseMove   += CustomButton_MouseMove;
                        b.MouseLeave  += CustomButton_MouseLeave;
                        b.DoubleClick += CustomButton_DoubleClick;
                        b.GotFocus    += CustomButton_GotFocus;
                        b.LostFocus   += CustomButton_LostFocus;
                        b.MouseClick  += CustomButton_MouseClick;
                        #endregion
                    };
                }
            }
            #endregion
        }