From 4af41ee77a75b75f415bb00ccd6f0ea00613b93a Mon Sep 17 00:00:00 2001 From: auria Date: Thu, 24 Mar 2011 22:51:17 +0000 Subject: [PATCH] Fixed label scrolling code git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@8048 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/guiengine/widgets/label_widget.cpp | 84 ++++++++++++++------------ src/guiengine/widgets/label_widget.hpp | 10 +-- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/guiengine/widgets/label_widget.cpp b/src/guiengine/widgets/label_widget.cpp index e9b4515d6..d0ddd8174 100644 --- a/src/guiengine/widgets/label_widget.cpp +++ b/src/guiengine/widgets/label_widget.cpp @@ -60,10 +60,18 @@ void LabelWidget::add() else if (m_properties[PROP_TEXT_ALIGN] == "right") align = EGUIA_LOWERRIGHT; EGUI_ALIGNMENT valign = EGUIA_CENTER ; //TODO: make label v-align configurable through XML file? - IGUIStaticText* irrwidget; - irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), widget_size, - false, word_wrap, m_parent, -1); - + IGUIStaticText* irrwidget; + if (m_scroll_offset) + { + IGUIElement* container = GUIEngine::getGUIEnv()->addButton(widget_size, m_parent, -1); + irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), core::rect( core::position2di(0,0), widget_size.getSize()), + false, word_wrap, /*m_parent*/ container, -1); + } + else + { + irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), widget_size, + false, word_wrap, m_parent, -1); + } #if IRRLICHT_VERSION_MAJOR > 1 || (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) irrwidget->setRightToLeft( translations->isRTLLanguage() ); #endif @@ -89,7 +97,18 @@ void LabelWidget::add() m_element->setTabStop(false); m_element->setTabGroup(false); - m_element->setNotClipped(true); + if (!m_scroll_offset) + { + m_element->setNotClipped(true); + } + + if (m_scroll_speed > 0) + { + IGUIFont* font = m_title_font ? GUIEngine::getTitleFont() + : GUIEngine::getFont(); + core::dimension2du r = font->getDimension(getText().c_str()); + m_scroll_offset = r.Width; + } } // add // ---------------------------------------------------------------------------- @@ -106,31 +125,13 @@ void LabelWidget::add() void LabelWidget::setText(const wchar_t *text) { m_scroll_offset = 0; - if(m_scroll_speed<=0) + + if (m_scroll_speed > 0) { - Widget::setText(text); - return; + m_scroll_offset = m_element->getAbsolutePosition().getWidth(); } - - // 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; i1) + if (m_scroll_speed != 0) { - 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 --; + m_scroll_offset -= dt*m_scroll_speed*5.0f; + + //printf("m_scroll_offset = %f; x = %f\n", m_scroll_offset, m_x + m_scroll_offset); + + m_element->setRelativePosition( core::position2di( /*m_x +*/ m_scroll_offset, /*m_y*/ 0 ) ); } } // update // ---------------------------------------------------------------------------- @@ -156,5 +155,14 @@ void LabelWidget::update(float dt) */ bool LabelWidget::scrolledOff() const { - return getText().size()==0; -} \ No newline at end of file + return m_scroll_offset <= -m_element->getAbsolutePosition().getWidth(); +} + +// ---------------------------------------------------------------------------- +/** Sets horizontal scroll speed. */ +void LabelWidget::setScrollSpeed(float speed) +{ + m_scroll_offset = -m_element->getAbsolutePosition().getWidth() - 10; // start scrolled off + m_scroll_speed = speed; +} // setScrollSpeed + diff --git a/src/guiengine/widgets/label_widget.hpp b/src/guiengine/widgets/label_widget.hpp index 75c27eebb..7a6789ea9 100644 --- a/src/guiengine/widgets/label_widget.hpp +++ b/src/guiengine/widgets/label_widget.hpp @@ -56,19 +56,19 @@ namespace GUIEngine 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 + void setScrollSpeed(float speed); + // -------------------------------------------------------------------- bool scrolledOff() const;