Load layout strings from file for position and size of widget

0.8
Bruno Van de Velde 2015-10-24 13:00:52 +02:00
parent adb3d33911
commit 40b248c982
3 changed files with 86 additions and 15 deletions

View File

@ -270,6 +270,28 @@ namespace tgui
void scale(const Layout& x, const Layout& y); void scale(const Layout& x, const Layout& y);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/// @brief Returns the layout object that is being used for the position
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Layout2d getPositionLayout() const
{
return m_position;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
/// @brief Returns the layout object that is being used for the size
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Layout2d getSizeLayout() const
{
return m_size;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected: protected:

View File

@ -851,8 +851,8 @@ namespace tgui
xExpr += expression.substr(currentPos); xExpr += expression.substr(currentPos);
yExpr += expression.substr(currentPos); yExpr += expression.substr(currentPos);
x = {xExpr}; x = {trim(xExpr)};
y = {yExpr}; y = {trim(yExpr)};
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -58,21 +58,68 @@ namespace
throw tgui::Exception{"Failed to parse boolean in '" + str + "'"}; throw tgui::Exception{"Failed to parse boolean in '" + str + "'"};
} }
sf::Vector2f parseVector2f(std::string str) tgui::Layout2d parseLayout(std::string str)
{ {
if (str.empty() || str.front() != '(' || str.back() != ')') if (str.empty())
throw tgui::Exception{"Failed to parse position '" + str + "'. Expected brackets."}; throw tgui::Exception{"Failed to parse layout. String was empty."};
// Check if the layout is an (x, y) vector or a quoted string
if ((str.front() == '(') && (str.back() == ')'))
{
str = str.substr(1, str.length() - 2); str = str.substr(1, str.length() - 2);
if (str.empty())
return {0, 0};
tgui::Layout x;
tgui::Layout y;
auto commaPos = str.find(','); auto commaPos = str.find(',');
if (commaPos == std::string::npos) if (commaPos == std::string::npos)
throw tgui::Exception{"Failed to parse position '" + str + "'. Expected numbers separated with a comma."}; throw tgui::Exception{"Failed to parse layout '" + str + "'. Expected numbers separated with a comma."};
if (str.find(',', commaPos + 1) != std::string::npos) // Check if the first part is quoted
throw tgui::Exception{"Failed to parse position '" + str + "'. Expected only one comma."}; auto openingQuotePos = str.find('"');
if (commaPos > openingQuotePos)
{
auto closingQuotePos = str.find('"', openingQuotePos + 1);
if (closingQuotePos == std::string::npos)
throw tgui::Exception{"Failed to parse layout '" + str + "'. Expected closing quote."};
return {tgui::stof(str.substr(0, commaPos)), tgui::stof(str.substr(commaPos + 1))}; // Make sure we didn't select a quote inside the string
if (commaPos < closingQuotePos)
{
commaPos = str.find(',', closingQuotePos + 1);
if (commaPos == std::string::npos)
throw tgui::Exception{"Failed to parse layout '" + str + "'. Expected numbers separated with a comma."};
}
x = {str.substr(openingQuotePos + 1, closingQuotePos - openingQuotePos - 1)};
}
else // Normal value
x = {tgui::stof(tgui::trim(str.substr(0, commaPos)))};
// Check if the second part is quoted
openingQuotePos = str.find('"', commaPos + 1);
if (openingQuotePos != std::string::npos)
{
auto closingQuotePos = str.find('"', openingQuotePos + 1);
if (closingQuotePos == std::string::npos)
throw tgui::Exception{"Failed to parse layout '" + str + "'. Expected closing quote."};
y = {str.substr(openingQuotePos + 1, closingQuotePos - openingQuotePos - 1)};
}
else // Normal value
y = {tgui::stof(tgui::trim(str.substr(commaPos + 1)))};
return {x, y};
}
else if ((str.front() == '"') && (str.back() == '"'))
{
str = str.substr(1, str.length() - 2);
return {str};
}
else
throw tgui::Exception{"Failed to parse layout '" + str + "'. Expected (x,y) or a quoted layout string."};
} }
} }
@ -112,9 +159,9 @@ namespace tgui
widget->disable(); widget->disable();
} }
if (node->propertyValuePairs["position"]) if (node->propertyValuePairs["position"])
widget->setPosition(parseVector2f(node->propertyValuePairs["position"]->value)); widget->setPosition(parseLayout(node->propertyValuePairs["position"]->value));
if (node->propertyValuePairs["size"]) if (node->propertyValuePairs["size"])
widget->setSize(parseVector2f(node->propertyValuePairs["size"]->value)); widget->setSize(parseLayout(node->propertyValuePairs["size"]->value));
if (node->propertyValuePairs["opacity"]) if (node->propertyValuePairs["opacity"])
widget->setOpacity(tgui::stof(node->propertyValuePairs["opacity"]->value)); widget->setOpacity(tgui::stof(node->propertyValuePairs["opacity"]->value));
@ -501,9 +548,11 @@ namespace tgui
else else
picture = std::make_shared<Picture>(); picture = std::make_shared<Picture>();
loadWidget(node, picture);
if (node->propertyValuePairs["filename"]) if (node->propertyValuePairs["filename"])
picture = std::make_shared<Picture>(DESERIALIZE_STRING("filename")); picture = std::make_shared<Picture>(DESERIALIZE_STRING("filename"));
loadWidget(node, picture);
if (node->propertyValuePairs["smooth"]) if (node->propertyValuePairs["smooth"])
picture->setSmooth(parseBoolean(node->propertyValuePairs["smooth"]->value)); picture->setSmooth(parseBoolean(node->propertyValuePairs["smooth"]->value));