tModLoader [Interface/UI] Some of my Snippets and Tutorials

Subaro

Terrarian
You know it! You want to create a mod but you stuck on some obstacle. Then you loose interest in searching and give up and we maybe lost a good mod.Sometimes it is hard to find things in the forum. So I just want to share and create some Snippets and tutorials. Especially for implementing custom UIs to make new more extending mods.

Give me some time to build this up

Snippets:
1. Remove vanilla interfaces and replace them with your own:
Terraria has different layers for the interface. Most things are separated between different layers like the map, inventory, resource bars etc...

If you want to create your own health and mana bars you can just remove the vanilla layer. Now it will not be drawn and you can create your own health bar (later in tutorial)

The name of the layers can be found at the official tModLoader wiki: https://github.com/bluemagic123/tModLoader/wiki/Vanilla-Interface-layers-values

In your mod go to your class that inheritances Mod and implement:
Code:
public override void ModifyInterfaceLayers(List<MethodSequenceListItem> layers)
        {
            for (int i = 0; i < layers.Count; i++)
            {
                //Remove Health bars
                if (layers[i].Name.Contains("Resource Bars")) //Here goes the layername you want to remove
                {
                    layers.RemoveAt(i);

                    //Add you own layer
                    layers.Insert(i, new MethodSequenceListItem(
                    "MyModNameHere: Custom Health Bar",
                    delegate
                    {
                        if (CustomHealthBar.visible)
                        {
                            //Update CustomHealthBar
                            customHealtBar.Update(Main._drawInterfaceGameTime);
                            customHealtBar.Draw(Main.spriteBatch);
                        }
                        return true;
                    },
                    null)
                );
                }
            }
}

2. Make UIElement draggable (Credits: ExampleMod :) ):
Create method DragStart and DragEnd in your UIElement class.
On the element that you want to be draggable you add the created methods to the OnMouseDown and OnMouseUp.
Code:
characterPanel.OnMouseDown += new UIElement.MouseEvent(DragStart);
characterPanel.OnMouseUp += new UIElement.MouseEvent(DragEnd);
Now add the methods:
Code:
private Vector2 offset;
        public bool dragging = false;

        private void DragStart(UIMouseEvent evt, UIElement listeningElement)
        {
            offset = new Vector2(evt.MousePosition.X - characterPanel.Left.Pixels, evt.MousePosition.Y - characterPanel.Top.Pixels);
            dragging = true;
        }

        private void DragEnd(UIMouseEvent evt, UIElement listeningElement)
        {
            Vector2 end = evt.MousePosition;
            dragging = false;

            characterPanel.Left.Set(end.X - offset.X, 0f);
            characterPanel.Top.Set(end.Y - offset.Y, 0f);

            Recalculate();
        }
characterPanel is the same UIElement that got the OnMouseDown ... etc methods assigned.

Tutorials:
Create Simple UI - Custom Resource Bars:
https://forums.terraria.org/index.p...reating-simple-ui-custom-resource-bars.53417/

Credits:
Myself :)
the tModLoader Team & Co- creating tModloader, ExampleMod
Re-Logic - Creating the best game ever
 
Last edited:
Back
Top Bottom