I have a quesion about how my GUI should respond in a certain situation.
Here is an example code on how you can currently use my GUI.
void init(tgui::Window& window)
{
tgui::Picture* picture = window.addPicture("pic1");
picture->load("filename.png");
picture->SetScale(3, 3);
}
int main()
{
tgui::Window window(...);
init(window);
window.getPicture("Pic1")->SetScale(2, 2); // <- this is the line I have a question about.
// Other things
}
When you create an object you create and add them to the window with the addObject functions (addPicture, addButton, addCheckbox, ...).
You will receive a pointer to the object so that you can load it, scale it, change it's position, ...
You probably don't store the pointer and let it go out of scope, which is no problem.
But when you need the pointer back (for e.g. changing the scale) then you will need to call a getObject function.
To this function you must pass the name of the object, which is the same as the one that you passed to addObject.
My question is, what do you think is best when you pass a wrong name (usually this can only occur when you made a typing mistake).
- Should the function return a NULL pointer? This could be a good solution, but if you would do this: window.getObject("name")->SetScale(2, 2) when "name" would be wrong then you will get a segmentation fault. But when you would get that segfault you will know that you made a typo and you will find the problem easily.
- The other option, the one that is currently used, isn't great either. On startup some empty objects will be created. When you pass a wrong name than the empty object is returned. You will not get a segfault, but the scaling won't have any effect. You will also get into problems when trying to call GetPosition(), because this will not return what you would expect. You might face some dificulties finding out why your object isn't scaled like you wanted.
- Another option would be to return a boolean and take a reference as parameter. Then you will have to check if the function returns true, and if it does then you are allowed to change the object. But just like the first solution you will no longer be able to do window.getObject("name")->SetScale(2, 2).
At first sight the third solutions look the best, but it will make the following code completely unusable:
if (window.getCheckbox("check")->isChecked())
window.getLoadingbar("load")->incrementValue();
If I would still want the code I would have to write:
tgui::Checkbox* checkbox;
if (window.getCheckbox("check", checkbox))
{
if (checkbox->isChecked())
{
tgui::LoadingBar* loadingBar;
if (window.getLoadingBar("load", loadingBar))
loadingBar->incrementValue();
}
}
I hope that it is clair that this isn't really what I want.
I think changing to the first solution is the best thing to do, despite the fact that you might get segfaults if you mistype the name.
Anyone has an opinion about it? Maybe a better solution?
Thanks in advance.