factor/library/ui/world.factor

93 lines
2.4 KiB
Factor
Raw Normal View History

2005-01-31 22:32:06 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: alien generic kernel lists math namespaces sdl sdl-event ;
! The hand is a special gadget that holds mouse position and
! mouse button click state.
TUPLE: hand clicked buttons delegate ;
C: hand ( -- hand ) 0 <gadget> over set-hand-delegate ;
GENERIC: hand-gesture ( hand gesture -- )
M: alien hand-gesture ( hand gesture -- ) 2drop ;
: button/ ( n hand -- )
[ hand-buttons unique ] keep set-hand-buttons ;
: button\ ( n hand -- )
[ hand-buttons remove ] keep set-hand-buttons ;
M: button-down-event hand-gesture ( hand gesture -- )
2dup
dup button-event-x swap button-event-y rect>
swap set-hand-clicked
button-event-button swap button/ ;
M: button-up-event hand-gesture ( hand gesture -- )
button-event-button swap button\ ;
2005-02-01 19:00:16 -05:00
M: motion-event hand-gesture ( hand gesture -- )
dup motion-event-x swap motion-event-y rot move-gadget ;
2005-01-31 22:32:06 -05:00
! The world gadget is the top level gadget that all (visible)
! gadgets are contained in. The current world is stored in the
! world variable.
2005-02-01 19:00:16 -05:00
TUPLE: world running? hand delegate redraw? ;
TUPLE: redraw-gesture ;
C: redraw-gesture ;
: redraw ( gadget -- )
<redraw-gesture> swap handle-gesture ;
2005-01-31 22:32:06 -05:00
M: hand handle-gesture* ( gesture hand -- ? )
2dup swap hand-gesture
world get pick-up handle-gesture* ;
: <world-box> ( -- box )
0 0 1000 1000 <rect> <gadget> <box> ;
C: world ( -- world )
<world-box> over set-world-delegate
t over set-world-running?
2005-02-01 19:00:16 -05:00
t over set-world-redraw?
2005-01-31 22:32:06 -05:00
<hand> over set-world-hand ;
GENERIC: world-gesture ( world gesture -- )
M: alien world-gesture ( world gesture -- ) 2drop ;
M: quit-event world-gesture ( world gesture -- )
drop f swap set-world-running? ;
2005-02-01 19:00:16 -05:00
M: redraw-gesture world-gesture ( world gesture -- )
drop t swap set-world-redraw? ;
2005-01-31 22:32:06 -05:00
M: world handle-gesture* ( gesture world -- ? )
swap world-gesture f ;
: my-hand ( -- hand ) world get world-hand ;
2005-02-01 19:00:16 -05:00
: draw-world ( -- )
world get dup world-redraw? [
[
f over set-world-redraw?
draw
] with-surface
] [
drop
] ifte ;
2005-01-31 22:32:06 -05:00
: run-world ( -- )
world get world-running? [
<event> dup SDL_WaitEvent 1 = [
2005-02-01 19:00:16 -05:00
my-hand handle-gesture draw-world run-world
2005-01-31 22:32:06 -05:00
] [
drop
] ifte
] when ;
global [ <world> world set ] bind