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-02-26 02:11:25 -05:00
|
|
|
USING: generic kernel lists math namespaces sdl ;
|
2005-01-31 14:02:09 -05:00
|
|
|
|
2005-02-01 19:00:16 -05:00
|
|
|
! Shape protocol. Shapes are immutable; moving or resizing a
|
|
|
|
! shape makes a new shape.
|
2005-01-31 14:02:09 -05:00
|
|
|
|
|
|
|
! These dynamically-bound variables affect the generic word
|
2005-02-27 03:48:27 -05:00
|
|
|
! inside? and others.
|
2005-02-01 19:00:16 -05:00
|
|
|
SYMBOL: x
|
|
|
|
SYMBOL: y
|
|
|
|
|
|
|
|
GENERIC: inside? ( point shape -- ? )
|
2005-01-31 14:02:09 -05:00
|
|
|
|
|
|
|
! A shape is an object with a defined bounding
|
|
|
|
! box, and a notion of interior.
|
|
|
|
GENERIC: shape-x
|
|
|
|
GENERIC: shape-y
|
|
|
|
GENERIC: shape-w
|
|
|
|
GENERIC: shape-h
|
|
|
|
|
2005-02-02 22:00:46 -05:00
|
|
|
GENERIC: move-shape ( x y shape -- )
|
|
|
|
GENERIC: resize-shape ( w h shape -- )
|
2005-01-31 14:02:09 -05:00
|
|
|
|
2005-03-07 22:11:36 -05:00
|
|
|
! The painting protocol. Painting is controlled by various
|
|
|
|
! dynamically-scoped variables.
|
|
|
|
|
|
|
|
! Colors are lists of three integers, 0..255.
|
|
|
|
SYMBOL: foreground ! Used for text and outline shapes.
|
|
|
|
SYMBOL: background ! Used for filled shapes.
|
|
|
|
SYMBOL: reverse-video
|
|
|
|
|
|
|
|
: fg reverse-video get background foreground ? get ;
|
|
|
|
: bg reverse-video get foreground background ? get ;
|
|
|
|
|
|
|
|
SYMBOL: font ! a list of two elements, a font name and size.
|
|
|
|
|
|
|
|
GENERIC: draw-shape ( obj -- )
|
|
|
|
|
|
|
|
! Utility words
|
|
|
|
|
2005-02-26 02:11:25 -05:00
|
|
|
: with-trans ( shape quot -- )
|
2005-01-31 14:02:09 -05:00
|
|
|
#! All drawing done inside the quotation is translated
|
|
|
|
#! relative to the shape's origin.
|
|
|
|
[
|
|
|
|
>r dup
|
|
|
|
shape-x x [ + ] change
|
|
|
|
shape-y y [ + ] change
|
|
|
|
r> call
|
|
|
|
] with-scope ; inline
|
|
|
|
|
2005-02-02 19:50:13 -05:00
|
|
|
: max-width ( list -- n )
|
|
|
|
#! The width of the widest shape.
|
2005-02-05 22:51:41 -05:00
|
|
|
[ [ shape-w ] map [ > ] top ] [ 0 ] ifte* ;
|
2005-02-02 19:50:13 -05:00
|
|
|
|
2005-02-02 22:00:46 -05:00
|
|
|
: max-height ( list -- n )
|
|
|
|
#! The height of the tallest shape.
|
2005-02-05 22:51:41 -05:00
|
|
|
[ [ shape-h ] map [ > ] top ] [ 0 ] ifte* ;
|
2005-02-02 22:00:46 -05:00
|
|
|
|
2005-02-15 18:05:28 -05:00
|
|
|
: accumilate ( gap list -- n list )
|
|
|
|
#! The nth element of the resulting list is the sum of the
|
|
|
|
#! first n elements of the given list plus gap, n times.
|
|
|
|
[ 0 swap [ over , + over + ] each ] make-list >r swap - r> ;
|
|
|
|
|
|
|
|
: run-widths ( gap list -- w list )
|
2005-02-02 22:00:46 -05:00
|
|
|
#! Compute a list of running sums of widths of shapes.
|
2005-02-15 18:05:28 -05:00
|
|
|
[ shape-w ] map accumilate ;
|
2005-02-02 22:00:46 -05:00
|
|
|
|
2005-02-15 18:05:28 -05:00
|
|
|
: run-heights ( gap list -- h list )
|
2005-02-02 22:00:46 -05:00
|
|
|
#! Compute a list of running sums of heights of shapes.
|
2005-02-15 18:05:28 -05:00
|
|
|
[ shape-h ] map accumilate ;
|
2005-02-02 19:50:13 -05:00
|
|
|
|
2005-02-05 11:52:24 -05:00
|
|
|
: shape-pos ( shape -- pos )
|
|
|
|
dup shape-x swap shape-y rect> ;
|