SFML community forums

Help => Graphics => Topic started by: user31182 on December 26, 2018, 01:16:42 pm

Title: SFML Drawing Hierarchies
Post by: user31182 on December 26, 2018, 01:16:42 pm
I'm not really sure where to direct this question, so I guessed graphics as the most appropriate subforum.

I'm attempting to implement a kind of GUI toolkit for SFML. This project actually started some months ago now. I'm aware that such things already exist but I wanted a GUI library to do a specific thing which is why I started my own.

I put this project on hold and then had another idea for something else recently so I've started work on it again. I didn't actually get very far in the first place.

So far I have something like this:

1: A base class which inherits sf::Drawable. This class also adds an "event" function which events are passed to
2: Some gui objects, textbox, button, context menu, which inherit from the base class
3: Some of my gui objects are nested, ie; the context menu has context menu items

My question is about drawing these objects.

All the gui objects have a sf::RectangleShape which is used to draw the background of the object.

The override draw method calls

sf::RenderTarget target;
target.draw(background);

This is just to show the basic principle of how everything is going to work.

At the moment, for all my objects, I have a setPosition method which changes the sf::RectangleShape position.

For my example of context menu, which has a parent-child relationship in that several context menu items are children of the parent, I am unsure of how to proceed here.

One possibly solution is to use absolute positions, which is what sfml seems to want to use. In this way, if the position of the context menu is changed, then the positions of all the child items would also have to be changed.

This doesn't seem like a good solution, since I would prefer to store the relative locations of the child objects relative to the parent rather than the absolute position in the window.

Is there a better way of approaching this problem?
Title: Re: SFML Drawing Hierarchies
Post by: 51423benam on December 26, 2018, 05:41:10 pm
Hello,

well, why not just use absolute coordinates (which you have to) but hide it as much as possible? For example, if you construct the child widget:

Widget* child = new ChildWidget (myCoordinates + relativeCoordinates);

You could also implement a move () function, which you pass relative coordinates to, and then, if your parent widget gets moved, you just call move() with the exact same vector.
Title: Re: SFML Drawing Hierarchies
Post by: Laurent on December 27, 2018, 10:06:58 am
You should probably read this first:
https://www.sfml-dev.org/tutorials/2.5/graphics-transform.php#object-hierarchies-scene-graph