new system for dynamic sizing of buttons & text boxes

actorid
scrawl 12 years ago
parent 35dd49f948
commit b66c8099eb

@ -66,26 +66,6 @@ namespace MWGui
getWidget(itemView, "ItemView");
setWidgets(containerWidget, itemView);
// adjust size of buttons to fit text
int curX = 0;
mFilterAll->setSize( mFilterAll->getTextSize().width + 24, mFilterAll->getSize().height );
curX += mFilterAll->getTextSize().width + 24 + 4;
mFilterWeapon->setPosition(curX, mFilterWeapon->getPosition().top);
mFilterWeapon->setSize( mFilterWeapon->getTextSize().width + 24, mFilterWeapon->getSize().height );
curX += mFilterWeapon->getTextSize().width + 24 + 4;
mFilterApparel->setPosition(curX, mFilterApparel->getPosition().top);
mFilterApparel->setSize( mFilterApparel->getTextSize().width + 24, mFilterApparel->getSize().height );
curX += mFilterApparel->getTextSize().width + 24 + 4;
mFilterMagic->setPosition(curX, mFilterMagic->getPosition().top);
mFilterMagic->setSize( mFilterMagic->getTextSize().width + 24, mFilterMagic->getSize().height );
curX += mFilterMagic->getTextSize().width + 24 + 4;
mFilterMisc->setPosition(curX, mFilterMisc->getPosition().top);
mFilterMisc->setSize( mFilterMisc->getTextSize().width + 24, mFilterMisc->getSize().height );
mFilterAll->eventMouseButtonClick += MyGUI::newDelegate(this, &InventoryWindow::onFilterChanged);
mFilterWeapon->eventMouseButtonClick += MyGUI::newDelegate(this, &InventoryWindow::onFilterChanged);
mFilterApparel->eventMouseButtonClick += MyGUI::newDelegate(this, &InventoryWindow::onFilterChanged);

@ -789,3 +789,165 @@ void MWDynamicStat::initialiseOverride()
assignWidget(mBarWidget, "Bar");
assignWidget(mBarTextWidget, "BarText");
}
// ---------------------------------------------------------------------------------------------------------------------
void AutoSizedWidget::notifySizeChange (MyGUI::Widget* w)
{
if (w->getParent () != 0)
{
Box* b = dynamic_cast<Box*>(w->getParent());
if (b)
b->notifyChildrenSizeChanged ();
}
}
MyGUI::IntSize AutoSizedTextBox::getRequestedSize()
{
return getTextSize();
}
void AutoSizedTextBox::setCaption(const MyGUI::UString& _value)
{
TextBox::setCaption(_value);
notifySizeChange (this);
}
MyGUI::IntSize AutoSizedButton::getRequestedSize()
{
return getTextSize() + MyGUI::IntSize(24,0);
}
void AutoSizedButton::setCaption(const MyGUI::UString& _value)
{
Button::setCaption(_value);
notifySizeChange (this);
}
Box::Box()
: mSpacing(4)
{
}
void Box::setPropertyOverride(const std::string& _key, const std::string& _value)
{
if (_key == "Spacing")
{
mSpacing = MyGUI::utility::parseValue<int>(_value);
}
}
void Box::notifyChildrenSizeChanged ()
{
align();
}
void HBox::align ()
{
unsigned int count = getChildCount ();
size_t h_stretched_count = 0;
int total_width = 0;
std::vector< std::pair<MyGUI::IntSize, bool> > sizes;
for (unsigned int i = 0; i < count; ++i)
{
MyGUI::Widget* w = getChildAt(i);
bool hstretch = w->getUserString ("HStretch") == "true";
h_stretched_count += hstretch;
AutoSizedWidget* aw = dynamic_cast<AutoSizedWidget*>(w);
if (aw)
{
sizes.push_back(std::make_pair(aw->getRequestedSize (), hstretch));
total_width += aw->getRequestedSize ().width;
if (i != count-1)
total_width += mSpacing;
}
else
sizes.push_back (std::make_pair(MyGUI::IntSize(0,0), hstretch));
}
int curX = 0;
for (unsigned int i = 0; i < count; ++i)
{
MyGUI::Widget* w = getChildAt(i);
MyGUI::IntCoord widgetCoord;
widgetCoord.left = curX;
widgetCoord.top = 0; /// \todo
int width = sizes[i].second ? sizes[i].first.width + (getSize().width - total_width)/h_stretched_count
: sizes[i].first.width;
widgetCoord.width = width;
widgetCoord.height = getSize().height; /// \todo
w->setCoord(widgetCoord);
curX += width;
if (i != count-1)
curX += mSpacing;
}
}
void HBox::setSize (const MyGUI::IntSize& _value)
{
MyGUI::Widget::setSize (_value);
align();
}
MyGUI::IntSize HBox::getRequestedSize ()
{
MyGUI::IntSize size(0,0);
for (unsigned int i = 0; i < getChildCount (); ++i)
{
AutoSizedWidget* w = dynamic_cast<AutoSizedWidget*>(getChildAt(i));
if (w)
{
MyGUI::IntSize requested = w->getRequestedSize ();
size.height = std::max(size.height, requested.height);
size.width = size.width + requested.width;
if (i != getChildCount()-1)
size.width += mSpacing;
}
}
return size;
}
void VBox::align ()
{
}
void VBox::setSize (const MyGUI::IntSize& _value)
{
MyGUI::Widget::setSize (_value);
align();
}
MyGUI::IntSize VBox::getRequestedSize ()
{
MyGUI::IntSize size(0,0);
for (unsigned int i = 0; i < getChildCount (); ++i)
{
AutoSizedWidget* w = dynamic_cast<AutoSizedWidget*>(getChildAt(i));
if (w)
{
MyGUI::IntSize requested = w->getRequestedSize ();
size.width = std::max(size.width, requested.width);
size.height = size.height + requested.height;
if (i != getChildCount()-1)
size.height += mSpacing;
}
}
return size;
}

@ -299,6 +299,85 @@ namespace MWGui
MyGUI::TextBox* mBarTextWidget;
};
typedef MWDynamicStat* MWDynamicStatPtr;
// ---------------------------------------------------------------------------------------------------------------------
class AutoSizedWidget
{
public:
virtual MyGUI::IntSize getRequestedSize() = 0;
protected:
void notifySizeChange(MyGUI::Widget* w);
};
class AutoSizedTextBox : public AutoSizedWidget, public MyGUI::TextBox
{
MYGUI_RTTI_DERIVED( AutoSizedTextBox )
public:
virtual MyGUI::IntSize getRequestedSize();
virtual void setCaption(const MyGUI::UString& _value);
};
class AutoSizedButton : public AutoSizedWidget, public MyGUI::Button
{
MYGUI_RTTI_DERIVED( AutoSizedButton )
public:
virtual MyGUI::IntSize getRequestedSize();
virtual void setCaption(const MyGUI::UString& _value);
};
/**
* @brief A container widget that automatically sizes its children
* @note the box being an AutoSizedWidget as well allows to put boxes inside a box
*/
class Box : public AutoSizedWidget
{
public:
Box();
void notifyChildrenSizeChanged();
protected:
virtual void align() = 0;
virtual void setPropertyOverride(const std::string& _key, const std::string& _value);
int mSpacing; // how much space to put between elements
};
class HBox : public Box, public MyGUI::Widget
{
MYGUI_RTTI_DERIVED( HBox )
public:
virtual void setSize (const MyGUI::IntSize &_value);
protected:
virtual void align();
virtual MyGUI::IntSize getRequestedSize();
};
class VBox : public Box, public MyGUI::Widget
{
MYGUI_RTTI_DERIVED( VBox)
public:
virtual void setSize (const MyGUI::IntSize &_value);
protected:
virtual void align();
virtual MyGUI::IntSize getRequestedSize();
};
}
}

@ -102,6 +102,10 @@ WindowManager::WindowManager(
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWSpellEffect>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWDynamicStat>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::MWList>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::HBox>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::VBox>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::AutoSizedTextBox>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::Widgets::AutoSizedButton>("Widget");
MyGUI::LanguageManager::getInstance().eventRequestTag = MyGUI::newDelegate(this, &WindowManager::onRetrieveTag);

@ -30,20 +30,20 @@
</Widget>
<!-- Categories -->
<Widget type="Widget" position="0 8 350 24" align="Left Top HStretch" name="Categories">
<Widget type="Button" skin="MW_Button" position="0 0 60 24" name="AllButton">
<Widget type="HBox" position="0 8 350 24" align="Left Top HStretch" name="Categories">
<Widget type="AutoSizedButton" skin="MW_Button" position="0 0 60 24" name="AllButton">
<Property key="Caption" value="#{sAllTab}"/>
</Widget>
<Widget type="Button" skin="MW_Button" position="0 0 60 24" name="WeaponButton">
<Widget type="AutoSizedButton" skin="MW_Button" position="0 0 60 24" name="WeaponButton">
<Property key="Caption" value="#{sWeaponTab}"/>
</Widget>
<Widget type="Button" skin="MW_Button" position="0 0 60 24" name="ApparelButton">
<Widget type="AutoSizedButton" skin="MW_Button" position="0 0 60 24" name="ApparelButton">
<Property key="Caption" value="#{sApparelTab}"/>
</Widget>
<Widget type="Button" skin="MW_Button" position="0 0 60 24" name="MagicButton">
<Widget type="AutoSizedButton" skin="MW_Button" position="0 0 60 24" name="MagicButton">
<Property key="Caption" value="#{sMagicTab}"/>
</Widget>
<Widget type="Button" skin="MW_Button" position="0 0 60 24" name="MiscButton">
<Widget type="AutoSizedButton" skin="MW_Button" position="0 0 60 24" name="MiscButton">
<Property key="Caption" value="#{sMiscTab}"/>
</Widget>
</Widget>

@ -43,14 +43,18 @@
</Widget>
<Widget type="Button" skin="MW_Button" position="4 200 34 24" align="Left Top" name="SubtitlesButton"/>
<Widget type="TextBox" skin="SandText" position="44 200 200 24" align="Left Top">
<Property key="Caption" value="#{sSubtitles}"/>
<Widget type="HBox" skin="" position="4 200 260 24">
<Widget type="AutoSizedButton" skin="MW_Button" align="Left Top" name="SubtitlesButton"/>
<Widget type="AutoSizedTextBox" skin="SandText" align="Left Top">
<Property key="Caption" value="#{sSubtitles}"/>
</Widget>
</Widget>
<Widget type="Button" skin="MW_Button" position="4 230 34 24" align="Left Top" name="CrosshairButton"/>
<Widget type="TextBox" skin="SandText" position="44 230 200 24" align="Left Top">
<Property key="Caption" value="#{sCursorOff}"/>
<Widget type="HBox" skin="" position="4 230 260 24">
<Widget type="AutoSizedButton" skin="MW_Button" align="Left Top" name="CrosshairButton"/>
<Widget type="AutoSizedTextBox" skin="SandText" align="Left Top">
<Property key="Caption" value="#{sCursorOff}"/>
</Widget>
</Widget>
</Widget>

Loading…
Cancel
Save