Add Progress-Bars (not tested)

master
sfan5 2012-05-04 21:23:32 +02:00
parent fa3a0911cf
commit 06ca1e21f1
3 changed files with 124 additions and 5 deletions

58
src/IProgressBar.cpp Normal file
View File

@ -0,0 +1,58 @@
#include "iProgressBar.h"
IProgressBar::IProgressBar(IGUIEnvironment * guienv,const core::rect<s32>& rectangle,s32 id,IGUIElement * parent, IGUIFont * font) : IGUIElement(EGUIET_ELEMENT,guienv,parent,id,rectangle)
{
total = rectangle.LowerRightCorner.X - rectangle.UpperLeftCorner.X;
gui = guienv;
bar = rectangle;
textfont = font;
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);
this->setText("");
}
void IProgressBar::setColors(irr::video::SColor progress,irr::video::SColor filling)
{
fillcolor = progress;
emptycolor = filling;
}
void setText(std::string s)
{
text = s;
}
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);
textfont->draw(text, position, video::SColor color, false, true, bar)
}

48
src/IProgressBar.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef IPROGRESSBAR_HEADER
#define IPROGRESSBAR_HEADER
#include "common_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, IGUIFont * font=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));
/*Set the Text of the Progress Bar*/
void setText(std::string s);
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
std::string text; // Text in the Progress Bar
irr::video::SColor fillcolor;
irr::video::SColor emptycolor;
irr::video::SColor bordercolor;
irr::video::IVideoDriver * vdriver;
irr::gui::IGUIFont * textfont;
};
#endif

View File

@ -68,6 +68,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#endif
#include "event_manager.h"
#include <list>
#include "IProgressBar.h"
/*
Setting this to 1 enables a special camera mode that forces
@ -506,7 +507,7 @@ PointedThing getPointedThing(Client *client, v3f player_position,
*/
/*gui::IGUIStaticText **/
void draw_load_screen(const std::wstring &text,
video::IVideoDriver* driver, gui::IGUIFont* font)
video::IVideoDriver* driver, gui::IGUIFont* font, bool end_scene=true)
{
v2u32 screensize = driver->getScreenSize();
const wchar_t *loadingtext = text.c_str();
@ -521,7 +522,7 @@ void draw_load_screen(const std::wstring &text,
driver->beginScene(true, true, video::SColor(255,0,0,0));
guienv->drawAll();
driver->endScene();
if(end_scene) driver->endScene();
guitext->remove();
@ -1094,10 +1095,22 @@ void the_game(
ss<<L" Item definitions\n";
ss<<(client.nodedefReceived()?L"[X]":L"[ ]");
ss<<L" Node definitions\n";
ss<<L"["<<(int)(client.mediaReceiveProgress()*100+0.5)<<L"%] ";
ss<<L" Media\n";
//ss<<L"["<<(int)(client.mediaReceiveProgress()*100+0.5)<<L"%] ";
//ss<<L" Media\n";
draw_load_screen(ss.str(), driver, font);
draw_load_screen(ss.str(), driver, font, false);
v2u32 screensize = driver->getScreenSize();
core::vector2d<u32> textsize_u = font->getDimension(ss.str().c_str());
core::vector2d<s32> textsize(textsize_u.X,textsize_u.Y);
core::vector2d<s32> center(screensize.X/2, screensize.Y/2);
core::rect<s32> progrect(center - textsize/2, (center + textsize/2) + screensize.Y/16);
IProgressBar * pb = new IProgressBar(guienv,progrect,-1,0,font);
pb->setProgress((irr::u32)(client.mediaReceiveProgress()*100+0.5));
pb->addBorder(1);
pb->setText("Media");
pb->drop();
driver->endScene();
// Delay a bit
sleep_ms(1000*frametime);