Hi guys!
For those of you who have been looking for an automatic layout system to manage DirectGui widgets, I might have some good news !
As I already implemented such a system for my own Panda3D Studio project, I decided to try and adapt it to work with DirectGui, and the results seem quite promising!
This should be considered a work in progress, as I haven’t fully tested things like DirectScrolledFrame management or adding/removing/resizing widgets at runtime, so certain functionality might still be missing while there could also be some redundant left-over code in there that needs to be cleaned up.
If you want to know what all the fuss is about, head on over to the Git repository and check it out.
In a nutshell, with this system you won’t ever have to worry about widget placement ever again, ever. All you do is define how a widget should be placed on the screen (left-aligned, centered, etc.) and how much space should surround it (left, right, bottom and top borders), as well as how it can stretch (horizontally, vertically, both or not at all). And that’s basically it; no need for exact positioning or sizing, as this will be done automatically for you, each time the window size changes.
This is accomplished by using “sizers” (people who used wxPython before will know what I’m talking about). You fill these abstract containers with objects in a certain direction, either horizontally or vertically.
(There is a GridSizer too, combining both horizontal and vertical sizers, but I haven’t ported it yet.)
These objects can not only be widgets, but also sizes (empty space) or even other sizers, so you can nest them as deeply as you like.
As you add each object to a sizer along its own direction, you can decide whether this object should fill out the entire available space in the other direction (the “expand” keyword argument is used for this) or whether it should be aligned to one side or centered (relative to this particular sizer, not to the entire screen).
If you need the object to stretch along the sizer’s direction itself, you can work with proportions.
For example, if you want to center a widget horizontally in a horizontal sizer, first add a (0, 0) size with a proportion of 1., then add the widget without a proportion, finally add another (0, 0) size with a proportion of 1. and Bob’s your uncle (or aunt, you never know these days).
Once you understand the subtleties of this approach, you will likely find it far more intuitive than the tedious trial and error routine of specifying position and size manually, only to find that resizing the window messes up your layout anyway.
If you’re interested in this project, please do let me know, otherwise I’m not sure how motivated I’ll be to keep working on it, as I’m not really into DirectGui anymore.
Currently this is just a little experiment, but if there’s enough interest, it might become something really useful.
To those who have a solid understanding of DirectGui (specifically those whose username starts with “Thauma” or ends with “db” ) I have a question (or two):
- What is the best/easiest way to know the exact size of any DirectGui widget? I’ve tried the
getBounds
method which works well, but only after at least one frame is rendered, which forced me to use some ugly workaround to get the correct values after creating the widgets. - When I align the text of a DirectGui widget (e.g. with
TextNode.A_center
), resizing the widget afterwards usingwidget["frameSize"]
doesn’t retain the alignment, i.e. the text just stays where it was. Is there some way to force an update of the text position on the widget?