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