From 3e29bee73b84feadcfbdbb35f04e037b4598a0b1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 31 Jan 2006 01:35:55 +0000 Subject: [PATCH] IRC bot fixes, UI fix, canvas gadget example --- examples/canvas.factor | 77 ++++++++++++++++++++++++++++++ examples/factorbot.factor | 42 ++++++++-------- library/opengl/opengl-utils.factor | 6 ++- library/ui/events.factor | 4 +- library/ui/gadgets.factor | 9 +++- library/ui/hierarchy.factor | 35 ++++++++++---- library/ui/layouts.factor | 2 +- library/ui/paragraphs.factor | 2 +- 8 files changed, 138 insertions(+), 39 deletions(-) create mode 100644 examples/canvas.factor diff --git a/examples/canvas.factor b/examples/canvas.factor new file mode 100644 index 0000000000..0f705db08f --- /dev/null +++ b/examples/canvas.factor @@ -0,0 +1,77 @@ +! Copyright (C) 2006 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. + +! This example only runs in the UI listener. + +! Pass with-canvas a quotation calling these words: +! - turn-by +! - move-by +! - plot-point +! - line-to +! - new-pen + +! plot-string doesn't yet work. + +! other GL calls can be made, but be careful. + +IN: gadgets-canvas +USING: arrays errors freetype gadgets gadgets-labels +gadgets-layouts gadgets-panes gadgets-theme generic kernel math +namespaces opengl sequences styles ; + +SYMBOL: canvas-font + +{ "monospaced" plain 12 } canvas-font set-global + +: turn-by ( angle -- ) 0 0 1 glRotated ; + +: move-by ( distance -- ) 0 0 glTranslated ; + +: plot-point ( -- ) + GL_POINTS [ 0 0 0 glVertex3d ] do-state ; + +: line-to ( distance -- ) + dup + GL_LINES [ 0 0 0 glVertex3d 0 0 glVertex3d ] do-state + move-by ; + +: plot-string ( string -- ) + canvas-font get open-font swap draw-string ; + +: new-pen ( quot -- ) GL_MODELVIEW swap do-matrix ; inline + +TUPLE: canvas quot id ; + +C: canvas ( quot -- ) + dup delegate>gadget [ set-canvas-quot ] keep ; + +M: canvas add-notify* ( gadget -- ) + canvas-quot GL_COMPILE [ with-scope ] make-dlist + swap set-canvas-id ; + +M: canvas draw-gadget* ( gadget -- ) + GL_MODELVIEW [ + dup rect-dim 2 v/n gl-translate + canvas-id glCallList + ] do-matrix ; + +: with-canvas ( size quot -- ) + dup solid-boundary [ set-gadget-dim ] keep gadget. ; + +: random-walk ( n -- ) + [ 2 random-int 1/2 - 180 * turn-by 10 line-to ] times ; + +: regular-polygon ( sides n -- ) + [ 360 swap / ] keep [ over line-to dup turn-by ] times 2drop ; + +: random-color + 4 [ drop 255 random-int 255 /f ] map gl-color ; + +: turtle-test + { 800 800 0 } [ + 36 [ + random-color + 10 line-to + 10 turn-by [ 60 17 regular-polygon ] new-pen + ] times + ] with-canvas ; diff --git a/examples/factorbot.factor b/examples/factorbot.factor index 4ef2f08967..9119112c86 100644 --- a/examples/factorbot.factor +++ b/examples/factorbot.factor @@ -1,10 +1,9 @@ ! Simple IRC bot written in Factor. ! Load the HTTP server first (contrib/httpd/load.factor). -! This file uses the url-encode and url-decode words. -USING: errors generic hashtables http io kernel math namespaces -parser prettyprint sequences strings unparser words ; +USING: errors generic hashtables html http io kernel math +namespaces parser prettyprint sequences strings words ; IN: factorbot SYMBOL: irc-stream @@ -48,7 +47,7 @@ M: object handle-irc ( line -- ) M: privmsg handle-irc ( line -- ) parse-privmsg " " split1 swap - [ "factorbot-commands" ] search dup + "factorbot-commands" lookup dup [ execute ] [ 2drop ] if ; M: ping handle-irc ( line -- ) @@ -63,25 +62,13 @@ M: ping handle-irc ( line -- ) : respond ( line -- ) receiver get nickname get = speaker receiver ? get say ; -: word-string ( word -- string ) - [ - "IN: " % dup word-vocabulary % - " " % dup definer word-name % - " " % dup word-name % - "stack-effect" word-prop [ " (" % % ")" % ] when* - ] "" make ; - -: word-url ( word -- url ) - [ - "http://factor.modalwebserver.co.nz/responder/browser/?vocab=" % - dup word-vocabulary url-encode % - "&word=" % - word-name url-encode % - ] "" make ; - : irc-loop ( -- ) - irc-stream get stream-readln - [ dup print flush parse-irc irc-loop ] when* ; + [ + irc-stream get stream-readln + [ dup print flush parse-irc irc-loop ] when* + ] [ + irc-stream get stream-close + ] cleanup ; : factorbot "irc.freenode.net" connect @@ -89,6 +76,11 @@ M: ping handle-irc ( line -- ) "#concatenative" join irc-loop ; +: factorbot-loop [ factorbot ] try factorbot-loop ; + +: multiline-respond ( string -- ) + lines [ respond ] each ; + IN: factorbot-commands : see ( text -- ) @@ -98,9 +90,13 @@ IN: factorbot-commands "Sorry, I couldn't find anything for " swap append respond ] [ nip [ - dup word-string " -- " rot word-url append3 respond + dup synopsis " -- http://factorcode.org" + rot browser-link-href append3 respond ] each ] if ; : quit ( text -- ) drop speaker get "slava" = [ disconnect ] when ; + +: memory ( text -- ) + drop [ room. ] string-out multiline-respond ; diff --git a/library/opengl/opengl-utils.factor b/library/opengl/opengl-utils.factor index 1321e303f6..e86e61031c 100644 --- a/library/opengl/opengl-utils.factor +++ b/library/opengl/opengl-utils.factor @@ -1,7 +1,8 @@ ! Copyright (C) 2005, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. IN: opengl -USING: alien errors kernel math namespaces opengl sdl sequences ; +USING: alien errors io kernel math namespaces opengl sdl +sequences ; : gl-color ( { r g b a } -- ) first4 glColor4d ; inline @@ -36,7 +37,8 @@ USING: alien errors kernel math namespaces opengl sdl sequences ; >r 0 gl-flags r> with-screen ; inline : gl-error ( -- ) - glGetError dup zero? [ drop ] [ gluErrorString throw ] if ; + glGetError dup zero? + [ drop ] [ "GL error: " write gluErrorString print ] if ; : with-gl-surface ( quot -- ) #! Execute a quotation, locking the current surface if it diff --git a/library/ui/events.factor b/library/ui/events.factor index ae93fe1e0b..fdaff6c80c 100644 --- a/library/ui/events.factor +++ b/library/ui/events.factor @@ -47,4 +47,6 @@ M: quit-event handle-event ( event -- ) M: resize-event handle-event ( event -- ) flush-fonts gl-resize - width get height get 0 3array world get set-gadget-dim ; + world get remove-notify + width get height get 0 3array world get set-gadget-dim + world get add-notify ; diff --git a/library/ui/gadgets.factor b/library/ui/gadgets.factor index b2304476de..ab4942abfb 100644 --- a/library/ui/gadgets.factor +++ b/library/ui/gadgets.factor @@ -90,9 +90,14 @@ M: gadget children-on ( rect/point gadget -- list ) : max-dim ( dims -- dim ) { 0 0 0 } [ vmax ] reduce ; +: each-child ( gadget quot -- ) + >r gadget-children r> each ; inline + +: each-child-with ( obj gadget quot -- ) + >r gadget-children r> each-with ; inline + : set-gadget-delegate ( delegate gadget -- ) - dup pick gadget-children [ set-gadget-parent ] each-with - set-delegate ; + dup pick [ set-gadget-parent ] each-child-with set-delegate ; ! Pointer help protocol GENERIC: gadget-help diff --git a/library/ui/hierarchy.factor b/library/ui/hierarchy.factor index e20acd9371..4a9caeb2ae 100644 --- a/library/ui/hierarchy.factor +++ b/library/ui/hierarchy.factor @@ -4,20 +4,36 @@ IN: gadgets USING: gadgets-layouts generic hashtables kernel lists math namespaces sequences vectors ; -: remove-gadget ( gadget parent -- ) - f pick set-gadget-parent - [ gadget-children delete ] keep - relayout ; +GENERIC: add-notify* ( gadget -- ) + +M: gadget add-notify* drop ; + +: add-notify ( gadget -- ) + dup [ add-notify ] each-child add-notify* ; + +GENERIC: remove-notify* ( gadget -- ) + +M: gadget remove-notify* drop ; + +: remove-notify ( gadget -- ) + dup [ remove-notify* ] each-child remove-notify* ; + +: (unparent) ( gadget -- ) + dup remove-notify + dup forget-pref-dim f swap set-gadget-parent ; : unparent ( gadget -- ) [ - dup forget-pref-dim - dup gadget-parent dup - [ 2dup remove-gadget ] when 2drop + dup gadget-parent dup [ + over (unparent) + [ gadget-children delete ] keep relayout + ] [ + 2drop + ] if ] when* ; : (clear-gadget) ( gadget -- ) - dup gadget-children [ f swap set-gadget-parent ] each + dup gadget-children [ (unparent) ] each f swap set-gadget-children ; : clear-gadget ( gadget -- ) @@ -26,7 +42,8 @@ namespaces sequences vectors ; : (add-gadget) ( gadget box -- ) over unparent dup pick set-gadget-parent - [ gadget-children ?push ] keep set-gadget-children ; + [ gadget-children ?push ] 2keep swapd set-gadget-children + add-notify ; : add-gadget ( gadget parent -- ) #! Add a gadget to a parent gadget. diff --git a/library/ui/layouts.factor b/library/ui/layouts.factor index 0b029d76e9..b18ad90a7c 100644 --- a/library/ui/layouts.factor +++ b/library/ui/layouts.factor @@ -59,7 +59,7 @@ M: gadget layout* drop ; DEFER: layout -: layout-children ( gadget -- ) gadget-children [ layout ] each ; +: layout-children ( gadget -- ) [ layout ] each-child ; : layout ( gadget -- ) #! Position the children of the gadget inside the gadget. diff --git a/library/ui/paragraphs.factor b/library/ui/paragraphs.factor index 90eaf9dc8c..5079b249e2 100644 --- a/library/ui/paragraphs.factor +++ b/library/ui/paragraphs.factor @@ -46,7 +46,7 @@ SYMBOL: margin : do-wrap ( paragraph quot -- dim | quot: pos child -- ) [ swap dup init-wrap - gadget-children [ wrap-step ] each-with wrap-dim + [ wrap-step ] each-child-with wrap-dim ] with-scope ; inline M: paragraph pref-dim* ( paragraph -- dim )