factor/library/ui/world.factor

70 lines
1.9 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 errors gadgets-layouts generic io kernel lists math
memory namespaces prettyprint sdl sequences sequences strings
threads vectors ;
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-07-08 01:32:29 -04:00
! world variable. The invalid slot is a list of gadgets that
! need to be layout.
2005-08-23 20:27:42 -04:00
TUPLE: world running? hand glass invalid ;
2005-02-01 19:00:16 -05:00
DEFER: <hand>
DEFER: update-hand
2005-08-26 00:55:56 -04:00
: add-layer ( gadget -- )
world get add-gadget ;
2005-01-31 22:32:06 -05:00
C: world ( -- world )
<stack> over set-delegate
2005-07-08 01:32:29 -04:00
t over set-gadget-root?
2005-08-23 20:27:42 -04:00
dup <hand> over set-world-hand ;
2005-01-31 22:32:06 -05:00
: add-invalid ( gadget -- )
world get [ world-invalid cons ] keep set-world-invalid ;
2005-07-08 01:32:29 -04:00
: pop-invalid ( -- list )
world get [ world-invalid f ] keep set-world-invalid ;
2005-07-08 01:32:29 -04:00
: layout-world ( -- )
world get world-invalid
[ pop-invalid [ layout ] each layout-world ] when ;
2005-07-08 01:32:29 -04:00
: hide-glass ( -- )
world get world-glass unparent f
world get set-world-glass ;
: show-glass ( gadget -- )
hide-glass
2005-08-26 22:22:00 -04:00
<gadget> dup add-layer dup world get set-world-glass
dupd add-gadget prefer ;
2005-02-19 17:54:04 -05:00
: draw-world ( world -- )
[
{ 0 0 0 } width get height get 0 3vector <rect> clip set
draw-gadget
] with-surface ;
2005-02-01 19:00:16 -05:00
2005-02-01 21:47:10 -05:00
DEFER: handle-event
2005-03-03 20:43:55 -05:00
: world-step ( -- ? )
world get dup world-invalid >r layout-world r>
[ dup world-hand update-hand draw-world ] [ drop ] ifte ;
2005-02-19 17:54:04 -05:00
2005-08-26 22:22:00 -04:00
: next-event ( -- event ? ) <event> dup SDL_PollEvent ;
2005-05-03 20:09:04 -04:00
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-05-03 20:09:04 -04:00
next-event [
handle-event run-world
] [
2005-08-23 18:16:42 -04:00
drop world-step do-timers
world get world-running? [ 10 sleep run-world ] when
2005-02-19 21:49:37 -05:00
] ifte ;
2005-01-31 22:32:06 -05:00
: start-world ( -- )
world get t over set-world-running? relayout ;