factor/basis/ui/gadgets/packs/packs.factor

63 lines
1.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2007-10-18 02:40:17 -04:00
USING: sequences ui.gadgets kernel math math.functions
2008-07-11 19:34:43 -04:00
math.vectors namespaces math.order accessors math.geometry.rect ;
2007-09-20 18:09:08 -04:00
IN: ui.gadgets.packs
TUPLE: pack < gadget
2008-08-31 02:42:30 -04:00
{ align initial: 0 }
{ fill initial: 0 }
{ gap initial: { 0 0 } } ;
2007-09-20 18:09:08 -04:00
: packed-dim-2 ( gadget sizes -- list )
2008-08-31 02:42:30 -04:00
[ over rect-dim over v- rot fill>> v*n v+ ] with map ;
2007-09-20 18:09:08 -04:00
: packed-dims ( gadget sizes -- seq )
2dup packed-dim-2 swap orient ;
: gap-locs ( gap sizes -- seq )
{ 0 0 } [ v+ over v+ ] accumulate 2nip ;
: aligned-locs ( gadget sizes -- seq )
2008-08-31 02:42:30 -04:00
[ >r dup align>> swap rect-dim r> v- n*v ] with map ;
2007-09-20 18:09:08 -04:00
: packed-locs ( gadget sizes -- seq )
2008-08-31 02:42:30 -04:00
over gap>> over gap-locs >r dupd aligned-locs r> orient ;
2007-09-20 18:09:08 -04:00
: round-dims ( seq -- newseq )
{ 0 0 } swap
[ swap v- dup [ ceiling >fixnum ] map [ swap v- ] keep ] map
nip ;
: pack-layout ( pack sizes -- )
2008-08-29 19:44:19 -04:00
round-dims over children>>
>r dupd packed-dims r> 2dup [ (>>dim) ] 2each
2008-09-01 23:44:43 -04:00
>r packed-locs r> [ (>>loc) ] 2each ;
2007-09-20 18:09:08 -04:00
: <pack> ( orientation -- pack )
pack new-gadget
swap >>orientation ;
2007-09-20 18:09:08 -04:00
: <pile> ( -- pack ) { 0 1 } <pack> ;
2008-08-31 02:42:30 -04:00
: <filled-pile> ( -- pack ) <pile> 1 over (>>fill) ;
2007-09-20 18:09:08 -04:00
: <shelf> ( -- pack ) { 1 0 } <pack> ;
: gap-dims ( gap sizes -- seeq )
[ dim-sum ] keep length 1 [-] rot n*v v+ ;
: pack-pref-dim ( gadget sizes -- dim )
2008-08-31 02:42:30 -04:00
over gap>> over gap-dims >r max-dim r>
2008-08-29 19:44:19 -04:00
rot orientation>> set-axis ;
2007-09-20 18:09:08 -04:00
M: pack pref-dim*
2008-08-29 19:44:19 -04:00
dup children>> pref-dims pack-pref-dim ;
2007-09-20 18:09:08 -04:00
M: pack layout*
2008-08-29 19:44:19 -04:00
dup children>> pref-dims pack-layout ;
2007-09-20 18:09:08 -04:00
M: pack children-on ( rect gadget -- seq )
2008-08-29 19:44:19 -04:00
dup orientation>> swap children>>
2007-09-20 18:09:08 -04:00
[ fast-children-on ] keep <slice> ;