Skip to content

Create GUI

MrScautHD edited this page Oct 23, 2023 · 2 revisions

Create GUI with Elements

Initially, begin by crafting a new class that extends from the Gui class. Following this, you'll choose a new data structure, in this case, Is called LabelData, and configure its properties as desired.

Once this setup is complete, you can proceed to create a new Element, for instance, a LabelElement, and seamlessly integrate it into the Gui using the AddElement method.

public class MyGui : Gui {
    
    public MyGui(string name) : base(name) { }

    protected override void Init() {
        base.Init();
        
        LabelData labelData = new LabelData() {
            Font = FontHelper.GetDefault(),
            FontSize = 50,
            Spacing = 4,
            Text = "Sparkle Engine!",
            Color = Color.WHITE,
            HoverColor = Color.GRAY,
            Rotation = 0
        };
        
        this.AddElement(new LabelElement("name", labelData, Anchor.Center, Vector2.Zero));

        // ADD MORE ELEMENTS HERE:
    }
}

Create your own GUI Elements

At first we need to create a new class and extend from GuiElements, after that we doing a new struct for all the datas that want to get implimented

Struct:

public struct MyGuiElementData {
    
    public string Text;
    // Add more...

    public MyGuiElementData() {
        Text = string.Empty;
        // Add more...
    }
}

GuiElement:

public class MyGuiElement : GuiElement {

    public string Text;

    public MyGuiElement(string name, MyGuiElementData data, Anchor anchor, Vector2 offset, Vector2 size, Func<bool>? clickClickFunc) : base(name, anchor, offset, size, clickClickFunc) {
        this.Text = data.Text;
    }

    protected override void Draw() { }
}