]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/maze/maze.factor
Harmonize spelling
[factor.git] / extra / maze / maze.factor
index c0623d96b6a64eb6da66f3797ed8ede3c8c94780..02a868641136cc8f7e7ced63800341e5463ce16d 100644 (file)
@@ -1,7 +1,7 @@
 ! From http://www.ffconsultancy.com/ocaml/maze/index.html
-USING: sequences namespaces math math.vectors opengl opengl.gl
-arrays kernel random ui ui.gadgets ui.gadgets.canvas ui.render
-math.order math.rectangles accessors ;
+USING: accessors arrays kernel math math.order math.vectors
+namespaces opengl.demo-support opengl.gl random sequences ui
+ui.gadgets ui.gadgets.canvas ui.gestures ui.render ;
 IN: maze
 
 CONSTANT: line-width 8
@@ -10,9 +10,6 @@ SYMBOL: visited
 
 : unvisited? ( cell -- ? ) first2 visited get ?nth ?nth ;
 
-: ?set-nth ( elt i seq -- )
-    2dup bounds-check? [ set-nth ] [ 3drop ] if ;
-
 : visit ( cell -- ) f swap first2 visited get ?nth ?set-nth ;
 
 : choices ( cell -- seq )
@@ -20,7 +17,7 @@ SYMBOL: visited
     [ v+ ] with map
     [ unvisited? ] filter ;
 
-: random-neighbour ( cell -- newcell ) choices random ;
+: random-neighbor ( cell -- newcell ) choices random ;
 
 : vertex ( pair -- )
     first2 [ 0.5 + line-width * ] bi@ glVertex2d ;
@@ -28,26 +25,26 @@ SYMBOL: visited
 : (draw-maze) ( cell -- )
     dup vertex
     glEnd
-    GL_POINTS glBegin dup vertex glEnd
+    GL_POINTS [ dup vertex ] do-state
     GL_LINE_STRIP glBegin
     dup vertex
     dup visit
-    dup random-neighbour dup [
+    dup random-neighbor [
         (draw-maze) (draw-maze)
     ] [
-        2drop
+        drop
         glEnd
         GL_LINE_STRIP glBegin
-    ] if ;
+    ] if* ;
 
 : draw-maze ( n -- )
     line-width 2 - glLineWidth
     line-width 2 - glPointSize
     1.0 1.0 1.0 1.0 glColor4d
-    dup [ drop t <array> ] with map visited set
-    GL_LINE_STRIP glBegin
-    { 0 0 } dup vertex (draw-maze)
-    glEnd ;
+    dup '[ _ t <array> ] replicate visited set
+    GL_LINE_STRIP [
+        { 0 0 } dup vertex (draw-maze)
+    ] do-state ;
 
 TUPLE: maze < canvas ;
 
@@ -61,7 +58,9 @@ M: maze draw-gadget* [ n draw-maze ] draw-canvas ;
 
 M: maze pref-dim* drop { 400 400 } ;
 
-: maze-window ( -- )
-    [ <maze> "Maze" open-window ] with-ui ;
+M: maze handle-gesture
+    over T{ button-down { # 1 } } =
+    [ nip relayout f ] [ call-next-method ] if ;
 
-MAIN: maze-window
+MAIN-WINDOW: maze-window { { title "Maze" } }
+    <maze> >>gadgets ;