! Copyright (C) 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: gadgets USING: alien errors generic kernel lists math memory namespaces prettyprint sdl sequences stdio strings threads ; ! The world gadget is the top level gadget that all (visible) ! gadgets are contained in. The current world is stored in the ! world variable. The menu slot ensures that only one menu is ! open at any one time. TUPLE: world running? hand menu ; : ( -- box ) 0 0 0 0 ; C: world ( -- world ) over set-delegate t over set-world-running? dup over set-world-hand ; M: world inside? ( point world -- ? ) 2drop t ; : hand world get world-hand ; : draw-world ( world -- ) dup gadget-redraw? [ [ draw-gadget ] with-surface ] [ drop ] ifte ; DEFER: handle-event : layout-world ( world -- ) dup 0 0 width get height get clip set-paint-prop layout ; : world-step ( world -- ? ) world get dup world-running? [ dup layout-world draw-world t ] [ drop f ] ifte ; : next-event ( -- event ? ) dup SDL_PollEvent ; : run-world ( -- ) #! Keep polling for events until there are no more events in #! the queue; then block for the next event. next-event [ handle-event run-world ] [ drop world-step [ yield run-world ] when ] ifte ; : ensure-ui ( -- ) #! Raise an error if the UI is not running. world get dup [ world-running? ] when [ "UI not running." throw ] unless ; : start-world ( -- ) world get t over set-world-running? relayout ;