custom gui element

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

custom gui element

Post by keless »

I am trying to create a custom gui element, "CGE_ProgressBar"

I have defined the class similar to the Button class, and as far as I can see everything is similar-- but it doesnt draw. In fact, its draw() function never gets called!

My code:

Code: Select all

class CGE_ProgressBar : public irr::gui::IGUIElement
{
	irr::s32 m_maxVal;	//value for bar to be 100%
	irr::s32 m_currVal; //current value of bar
	// bar completion = (m_currVal / m_maxVal)

	irr::video::SColor m_barColor;

	public:
		// class constructor
		CGE_ProgressBar(irr::gui::IGUIEnvironment *environment, irr::gui::IGUIElement *parent, irr::s32 id, irr::core::rect< irr::s32 > rectangle) ;

		// class destructor
		~CGE_ProgressBar();

		virtual void draw();
};

CGE_ProgressBar::CGE_ProgressBar(irr::gui::IGUIEnvironment *environment, irr::gui::IGUIElement *parent, irr::s32 id, irr::core::rect< irr::s32 > rectangle) :
		irr::gui::IGUIElement(environment, parent, id, rectangle)
{
	m_maxVal = 100;
	m_currVal = 100;

printf("CGE_PROG: creating bar\n");

	m_barColor = Environment->getSkin()->getColor(irr::gui::EGDC_3D_FACE);
	this->setEnabled(true);
	this->setVisible(true);
}

// class destructor
CGE_ProgressBar::~CGE_ProgressBar()
{
	// insert your code here
}

void CGE_ProgressBar::draw() {
printf("CGE_PROG: drawing\n");
	if (!IsVisible)
		return;
printf("CGE_PROG: drawing bar\n");
	irr::gui::IGUISkin* skin = Environment->getSkin();
	irr::video::IVideoDriver* driver = Environment->getVideoDriver();

	driver->draw2DRectangle(m_barColor, AbsoluteRect, &AbsoluteClippingRect);

	irr::gui::IGUIElement::draw();
}

usage:
irr::gui::IGUIElement* elem = new CGE_ProgressBar( env, 0, -1, irr::core::rect<irr::s32>(0, 0, 100, 100) );
elem->drop();
a screen cap is worth 0x100000 DWORDS
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

okay, the problem is that its not being set as the child of the GUIEnvironment.

in normal GUI elements, you call env->addSomeElement(), and this will set the parent to IGUIEnvironment if none is provided. That is done internally to the engine, because it knows the IGUIEnvironment object is actually a CGUIEnvironment which inherits from IGUIElement and thus has the addChild() function.

Because my custom gui element is outside of the library, it does not know or have access to this. If I try to cast it as a CGUIEnvironment or an IGUIElement, it doesnt work.

So basically in the current version of IrrLicht, 'custom' gui elements will only work if you set them as a child of some normal GUI element created by a normal env->addSomeElement call. This needs to be fixed-- perhaps you can declare IGUIEnvironment inherits from IGUIElement explicitly, and thus I'll have access to the addChild() function outside of the engine?

EDIT: hrm. i cant even get it to work as the child of a valid GUI Element.. its draw() function just never gets called..
a screen cap is worth 0x100000 DWORDS
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

keless wrote:Because my custom gui element is outside of the library, it does not know or have access to this. If I try to cast it as a CGUIEnvironment or an IGUIElement, it doesnt work.
That's why there is a method IGUIEnvironment->getRootGUIElement() (http://irrlicht.sourceforge.net/docu/cl ... t.html#a12). You can use this as parent. But I don't know why your element never is drawn...
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

using getRootGUIElement() to set the parent allows my CGE to draw itself, so now I am happy.
Last edited by keless on Tue Feb 03, 2004 5:57 pm, edited 1 time in total.
a screen cap is worth 0x100000 DWORDS
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

keless wrote:though now it doesnt seem to be removing itself. "if its not one thing, its another" lol
*g* strange
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

ah, it was my fault as I suspected. had an extra Parent->addChild() call in my CGE so that when it was being removed, there was still another instance of itself registered with the parent. removed this line, and it works fine.

tx for the replys niko
a screen cap is worth 0x100000 DWORDS
Post Reply