2010-01-25 01:10:17 -05:00
|
|
|
! Copyright (C) 2005, 2010 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-04-10 15:11:05 -04:00
|
|
|
USING: sequences ui.gadgets ui.baseline-alignment
|
|
|
|
ui.baseline-alignment.private kernel math math.functions math.vectors
|
|
|
|
math.order math.rectangles namespaces accessors fry combinators arrays ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.gadgets.packs
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: pack < gadget
|
2008-11-30 18:47:29 -05:00
|
|
|
{ align initial: 0 } { fill initial: 0 } { gap initial: { 0 0 } } ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: (packed-dims) ( gadget sizes -- list )
|
2008-11-30 18:47:29 -05:00
|
|
|
swap [ dim>> ] [ fill>> ] bi '[ _ over v- _ v*n v+ ] map ;
|
|
|
|
|
|
|
|
: orient ( seq1 seq2 gadget -- seq )
|
|
|
|
orientation>> '[ _ set-axis ] 2map ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: packed-dims ( gadget sizes -- seq )
|
2009-02-02 01:02:55 -05:00
|
|
|
[ (packed-dims) ] [ nip ] [ drop ] 2tri orient ;
|
|
|
|
|
|
|
|
: gap-locs ( sizes gap -- seq )
|
|
|
|
[ { 0 0 } ] dip '[ v+ _ v+ ] accumulate nip ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: numerically-aligned-locs ( sizes pack -- seq )
|
2009-02-14 21:46:35 -05:00
|
|
|
[ align>> ] [ dim>> ] bi '[ [ _ _ ] dip v- [ * >integer ] with map ] map ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: baseline-aligned-locs ( pack -- seq )
|
2009-02-17 07:10:02 -05:00
|
|
|
children>> align-baselines [ 0 swap 2array ] map ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: aligned-locs ( sizes pack -- seq )
|
|
|
|
dup align>> +baseline+ eq?
|
|
|
|
[ nip baseline-aligned-locs ]
|
|
|
|
[ numerically-aligned-locs ]
|
|
|
|
if ;
|
|
|
|
|
|
|
|
: packed-locs ( sizes pack -- seq )
|
|
|
|
[ aligned-locs ] [ gap>> gap-locs ] [ nip ] 2tri orient ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: round-dims ( seq -- newseq )
|
2009-02-02 01:02:55 -05:00
|
|
|
[ { 0 0 } ] dip
|
2012-04-18 20:46:01 -04:00
|
|
|
[ swap v- dup vceiling [ swap v- ] keep ] map
|
2007-09-20 18:09:08 -04:00
|
|
|
nip ;
|
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: pack-layout ( pack sizes -- )
|
2009-02-02 01:02:55 -05:00
|
|
|
[ round-dims packed-dims ] [ drop ] 2bi
|
2010-05-05 16:52:54 -04:00
|
|
|
[ children>> [ dim<< ] 2each ]
|
|
|
|
[ [ packed-locs ] [ children>> ] bi [ loc<< ] 2each ] 2bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <pack> ( orientation -- pack )
|
2009-02-16 05:04:32 -05:00
|
|
|
pack new
|
2008-07-10 21:32:17 -04:00
|
|
|
swap >>orientation ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: <pile> ( -- pack ) vertical <pack> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-27 15:36:04 -04:00
|
|
|
: <filled-pile> ( -- pack ) <pile> 1 >>fill ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: <shelf> ( -- pack ) horizontal <pack> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2009-02-12 02:40:02 -05:00
|
|
|
: gap-dim ( pack -- dim )
|
|
|
|
[ gap>> ] [ children>> length 1 [-] ] bi v*n ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-02 16:14:46 -05:00
|
|
|
: max-pack-dim ( pack sizes -- dim )
|
|
|
|
over align>> +baseline+ eq?
|
2009-02-17 07:10:02 -05:00
|
|
|
[ [ children>> ] dip measure-height 0 swap 2array ] [ nip max-dim ] if ;
|
2009-02-02 16:14:46 -05:00
|
|
|
|
|
|
|
: pack-pref-dim ( pack sizes -- dim )
|
2009-02-12 02:40:02 -05:00
|
|
|
[ max-pack-dim ]
|
|
|
|
[ [ gap-dim ] [ dim-sum ] bi* v+ ]
|
|
|
|
[ drop orientation>> ]
|
|
|
|
2tri 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
|
|
|
|
2009-02-02 01:02:55 -05:00
|
|
|
: vertical-baseline ( pack -- y )
|
2009-02-17 07:10:02 -05:00
|
|
|
children>> [ f ] [ first baseline ] if-empty ;
|
2009-02-02 01:02:55 -05:00
|
|
|
|
|
|
|
: horizontal-baseline ( pack -- y )
|
2009-02-17 07:10:02 -05:00
|
|
|
children>> dup pref-dims measure-metrics drop ;
|
|
|
|
|
|
|
|
: pack-cap-height ( pack -- n )
|
2009-04-10 15:11:05 -04:00
|
|
|
children>> [ cap-height ] map ?supremum ;
|
2009-02-02 01:02:55 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
M: pack baseline
|
|
|
|
dup orientation>> {
|
|
|
|
{ vertical [ vertical-baseline ] }
|
|
|
|
{ horizontal [ horizontal-baseline ] }
|
|
|
|
} case ;
|
|
|
|
|
2009-02-17 07:10:02 -05:00
|
|
|
M: pack cap-height pack-cap-height ;
|
|
|
|
|
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 )
|
2010-01-25 01:10:17 -05:00
|
|
|
[ orientation>> ] [ children>> ] bi [ loc>> ] fast-children-on ;
|