| 1 |
I just commited an implementation of GUI layout management, ported |
| 2 |
over from my game project last year*. What this means is that you no |
| 3 |
longer need to position your widgets manually in dialogs, and can |
| 4 |
instead lay them out in tables and boxes like the pros do. :) I've |
| 5 |
redone a few of the dialogs using the new scheme (I'm especially proud |
| 6 |
of the autopilot dialog: http://plausible.org/andy/autopilot-new.png), |
| 7 |
so you can see what the possibilities look like. |
| 8 |
|
| 9 |
* FWIW, this is almost the last of my useful code from last spring. |
| 10 |
Nasal and the Plib vertex splitting code are two other bits that |
| 11 |
were useful in isolation. I also had a terrain engine and stencil |
| 12 |
shadow implementation, but those weren't really production quality. |
| 13 |
|
| 14 |
Basically, the implementation is a preprocessor on top of the existing |
| 15 |
dialog properties, which sets x/y/width/height values based on |
| 16 |
constraints. The <group> objects, including the top-level one which |
| 17 |
represents the whole dialog, can now have a <layout> property, which |
| 18 |
can be "hbox", "vbox", or "table". |
| 19 |
|
| 20 |
The boxes simply lay out their children in order, either top-to-bottom |
| 21 |
or left-to-right. The box name comes from Qt and Gtk, but this is |
| 22 |
also the same thing that Java calls a "flow layout", or what the Tk |
| 23 |
"packer" does. You can set "constraint" properties on the children, |
| 24 |
to give the layout manager hints as to how to place the children. For |
| 25 |
the boxes, these are: |
| 26 |
|
| 27 |
equal: The box manager makes sure that all the widgets with this |
| 28 |
constraint set to true get equal sizes big enough to fit the |
| 29 |
largest one. This is very useful for button boxes to make |
| 30 |
the "OK" and "Cancel" buttons match, for example. |
| 31 |
stretch: Cells with "stretch" set to true get all the extra space, |
| 32 |
if any, the box has to allocate. These are useful for |
| 33 |
alignment purposes, especially when combined with <empty> |
| 34 |
"widgets" (which are ignored by the dialog creation code, |
| 35 |
but honored by the layout engine). |
| 36 |
|
| 37 |
The table layout will be a little more familiar to anyone with HTML |
| 38 |
experience. Children of tables get the following constraints: |
| 39 |
|
| 40 |
row: The row number containing the upper left corner of the widget. |
| 41 |
Table rows are zero-indexed. |
| 42 |
col: The column number containing the upper left corner of the widget. |
| 43 |
Table columns are zero-indexed. |
| 44 |
rowspan: The number of rows spanned by the widget. Defaults to one. |
| 45 |
colspan: The number of columns spanned by the widget. Defaults to |
| 46 |
one. |
| 47 |
|
| 48 |
Inside of each "cell", regardless of parent layout, there are some |
| 49 |
constraints that are used to position the widget within the space |
| 50 |
available: |
| 51 |
|
| 52 |
halign: The horizontal alignment. Can be "left", "right", |
| 53 |
"center", or "fill" (i.e. stretch to available space). |
| 54 |
valign: The vertical alignment. Can be "top", "bottom", |
| 55 |
"center", or "fill". |
| 56 |
padding: The number of pixels to leave between the edge of the |
| 57 |
cell and the widget. |
| 58 |
pref-height: |
| 59 |
pref-width: Overrides the default preferred size of the widget. |
| 60 |
Note that this is the size of the widget only, not the |
| 61 |
cell (which includes padding). |
| 62 |
|
| 63 |
Also, the padding values for cells in a group can be set to a default |
| 64 |
value with a <default-padding> property on the group widget. |
| 65 |
|
| 66 |
Some will ask why didn't I implement this as part of Pui. The problem |
| 67 |
is the pui just isn't set up for it. Not only is there no notion of |
| 68 |
"preferred" size for a widget, there isn't anything remote like a |
| 69 |
"constraint" system for attaching arbitrary values to widgets. With |
| 70 |
the property system, I have that for free (the original code was |
| 71 |
written to work with Nasal objects, btw). I can do the layout with |
| 72 |
the properties and on the properties, and our existing dialog code |
| 73 |
hardly needs to change at all. |
| 74 |
|
| 75 |
Anyway, give it a try and see if I've broken anything. Also, note |
| 76 |
that some of these changes *do* modify the visual appearance of the |
| 77 |
GUI. I think it looks better, but opinions will no doubt vary. Shout |
| 78 |
if you hate it. |
| 79 |
|
| 80 |
And finally, the text alignment doesn't quite look right with current |
| 81 |
plib due to some minor rendering bugs. Bug Steve to apply the patch I |
| 82 |
submitted a week or so ago. :) |
| 83 |
|
| 84 |
Andy |
| 85 |
|