Add resizing code for label widget

This commit is contained in:
CodingJellyfish 2024-04-28 11:15:41 +08:00
parent 97ca195e20
commit f6949972b3
4 changed files with 60 additions and 22 deletions

View File

@ -40,7 +40,9 @@ LabelWidget::LabelWidget(LabelType type) : Widget(WTYPE_LABEL)
{ {
m_type = type; m_type = type;
m_scroll_speed = 0; m_scroll_speed = 0;
m_per_character_size = 0;
m_scroll_offset = 0; m_scroll_offset = 0;
m_expand_if_needed = false;
if (m_type == BRIGHT) if (m_type == BRIGHT)
{ {
@ -109,6 +111,8 @@ void LabelWidget::add()
irrwidget->setOverrideColor( video::SColor(255,255,255,255) ); irrwidget->setOverrideColor( video::SColor(255,255,255,255) );
irrwidget->setOverrideFont( GUIEngine::getTinyTitleFont() ); irrwidget->setOverrideFont( GUIEngine::getTinyTitleFont() );
} }
m_per_character_size = getCurrentFont()->getDimension(L"X").Height;
//irrwidget->setBackgroundColor( video::SColor(255,255,0,0) ); //irrwidget->setBackgroundColor( video::SColor(255,255,0,0) );
//irrwidget->setDrawBackground(true); //irrwidget->setDrawBackground(true);
@ -129,19 +133,27 @@ void LabelWidget::add()
void LabelWidget::setText(const core::stringw& text, bool expandIfNeeded) void LabelWidget::setText(const core::stringw& text, bool expandIfNeeded)
{ {
m_scroll_offset = 0; m_scroll_offset = 0;
m_expand_if_needed = expandIfNeeded;
updateExpandedText(text);
Widget::setText(text);
} // setText
if (expandIfNeeded) // ----------------------------------------------------------------------------
irr::gui::IGUIFont* LabelWidget::getCurrentFont() const
{
IGUIFont* font = static_cast<IGUIStaticText*>(m_element)->getOverrideFont();
if (!font)
font = GUIEngine::getFont();
return font;
} // getCurrentFont
// ----------------------------------------------------------------------------
void LabelWidget::updateExpandedText(const irr::core::stringw& text)
{
if (m_expand_if_needed)
{ {
assert(m_element != NULL); assert(m_element != NULL);
int fwidth; int fwidth = getCurrentFont()->getDimension(text.c_str()).Width;
if(m_type == TITLE)
fwidth = GUIEngine::getTitleFont()->getDimension(text.c_str()).Width;
else if(m_type == SMALL_TITLE)
fwidth = GUIEngine::getSmallTitleFont()->getDimension(text.c_str()).Width;
else if(m_type == TINY_TITLE)
fwidth = GUIEngine::getTinyTitleFont()->getDimension(text.c_str()).Width;
else
fwidth = GUIEngine::getFont()->getDimension(text.c_str()).Width;
core::rect<s32> rect = m_element->getRelativePosition(); core::rect<s32> rect = m_element->getRelativePosition();
if (rect.getWidth() < fwidth) if (rect.getWidth() < fwidth)
@ -151,12 +163,7 @@ void LabelWidget::setText(const core::stringw& text, bool expandIfNeeded)
m_element->updateAbsolutePosition(); m_element->updateAbsolutePosition();
} }
} }
} // updateExpandedText
if (m_scroll_speed > 0)
m_scroll_offset = (float)m_w;
Widget::setText(text);
} // setText
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Needs to be called to update scrolling. /** Needs to be called to update scrolling.
@ -166,8 +173,9 @@ void LabelWidget::update(float dt)
{ {
if (m_scroll_speed != 0) if (m_scroll_speed != 0)
{ {
m_scroll_offset -= dt*m_scroll_speed*5.0f; m_scroll_offset -= dt * m_scroll_speed * 5.0f;
m_element->setRelativePosition( core::position2di( /*m_x +*/ (int)m_scroll_offset, int offset = (float)m_w + m_scroll_offset * m_per_character_size;
m_element->setRelativePosition( core::position2di( /*m_x +*/ offset,
/*m_y*/ 0 ) ); /*m_y*/ 0 ) );
} }
} // update } // update
@ -177,7 +185,8 @@ bool LabelWidget::scrolledOff() const
{ {
// This method may only be called after this widget has been add()ed // This method may only be called after this widget has been add()ed
assert(m_element != NULL); assert(m_element != NULL);
return m_scroll_offset <= -m_element->getAbsolutePosition().getWidth(); return (float)m_w + m_scroll_offset * m_per_character_size <=
-1.0f * (float)m_element->getAbsolutePosition().getWidth();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -189,6 +198,23 @@ void LabelWidget::setScrollSpeed(float speed)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void LabelWidget::resize()
{
m_per_character_size = getCurrentFont()->getDimension(L"X").Height;
if (m_scroll_speed != 0)
{
rect<s32> widget_size = rect<s32>(m_x, m_y, m_x + m_w, m_y + m_h);
core::rect<s32> r(core::position2di(0, 0), widget_size.getSize());
m_element->getParent()->setRelativePosition(widget_size);
m_element->setRelativePosition(r);
updateExpandedText(m_text);
}
else
Widget::resize();
} // resize
// ----------------------------------------------------------------------------
void LabelWidget::setColor(const irr::video::SColor& color) void LabelWidget::setColor(const irr::video::SColor& color)
{ {
assert(m_element != NULL); assert(m_element != NULL);

View File

@ -27,6 +27,11 @@
#include "utils/leak_check.hpp" #include "utils/leak_check.hpp"
#include "utils/ptr_vector.hpp" #include "utils/ptr_vector.hpp"
namespace irr
{
namespace gui { class IGUIFont; }
}
namespace GUIEngine namespace GUIEngine
{ {
/** \brief A simple label widget. /** \brief A simple label widget.
@ -51,10 +56,16 @@ namespace GUIEngine
LabelType m_type; LabelType m_type;
irr::video::SColor m_color; irr::video::SColor m_color;
bool m_has_color; bool m_has_color;
bool m_expand_if_needed;
irr::gui::IGUIFont* getCurrentFont() const;
void updateExpandedText(const irr::core::stringw& text);
/** Scroll speed in characters/seconds (0 if no scrolling). */ /** Scroll speed in characters/seconds (0 if no scrolling). */
float m_scroll_speed; float m_scroll_speed;
float m_per_character_size;
/** Current scroll offset. */ /** Current scroll offset. */
float m_scroll_offset; float m_scroll_offset;
@ -114,6 +125,7 @@ namespace GUIEngine
*/ */
bool scrolledOff() const; bool scrolledOff() const;
virtual void resize();
}; };
} }

View File

@ -86,7 +86,7 @@ MainMenuScreen::MainMenuScreen() : Screen("main_menu.stkgui")
void MainMenuScreen::loadedFromFile() void MainMenuScreen::loadedFromFile()
{ {
LabelWidget* w = getWidget<LabelWidget>("info_addons"); LabelWidget* w = getWidget<LabelWidget>("info_addons");
w->setScrollSpeed(GUIEngine::getFontHeight() / 2); w->setScrollSpeed(0.5f);
RibbonWidget* rw_top = getWidget<RibbonWidget>("menu_toprow"); RibbonWidget* rw_top = getWidget<RibbonWidget>("menu_toprow");
assert(rw_top != NULL); assert(rw_top != NULL);

View File

@ -450,7 +450,7 @@ void NetworkingLobby::onUpdate(float delta)
m_header->getIrrlichtElement()->remove(); m_header->getIrrlichtElement()->remove();
if (m_header_text_width > m_header->m_w) if (m_header_text_width > m_header->m_w)
{ {
m_header->setScrollSpeed(GUIEngine::getTitleFontHeight() / 2); m_header->setScrollSpeed(0.5f);
m_header->add(); m_header->add();
m_header->setText(m_header_text, true); m_header->setText(m_header_text, true);
} }