CProgressBar.h
Code: Select all
#ifndef IPROGRESSBAR_H_
#define IPROGRESSBAR_H_
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace gui;
class IProgressBar : public IGUIElement
{
public:
IProgressBar(IGUIEnvironment * guienv,const core::rect<s32>& rectangle,s32 id=-1,IGUIElement * parent=0);
/*Set percentage in positive percentual (0~100). Please note that a call to this function with others values, will set the progress bar to 0.*/
void setProgress(irr::u32 progress);
/*Set bar Colors*/
void setColors(irr::video::SColor progress= irr::video::SColor(255,255,255,255),irr::video::SColor filling= irr::video::SColor(255,0,0,0));
/*Allow you to add a "border" into your bar. You MUST specify the size (of course in pixel) of the border. You can also pass a color parameter (Black by default)*/
void addBorder(irr::s32 size,irr::video::SColor color = irr::video::SColor(255,0,0,0));
virtual void draw();
private:
IGUIEnvironment * gui; //GUI ENV. pointer
irr::s32 total; //Dimension (X) of the bar, to calculate relative percentage.
rect<s32> bar; //Dimension of the bar
rect<s32> position; //Bar
rect<s32> border; //Border
rect<s32> tofill; //Percentage indicator
rect<s32> empty; //"Empty" indicator
irr::video::SColor fillcolor;
irr::video::SColor emptycolor;
irr::video::SColor bordercolor;
irr::video::IVideoDriver * vdriver;
};
#endifCode: Select all
#include "CProgressBar.h"
IProgressBar::IProgressBar(IGUIEnvironment * guienv,const core::rect<s32>& rectangle,s32 id,IGUIElement * parent) : IGUIElement(EGUIET_ELEMENT,guienv,parent,id,rectangle)
{
total = rectangle.LowerRightCorner.X - rectangle.UpperLeftCorner.X;
gui = guienv;
bar = rectangle;
if(parent == 0)
guienv->getRootGUIElement()->addChild(this); //Ensure that draw method is called
vdriver = this->gui->getVideoDriver();
fillcolor.set(255,255,255,255);
emptycolor.set(255,0,0,0);
border = bar;
this->setProgress(0);
}
void IProgressBar::setColors(irr::video::SColor progress,irr::video::SColor filling)
{
fillcolor = progress;
emptycolor = filling;
}
void IProgressBar::addBorder(irr::s32 size,irr::video::SColor color)
{
bordercolor = color;
border = bar;
border.UpperLeftCorner.X -= size;
border.UpperLeftCorner.Y -= size;
border.LowerRightCorner.X += size;
border.LowerRightCorner.Y += size;
}
void IProgressBar::setProgress(irr::u32 progress)
{
if(progress > 100)
progress = 0;
u32 xpercentage;
xpercentage = (progress * total)/100; //Reducing to the bar size
tofill.UpperLeftCorner.set(bar.UpperLeftCorner.X,bar.UpperLeftCorner.Y);
tofill.LowerRightCorner.set(bar.UpperLeftCorner.X+xpercentage,bar.LowerRightCorner.Y);
empty.UpperLeftCorner.set(tofill.LowerRightCorner.X,tofill.UpperLeftCorner.Y);
empty.LowerRightCorner.set(bar.LowerRightCorner.X,bar.LowerRightCorner.Y);
}
void IProgressBar::draw()
{
if(this->IsVisible == false)
return;
vdriver->draw2DRectangle(bordercolor,border);
vdriver->draw2DRectangle(fillcolor,tofill);
vdriver->draw2DRectangle(emptycolor,empty);
}Code: Select all
IProgressBar * pb = new IProgressBar(guienv,rect<s32>(100,500,600,600));
pb->setProgress(90);
pb->addBorder(1);
pb->drop();



