Ahndrek Li'Cyri
Skeletron Prime
Hey there!
I just have a few quick questions about making custom UI elements.
Firstly, I have a working bar that displays a current percentage of something, and I have the bar working so it lowers as the percent lowers.
However, it looks very... plain as a solid color and I was wondering if I could texture it somehow.
Secondly, I would like to hide the text on the bars until the player hovers the mouse over it.
And lastly, I tried to get a texture in there that would show behind the bar as it shrunk, but I can't get it to display, it either shows up on top or not at all. How would I go about doing that?
Any help is appreciated, thanks!
I just have a few quick questions about making custom UI elements.
Firstly, I have a working bar that displays a current percentage of something, and I have the bar working so it lowers as the percent lowers.
However, it looks very... plain as a solid color and I was wondering if I could texture it somehow.
Secondly, I would like to hide the text on the bars until the player hovers the mouse over it.
And lastly, I tried to get a texture in there that would show behind the bar as it shrunk, but I can't get it to display, it either shows up on top or not at all. How would I go about doing that?
Any help is appreciated, thanks!
Bars In-Game
Raw Texture for Bar
Raw Texture for Bar
C#:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.ModLoader;
using Terraria.UI;
namespace MyMod.UI
{
class Bar_UI : UIState
{
private UIElement area;
private UIImage frame;
private UIText txtAmt;
public Color barCol;
public override void OnInitialize()
{
Vector2 position = MyMod.Config.barPos;
area = new UIElement();
area.Left.Set(position.X, 1);
area.Top.Set(position.Y, 1);
area.Width.Set(100, 0);
area.Height.Set(30, 0);
frame = new UIImage(ModContent.GetTexture("MyMod/UI/barBorder"));
frame.Left.Set(20, 0);
frame.Top.Set(0, 0);
frame.Width.Set(100, 0);
frame.Height.Set(30, 0);
txtAmt = new UIText("0/0", 0.8f);
txtAmt.Left.Set(20, 0);
txtAmt.Top.Set(9, 0);
txtAmt.Width.Set(100, 0);
txtAmt.Height.Set(30, 0);
barCol = new Color(0, 200, 200);
area.Append(txtAmt);
area.Append(frame);
Append(area);
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
base.DrawSelf(spriteBatch);
ModPlayer plr = Main.LocalPlayer.GetModPlayer<ModPlayer>();
float qtnt = (float)plr.barCurr / plr.barMax; // Returns value between 0-1.
Utils.Clamp(qtnt, 0, 1);
Rectangle box = frame.GetInnerDimensions().ToRectangle();
box.X += 6;
box.Y += 6;
box.Width -= 12;
box.Height -= 12;
int steps = (int)((box.Right - box.Left) * qtnt);
for(int i = 0; i < steps; i++)
{
float percent = (float)i / steps;
spriteBatch.Draw(Main.magicPixel, new Rectangle(box.Left + i, box.Y, 1, box.Height), barCol);
}
}
public override void Update(GameTime gameTime)
{
ModPlayer plr = Main.LocalPlayer.GetModPlayer<ModPlayer>();
txtAmt.SetText($"{plr.barCurr}/{plr.barMax}");
base.Update(gameTime);
Vector2 pos = MyMod.Config.barPos;
area.Left.Set(pos.X, 1);
area.Top.Set(pos.Y, 1);
}
}
}