Fixed bug in layouts when using comparison operator in string (fixes #51)

0.8
Bruno Van de Velde 2016-01-20 13:22:07 +01:00
parent 0f5eab2efa
commit a103a68365
2 changed files with 32 additions and 7 deletions

View File

@ -433,16 +433,16 @@ namespace tgui
return parseLayoutString(expression.substr(0, equalsPos)) == parseLayoutString(expression.substr(equalsPos + 2));
}
auto lessThanPos = expression.rfind('>');
auto greaterThanPos = expression.rfind('<');
auto lessEqualPos = expression.rfind(">=");
auto greaterEqualPos = expression.rfind("<=");
auto lessThanPos = expression.rfind('<');
auto greaterThanPos = expression.rfind('>');
auto lessEqualPos = expression.rfind("<=");
auto greaterEqualPos = expression.rfind(">=");
if ((lessThanPos != std::string::npos) || (greaterThanPos != std::string::npos))
{
if ((lessThanPos == 0) || (greaterThanPos == 0) || (lessEqualPos == 0) || (greaterEqualPos == 0))
return 0;
if ((greaterThanPos != std::string::npos) && ((lessThanPos == std::string::npos) || (lessThanPos < greaterThanPos)))
if ((greaterThanPos != std::string::npos) && ((lessThanPos == std::string::npos) || (greaterThanPos < lessThanPos)))
{
if ((greaterEqualPos != std::string::npos) && (greaterEqualPos == greaterThanPos))
return parseLayoutString(expression.substr(0, greaterEqualPos)) >= parseLayoutString(expression.substr(greaterEqualPos + 2));
@ -452,9 +452,9 @@ namespace tgui
else // < or <=
{
if ((lessEqualPos != std::string::npos) && (lessEqualPos == lessThanPos))
return parseLayoutString(expression.substr(0, lessEqualPos)) >= parseLayoutString(expression.substr(lessEqualPos + 2));
return parseLayoutString(expression.substr(0, lessEqualPos)) <= parseLayoutString(expression.substr(lessEqualPos + 2));
else
return parseLayoutString(expression.substr(0, lessThanPos)) > parseLayoutString(expression.substr(lessThanPos + 1));
return parseLayoutString(expression.substr(0, lessThanPos)) < parseLayoutString(expression.substr(lessThanPos + 1));
}
}

View File

@ -271,6 +271,25 @@ TEST_CASE("[Layouts]") {
REQUIRE(Layout("2 and 2").getValue() == 1);
REQUIRE(Layout("2 or 0").getValue() == 1);
REQUIRE(Layout("2 == 0").getValue() == 0);
REQUIRE(Layout("4 == 9").getValue() == 0);
REQUIRE(Layout("3 == 3").getValue() == 1);
REQUIRE(Layout("2 != 0").getValue() == 1);
REQUIRE(Layout("4 != 9").getValue() == 1);
REQUIRE(Layout("3 != 3").getValue() == 0);
REQUIRE(Layout("2 < 0").getValue() == 0);
REQUIRE(Layout("4 < 9").getValue() == 1);
REQUIRE(Layout("3 < 3").getValue() == 0);
REQUIRE(Layout("2 > 0").getValue() == 1);
REQUIRE(Layout("4 > 9").getValue() == 0);
REQUIRE(Layout("3 > 3").getValue() == 0);
REQUIRE(Layout("2 <= 0").getValue() == 0);
REQUIRE(Layout("4 <= 9").getValue() == 1);
REQUIRE(Layout("3 <= 3").getValue() == 1);
REQUIRE(Layout("2 >= 0").getValue() == 1);
REQUIRE(Layout("4 >= 9").getValue() == 0);
REQUIRE(Layout("3 >= 3").getValue() == 1);
REQUIRE(Layout2d("{2, -1}").getValue() == sf::Vector2f(2, -1));
REQUIRE(Layout2d("+{2, -1}").getValue() == sf::Vector2f(2, -1));
REQUIRE(Layout2d("-{2, -1}").getValue() == sf::Vector2f(-2, 1));
@ -395,6 +414,12 @@ TEST_CASE("[Layouts]") {
button1->setSize(30, 10);
REQUIRE(button3->getSize().x == 300);
button3->setSize({"(if b1.w <= b2.h then 2 * b1.w else b2.w / 4) * 3"});
REQUIRE(button3->getSize().x == 180);
button2->setSize(200, 50);
button1->setSize(300, 50);
REQUIRE(button3->getSize().x == 150);
button3->setPosition(bindIf(bindLeft(button1) != bindTop(button2), bindPosition(button1), 1.5 * bindPosition(button2)));
button3->setPosition({"b1.x != b2.y ? b1.pos : 1.5 * b2.position"});
REQUIRE(button3->getPosition() == sf::Vector2f(40, 60));