added to IGUIListBox setOverrideFont function [GUI][C++]

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
sp00n
Posts: 114
Joined: Wed Sep 13, 2006 9:39 am

added to IGUIListBox setOverrideFont function [GUI][C++]

Post by sp00n »

So, in my current project I need to use multiple fonts in the listbox, and i desided add it directly to Irrlicht.
I added two functions: setOverrideFont and getOverrideFont, also modifyed addItem. Well, this are modyfications:
IGUIListBox.h

Code: Select all

//! adds an list item with an icon
		//! \param text Text of list entry
		//! \param icon Sprite index of the Icon within the current sprite bank. Set it to -1 if you want no icon
		//! \param new_font Font of the item, if 0 uses default font
		//! \return
		//! returns the id of the new created item
		virtual u32 addItem(const wchar_t* text, s32 icon, IGUIFont * new_font = 0) = 0;

		//! sets an override font ot item
		virtual void setItemOverrideFont(s32 id, IGUIFont * ov_font) = 0;

		//! gets the font of the item
		virtual IGUIFont * getItemOverrideFont (s32 id) = 0;
CGUIListBox.h

Code: Select all

public:
...
//! adds an list item with an icon
		//! \param text Text of list entry
		//! \param icon Sprite index of the Icon within the current sprite bank. Set it to -1 if you want no icon
		//! \param new_font Font of the item, if 0 uses default font
		//! \return
		//! returns the id of the new created item
		virtual u32 addItem(const wchar_t* text, s32 icon, IGUIFont * new_font = 0);

		//! sets the font of the item
		virtual void setItemOverrideFont(s32 id, IGUIFont * ov_font);

		//! gets the font of the item
		virtual IGUIFont * getItemOverrideFont (s32 id);
...
private:
struct ListItem
		{
			ListItem() : icon(-1) 
			{}

			core::stringw text;
			s32 icon;
			gui::IGUIFont* itemFont;//Font of the item

			// A multicolor extension
			struct ListItemOverrideColor
			{
				ListItemOverrideColor() : Use(false) {}
				bool Use;
				video::SColor Color;
			};
			ListItemOverrideColor OverrideColors[EGUI_LBC_COUNT];
		};
...
CGUIListBox.h

Code: Select all

...
void CGUIListBox::draw()
{
...
textRect.UpperLeftCorner.X += ItemsIconWidth+3;

				if ( i==Selected && hl )
				{
					if (Items[i].itemFont)
						Items[i].itemFont->draw(Items[i].text.c_str(), textRect,
						hasItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) ? getItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_TEXT_HIGHLIGHT),
						false, true, &clientClip);
					else
					Font->draw(Items[i].text.c_str(), textRect,
						hasItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) ? getItemOverrideColor(i, EGUI_LBC_TEXT_HIGHLIGHT) : getItemDefaultColor(EGUI_LBC_TEXT_HIGHLIGHT),
						false, true, &clientClip);
				}
				else
				{
					if (Items[i].itemFont)
						Items[i].itemFont->draw(Items[i].text.c_str(), textRect,
						hasItemOverrideColor(i, EGUI_LBC_TEXT) ? getItemOverrideColor(i, EGUI_LBC_TEXT) : getItemDefaultColor(EGUI_LBC_TEXT),
						false, true, &clientClip);
					else
					Font->draw(Items[i].text.c_str(), textRect,
						hasItemOverrideColor(i, EGUI_LBC_TEXT) ? getItemOverrideColor(i, EGUI_LBC_TEXT) : getItemDefaultColor(EGUI_LBC_TEXT),
						false, true, &clientClip);
				}

				textRect.UpperLeftCorner.X -= ItemsIconWidth+3;
...}

//! adds an list item with an icon
u32 CGUIListBox::addItem(const wchar_t* text, s32 icon, IGUIFont * new_font)
{
	ListItem i;
	i.text = text;
	i.icon = icon;
	if (new_font)
		i.itemFont = new_font;
	else
		if (Font)
			i.itemFont = Font;
		else
			i.itemFont = 0;

	Items.push_back(i);
	recalculateItemHeight();
	recalculateItemWidth(icon);

	return Items.size() - 1;
}

void CGUIListBox::setItemOverrideFont(s32 id, IGUIFont * ov_font)
{
	if (ov_font)
	{
		Items[id].itemFont = ov_font;
	}
}

IGUIFont* CGUIListBox::getItemOverrideFont(s32 id)
{
	if (Items[id].itemFont)
		return Items[id].itemFont;
	else
		return 0;
}
...
Now I have a question - did i miss any memory leaks? Because when i add to the destructor loop with all items drop their fonts it asserts me on drop(), when i remove it all ok and compiler/linker doesn't say me anything about leaks.
p.s. here's the link to sources - http://www.megaupload.com/?d=4UV3NUVC
p.p.s. oh and it needs to modify recalculateItemHeight() function, but just it no needs in my current project (i have all fonts with the same height) i didn't realize it yet, but may be tommorow i'll modify it :)
Best regards.
Here's the screen:
Image
Post Reply