Widget Offset
« on: 13 May 2018, 16:01:01 »
How can I set the offset between widgets in a grid?
Code: [Select]
auto grid = tgui::Grid::create();
grid->setPosition(400, 200);
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j) {
auto button = tgui::Button::create("Hi");
button->setRenderer(theme.getRenderer("ButtonInv"));
button->setSize(40, 40);
grid->addWidget(button, i, j);
}
}
grid->setSize(200, 200); // I have to make sure that there is free space between the buttons
gui.add(grid);
I can get the offset values for the widget from
Code: [Select]
grid->getWidgetOffset(); But how do I change them?
I need to achieve an offset of 2 pixels between the buttons and each time it is inconvenient to select the grid size.
« Last Edit: 13 May 2018, 16:04:39 by SurvRabbit »

*

texus

  • *****
  • 1254
    • View Profile
    • Texus's Blog
Re: Widget Offset
« Reply #1 on: 13 May 2018, 16:08:06 »
You don't actually have to set the grid size. If you call grid->setSize then you let the gui calculate the offsets to fill the size, while you want to manually set offsets. The addWidget function takes 5 parameters in total, the 4th one being a border to place around the widget (I guess this should probably be renamed to "padding" instead of "border"). So the following will add a button with 2 pixels on each side:
grid->addWidget(button, i, j, tgui::Padding(2));

You might want to choose the padding differently, because like this you get 4 pixels between each widget (since each has a 2 pixel padding around it). Also the Padding constructor takes more parameters if not all sides need to have the same border width.
« Last Edit: 13 May 2018, 16:10:24 by texus »

Re: Widget Offset
« Reply #2 on: 13 May 2018, 16:12:21 »
Thank you