factor/library/ui/gadgets.factor

94 lines
2.3 KiB
Factor
Raw Normal View History

2005-01-31 14:02:09 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
2005-06-23 22:35:41 -04:00
USING: generic hashtables kernel lists math matrices namespaces
2005-07-17 02:49:07 -04:00
sequences styles vectors ;
2005-01-31 14:02:09 -05:00
SYMBOL: origin
global [ { 0 0 0 } origin set ] bind
TUPLE: rectangle loc dim ;
GENERIC: inside? ( loc shape -- ? )
: shape-bounds ( shape -- loc dim )
dup rectangle-loc swap rectangle-dim ;
: shape-extent ( shape -- loc dim )
dup rectangle-loc dup rot rectangle-dim v+ ;
: screen-bounds ( shape -- rect )
shape-bounds >r origin get v+ r> <rectangle> ;
M: rectangle inside? ( loc rect -- ? )
screen-bounds shape-bounds { 1 1 1 } v- { 0 0 0 } vmax
>r v- { 0 0 0 } r> vbetween? conj ;
: intersect ( shape shape -- rect )
>r shape-extent r> shape-extent
swapd vmin >r vmax dup r> swap v- { 0 0 0 } vmax
<rectangle> ;
! A gadget is a rectangle, a paint, a mapping of gestures to
! actions, and a reference to the gadget's parent.
2005-07-17 00:21:10 -04:00
TUPLE: gadget
paint gestures visible? relayout? root?
parent children ;
2005-01-31 14:02:09 -05:00
2005-07-16 22:16:18 -04:00
: gadget-child gadget-children first ;
2005-07-13 21:03:34 -04:00
C: gadget ( -- gadget )
2005-07-17 00:21:10 -04:00
{ 0 0 0 } dup <rectangle> over set-delegate
t over set-gadget-visible? ;
2005-01-31 14:02:09 -05:00
2005-07-08 01:32:29 -04:00
DEFER: add-invalid
: invalidate ( gadget -- )
t swap set-gadget-relayout? ;
: relayout ( gadget -- )
2005-05-05 22:30:58 -04:00
#! Relayout and redraw a gadget and its parent before the
#! next iteration of the event loop.
2005-04-30 17:17:10 -04:00
dup gadget-relayout? [
drop
] [
2005-07-08 01:32:29 -04:00
dup invalidate
dup gadget-root?
[ add-invalid ]
2005-07-08 01:32:29 -04:00
[ gadget-parent [ relayout ] when* ] ifte
2005-04-30 17:17:10 -04:00
] ifte ;
: (relayout-down)
dup invalidate gadget-children [ (relayout-down) ] each ;
2005-07-08 01:32:29 -04:00
: relayout-down ( gadget -- )
2005-05-05 22:30:58 -04:00
#! Relayout a gadget and its children.
dup add-invalid (relayout-down) ;
2005-04-30 17:17:10 -04:00
: set-gadget-dim ( dim gadget -- )
2dup rectangle-dim =
[ 2drop ] [ [ set-rectangle-dim ] keep relayout-down ] ifte ;
2005-06-28 23:50:23 -04:00
GENERIC: pref-dim ( gadget -- dim )
M: gadget pref-dim rectangle-dim ;
2005-03-07 23:15:00 -05:00
2005-03-07 22:11:36 -05:00
GENERIC: layout* ( gadget -- )
2005-03-07 23:15:00 -05:00
2005-06-28 23:50:23 -04:00
: prefer ( gadget -- ) dup pref-dim swap set-gadget-dim ;
2005-03-07 23:15:00 -05:00
M: gadget layout* drop ;
2005-02-19 21:49:37 -05:00
2005-03-07 22:11:36 -05:00
GENERIC: user-input* ( ch gadget -- ? )
2005-06-23 22:35:41 -04:00
2005-03-07 22:11:36 -05:00
M: gadget user-input* 2drop t ;
GENERIC: focusable-child* ( gadget -- gadget/t )
M: gadget focusable-child* drop t ;
: focusable-child ( gadget -- gadget )
dup focusable-child*
dup t = [ drop ] [ nip focusable-child ] ifte ;