(C++) Simple scrolling fading message list

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
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

(C++) Simple scrolling fading message list

Post by rogerborg »

NOTE: This code requires a recent SVN build of Irrlicht.

Here's some code for a simple message list suitable for consoles, message windows, chat apps or similiar. You just tell it how big you want the text area to be and what font you want to use, and it handles everything from there.

It scrolls the text up or down the screen as you add new strings, and fades it out as it scrolls along the list. I needed this because I'm using it for a message log that overlays my main screen, and I want it to fade out as it approaches the centre of the screen.

Code: Select all

class CFadingTextList
{
public:

	//! \param[in] gui: Your GUI environment
    //! \param[in] font: The font to use for all the text
    //! \param[in] totalSize: The total size covered by the whole list
    //! \param[in] initialAlpha: The alpha value for the most recent text
    //! \param[in] finalAlpha: The alpha value for the oldest text
    //! \param[in] scrollDown: If true, new text appears at the top of the area and scrolls down
	CFadingTextList(IGUIEnvironment * gui, IGUIFont * font, rect<s32> const & totalSize,
					u8 initialAlpha = 255, u8 finalAlpha = 64, bool scrollDown = true)
	{
		if(!gui || !font)
			return;

		s32 itemHeight = font->getDimension(L"A").Height;
		m_maxItems = (totalSize.LowerRightCorner.Y - totalSize.UpperLeftCorner.Y) / itemHeight;

		m_texts = (IGUIStaticText * *)malloc(sizeof(IGUIStaticText *) * m_maxItems);

		f32 currentAlpha = (f32)initialAlpha;
		f32 stepAlpha = (currentAlpha - finalAlpha) / (m_maxItems - 1);

		for(int item = 0; item < m_maxItems; ++item)
		{
			m_texts[item] = gui->addStaticText(L"", totalSize);
			m_texts[item]->setOverrideFont(font);
			m_texts[item]->enableOverrideColor(true);

            s32 topOfLine = 0;

			if(scrollDown)
				topOfLine = totalSize.UpperLeftCorner.Y + (itemHeight * item);
			else
				topOfLine = totalSize.LowerRightCorner.Y - (itemHeight * (item + 1));

			m_texts[item]->setRelativePosition(rect<s32>(totalSize.UpperLeftCorner.X,
														topOfLine,
														totalSize.LowerRightCorner.X,
														topOfLine + itemHeight));

			m_texts[item]->setOverrideColor(SColor((s8)currentAlpha, 255, 255, 255));
			currentAlpha -= stepAlpha;
		}
	}

	virtual ~CFadingTextList()
	{
		free(m_texts);
		m_texts = 0;
	}

    //! \param text: The new text, as a wchar_t string
    //! \param colour: the colour of the new text (the alpha will be ignored)
	void addText(wchar_t const * text, SColor const & colour = SColor(255, 255, 255, 255))
	{
	   // Scroll all the existing text
		for(int item = m_maxItems - 1; item > 0; --item)
		{
			m_texts[item]->setText(m_texts[item - 1]->getText());
			SColor movedColour(m_texts[item - 1]->getOverrideColor());
			movedColour.setAlpha(m_texts[item ]->getOverrideColor().getAlpha());
			m_texts[item]->setOverrideColor(movedColour);
		}

        // Set the text at the start of the list
		SColor newColour(colour);
		newColour.setAlpha(m_texts[0]->getOverrideColor().getAlpha());
		m_texts[0]->setOverrideColor(newColour);
		m_texts[0]->setText(text);

	}

    //! \param text: The new text, as an ASCII string
    //! \param colour: the colour of the new text (the alpha will be ignored)
	void addText(char const * text, SColor const & colour = SColor(255, 255, 255, 255))
	{
		irr::core::stringw wideText;
		irr::core::stringc_to_stringw(wideText, text);
		addText(wideText.c_str(), colour);
	}

private:
	IGUIStaticText * *	m_texts;
	int					m_maxItems;
}; // class CFadingTextList
PS: I'm sure I posted this yesterday, but perhaps I snafud the post. Apologies if it appears twice.
Last edited by rogerborg on Thu Feb 15, 2007 8:23 am, edited 1 time in total.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

Hi,
Good idea.

I'm trying to get it running, just to see how it looks. A question about the line

Code: Select all

SColor movedColour(m_texts[item - 1]->getOverrideColor());
Where can I find infos about the getOverrideColor method? Cant find it on IGUIStaticText page
http://irrlicht.sourceforge.net/docu/cl ... _text.html :oops:

Also, stringc_to_stringw() isn't recognized by the compiler.
Did I miss an include?

Thanks in advance
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Oops, sorry; getOverrideColor() is ony available in recent SVN source; I had to add it to support this functionality.

stringc_to_stringw() is in irrstring.h. I should have qualified it as irr::core::
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Post Reply