"/library/ui/gestures.factor"\r
"/library/ui/hand.factor"\r
"/library/ui/world.factor"\r
+ "/library/ui/label.factor"\r
+ "/library/ui/events.factor"\r
] [\r
dup print\r
run-resource\r
USE: streams
USE: strings
USE: sdl-ttf
+USE: hashtables
SYMBOL: surface
SYMBOL: width
: <font> ( name ptsize -- font )
>r resource-path swap cat2 r> TTF_OpenFont ;
-: font ( name ptsize -- font )
+SYMBOL: logical-fonts
+
+: logical-font ( name -- name )
+ dup logical-fonts get hash dup [ nip ] [ drop ] ifte ;
+
+global [
+ {{
+ [[ "Monospaced" "/fonts/VeraMono.ttf" ]]
+ }} logical-fonts set
+] bind
+
+: lookup-font ( [[ name ptsize ]] -- font )
fonts get [
- 2dup cons get [
- 2nip
+ unswons logical-font swons dup get [
+ nip
] [
- 2dup cons >r <font> dup r> set
+ [ uncons <font> dup ] keep set
] ifte*
] bind ;
dup surface-w swap surface-h make-rect ;
: draw-surface ( x y surface -- )
+ surface get SDL_UnlockSurface
[
[ surface-rect ] keep swap surface get 0 0
- ] keep surface-rect swap rot SDL_UpperBlit drop ;
+ ] keep surface-rect swap rot SDL_UpperBlit drop
+ surface get dup must-lock-surface? [
+ SDL_LockSurface
+ ] when drop ;
: draw-string ( x y font text fg bg -- width )
pick str-length 0 = [
pick-up
] bind
] unit-test
-
-[ 30 ] [
- default-paint [
- 35 10 rect>
- [ 10 30 50 70 ] [ funny-rect ] map
- 0 0 200 200 <rect> <gadget> <ghost> <box>
- [ set-box-contents ] keep
- pick-up shape-x
- ] bind
-] unit-test
] ifte ;
: set-console-font ( font ptsize )
- font dup console-font set
+ cons lookup-font dup console-font set
TTF_FontHeight line-height set ;
: init-console ( -- )
#! paint, just call the quotation.
>r gadget-paint r> bind ;
-M: gadget draw ( gadget -- )
- dup [ gadget-delegate draw ] with-gadget ;
+M: gadget draw ( gadget -- ) drop ;
M: gadget pick-up* inside? ;
[ set-gadget-delegate ] keep
redraw ;
-! An invisible gadget.
-WRAPPER: ghost
-M: ghost draw drop ;
-M: ghost pick-up* 2drop f ;
-M: ghost draw drop ;
+! A simple gadget that just draws its shape.
+TUPLE: stamp delegate ;
+
+C: stamp ( shape -- )
+ swap <gadget> over set-stamp-delegate ;
+
+M: stamp draw ( stamp -- )
+ dup [ gadget-delegate draw ] with-gadget ;
TUPLE: hand click-pos clicked buttons delegate ;
C: hand ( world -- hand )
- 0 <gadget> <ghost> <box>
+ 0 <gadget> <box>
over set-hand-delegate
[ set-gadget-parent ] keep ;
--- /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 ;
[[ y 0 ]]
[[ color [ 0 0 0 ] ]]
[[ filled f ]]
- [[ font [ "Monospaced" 12 ] ]]
+ [[ font [[ "Monospaced" 12 ]] ]]
}} ;
TUPLE: world running? hand delegate redraw? ;
: <world-box> ( -- box )
- 0 0 0 0 <rectangle> <everywhere> <gadget>
+ 0 0 0 0 <rectangle> <everywhere> <stamp>
dup blue 3list color set-paint-property
dup t filled set-paint-property
<box> ;