factor/library/ui/layouts.factor

98 lines
2.6 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?
dup gadget-paint [
2005-03-07 23:15:00 -05:00
dup layout*
gadget-children [ layout ] each
] bind
2005-02-03 19:11:06 -05:00
] [
drop
] ifte ;
2005-06-29 19:40:44 -04:00
GENERIC: alignment
GENERIC: filling
GENERIC: orientation
: pref-dims ( gadget -- list )
gadget-children [ pref-dim ] map ;
2005-06-29 19:40:44 -04:00
: orient ( gadget list1 list2 -- list )
zip >r orientation r> [ uncons rot set-axis ] map-with ;
: packed-dim-2 ( gadget sizes -- list )
2005-07-04 17:35:22 -04:00
[ over shape-dim { 1 1 1 } vmax over v- rot filling 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 )
over gadget-children >r (packed-dims) r>
zip [ uncons set-gadget-dim ] each ;
: packed-loc-1 ( sizes -- list )
{ 0 0 0 } [ v+ ] accumulate ;
: packed-loc-2 ( gadget sizes -- list )
2005-07-04 17:35:22 -04:00
>r dup shape-dim { 1 1 1 } vmax over r> packed-dim-2 [ v- ] map-with
>r dup alignment 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 -- )
over gadget-children >r (packed-locs) r>
zip [ uncons set-gadget-loc ] each ;
: packed-layout ( gadget sizes -- )
2dup packed-locs packed-dims ;
TUPLE: pack align fill vector ;
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.
[ <empty-gadget> swap set-delegate ] keep
[ set-pack-vector ] keep
[ set-pack-fill ] keep
[ set-pack-align ] keep ;
: <pile> { 0 1 0 } <pack> ;
: <line-pile> 0 1 <pile> ;
: <shelf> { 1 0 0 } <pack> ;
: <line-shelf> 0 1 <shelf> ;
M: pack orientation pack-vector ;
M: pack filling pack-fill ;
M: pack alignment pack-align ;
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
] keep orientation set-axis ;
2005-06-29 19:40:44 -04:00
M: pack layout* ( pack -- )
dup pref-dims packed-layout ;
: <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 ;