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
|
2005-02-01 20:14:03 -05:00
|
|
|
USING: alien generic kernel lists math namespaces sdl sdl-event
|
|
|
|
sdl-video ;
|
2005-02-01 19:00:16 -05:00
|
|
|
|
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? ;
|
|
|
|
|
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 )
|
2005-02-01 20:14:03 -05:00
|
|
|
0 0 0 0 <rectangle> <everywhere> <gadget>
|
|
|
|
dup blue 3list color set-paint-property
|
|
|
|
dup t filled set-paint-property
|
|
|
|
<box> ;
|
2005-01-31 22:32:06 -05:00
|
|
|
|
|
|
|
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 20:14:03 -05:00
|
|
|
M: resize-event world-gesture ( world gesture -- ? )
|
|
|
|
dup resize-event-w swap resize-event-h
|
|
|
|
[ rot resize-gadget ] 2keep
|
|
|
|
0 SDL_HWSURFACE SDL_RESIZABLE bitor init-screen
|
|
|
|
world get redraw ;
|
|
|
|
|
2005-02-01 19:00:16 -05:00
|
|
|
M: redraw-gesture world-gesture ( world gesture -- )
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2005-02-01 19:00:16 -05:00
|
|
|
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?
|
2005-02-01 20:14:03 -05:00
|
|
|
dup draw
|
|
|
|
world-hand draw
|
2005-02-01 19:00:16 -05:00
|
|
|
] 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 ;
|
|
|
|
|
2005-02-01 20:14:03 -05:00
|
|
|
: init-world ( w h -- )
|
|
|
|
t world get set-world-running?
|
|
|
|
t world get set-world-redraw?
|
|
|
|
world get resize-gadget ;
|
|
|
|
|
|
|
|
: world-flags SDL_HWSURFACE SDL_RESIZABLE bitor ;
|
|
|
|
|
|
|
|
: start-world ( w h -- )
|
|
|
|
#! Start the Factor graphics subsystem with the given screen
|
|
|
|
#! dimensions.
|
|
|
|
2dup init-world 0 world-flags
|
|
|
|
default-paint [ [ run-world ] with-screen ] bind ;
|
|
|
|
|
2005-01-31 22:32:06 -05:00
|
|
|
global [ <world> world set ] bind
|