I first tried to implement both minimalizing and maximizing buttons in ChildWindow Widget with TGUI 1.1, backend SFML on Windows with GCC 13.1 compiler. I got some strange behaviors (code attached at the end)
1. Movement of the widget inside a parent widget
If widget
2. Implementing Maximize button
I have tried implemented it by lambda and it has some limitations (I will discuss later). But Maximize button does not work correctly as
3. Implementing Maximize and Minimize buttons
Using separated lambdas for each button which are not aware of status of the seconds is not ideal. Is there another way to do it?
I have only one idea and to create new widget which inherites from ChildWidget and have private members storing information about status of the widget (maximalized, minimalized), position and size before that.
1. Movement of the widget inside a parent widget
If widget
Code Select
setPositionLocked(false) and Code Select
setKeepInParent(true) is true then the movement of the widget is only possible on the edges of the parent widget. If Code Select
setKeepInParent(false), than movement is freely available anywhere.2. Implementing Maximize button
I have tried implemented it by lambda and it has some limitations (I will discuss later). But Maximize button does not work correctly as
Code Select
childWindow->setPosition(tgui::Vector2f{nonMaxPosition.x, nonMaxPosition.y}); is completely ignored. I stream the position of the widget before that and after that with information about variable nonMaxPosition to std::cout and it shows that variable nonMaxPosition is correct but the row mentioned above do not set new position of the widget so everytime maximize button is pushed when widget is already maximize, the new position is set to left top corner of parent widget ignoring setting new position of stored previous position.3. Implementing Maximize and Minimize buttons
Using separated lambdas for each button which are not aware of status of the seconds is not ideal. Is there another way to do it?
I have only one idea and to create new widget which inherites from ChildWidget and have private members storing information about status of the widget (maximalized, minimalized), position and size before that.
Code Select
#include <TGUI/Widgets/ChildWindow.hpp>
#include <TGUI/Widgets/Group.hpp>
#include <TGUI/Backend/SFML-Graphics.hpp>
#include <iostream>
int main ()
{
sf::RenderWindow window({800, 600}, "TGUI example (SFML-Graphics)");
tgui::Gui gui(window);
auto group = tgui::Group::create();
group->setSize({700,500});
group->setPosition({50,50});
gui.add(group);
auto childWindow = tgui::ChildWindow::create("Settings");
group->add(childWindow);
childWindow->setSize({150,100});
childWindow->setPosition(50,50);
childWindow->setPositionLocked(false);
childWindow->setTitleButtons(tgui::ChildWindow::TitleButton::Maximize
| tgui::ChildWindow::TitleButton::Minimize
| tgui::ChildWindow::TitleButton::Close);
childWindow->setResizable(true);
childWindow->setKeepInParent(true); // Prevents any part of the window to go outside the screen
childWindow->onMaximize([&](){
static bool maximized (false);
static bool keepInParent {childWindow->isKeptInParent()};
static tgui::Vector2f nonMaxSize {childWindow->getSize()};
static tgui::Vector2f nonMaxPosition (childWindow->getPosition());
if (maximized)
{
childWindow->setSize(nonMaxSize);
childWindow->setPosition(tgui::Vector2f{nonMaxPosition.x, nonMaxPosition.y}); // DO NOT WORK
childWindow->setKeepInParent(keepInParent);
childWindow->setPositionLocked(false);
}
else
{
keepInParent = childWindow->isKeptInParent();
nonMaxSize = childWindow->getSize();
nonMaxPosition = childWindow->getPosition();
childWindow->setPosition({0,0});
childWindow->setSize(childWindow->getParent()->getSize());
childWindow->setKeepInParent(true);
childWindow->setPositionLocked(true);
}
maximized = !maximized;
});
childWindow->onMinimize([&](){
static bool minimized {false};
static tgui::Vector2f nonMinimizedSize {childWindow->getSize()};
if (minimized)
childWindow->setSize(nonMinimizedSize);
else
{
if (childWindow->getInnerSize().y !=0 && childWindow->getMaximumSize() != childWindow->getSize())
nonMinimizedSize = childWindow->getSize();
childWindow->setClientSize({childWindow->getSize().x,0});
}
minimized = !minimized;
});
gui.mainLoop();
return 0;
}