Added support for horizontally scrolling text.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@8045 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2011-03-24 21:06:11 +00:00
parent 4812ba21dd
commit 8f34aea2b4
3 changed files with 92 additions and 3 deletions

View File

@@ -401,7 +401,7 @@ namespace GUIEngine
* \note This mehtod uses the virtual setText(wchar_t*) function, so only the latter
* needs to be overwritten by other classes.
*/
void setText(const irr::core::stringw &s) { setText(s.c_str()); }
virtual void setText(const irr::core::stringw &s) { setText(s.c_str()); }
/** Returns the text of a widget. */
const irr::core::stringw &getText() const {return m_text; }

View File

@@ -35,8 +35,9 @@ using namespace irr;
*/
LabelWidget::LabelWidget(bool title, bool bright) : Widget(WTYPE_LABEL)
{
m_title_font = title;
m_has_color = false;
m_title_font = title;
m_has_color = false;
m_scroll_speed = 0;
if (bright)
{
@@ -92,3 +93,68 @@ void LabelWidget::add()
} // add
// ----------------------------------------------------------------------------
/** Sets the text. This is the function used by overloaded functions
* as well. It takes esp. care of horizontal scrolling by adding
* spaces to the left and right of the string, so that it appears
* that the text is appearing from the left and going to the right.
* Then a character is removed on the left, resulting in the text
* appearing to scroll from the left.
* It is important that the scrolling speed (if any) is set before
* calling this function!
* \param text The string to use as text for this widget.
*/
void LabelWidget::setText(const wchar_t *text)
{
m_scroll_offset = 0;
if(m_scroll_speed<=0)
{
Widget::setText(text);
return;
}
// Now the widget is scrolling. So add enough spaces
// to the left and right of the string so that it scrolls
// in.
IGUIFont* font = m_title_font ? GUIEngine::getTitleFont()
: GUIEngine::getFont();
core::dimension2du r = font->getDimension(stringw(" ").c_str());
unsigned int n = m_w / r.Width+1;
stringw spaces="";
spaces.reserve(n+1); // include end 0
for(unsigned int i=0; i<n; i++)
{
spaces.append(' ');
}
// The c_str is important, otherwise it will call
// LabelWidget::setText(wstring), which will turn
// this function again.
Widget::setText((spaces+text+spaces).c_str());
} // setText
// ----------------------------------------------------------------------------
/** Needs to be called to update scrolling.
* \param dt Time step size.
*/
void LabelWidget::update(float dt)
{
m_scroll_offset += dt*m_scroll_speed;
while(m_scroll_offset>1)
{
const core::stringw &w= getText();
// The c_str is important, otherwise it will call
// LabelWidget::setText(wstring), which would add
// more spaces again.
Widget::setText(w.subString(1, w.size()-1).c_str());
m_scroll_offset --;
}
} // update
// ----------------------------------------------------------------------------
/** Returns true if the text has completely scrolled off. This is detected
* by checking if the remaining text is empty.
*/
bool LabelWidget::scrolledOff() const
{
return getText().size()==0;
}

View File

@@ -35,6 +35,10 @@ namespace GUIEngine
bool m_has_color;
irr::video::SColor m_color;
/** Scroll speed in characters/seconds (0 if no scrolling). */
float m_scroll_speed;
/** Current scroll offset. */
float m_scroll_offset;
public:
LabelWidget(bool title=false, bool bright=false);
virtual ~LabelWidget() {}
@@ -49,6 +53,25 @@ namespace GUIEngine
m_has_color = true;
} // setColor
virtual void setText(const wchar_t *text);
virtual void update(float dt);
// --------------------------------------------------------------------
/** Overloaded function which takes a stringw. */
virtual void setText(const irr::core::stringw &s)
{
setText(s.c_str());
}
// --------------------------------------------------------------------
/** Sets horizontal scroll speed. */
void setScrollSpeed(float speed)
{
m_scroll_offset = 0;
m_scroll_speed = speed;
} // setScrollSpeed
// --------------------------------------------------------------------
bool scrolledOff() const;
};
}