2016-06-30 07:12:43 -07:00
|
|
|
#include "Tests.hpp"
|
|
|
|
#include <TGUI/Widgets/Panel.hpp>
|
|
|
|
|
|
|
|
void mouseCallback(unsigned int& count, sf::Vector2f pos)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
REQUIRE(pos == sf::Vector2f(75, 50));
|
|
|
|
}
|
|
|
|
|
|
|
|
void genericCallback(unsigned int& count)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void testWidgetSignals(tgui::Widget::Ptr widget)
|
|
|
|
{
|
2016-07-10 15:01:18 -07:00
|
|
|
// TODO: Test other signals than MouseEntered and MouseLeft
|
|
|
|
|
2016-06-30 07:12:43 -07:00
|
|
|
SECTION("mouse move")
|
|
|
|
{
|
|
|
|
unsigned int mouseEnteredCount = 0;
|
|
|
|
unsigned int mouseLeftCount = 0;
|
|
|
|
|
2017-08-30 09:51:21 -07:00
|
|
|
widget->connect("MouseEntered", [&]{ genericCallback(mouseEnteredCount); });
|
|
|
|
widget->connect("MouseLeft", [&]{ genericCallback(mouseLeftCount); });
|
2016-06-30 07:12:43 -07:00
|
|
|
|
2016-09-14 11:11:20 -07:00
|
|
|
auto parent = tgui::Panel::create({300, 200});
|
2016-09-08 06:30:41 -07:00
|
|
|
parent->setPosition({30, 25});
|
2016-06-30 07:12:43 -07:00
|
|
|
parent->add(widget);
|
|
|
|
|
2016-09-08 06:30:41 -07:00
|
|
|
widget->setPosition({40, 30});
|
|
|
|
widget->setSize({150, 100});
|
2016-06-30 07:12:43 -07:00
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->mouseMoved({40, 40});
|
2016-06-30 07:12:43 -07:00
|
|
|
REQUIRE(mouseEnteredCount == 0);
|
|
|
|
REQUIRE(mouseLeftCount == 0);
|
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->mouseMoved({70, 55});
|
2016-06-30 07:12:43 -07:00
|
|
|
REQUIRE(mouseEnteredCount == 1);
|
|
|
|
REQUIRE(mouseLeftCount == 0);
|
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->mouseMoved({219, 154});
|
2016-06-30 07:12:43 -07:00
|
|
|
REQUIRE(mouseEnteredCount == 1);
|
|
|
|
REQUIRE(mouseLeftCount == 0);
|
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->mouseMoved({220, 155});
|
2016-06-30 07:12:43 -07:00
|
|
|
REQUIRE(mouseEnteredCount == 1);
|
|
|
|
REQUIRE(mouseLeftCount == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testClickableWidgetSignals(tgui::ClickableWidget::Ptr widget)
|
|
|
|
{
|
|
|
|
testWidgetSignals(widget);
|
|
|
|
|
|
|
|
unsigned int mousePressedCount = 0;
|
|
|
|
unsigned int mouseReleasedCount = 0;
|
|
|
|
unsigned int clickedCount = 0;
|
|
|
|
|
2016-09-08 06:30:41 -07:00
|
|
|
widget->setPosition({40, 30});
|
|
|
|
widget->setSize({150, 100});
|
2016-06-30 07:12:43 -07:00
|
|
|
|
2017-08-30 09:51:21 -07:00
|
|
|
widget->connect("MousePressed", [&](sf::Vector2f pos){ mouseCallback(mousePressedCount, pos); });
|
|
|
|
widget->connect("MouseReleased", [&](sf::Vector2f pos){ mouseCallback(mouseReleasedCount, pos); });
|
|
|
|
widget->connect("Clicked", [&](sf::Vector2f pos){ mouseCallback(clickedCount, pos); });
|
2016-06-30 07:12:43 -07:00
|
|
|
|
|
|
|
SECTION("mouseOnWidget")
|
|
|
|
{
|
2017-07-05 06:12:24 -07:00
|
|
|
REQUIRE(!widget->mouseOnWidget({39, 29}));
|
|
|
|
REQUIRE(widget->mouseOnWidget({40, 30}));
|
|
|
|
REQUIRE(widget->mouseOnWidget({115, 80}));
|
|
|
|
REQUIRE(widget->mouseOnWidget({189, 129}));
|
|
|
|
REQUIRE(!widget->mouseOnWidget({190, 130}));
|
2016-06-30 07:12:43 -07:00
|
|
|
|
|
|
|
REQUIRE(mousePressedCount == 0);
|
|
|
|
REQUIRE(mouseReleasedCount == 0);
|
|
|
|
REQUIRE(clickedCount == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("mouse click")
|
|
|
|
{
|
2016-09-14 11:11:20 -07:00
|
|
|
auto parent = tgui::Panel::create({300, 200});
|
2016-09-08 06:30:41 -07:00
|
|
|
parent->setPosition({60, 55});
|
2016-08-27 15:31:25 -07:00
|
|
|
parent->add(widget);
|
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->leftMouseReleased({175, 135});
|
2016-06-30 07:12:43 -07:00
|
|
|
|
|
|
|
REQUIRE(mouseReleasedCount == 1);
|
|
|
|
REQUIRE(clickedCount == 0);
|
|
|
|
|
|
|
|
SECTION("mouse press")
|
|
|
|
{
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->leftMousePressed({175, 135});
|
2016-06-30 07:12:43 -07:00
|
|
|
|
|
|
|
REQUIRE(mousePressedCount == 1);
|
|
|
|
REQUIRE(mouseReleasedCount == 1);
|
|
|
|
REQUIRE(clickedCount == 0);
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:12:24 -07:00
|
|
|
parent->leftMouseReleased({175, 135});
|
2016-06-30 07:12:43 -07:00
|
|
|
|
|
|
|
REQUIRE(mousePressedCount == 1);
|
|
|
|
REQUIRE(mouseReleasedCount == 2);
|
|
|
|
REQUIRE(clickedCount == 1);
|
|
|
|
}
|
|
|
|
}
|
2016-07-17 10:33:14 -07:00
|
|
|
|
|
|
|
void testWidgetRenderer(tgui::WidgetRenderer* renderer)
|
|
|
|
{
|
|
|
|
SECTION("WidgetRenderer")
|
|
|
|
{
|
|
|
|
SECTION("set serialized property")
|
|
|
|
{
|
|
|
|
REQUIRE_NOTHROW(renderer->setProperty("Opacity", "0.8"));
|
2017-02-09 15:35:24 -08:00
|
|
|
REQUIRE_NOTHROW(renderer->setProperty("Font", "resources/DejaVuSans.ttf"));
|
2016-07-17 10:33:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("set object property")
|
|
|
|
{
|
|
|
|
REQUIRE_NOTHROW(renderer->setProperty("Opacity", 0.8f));
|
2017-02-09 15:35:24 -08:00
|
|
|
REQUIRE_NOTHROW(renderer->setProperty("Font", tgui::Font{"resources/DejaVuSans.ttf"}));
|
2016-07-17 10:33:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("functions")
|
|
|
|
{
|
|
|
|
renderer->setOpacity(0.8f);
|
2017-02-09 15:35:24 -08:00
|
|
|
renderer->setFont({"resources/DejaVuSans.ttf"});
|
2016-07-17 10:33:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE(renderer->getProperty("Opacity").getNumber() == 0.8f);
|
|
|
|
REQUIRE(renderer->getProperty("Font").getFont() != nullptr);
|
|
|
|
|
|
|
|
REQUIRE(renderer->getOpacity() == 0.8f);
|
2017-02-09 15:35:24 -08:00
|
|
|
REQUIRE(renderer->getFont().getId() == "resources/DejaVuSans.ttf");
|
2016-07-17 10:33:14 -07:00
|
|
|
|
|
|
|
REQUIRE_THROWS_AS(renderer->setProperty("NonexistentProperty", ""), tgui::Exception);
|
|
|
|
}
|
|
|
|
}
|