2006-08-24 18:23:48 -04:00
|
|
|
! Copyright (C) 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-08-24 23:19:22 -04:00
|
|
|
USING: arrays definitions kernel gadgets sequences strings math
|
|
|
|
words generic namespaces hashtables help ;
|
2006-08-25 20:52:13 -04:00
|
|
|
IN: gadgets
|
|
|
|
|
|
|
|
TUPLE: command group name gesture quot ;
|
2006-08-24 18:23:48 -04:00
|
|
|
|
2006-08-25 20:52:13 -04:00
|
|
|
M: command equal? eq? ;
|
2006-08-24 18:23:48 -04:00
|
|
|
|
2006-08-30 03:50:02 -04:00
|
|
|
GENERIC: invoke-command ( target command -- )
|
|
|
|
|
|
|
|
M: command invoke-command ( target command -- )
|
|
|
|
command-quot call ;
|
|
|
|
|
2006-08-24 18:23:48 -04:00
|
|
|
GENERIC: gesture>string ( gesture -- string )
|
|
|
|
|
2006-08-27 23:22:30 -04:00
|
|
|
: modifiers>string ( modifiers -- string )
|
|
|
|
[ word-name ] map concat >string ;
|
|
|
|
|
2006-08-24 18:23:48 -04:00
|
|
|
M: key-down gesture>string
|
2006-08-27 23:22:30 -04:00
|
|
|
dup key-down-mods modifiers>string
|
2006-08-24 18:23:48 -04:00
|
|
|
swap key-down-sym append ;
|
|
|
|
|
|
|
|
M: button-up gesture>string
|
2006-08-27 23:22:30 -04:00
|
|
|
[
|
|
|
|
dup button-up-mods modifiers>string %
|
|
|
|
"Mouse Up" %
|
|
|
|
button-up-# [ " " % # ] when*
|
|
|
|
] "" make ;
|
2006-08-24 18:23:48 -04:00
|
|
|
|
2006-08-30 03:50:02 -04:00
|
|
|
M: button-down gesture>string
|
2006-08-27 23:22:30 -04:00
|
|
|
[
|
|
|
|
dup button-down-mods modifiers>string %
|
2006-08-28 00:53:55 -04:00
|
|
|
"Mouse Up" %
|
|
|
|
button-down-# [ " " % # ] when*
|
|
|
|
] "" make ;
|
2006-08-24 18:23:48 -04:00
|
|
|
|
|
|
|
M: object gesture>string drop f ;
|
|
|
|
|
2006-08-30 03:50:02 -04:00
|
|
|
: command-gestures ( commands -- hash )
|
2006-08-24 18:23:48 -04:00
|
|
|
[ command-gesture ] subset
|
2006-08-30 03:50:02 -04:00
|
|
|
[ dup command-gesture swap [ invoke-command ] curry ]
|
|
|
|
map>hash ;
|
|
|
|
|
|
|
|
: define-commands* ( class specs -- )
|
|
|
|
2dup "commands" set-word-prop
|
|
|
|
command-gestures "gestures" set-word-prop ;
|
|
|
|
|
|
|
|
: <commands> ( specs -- commands )
|
|
|
|
#! Specs is an array of { group name gesture quot }
|
|
|
|
[ first4 <command> ] map ;
|
|
|
|
|
|
|
|
: define-commands ( class specs -- )
|
|
|
|
<commands> define-commands* ;
|
2006-08-24 18:23:48 -04:00
|
|
|
|
|
|
|
: commands ( gadget -- seq )
|
2006-08-24 22:44:42 -04:00
|
|
|
delegates [ class "commands" word-prop ] map concat ;
|
2006-08-24 19:15:50 -04:00
|
|
|
|
2006-08-26 03:04:02 -04:00
|
|
|
: all-commands ( gadget -- assoc )
|
|
|
|
[
|
|
|
|
parents [
|
|
|
|
dup commands [ set ] each-with
|
|
|
|
] each
|
|
|
|
] make-hash
|
|
|
|
hash>alist [ [ first command-name ] 2apply <=> ] sort ;
|
2006-08-25 20:52:13 -04:00
|
|
|
|
2006-08-27 23:22:30 -04:00
|
|
|
: resend-button-down ( gesture world -- )
|
|
|
|
hand-loc get-global swap send-button-down ;
|
|
|
|
|
|
|
|
: resend-button-up ( gesture world -- )
|
|
|
|
hand-loc get-global swap send-button-up ;
|
|
|
|
|
|
|
|
world H{
|
|
|
|
{ T{ key-down f { C+ } "x" } [ T{ cut-action } send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "c" } [ T{ copy-action } send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "v" } [ T{ paste-action } send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "a" } [ T{ select-all-action } send-action ] }
|
|
|
|
{ T{ button-down f { C+ } 1 } [ T{ button-down f f 3 } swap resend-button-down ] }
|
|
|
|
{ T{ button-down f { A+ } 1 } [ T{ button-down f f 2 } swap resend-button-down ] }
|
|
|
|
{ T{ button-up f { C+ } 1 } [ T{ button-up f f 3 } swap resend-button-up ] }
|
|
|
|
{ T{ button-up f { A+ } 1 } [ T{ button-up f f 2 } swap resend-button-up ] }
|
|
|
|
} set-gestures
|
2006-08-24 22:44:42 -04:00
|
|
|
|
2006-08-30 03:50:02 -04:00
|
|
|
SYMBOL: +name+
|
|
|
|
SYMBOL: +button+
|
|
|
|
SYMBOL: +group+
|
|
|
|
SYMBOL: +quot+
|
2006-09-01 01:10:30 -04:00
|
|
|
SYMBOL: +listener+
|
2006-08-30 03:50:02 -04:00
|
|
|
SYMBOL: +gesture+
|
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
TUPLE: operation predicate tags gesture listener? ;
|
2006-08-31 21:59:57 -04:00
|
|
|
|
|
|
|
: (operation) ( -- command )
|
|
|
|
f +name+ get +gesture+ get +quot+ get <command> ;
|
|
|
|
|
|
|
|
: (tags) ( -- seq ) +button+ get +group+ get 2array ;
|
2006-08-30 03:50:02 -04:00
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
C: operation ( predicate hash -- operation )
|
2006-08-30 03:50:02 -04:00
|
|
|
swap [
|
2006-08-31 21:59:57 -04:00
|
|
|
(operation) over set-delegate
|
|
|
|
(tags) over set-operation-tags
|
2006-09-01 01:10:30 -04:00
|
|
|
+listener+ get over set-operation-listener?
|
2006-08-30 03:50:02 -04:00
|
|
|
] bind
|
2006-09-01 01:20:38 -04:00
|
|
|
[ set-operation-predicate ] keep ;
|
2006-08-30 03:50:02 -04:00
|
|
|
|
2006-08-24 22:44:42 -04:00
|
|
|
SYMBOL: operations
|
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
: object-operations ( obj -- operations )
|
|
|
|
operations get [ operation-predicate call ] subset-with ;
|
|
|
|
|
2006-08-30 03:50:02 -04:00
|
|
|
: class-operations ( class -- operations )
|
2006-09-01 01:20:38 -04:00
|
|
|
"predicate" word-prop
|
|
|
|
operations get [ operation-predicate = ] subset-with ;
|
2006-08-30 03:50:02 -04:00
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
: tagged-operations ( obj tag -- commands )
|
|
|
|
swap object-operations
|
2006-08-30 03:50:02 -04:00
|
|
|
[ operation-tags member? ] subset-with ;
|
2006-08-24 22:44:42 -04:00
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
: mouse-operation ( obj button# -- command )
|
2006-08-30 03:50:02 -04:00
|
|
|
tagged-operations dup empty? [ drop f ] [ peek ] if ;
|
2006-08-28 15:54:40 -04:00
|
|
|
|
2006-09-01 01:20:38 -04:00
|
|
|
: mouse-operations ( obj -- seq )
|
2006-08-30 03:50:02 -04:00
|
|
|
3 [ 1+ mouse-operation ] map-with ;
|