factor/library/ui/world.factor

96 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
2005-03-02 21:26:11 -05:00
USING: alien errors generic kernel lists math memory namespaces
sdl sdl-event sdl-video stdio strings threads ;
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
2005-03-01 22:11:08 -05:00
! world variable. The menu slot ensures that only one menu is
! open at any one time.
TUPLE: world running? hand menu 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-19 17:54:04 -05:00
: draw-world ( world -- )
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-03-01 00:45:50 -05:00
: layout-world ( world -- )
dup
0 0 width get height get <rectangle> clip set-paint-property
dup layout world-hand update-hand ;
2005-02-02 19:50:13 -05:00
2005-02-19 17:54:04 -05:00
: world-step ( world -- ? )
dup world-running? [
dup layout-world draw-world t
] [
drop f
] ifte ;
2005-03-01 18:55:25 -05:00
: run-world ( -- )
#! Keep polling for events until there are no more events in
#! the queue; then block for the next event.
2005-03-01 18:55:25 -05:00
<event> dup SDL_PollEvent [
[ handle-event ] in-thread drop run-world
] [
2005-03-01 18:55:25 -05:00
drop world get world-step [ yield run-world ] when
2005-02-19 21:49:37 -05:00
] ifte ;
2005-01-31 22:32:06 -05:00
2005-03-02 21:26:11 -05:00
: ensure-ui ( -- )
#! Raise an error if the UI is not running.
world get dup [ world-running? ] when [
"Inspector cannot be used if UI not running." throw
] unless ;
global [
2005-03-01 22:11:08 -05:00
<world> world set
2005-03-01 22:11:08 -05:00
2005-03-01 18:55:25 -05:00
1280 1024 world get resize-gadget
2005-03-01 22:11:08 -05:00
{{
2005-03-01 18:55:25 -05:00
2005-02-11 12:45:24 -05:00
[[ background [ 255 255 255 ] ]]
2005-03-01 18:55:25 -05:00
[[ foreground [ 0 0 0 ] ]]
[[ reverse-video f ]]
2005-03-01 22:19:26 -05:00
[[ font [[ "Sans Serif" 12 ]] ]]
}} world get set-gadget-paint
] bind
2005-02-27 16:00:55 -05:00
2005-03-01 22:11:08 -05:00
: title ( -- str )
"Factor " version cat2 ;
2005-02-27 16:00:55 -05:00
IN: shells
: ui ( -- )
#! 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 [
title dup SDL_WM_SetCaption
2005-03-01 22:11:08 -05:00
run-world
2005-02-27 16:00:55 -05:00
] with-screen
] with-scope ;