factor/library/ui/gadgets.factor

122 lines
3.1 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: rect loc dim ;
GENERIC: inside? ( loc rect -- ? )
: rect-bounds ( rect -- loc dim )
dup rect-loc swap rect-dim ;
: rect-extent ( rect -- loc dim )
dup rect-loc dup rot rect-dim v+ ;
: screen-loc ( rect -- loc )
rect-loc origin get v+ ;
: screen-bounds ( rect -- rect )
dup screen-loc swap rect-dim <rect> ;
2005-08-24 00:30:07 -04:00
M: rect inside? ( loc rect -- ? )
screen-bounds rect-bounds { 1 1 1 } v- { 0 0 0 } vmax
>r v- { 0 0 0 } r> vbetween? conjunction ;
: intersect ( rect rect -- rect )
>r rect-extent r> rect-extent swapd vmin >r vmax dup r>
swap v- { 0 0 0 } vmax <rect> ;
: intersects? ( rect rect -- ? )
>r rect-extent r> rect-extent swapd vmin >r vmax r> v-
[ 0 < ] contains? ;
! 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
2005-08-23 20:27:42 -04:00
paint gestures visible? relayout? root?
2005-07-17 00:21:10 -04:00
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 )
{ 0 0 0 } dup <rect> over set-delegate
2005-07-17 00:21:10 -04:00
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 rect-dim =
[ 2drop ] [ [ set-rect-dim ] keep relayout-down ] ifte ;
2005-06-28 23:50:23 -04:00
GENERIC: pref-dim ( gadget -- dim )
M: gadget pref-dim rect-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 ;
GENERIC: pick-up* ( point gadget -- gadget )
: pick-up-list ( point gadgets -- gadget )
[
dup gadget-visible? [ inside? ] [ 2drop f ] ifte
] find-with nip ;
M: gadget pick-up* ( point gadget -- gadget )
gadget-children pick-up-list ;
: pick-up ( point gadget -- gadget )
#! The logic is thus. If the point is definately outside the
#! box, return f. Otherwise, see if the point is contained
#! in any subgadget. If not, see if it is contained in the
#! box delegate.
dup gadget-visible? >r 2dup inside? r> drop [
[ rect-loc v- ] keep 2dup
pick-up* [ pick-up ] [ nip ] ?ifte
] [
2drop f
] ifte ;