Add resizing code for spinner widget

This commit is contained in:
CodingJellyfish 2024-04-25 10:13:24 +08:00
parent 0dffd116a9
commit 5adbb3f16b
2 changed files with 28 additions and 31 deletions

View File

@ -96,6 +96,7 @@ SpinnerWidget::SpinnerWidget(const bool gauge) : Widget(WTYPE_SPINNER)
m_check_inside_me = true; //FIXME: not sure this is necessary
m_supports_multiplayer = true;
m_value = -1;
m_badge_x_shift = 0;
m_use_background_color=false;
m_spinner_widget_player_id=-1;
m_min = 0;
@ -169,34 +170,28 @@ void SpinnerWidget::add()
widgetID = getNewID();
}
rect<s32> widget_size = rect<s32>(m_x, m_y, m_x + m_w, m_y + m_h);
// To be resized later
rect<s32> widget_size = rect<s32>(0, 0, 1, 1);
IGUIButton * btn = new SpinnerIrrElement(widget_size, m_parent, widgetID, this);
m_element = btn;
m_element->setTabOrder( m_element->getID() );
// left arrow
rect<s32> subsize_left_arrow = rect<s32>(0 ,0, m_h, m_h);
IGUIButton * left_arrow = GUIEngine::getGUIEnv()->addButton(subsize_left_arrow, btn, getNewNoFocusID(), L" ");
IGUIButton * left_arrow = GUIEngine::getGUIEnv()->addButton(widget_size, btn, getNewNoFocusID(), L" ");
m_children[0].m_element = left_arrow;
left_arrow->setTabStop(false);
m_children[0].m_event_handler = this;
m_children[0].m_properties[PROP_ID] = "left";
m_children[0].m_id = m_children[0].m_element->getID();
m_badge_x_shift = subsize_left_arrow.getWidth();
// label
if (m_graphical)
{
ITexture* texture = getTexture();
assert(texture != NULL);
const int texture_width = texture->getSize().Width;
const int free_h_space = m_w - m_h*2 - texture_width; // to center image
rect<s32> subsize_label = rect<s32>(m_h + free_h_space/2, 0, m_w - m_h+ free_h_space/2, m_h);
IGUIImage * subbtn = GUIEngine::getGUIEnv()->addImage(subsize_label, btn, getNewNoFocusID());
IGUIImage * subbtn = GUIEngine::getGUIEnv()->addImage(widget_size, btn, getNewNoFocusID());
m_children[1].m_element = subbtn;
m_children[1].m_id = subbtn->getID();
m_children[1].m_event_handler = this;
@ -208,9 +203,8 @@ void SpinnerWidget::add()
}
else
{
rect<s32> subsize_label = rect<s32>(m_h, 0, m_w - m_h, m_h);
stringw text = stringw(m_value);
IGUIStaticText* label = GUIEngine::getGUIEnv()->addStaticText(text.c_str(), subsize_label,
IGUIStaticText* label = GUIEngine::getGUIEnv()->addStaticText(text.c_str(), widget_size,
false /* border */, true /* word wrap */,
btn, getNewNoFocusID());
m_children[1].m_element = label;
@ -225,17 +219,11 @@ void SpinnerWidget::add()
{
label->setText(m_labels[m_value].c_str() );
}
if (widget_size.getHeight() < GUIEngine::getFontHeight())
{
label->setOverrideFont(GUIEngine::getSmallFont());
}
}
// right arrow
rect<s32> subsize_right_arrow = rect<s32>(m_w - m_h, 0, m_w, m_h);
IGUIButton * right_arrow = GUIEngine::getGUIEnv()->addButton(subsize_right_arrow, btn, getNewNoFocusID(), L" ");
IGUIButton * right_arrow = GUIEngine::getGUIEnv()->addButton(widget_size, btn, getNewNoFocusID(), L" ");
right_arrow->setTabStop(false);
m_children[2].m_element = right_arrow;
m_children[2].m_event_handler = this;
@ -243,8 +231,7 @@ void SpinnerWidget::add()
m_children[2].m_id = m_children[2].m_element->getID();
// refresh display
resize();
setValue(m_value);
}
// -----------------------------------------------------------------------------
@ -260,12 +247,13 @@ ITexture* SpinnerWidget::getTexture()
// -----------------------------------------------------------------------------
void SpinnerWidget::move(const int x, const int y, const int w, const int h)
void SpinnerWidget::resize()
{
Widget::move(x, y, w, h);
Widget::resize();
rect<s32> subsize_left_arrow = rect<s32>(0 ,0, h, h);
rect<s32> subsize_left_arrow = rect<s32>(0 ,0, m_h, m_h);
m_children[0].m_element->setRelativePosition(subsize_left_arrow);
m_badge_x_shift = subsize_left_arrow.getWidth();
if (m_graphical)
{
@ -273,18 +261,27 @@ void SpinnerWidget::move(const int x, const int y, const int w, const int h)
assert(texture != NULL);
const int texture_width = texture->getSize().Width;
const int free_h_space = w-h*2-texture_width; // to center image
const int free_h_space = m_w-m_h*2-texture_width; // to center image
rect<s32> subsize_label = rect<s32>(h+free_h_space/2, 0, w-h+free_h_space/2, h);
rect<s32> subsize_label = rect<s32>(m_h+free_h_space/2, 0, m_w-m_h+free_h_space/2, m_h);
m_children[1].m_element->setRelativePosition(subsize_label);
}
else
{
rect<s32> subsize_label = rect<s32>(h, 0, w-h, h);
m_children[1].m_element->setRelativePosition(subsize_label);
rect<s32> subsize_label = rect<s32>(m_h, 0, m_w-m_h, m_h);
IGUIStaticText* label = static_cast<IGUIStaticText*>(m_children[1].m_element);
label->setRelativePosition(subsize_label);
if (m_h < GUIEngine::getFontHeight())
{
label->setOverrideFont(GUIEngine::getSmallFont());
}
else
{
label->setOverrideFont(NULL);
}
}
rect<s32> subsize_right_arrow = rect<s32>(w-h, 0, w, h);
rect<s32> subsize_right_arrow = rect<s32>(m_w-m_h, 0, m_w, m_h);
m_children[2].m_element->setRelativePosition(subsize_right_arrow);
}

View File

@ -120,7 +120,7 @@ namespace GUIEngine
SpinnerWidget(const bool gauge=false);
virtual ~SpinnerWidget() {}
virtual void move(const int x, const int y, const int w, const int h);
virtual void resize();
void addLabel(irr::core::stringw label);
void clearLabels();