Skip to content

Commit

Permalink
Add resizing code for bubble widget
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingJellyfish committed Apr 30, 2024
1 parent 0716965 commit c9d4473
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/guiengine/widgets/bubble_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ BubbleWidget::BubbleWidget() : Widget(WTYPE_BUBBLE)

// ----------------------------------------------------------------------------

rect<s32> BubbleWidget::getShrinkedSize()
{
recti size = rect<s32>(m_x, m_y, m_x + m_w - BUBBLE_MARGIN_ON_RIGHT, m_y + m_h);
size.LowerRightCorner.Y -= BOTTOM_MARGIN;
return size;
}

// ----------------------------------------------------------------------------

void BubbleWidget::add()
{
m_shrinked_size = rect<s32>(m_x, m_y, m_x + m_w - BUBBLE_MARGIN_ON_RIGHT, m_y + m_h);
m_shrinked_size = getShrinkedSize();
stringw message = getText();

m_shrinked_size.LowerRightCorner.Y -= BOTTOM_MARGIN;

IGUIStaticText* irrwidget;
irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), m_shrinked_size,
false, true /* word wrap */, m_parent,
Expand All @@ -74,23 +81,20 @@ void BubbleWidget::add()
m_element->setNotClipped(true);
}

void BubbleWidget::replaceText()
void BubbleWidget::resize()
{
IGUIStaticText* irrwidget = (IGUIStaticText*) m_element;
// Take border into account for line breaking (happens in setText)
irrwidget->setDrawBorder(true);

stringw message = getText();

EGUI_ALIGNMENT align = EGUIA_UPPERLEFT;
if (m_properties[PROP_TEXT_ALIGN] == "center") align = EGUIA_CENTER;
else if (m_properties[PROP_TEXT_ALIGN] == "right") align = EGUIA_LOWERRIGHT;

EGUI_ALIGNMENT valign = EGUIA_CENTER;
if (m_properties[PROP_TEXT_VALIGN] == "top") valign = EGUIA_UPPERLEFT;
if (m_properties[PROP_TEXT_VALIGN] == "bottom") valign = EGUIA_LOWERRIGHT;
m_shrinked_size = getShrinkedSize();
m_element->setRelativePosition(m_shrinked_size);
updateForNewSize();
updateSize();
}

void BubbleWidget::updateForNewSize()
{
IGUIStaticText* irrwidget = static_cast<IGUIStaticText*>(m_element);
// find expanded bubble size
stringw message = getText();
irrwidget->setText(message);
int text_height = irrwidget->getTextHeight();

m_expanded_size = m_shrinked_size;
Expand All @@ -106,11 +110,28 @@ void BubbleWidget::replaceText()
while (text_height > m_shrinked_size.getHeight() && message.size() > 10)
{
message = message.subString(0, message.size() - 10) + "...";
irrwidget->setText(message.c_str());
irrwidget->setText(message);
text_height = irrwidget->getTextHeight();
}
}
m_shrinked_text = message;
}

void BubbleWidget::replaceText()
{
IGUIStaticText* irrwidget = (IGUIStaticText*) m_element;
// Take border into account for line breaking (happens in setText)
irrwidget->setDrawBorder(true);

EGUI_ALIGNMENT align = EGUIA_UPPERLEFT;
if (m_properties[PROP_TEXT_ALIGN] == "center") align = EGUIA_CENTER;
else if (m_properties[PROP_TEXT_ALIGN] == "right") align = EGUIA_LOWERRIGHT;

EGUI_ALIGNMENT valign = EGUIA_CENTER;
if (m_properties[PROP_TEXT_VALIGN] == "top") valign = EGUIA_UPPERLEFT;
if (m_properties[PROP_TEXT_VALIGN] == "bottom") valign = EGUIA_LOWERRIGHT;

updateForNewSize();
irrwidget->setTextAlignment( align, valign );
}

Expand Down
5 changes: 5 additions & 0 deletions src/guiengine/widgets/bubble_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ namespace GUIEngine
/** Will add/replace text in the bubble. If it doesn't fit, the text will get shrinked. **/
void replaceText();

void updateForNewSize();

irr::core::rect<irr::s32> getShrinkedSize();
public:

LEAK_CHECK()
Expand All @@ -70,6 +73,8 @@ namespace GUIEngine
void setText(const irr::core::stringw &s);

virtual int getHeightNeededAroundLabel() const { return 10; }

virtual void resize();
};

}
Expand Down

0 comments on commit c9d4473

Please sign in to comment.