- doc comments of generics\r
- proper ordering for classes\r
- tuples: in/out syntax\r
+- tuples: gracefully handle changing shape\r
+- keep a list of getter/setter words\r
+- default constructor\r
\r
+ ffi:\r
\r
\r
+ listener/plugin:\r
\r
+- command to turn repl session into a source file\r
- update plugin docs\r
- extract word keeps indent\r
- word preview for remote words\r
"/library/ui/gestures.factor"\r
"/library/ui/hand.factor"\r
"/library/ui/world.factor"\r
- "/library/ui/label.factor"\r
+ "/library/ui/labels.factor"\r
+ "/library/ui/piles.factor"\r
"/library/ui/events.factor"\r
] [\r
dup print\r
USING: generic hashtables kernel lists namespaces ;
! A box is a gadget holding other gadgets.
-TUPLE: box contents delegate ;
+TUPLE: box children delegate ;
C: box ( gadget -- box )
[ set-box-delegate ] keep ;
+M: box gadget-children box-children ;
+
M: general-list draw ( list -- )
[ draw ] each ;
dup [
dup
box-delegate draw
- box-contents draw
+ box-children draw
] with-gadget
] with-translation ;
#! 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 [
- 2dup inside? [
- 2dup box-contents pick-up dup [
- 2nip
- ] [
- drop box-delegate pick-up*
- ] ifte
+ 2dup inside? [
+ 2dup [ translate ] keep box-children pick-up dup [
+ 2nip
] [
- 2drop f
+ drop box-delegate pick-up*
] ifte
- ] with-translation ;
+ ] [
+ 2drop f
+ ] ifte ;
: box- ( gadget box -- )
- [ 2dup box-contents remq swap set-box-contents ] keep
- redraw
+ [ 2dup box-children remq swap set-box-children ] keep
+ relayout
f swap set-gadget-parent ;
: (box+) ( gadget box -- )
- [ box-contents cons ] keep set-box-contents ;
+ [ box-children cons ] keep set-box-children ;
: unparent ( gadget -- )
dup gadget-parent dup [ box- ] [ 2drop ] ifte ;
over unparent
dup pick set-gadget-parent
tuck (box+)
- redraw ;
+ relayout ;
IN: gadgets
USING: generic hashtables kernel lists namespaces ;
+! A gadget is a shape, a paint, a mapping of gestures to
+! actions, and a reference to the gadget's parent. A gadget
+! delegates to its shape.
+TUPLE: gadget paint gestures parent relayout? redraw? delegate ;
+
! Gadget protocol.
GENERIC: pick-up* ( point gadget -- gadget/t )
#! exposed facade issue.
tuck pick-up* dup t = [ drop ] [ nip ] ifte ;
-! A gadget is a shape, a paint, a mapping of gestures to
-! actions, and a reference to the gadget's parent. A gadget
-! delegates to its shape.
-TUPLE: gadget paint gestures parent delegate ;
+GENERIC: gadget-children ( gadget -- list )
+M: gadget gadget-children drop f ;
+
+GENERIC: layout* ( gadget -- )
+M: gadget layout* drop ;
+
+: 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? [
+ f over set-gadget-relayout?
+ dup gadget-children [ layout ] each
+ layout*
+ ] [
+ drop
+ ] ifte ;
C: gadget ( shape -- gadget )
[ set-gadget-delegate ] keep
[ <namespace> swap set-gadget-paint ] keep
- [ <namespace> swap set-gadget-gestures ] keep ;
+ [ <namespace> swap set-gadget-gestures ] keep
+ [ t swap set-gadget-relayout? ] keep
+ [ t swap set-gadget-redraw? ] keep ;
: paint-property ( gadget key -- value )
swap gadget-paint hash ;
M: gadget pick-up* inside? ;
-DEFER: redraw ( gadget -- )
+: redraw ( gadget -- )
+ #! Redraw a gadget before the next iteration of the event
+ #! loop.
+ t over set-gadget-redraw?
+ gadget-parent [ redraw ] when* ;
+
+: relayout ( gadget -- )
+ #! Relayout a gadget before the next iteration of the event
+ #! loop. Since relayout also implies the visual
+ #! representation changed, we redraw the gadget too.
+ t over set-gadget-redraw?
+ t over set-gadget-relayout?
+ gadget-parent [ relayout ] when* ;
: move-gadget ( x y gadget -- )
[ move-shape ] keep
2drop
] ifte ;
-! Redraw gesture. Don't handle this yourself.
-: redraw ( gadget -- )
- \ redraw swap handle-gesture ;
-
! Mouse gestures are lists where the first element is one of:
SYMBOL: motion
SYMBOL: button-up
+++ /dev/null
-! Copyright (C) 2005 Slava Pestov.
-! See http://factor.sf.net/license.txt for BSD license.
-IN: gadgets
-USING: generic kernel lists math namespaces sdl ;
-
-! A label draws a text label, centered on the gadget's bounding
-! box.
-TUPLE: label text delegate ;
-
-: size-label ( label -- )
- [
- dup label-text swap gadget-paint
- [ font get lookup-font ] bind
- swap size-string
- ] keep resize-gadget ;
-
-C: label ( text -- )
- 0 0 0 0 <rectangle> <gadget> over set-label-delegate
- [ set-label-text ] keep
- [ size-label ] keep ;
-
-M: label draw ( label -- )
- dup shape-x x get +
- over shape-y y get +
- rot label-text
- >r font get lookup-font r>
- color get 3unlist make-color
- white make-color
- draw-string drop ;
--- /dev/null
+! Copyright (C) 2005 Slava Pestov.
+! See http://factor.sf.net/license.txt for BSD license.
+IN: gadgets
+USING: generic kernel lists math namespaces sdl ;
+
+! A label draws a text label, centered on the gadget's bounding
+! box.
+TUPLE: label text delegate ;
+
+C: label ( text -- )
+ 0 0 0 0 <rectangle> <gadget> over set-label-delegate
+ [ set-label-text ] keep ;
+
+M: label layout* ( label -- )
+ [
+ dup label-text swap gadget-paint
+ [ font get lookup-font ] bind
+ swap size-string
+ ] keep resize-gadget ;
+
+M: label draw ( label -- )
+ dup shape-x x get +
+ over shape-y y get +
+ rot label-text
+ >r font get lookup-font r>
+ color get 3unlist make-color
+ white make-color
+ draw-string drop ;
--- /dev/null
+! Copyright (C) 2005 Slava Pestov.
+! See http://factor.sf.net/license.txt for BSD license.
+IN: gadgets
+USING: generic hashtables kernel lists math namespaces ;
+
+! A pile is a box that lays out its contents vertically.
+TUPLE: pile delegate ;
+
+C: pile ( gadget -- pile )
+ [ >r <box> r> set-pile-delegate ] keep ;
+
+M: pile layout* ( pile -- )
+ dup gadget-children run-heights >r >r
+ dup gadget-children max-width r> pick resize-gadget
+ gadget-children r> zip [
+ uncons 0 swap rot move-gadget
+ ] each ;
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
-USING: generic kernel math namespaces ;
+USING: generic kernel lists math namespaces ;
! Shape protocol. Shapes are immutable; moving or resizing a
! shape makes a new shape.
r> call
] with-scope ; inline
+: translate ( point shape -- point )
+ #! Translate a point relative to the shape.
+ #! The rect>'ing of the given point won't be necessary as
+ #! soon as all generics delegate.
+ >r dup shape-x swap shape-y rect> r>
+ dup shape-x swap shape-y rect> - ;
+
+: max-width ( list -- n )
+ #! The width of the widest shape.
+ [ shape-w ] map [ > ] top ;
+
+: run-heights ( list -- h list )
+ #! Compute a list of accumilative sums of heights of shapes.
+ [ 0 swap [ over , shape-h + ] each ] make-list ;
+
! A point, represented as a complex number, is the simplest type
! of shape.
M: number inside? = ;
! The world gadget is the top level gadget that all (visible)
! gadgets are contained in. The current world is stored in the
! world variable.
-TUPLE: world running? hand delegate redraw? ;
+TUPLE: world running? hand delegate ;
: <world-box> ( -- box )
0 0 0 0 <rectangle> <everywhere> <stamp>
C: world ( -- world )
<world-box> over set-world-delegate
t over set-world-running?
- t over set-world-redraw?
dup <hand> over set-world-hand ;
: my-hand ( -- hand ) world get world-hand ;
: draw-world ( -- )
- world get dup world-redraw? [
+ world get dup gadget-redraw? [
[
- f over set-world-redraw?
+ f over set-gadget-redraw?
dup draw
world-hand draw
] with-surface
DEFER: handle-event
+: layout-world world get layout ;
+
: run-world ( -- )
world get world-running? [
<event> dup SDL_WaitEvent 1 = [
- handle-event draw-world run-world
+ handle-event draw-world layout-world run-world
] [
drop
] ifte
: init-world ( w h -- )
t world get set-world-running?
- t world get set-world-redraw?
- world get [ t swap set-world-redraw? ] \ redraw set-action
world get resize-gadget ;
: world-flags SDL_HWSURFACE SDL_RESIZABLE bitor ;