Example Showcase - SirePi/duality-ui GitHub Wiki

The code in this page will create a UI that contains all the Controls available in the plugin, and shows some ways the controls can interact with each other.

using Duality;
using Duality.Resources;
using SnowyPeak.Duality.Plugins.YAUI;
using SnowyPeak.Duality.Plugins.YAUI.Controls;
using SnowyPeak.Duality.Plugins.YAUI.Controls.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Duality_
{
    public class ShowcaseUI : UI
    {
        protected override ControlsContainer CreateUI()
        {
            this.IsFullScreen = true;
            DockPanel root = new DockPanel();

            // Grid: 4 rows, 2 columns
            GridPanel grid = new GridPanel()
            {
                Docking = Dock.Left,
                Rows = new string[] { "*", "*", "*", "*" },
                Columns = new string[] { "*", "*" },
                Margin = new Border(5),
                Size = new Size(200)
            };

            // some grouped radiobuttons
            for (int r = 0; r < grid.Rows.Count() - 1; r++)
            {
                for (int c = 0; c < grid.Columns.Count(); c++)
                {
                    RadioButton rb = new RadioButton()
                    {
                        RadioGroup = "RadioGroup",
                        Text = String.Format("Radio/n@Cell {0},{1}", r, c),
                        Cell = new CellInfo()
                        {
                            Row = r,
                            Column = c
                        }
                    };

                    rb.TextConfiguration.Alignment = Alignment.Center;
                    rb.GlyphConfiguration.Alignment = Alignment.Top;

                    grid.Add(rb);
                }
            }

            // A column-spanning listbox
            ListBox listBox = new ListBox()
            {
                MultiSelection = true,
                Cell = new CellInfo()
                {
                    Row = grid.Rows.Count() - 1,
                    ColSpan = 2
                }
            };
            listBox.SetItems(new string[] { "One", "Two", "3", "Four", "5!", "Six", "Seven", "Even 8" });

            grid.Add(listBox);

            // Vertical stack
            StackPanel stackV = new StackPanel()
            {
                Docking = Dock.Right,
                Direction = Direction.UpToDown,
                Margin = new Border(5),
                Size = new Size(200)
            };

            // Buttons to change skin.
            // Notice: once you change skin, you lose all the preferences you set in this one
            // Supported but not that useful IMHO
            Button btnLightSkin = new Button()
            {
                Text = "Apply Light Skin",
                Size = new Size(30)
            };
            btnLightSkin.OnMouseButton += (sender, args) =>
            {
                if (args.IsPressed && args.Button == Duality.Input.MouseButton.Left)
                    root.ApplySkin(Skin.YAUI_ROUNDED);
            };

            Button btnDarkSkin = new Button()
            {
                Text = "Apply Dark Skin",
                Size = new Size(30)
            };
            btnDarkSkin.OnMouseButton += (sender, args) =>
            {
                if (args.IsPressed && args.Button == Duality.Input.MouseButton.Left)
                    root.ApplySkin(Skin.YAUI_DARK);
            };

            Button btnFathomsSkin = new Button()
            {
                Text = "Apply Fathoms Skin",
                Size = new Size(30)
            };
            btnFathomsSkin.OnMouseButton += (sender, args) =>
            {
                if (args.IsPressed && args.Button == Duality.Input.MouseButton.Left)
                    root.ApplySkin(Skin.YAUI_FATHOMS);
            };

            stackV.Add(btnLightSkin);
            stackV.Add(new Separator() { Size = new Size(5) });
            stackV.Add(btnDarkSkin);
            stackV.Add(new Separator() { Size = new Size(5) });
            stackV.Add(btnFathomsSkin);

            // Some textboxes now.. disabled, enabled and password
            stackV.Add(new Separator() { Size = new Size(15) });
            TextBox textBoxDisabled = new TextBox()
            {
                Text = "This is disabled.. :(",
                Size = new Size(30),
                Status = Control.ControlStatus.Disabled,
            };
            textBoxDisabled.TextConfiguration.Alignment = Alignment.Right;

            stackV.Add(textBoxDisabled);
            stackV.Add(new Separator() { Size = new Size(5) });
            TextBox textBox = new TextBox()
            {
                Text = "This is not :)",
                Size = new Size(30)
            };
            stackV.Add(textBox);
            stackV.Add(new Separator() { Size = new Size(5) });
            stackV.Add(new TextBox()
            {
                Text = "Password!",
                IsPassword = true,
                MaxLength = 15,
                Size = new Size(30),
            });

            // Make some space
            stackV.Add(new Separator() { Size = new Size(15) });

            // Auto-increasing progress bar
            ProgressBar pBar = new ProgressBar()
            {
                Size = new Size(30)
            };
            pBar.TextConfiguration.Alignment = Alignment.Center;
            pBar.OnGameUpdate += (sender, lastDeltaMs) =>
            {
                ProgressBar p = sender as ProgressBar;
                p.Value += (lastDeltaMs / 1000 / 10);
                if (p.Value == 1) p.Value = 0;

                p.Text = String.Format("Restart in {0:0}...", (1 - p.Value) * 10);
            };
            stackV.Add(pBar);
            stackV.Add(new Separator() { Size = new Size(15) });

            // A textblock
            stackV.Add(new TextBlock()
            {
                Size = new Size(80),
                Text = "Text/nNewLine/nAnd Another"
            });

            // Horizontal stack...
            StackPanel stackH = new StackPanel()
            {
                Docking = Dock.Bottom,
                Direction = Direction.LeftToRight,
                Margin = new Border(5),
                Size = new Size(80)
            };

            // ... with 3 togglebuttons
            for (int i = 0; i < 3; i++)
            {
                ToggleButton b = new ToggleButton()
                {
                    Text = "Toggle/nButton",
                    Size = new Size(120)
                };

                b.TextConfiguration.Alignment = Alignment.Right;

                stackH.Add(b);
                stackH.Add(new Separator() { Size = new Size(5) });
            }

            // This togglebutton controls the following progressbar
            ToggleButton stopAndGo = new ToggleButton()
            {
                Text = "Stop&Go",
                Size = new Size(80)
            };
            stackH.Add(stopAndGo);

            // and this progressbar won't increase unless the previous togglebutton is Toggled
            ProgressBar vBar = new ProgressBar()
            {
                Size = new Size(40),
            };
            vBar.ProgressConfiguration.Direction = Direction.DownToUp;
            vBar.OnGameUpdate += (sender, lastDeltaMs) =>
            {
                if (stopAndGo.Toggled)
                {
                    ProgressBar p = sender as ProgressBar;
                    p.Value += (lastDeltaMs / 1000 / 8); // takes 8 seconds to fill
                    if (p.Value == 1) p.Value = 0;
                }
            };

            stackH.Add(vBar);

            // Central panel: canvas w/ scrollbars!
            DockPanel central = new DockPanel();
            CanvasPanel canvas = new CanvasPanel();

            HorizontalScrollBar hScrollBar = new HorizontalScrollBar();
            hScrollBar.OnValueChange += (sender, previous, current) =>
            {
                canvas.Offset.X = -(sender.Value * 2);
            };

            VerticalScrollBar vScrollBar = new VerticalScrollBar()
            {
                Docking = Dock.Right
            };
            vScrollBar.OnValueChange += (sender, previous, current) =>
            {
                canvas.Offset.Y = -(sender.Value * 2);
            };

            // Stupid function checkbutton.. makes things move
            CheckButton funButton = new CheckButton()
            {
                Size = new Size(150, 50),
                Position = new Vector2(100, 200),
                Text = "Checked = fun!"
            };
            funButton.OnGameUpdate += (sender, lastDeltaMs) =>
            {
                if ((sender as CheckButton).Checked)
                {
                    bool growing = Convert.ToBoolean(sender.Tag);

                    stackV.Size.X += (lastDeltaMs * .1f * (growing ? 1 : -1));
                    if (stackV.Size.X > 300) sender.Tag = false;
                    if (stackV.Size.X < 150) sender.Tag = true;
                }
            };

            canvas.Add(funButton);

            // Pack it all up together
            DockPanel cBottom = new DockPanel() { Docking = Dock.Bottom, Size = new Size(20) };
            cBottom.Add(new Separator() { Docking = Dock.Right, Size = new Size(20) });
            cBottom.Add(hScrollBar);

            central
                .Add(cBottom)
                .Add(vScrollBar)
                .Add(canvas);

            return root.Add(stackH)
                .Add(grid)
                .Add(stackV)
                .Add(central);
        }
    }
}