]> gitweb.factorcode.org Git - factor.git/commitdiff
working on hand gadget
authorSlava Pestov <slava@factorcode.org>
Wed, 2 Feb 2005 01:14:03 +0000 (01:14 +0000)
committerSlava Pestov <slava@factorcode.org>
Wed, 2 Feb 2005 01:14:03 +0000 (01:14 +0000)
library/bootstrap/boot-stage2.factor
library/sdl/sdl-utils.factor
library/stack.factor
library/ui/boxes.factor
library/ui/gadgets.factor
library/ui/gestures.factor
library/ui/hand.factor [new file with mode: 0644]
library/ui/paint.factor
library/ui/shapes.factor
library/ui/world.factor

index e75e4fc9635bc32113e5fd666f07858e7935b1be..d88965f0c2eb3df72fb87f7a3f73db7a3b21ac30 100644 (file)
@@ -157,6 +157,7 @@ cpu "x86" = [
         "/library/ui/gadgets.factor"\r
         "/library/ui/boxes.factor"\r
         "/library/ui/gestures.factor"\r
+        "/library/ui/hand.factor"\r
         "/library/ui/world.factor"\r
     ] [\r
         dup print\r
index 9a5234c4754ea6f417f26bedf8d7585ef99f9ccd..efba5137a9812cdf6a769b36029543b36d3c1de7 100644 (file)
@@ -55,6 +55,7 @@ SYMBOL: surface
 
 : with-screen ( width height bpp flags quot -- )
     #! Set up SDL graphics and call the quotation.
+    SDL_INIT_EVERYTHING SDL_Init drop  TTF_Init
     [ >r init-screen r> call SDL_Quit ] with-scope ; inline
 
 : rgb ( r g b -- n )
index deff179ff3fbe764b399e1e147340bfdb8f69bd1..2894406aaa1911ba36736664cb03f77893bc10a1 100644 (file)
@@ -10,7 +10,7 @@ IN: kernel
 : -rot ( x y z -- z x y ) swap >r swap r> ; inline
 : dupd ( x y -- x x y ) >r dup r> ; inline
 : swapd ( x y z -- y x z ) >r swap r> ; inline
-: 2swap ( x y z t -- z t x y ) >r rot r> rot ; inline
+: 2swap ( x y z t -- z t x y ) rot >r rot r> ; inline
 : nip ( x y -- y ) swap drop ; inline
 : 2nip ( x y z -- z ) >r drop drop r> ; inline
 : tuck ( x y -- y x y ) dup >r swap r> ; inline
index a8ccf80cca9cdfe169761a83fee0c12b50b71d1f..37668b581f0620bfb2c1adf9de221f122f407f79 100644 (file)
@@ -15,8 +15,9 @@ M: general-list draw ( list -- )
 M: box draw ( box -- )
     dup [
         dup [
-            dup box-contents draw
+            dup
             box-delegate draw
+            box-contents draw
         ] with-gadget
     ] with-translation ;
 
@@ -49,10 +50,18 @@ M: box pick-up* ( point box -- gadget )
     ] with-translation ;
 
 : box- ( gadget box -- )
-    2dup box-contents remove swap set-box-contents
+    2dup box-contents remove swap tuck set-box-contents redraw
     f swap set-gadget-parent ;
 
+: (box+) ( gadget box -- )
+    [ box-contents cons ] keep set-box-contents ;
+
+: unparent ( gadget -- )
+    dup gadget-parent dup [ box- ] [ 2drop ] ifte ;
+
 : box+ ( gadget box -- )
     #! Add a gadget to a box.
-    over gadget-parent [ pick swap box- ] when*
-    [ box-contents cons ] keep set-box-contents ;
+    over unparent
+    dup pick set-gadget-parent
+    tuck (box+)
+    redraw ;
index 966105cdc41e73d80fdc86b61b65fdcd2c768d73..149245999e1dfd4039857ed757b93faff979211c 100644 (file)
@@ -4,7 +4,6 @@ IN: gadgets
 USING: generic hashtables kernel lists namespaces ;
 
 ! Gadget protocol.
-
 GENERIC: pick-up* ( point gadget -- gadget/t )
 GENERIC: handle-gesture* ( gesture gadget -- ? )
 
@@ -40,8 +39,17 @@ M: gadget pick-up* inside? ;
 
 M: gadget handle-gesture* 2drop t ;
 
+GENERIC: redraw ( gadget -- )
+
 : move-gadget ( x y gadget -- )
-    [ move-shape ] keep set-gadget-delegate ;
+    [ move-shape ] keep
+    [ set-gadget-delegate ] keep
+    redraw ;
+
+: resize-gadget ( w h gadget -- )
+    [ resize-shape ] keep
+    [ set-gadget-delegate ] keep
+    redraw ;
 
 ! An invisible gadget.
 WRAPPER: ghost
index 0d6c4e8bd55c6a952d3ca578fd7c6feac67b88d4..058eee2e39b7036bbc04c6f23deb989ddab8bee0 100644 (file)
@@ -16,3 +16,9 @@ USING: generic kernel lists sdl-event ;
     ] [
         2drop
     ] ifte ;
+
+TUPLE: redraw-gesture ;
+C: redraw-gesture ;
+
+M: object redraw ( gadget -- )
+    <redraw-gesture> swap handle-gesture ;
diff --git a/library/ui/hand.factor b/library/ui/hand.factor
new file mode 100644 (file)
index 0000000..8868b5d
--- /dev/null
@@ -0,0 +1,40 @@
+! Copyright (C) 2005 Slava Pestov.
+! See http://factor.sf.net/license.txt for BSD license.
+IN: gadgets
+USING: alien generic kernel lists math namespaces sdl sdl-event
+sdl-video ;
+
+! The hand is a special gadget that holds mouse position and
+! mouse button click state. The hand's parent is the world, but
+! it is special in that the world does not list it as part of
+! its contents.
+TUPLE: hand click-pos clicked buttons delegate ;
+
+C: hand ( -- hand )
+    0 <gadget> <ghost> <box>
+    over set-hand-delegate ;
+
+GENERIC: hand-gesture ( hand gesture -- )
+
+M: object hand-gesture ( hand gesture -- ) 2drop ;
+
+: button/ ( n hand -- )
+    [ hand-buttons unique ] keep set-hand-buttons ;
+
+: button\ ( n hand -- )
+    [ hand-buttons remove ] keep set-hand-buttons ;
+
+M: button-down-event hand-gesture ( hand gesture -- )
+    2dup
+    dup button-event-x swap button-event-y rect>
+    swap set-hand-click-pos
+    button-event-button swap button/ ;
+
+M: button-up-event hand-gesture ( hand gesture -- )
+    button-event-button swap button\ ;
+
+M: motion-event hand-gesture ( hand gesture -- )
+    dup motion-event-x swap motion-event-y rot move-gadget ;
+
+M: hand redraw ( hand -- )
+    drop world get redraw ;
index 6e08f10349f04b0e5e18d4f90beac5a85ebce8ea..0435cb90ee1a46e9f293da74e47df161632df483 100644 (file)
@@ -21,7 +21,13 @@ SYMBOL: filled ! is the interior of the shape filled?
 
 GENERIC: draw ( obj -- )
 
-M: rect draw ( rect -- )
+M: ghost draw ( ghost -- )
+    drop ;
+
+M: number draw ( point -- )
+    >r surface get r> >rect rgb-color pixelColor ;
+
+M: rectangle draw ( rect -- )
     >r surface get r> shape>screen rgb-color
     filled get [ boxColor ] [ rectangleColor ] ifte ;
 
index 6c015c12e7563cda98f416040eff40166cfff7df..048ed402f2b6f5ac6bbb213358317c115f84417a 100644 (file)
@@ -45,38 +45,48 @@ M: number shape-h drop 0 ;
 M: number move-shape ( x y point -- point ) drop rect> ;
 
 ! A rectangle maps trivially to the shape protocol.
-TUPLE: rect x y w h ;
-M: rect shape-x rect-x ;
-M: rect shape-y rect-y ;
-M: rect shape-w rect-w ;
-M: rect shape-h rect-h ;
+TUPLE: rectangle x y w h ;
+M: rectangle shape-x rectangle-x ;
+M: rectangle shape-y rectangle-y ;
+M: rectangle shape-w rectangle-w ;
+M: rectangle shape-h rectangle-h ;
 
 : fix-neg ( a b c -- a+c b -c )
     dup 0 < [ neg tuck >r >r + r> r> ] when ;
 
-C: rect ( x y w h -- rect )
+C: rectangle ( x y w h -- rect )
     #! We handle negative w/h for convinience.
     >r fix-neg >r fix-neg r> r>
-    [ set-rect-h ] keep
-    [ set-rect-w ] keep
-    [ set-rect-y ] keep
-    [ set-rect-x ] keep ;
+    [ set-rectangle-h ] keep
+    [ set-rectangle-w ] keep
+    [ set-rectangle-y ] keep
+    [ set-rectangle-x ] keep ;
 
 M: number resize-shape ( w h point -- rect )
-     >rect 2swap <rect> ;
+     >rect 2swap <rectangle> ;
 
-M: rect move-shape ( x y rect -- rect )
-    [ rect-w ] keep rect-h <rect> ;
+M: rectangle move-shape ( x y rect -- rect )
+    [ rectangle-w ] keep rectangle-h <rectangle> ;
 
-M: rect resize-shape ( w h rect -- rect )
-    [ rect-x ] keep rect-y 2swap <rect> ;
+M: rectangle resize-shape ( w h rect -- rect )
+    [ rectangle-x ] keep rectangle-y 2swap <rectangle> ;
 
-: rect-x-extents ( rect -- x1 x2 )
-    dup rect-x x get + swap rect-w dupd + ;
+: rectangle-x-extents ( rect -- x1 x2 )
+    dup rectangle-x x get + swap rectangle-w dupd + ;
 
-: rect-y-extents ( rect -- x1 x2 )
-    dup rect-y y get + swap rect-h dupd + ;
+: rectangle-y-extents ( rect -- x1 x2 )
+    dup rectangle-y y get + swap rectangle-h dupd + ;
 
-M: rect inside? ( point rect -- ? )
-    over shape-x over rect-x-extents between? >r
-    swap shape-y swap rect-y-extents between? r> and ;
+M: rectangle inside? ( point rect -- ? )
+    over shape-x over rectangle-x-extents between? >r
+    swap shape-y swap rectangle-y-extents between? r> and ;
+
+! Delegates to a bounded shape, but absorbs all points.
+WRAPPER: everywhere
+M: everywhere inside? ( point world -- ? ) 2drop t ;
+
+M: everywhere move-shape ( x y everywhere -- )
+    everywhere-delegate move-shape <everywhere> ;
+
+M: everywhere resize-shape ( w h everywhere -- )
+    everywhere-delegate resize-shape <everywhere> ;
index 740a0f915ccdae32f5d79e8b5d13b758cf356490..9c380bf527b46e1d8eeff7c3529c136eb1a240ce 100644 (file)
@@ -1,53 +1,23 @@
 ! Copyright (C) 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: gadgets
-USING: alien generic kernel lists math namespaces sdl sdl-event ;
-
-! The hand is a special gadget that holds mouse position and
-! mouse button click state.
-TUPLE: hand clicked buttons delegate ;
-
-C: hand ( -- hand ) 0 <gadget> over set-hand-delegate ;
-
-GENERIC: hand-gesture ( hand gesture -- )
-
-M: alien hand-gesture ( hand gesture -- ) 2drop ;
-
-: button/ ( n hand -- )
-    [ hand-buttons unique ] keep set-hand-buttons ;
-
-: button\ ( n hand -- )
-    [ hand-buttons remove ] keep set-hand-buttons ;
-
-M: button-down-event hand-gesture ( hand gesture -- )
-    2dup
-    dup button-event-x swap button-event-y rect>
-    swap set-hand-clicked
-    button-event-button swap button/ ;
-
-M: button-up-event hand-gesture ( hand gesture -- )
-    button-event-button swap button\ ;
-
-M: motion-event hand-gesture ( hand gesture -- )
-    dup motion-event-x swap motion-event-y rot move-gadget ;
+USING: alien generic kernel lists math namespaces sdl sdl-event
+sdl-video ;
 
 ! The world gadget is the top level gadget that all (visible)
 ! gadgets are contained in. The current world is stored in the
 ! world variable.
 TUPLE: world running? hand delegate redraw? ;
 
-TUPLE: redraw-gesture ;
-C: redraw-gesture ;
-
-: redraw ( gadget -- )
-    <redraw-gesture> swap handle-gesture ;
-
 M: hand handle-gesture* ( gesture hand -- ? )
     2dup swap hand-gesture
     world get pick-up handle-gesture* ;
 
 : <world-box> ( -- box )
-    0 0 1000 1000 <rect> <gadget> <box> ;
+    0 0 0 0 <rectangle> <everywhere> <gadget>
+    dup blue 3list color set-paint-property
+    dup t filled set-paint-property
+    <box> ;
 
 C: world ( -- world )
     <world-box> over set-world-delegate
@@ -62,7 +32,14 @@ M: alien world-gesture ( world gesture -- ) 2drop ;
 M: quit-event world-gesture ( world gesture -- )
     drop f swap set-world-running? ;
 
+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 ;
+
 M: redraw-gesture world-gesture ( world gesture -- )
+
     drop t swap set-world-redraw? ;
 
 M: world handle-gesture* ( gesture world -- ? )
@@ -74,7 +51,8 @@ M: world handle-gesture* ( gesture world -- ? )
     world get dup world-redraw? [
         [
             f over set-world-redraw?
-            draw
+            dup draw
+            world-hand draw
         ] with-surface
     ] [
         drop
@@ -89,4 +67,17 @@ M: world handle-gesture* ( gesture world -- ? )
         ] ifte
     ] when ;
 
+: 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 ;
+
 global [ <world> world set ] bind