factor/library/ui/world.factor

81 lines
2.0 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
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-02 19:50:13 -05:00
TUPLE: world running? hand delegate ;
2005-02-01 19:00:16 -05:00
2005-01-31 22:32:06 -05:00
: <world-box> ( -- box )
0 0 0 0 <plain-rect> <gadget> ;
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 21:47:10 -05:00
dup <hand> over set-world-hand ;
2005-01-31 22:32:06 -05:00
M: world inside? ( point world -- ? ) 2drop t ;
2005-01-31 22:32:06 -05:00
: my-hand ( -- hand ) world get world-hand ;
2005-02-01 19:00:16 -05:00
: draw-world ( -- )
2005-02-02 19:50:13 -05:00
world get dup gadget-redraw? [
2005-02-15 18:05:28 -05:00
dup world-hand update-hand [
2005-02-02 19:50:13 -05:00
f over set-gadget-redraw?
dup draw-gadget
dup gadget-paint [ world-hand draw-gadget ] bind
2005-02-01 19:00:16 -05:00
] with-surface
] [
drop
] ifte ;
2005-02-01 21:47:10 -05:00
DEFER: handle-event
2005-02-15 18:05:28 -05:00
: layout-world world get dup layout world-hand update-hand ;
2005-02-02 19:50:13 -05:00
: eat-events ( event -- )
#! Keep polling for events until there are no more events in
#! the queue; then block for the next event.
dup SDL_PollEvent [
dup handle-event eat-events
] [
SDL_WaitEvent
] ifte ;
2005-01-31 22:32:06 -05:00
: run-world ( -- )
world get world-running? [
layout-world draw-world
<event> dup eat-events [
handle-event run-world
2005-01-31 22:32:06 -05:00
] [
drop
] ifte
] when ;
: start-world ( -- )
2005-02-01 20:14:03 -05:00
#! Start the Factor graphics subsystem with the given screen
#! dimensions.
t world get set-world-running?
world get shape-w world get shape-h 0 SDL_RESIZABLE
[
0 x set
0 y set
[ run-world ] with-screen
] with-scope ;
2005-02-01 20:14:03 -05:00
global [
<world> world set
2005-02-11 12:45:24 -05:00
1024 768 world get resize-gadget
{{
2005-02-11 12:45:24 -05:00
[[ background [ 255 255 255 ] ]]
[[ foreground [ 0 0 102 ] ]]
[[ bevel-1 [ 224 224 255 ] ]]
[[ bevel-2 [ 192 192 216 ] ]]
[[ bevel-up? t ]]
2005-02-11 12:45:24 -05:00
[[ font [[ "Sans Serif" 14 ]] ]]
}} world get set-gadget-paint
] bind