factor/library/ui/layouts.factor

89 lines
2.5 KiB
Factor
Raw Normal View History

2005-02-02 19:50:13 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
2005-06-28 16:25:27 -04:00
USING: errors generic hashtables kernel lists math matrices
namespaces sdl sequences ;
2005-02-02 19:50:13 -05:00
2005-02-03 19:11:06 -05:00
: layout ( gadget -- )
#! Set the gadget's width and height to its preferred width
#! and height. The gadget's children are laid out first.
#! Note that nothing is done if the gadget does not need to
#! be laid out.
dup gadget-relayout? [
2005-04-30 17:17:10 -04:00
f over set-gadget-relayout?
2005-07-13 21:03:34 -04:00
dup layout*
gadget-children [ layout ] each
2005-02-03 19:11:06 -05:00
] [
drop
] ifte ;
2005-07-06 03:29:42 -04:00
TUPLE: pack align fill vector ;
: pref-dims ( gadget -- list )
2005-07-16 23:01:51 -04:00
gadget-children [ pref-dim ] map >list ;
2005-06-29 19:40:44 -04:00
: orient ( gadget list1 list2 -- list )
2005-07-06 01:57:58 -04:00
zip >r pack-vector r> [ uncons rot set-axis ] map-with ;
2005-06-29 19:40:44 -04:00
: packed-dim-2 ( gadget sizes -- list )
2005-07-06 01:57:58 -04:00
[
over shape-dim { 1 1 1 } vmax over v-
rot pack-fill v*n v+
] map-with ;
2005-06-29 19:40:44 -04:00
: (packed-dims) ( gadget sizes -- list )
2dup packed-dim-2 swap orient ;
: packed-dims ( gadget sizes -- list )
2005-07-16 22:16:18 -04:00
over gadget-children >list >r (packed-dims) r>
2005-06-29 19:40:44 -04:00
zip [ uncons set-gadget-dim ] each ;
: packed-loc-1 ( sizes -- list )
{ 0 0 0 } [ v+ ] accumulate ;
: packed-loc-2 ( gadget sizes -- list )
2005-07-06 01:57:58 -04:00
>r dup shape-dim { 1 1 1 } vmax over r>
packed-dim-2 [ v- ] map-with
>r dup pack-align swap shape-dim { 1 1 1 } vmax r>
2005-06-29 19:40:44 -04:00
[ >r 2dup r> v- n*v ] map 2nip ;
: (packed-locs) ( gadget sizes -- list )
dup packed-loc-1 >r dupd packed-loc-2 r> orient ;
: packed-locs ( gadget sizes -- )
2005-07-16 22:16:18 -04:00
over gadget-children >list >r (packed-locs) r>
2005-07-08 01:32:29 -04:00
zip [ uncons set-shape-loc ] each ;
2005-06-29 19:40:44 -04:00
: packed-layout ( gadget sizes -- )
2dup packed-locs packed-dims ;
C: pack ( align fill vector -- pack )
#! align: 0 left aligns, 1/2 center, 1 right.
#! gap: between each child.
#! fill: 0 leaves default width, 1 fills to pack width.
2005-07-13 21:03:34 -04:00
[ <gadget> swap set-delegate ] keep
2005-06-29 19:40:44 -04:00
[ set-pack-vector ] keep
[ set-pack-fill ] keep
[ set-pack-align ] keep ;
: <pile> { 0 1 0 } <pack> ;
: <line-pile> 0 0 <pile> ;
2005-06-29 19:40:44 -04:00
: <shelf> { 1 0 0 } <pack> ;
: <line-shelf> 0 0 <shelf> ;
2005-06-29 19:40:44 -04:00
2005-07-01 19:52:08 -04:00
M: pack pref-dim ( pack -- dim )
[
pref-dims
[ { 0 0 0 } [ vmax ] reduce ] keep
{ 0 0 0 } [ v+ ] reduce
2005-07-06 01:57:58 -04:00
] keep pack-vector set-axis ;
2005-06-29 19:40:44 -04:00
2005-07-06 01:57:58 -04:00
M: pack layout* ( pack -- ) dup pref-dims packed-layout ;
2005-06-29 19:40:44 -04:00
: <stack> ( list -- gadget )
#! A stack lays out all its children on top of each other.
2005-07-01 19:52:08 -04:00
0 1 { 0 0 1 } <pack> swap [ over add-gadget ] each ;