[C# IrrlichtLime] Progress Bar

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
sprunth
Posts: 1
Joined: Sun Jan 22, 2012 1:28 am

[C# IrrlichtLime] Progress Bar

Post by sprunth »

I ported the progress bar found (http://irrlicht.sourceforge.net/forum/v ... =9&t=39356) for C#.

I'm currently using IrrlichtLime, but I assume any wrapper would work. I've fixed the issue with the original where the progress bar wouldn't abide by the parent's position. This code has GUIElement implemented, but there is no GUIElementFactory in the wrapper so the type is just unknown.

Enjoy!

Code: Select all

 class ProgressBar : GUIElement
    {
        GUIEnvironment gui;
        int xWidth;
        Recti bar, border, toFill, empty;
        Color fillcolor, emptycolor, bordercolor;
        VideoDriver videoDriver;
 
        public ProgressBar(GUIEnvironment guienv, Recti rectangle, int id = -1, GUIElement parent = null)
            : base(GUIElementType.Unknown, guienv, parent, rectangle)
        {
            xWidth = rectangle.LowerRightCorner.X - rectangle.UpperLeftCorner.X;
            gui = guienv;
            if (Parent != null)
            { bar = new Recti(Parent.RelativePosition.UpperLeftCorner + rectangle.UpperLeftCorner, Parent.RelativePosition.UpperLeftCorner + rectangle.LowerRightCorner); }
            else { bar = rectangle; }
 
            if (parent != null)
                gui.RootElement.AddChild(this); //Ensure that draw method is called
            videoDriver = gui.VideoDriver;
            fillcolor = Color.OpaqueGreen;
            emptycolor = Color.OpaqueRed;
            bordercolor = Color.OpaqueBlack;
            border = bar;
            toFill = new Recti();
            empty = new Recti();
            SetProgress(0);
 
        }
        public void SetColors(Color progress, Color filling)
        {
            fillcolor = progress;
            emptycolor = filling;
        }
        public void AddBorder(int size, Color color)
        {
            bordercolor = color;
            border = new Recti(border.UpperLeftCorner.X - size, border.UpperLeftCorner.Y - size, border.LowerRightCorner.X + size, border.LowerRightCorner.Y + size);
        }
        public void SetProgress(uint progress)
        {
            if (progress > 100)
                progress = 0;
 
            int xpercentage;
            xpercentage = (int)(progress * xWidth) / 100; //Reducing to the bar size
            toFill.UpperLeftCorner.Set(bar.UpperLeftCorner.X, bar.UpperLeftCorner.Y);
            toFill = new Recti(bar.UpperLeftCorner.X, bar.UpperLeftCorner.Y, bar.UpperLeftCorner.X + xpercentage, bar.LowerRightCorner.Y);
            empty = new Recti(new Vector2Di(toFill.LowerRightCorner.X, toFill.UpperLeftCorner.Y), new Vector2Di(bar.LowerRightCorner.X, bar.LowerRightCorner.Y));
        }
        public new void Draw()
        {
            if (this.Visible == false)
                return;
 
            videoDriver.Draw2DRectangle(border, bordercolor);
            videoDriver.Draw2DRectangle(toFill, fillcolor);
            videoDriver.Draw2DRectangle(empty, emptycolor);
        }
    } 
Post Reply