1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs kernel math models namespaces
4 sequences words strings system hashtables math.parser
5 math.vectors classes.tuple classes ui.gadgets boxes
6 calendar alarms symbols combinators sets ;
9 : set-gestures ( class hash -- ) "gestures" set-word-prop ;
11 GENERIC: handle-gesture* ( gadget gesture delegate -- ? )
13 : default-gesture-handler ( gadget gesture delegate -- ? )
14 class "gestures" word-prop at dup
15 [ call f ] [ 2drop t ] if ;
17 M: object handle-gesture* default-gesture-handler ;
19 : handle-gesture ( gesture gadget -- ? )
20 tuck delegates [ >r 2dup r> handle-gesture* ] all? 2nip ;
22 : send-gesture ( gesture gadget -- ? )
23 [ dupd handle-gesture ] each-parent nip ;
25 : user-input ( str gadget -- )
27 [ [ dupd user-input* ] each-parent ] unless
31 TUPLE: motion ; C: <motion> motion
32 TUPLE: drag # ; C: <drag> drag
33 TUPLE: button-up mods # ; C: <button-up> button-up
34 TUPLE: button-down mods # ; C: <button-down> button-down
35 TUPLE: mouse-scroll ; C: <mouse-scroll> mouse-scroll
36 TUPLE: mouse-enter ; C: <mouse-enter> mouse-enter
37 TUPLE: mouse-leave ; C: <mouse-leave> mouse-leave
38 TUPLE: lose-focus ; C: <lose-focus> lose-focus
39 TUPLE: gain-focus ; C: <gain-focus> gain-focus
41 ! Higher-level actions
42 TUPLE: cut-action ; C: <cut-action> cut-action
43 TUPLE: copy-action ; C: <copy-action> copy-action
44 TUPLE: paste-action ; C: <paste-action> paste-action
45 TUPLE: delete-action ; C: <delete-action> delete-action
46 TUPLE: select-all-action ; C: <select-all-action> select-all-action
48 TUPLE: left-action ; C: <left-action> left-action
49 TUPLE: right-action ; C: <right-action> right-action
50 TUPLE: up-action ; C: <up-action> up-action
51 TUPLE: down-action ; C: <down-action> down-action
53 TUPLE: zoom-in-action ; C: <zoom-in-action> zoom-in-action
54 TUPLE: zoom-out-action ; C: <zoom-out-action> zoom-out-action
56 : generalize-gesture ( gesture -- newgesture )
57 tuple>array 1 head* >tuple ;
60 SYMBOLS: C+ A+ M+ S+ ;
62 TUPLE: key-down mods sym ;
64 : <key-gesture> ( mods sym action? class -- mods' sym' )
65 >r [ S+ rot remove swap ] unless r> boa ; inline
67 : <key-down> ( mods sym action? -- key-down )
68 key-down <key-gesture> ;
70 TUPLE: key-up mods sym ;
72 : <key-up> ( mods sym action? -- key-up )
73 key-up <key-gesture> ;
77 ! Note that these are only really useful inside an event
78 ! handler, and that the locations hand-loc and hand-click-loc
79 ! are in the co-ordinate system of the world which contains
80 ! the gadget in question.
84 { 0 0 } hand-loc set-global
87 SYMBOL: hand-click-loc
89 SYMBOL: hand-last-button
90 SYMBOL: hand-last-time
91 0 hand-last-button set-global
92 0 hand-last-time set-global
95 V{ } clone hand-buttons set-global
97 SYMBOL: scroll-direction
98 { 0 0 } scroll-direction set-global
100 SYMBOL: double-click-timeout
101 300 double-click-timeout set-global
103 : hand-moved? ( -- ? )
104 hand-loc get hand-click-loc get = not ;
106 : button-gesture ( gesture -- )
107 hand-clicked get-global 2dup send-gesture [
108 >r generalize-gesture r> send-gesture drop
113 : drag-gesture ( -- )
114 hand-buttons get-global first <drag> button-gesture ;
118 <box> drag-timer set-global
120 : start-drag-timer ( -- )
121 hand-buttons get-global empty? [
123 300 milliseconds from-now
125 add-alarm drag-timer get-global >box
128 : stop-drag-timer ( -- )
129 hand-buttons get-global empty? [
130 drag-timer get-global ?box
131 [ cancel-alarm ] [ drop ] if
135 hand-buttons get-global empty? [
136 T{ motion } hand-gadget get-global send-gesture drop
141 : each-gesture ( gesture seq -- )
142 [ handle-gesture drop ] with each ;
144 : hand-gestures ( new old -- )
145 drop-prefix <reversed>
146 T{ mouse-leave } swap each-gesture
147 T{ mouse-enter } swap each-gesture ;
149 : forget-rollover ( -- )
150 f hand-world set-global
151 hand-gadget get-global >r
152 f hand-gadget set-global
153 f r> parents hand-gestures ;
155 : send-lose-focus ( gadget -- )
156 T{ lose-focus } swap handle-gesture drop ;
158 : send-gain-focus ( gadget -- )
159 T{ gain-focus } swap handle-gesture drop ;
161 : focus-child ( child gadget ? -- )
167 dupd set-gadget-focus [
174 : modifier ( mod modifiers -- seq )
175 [ second swap bitand 0 > ] with subset
176 0 <column> prune dup empty? [ drop f ] [ >array ] if ;
178 : drag-loc ( -- loc )
179 hand-loc get-global hand-click-loc get-global v- ;
181 : hand-rel ( gadget -- loc )
182 hand-loc get-global swap screen-loc v- ;
184 : hand-click-rel ( gadget -- loc )
185 hand-click-loc get-global swap screen-loc v- ;
187 : multi-click-timeout? ( -- ? )
188 millis hand-last-time get - double-click-timeout get <= ;
190 : multi-click-button? ( button -- button ? )
191 dup hand-last-button get = ;
193 : multi-click-position? ( -- ? )
194 hand-loc get hand-click-loc get v- norm 10 <= ;
196 : multi-click? ( button -- ? )
198 { [ multi-click-timeout? not ] [ f ] }
199 { [ multi-click-button? not ] [ f ] }
200 { [ multi-click-position? not ] [ f ] }
201 { [ multi-click-position? not ] [ f ] }
205 : update-click# ( button -- )
213 millis hand-last-time set
216 : update-clicked ( -- )
217 hand-gadget get-global hand-clicked set-global
218 hand-loc get-global hand-click-loc set-global ;
220 : under-hand ( -- seq )
221 hand-gadget get-global parents <reversed> ;
223 : move-hand ( loc world -- )
224 dup hand-world set-global
225 under-hand >r over hand-loc set-global
226 pick-up hand-gadget set-global
227 under-hand r> hand-gestures ;
229 : send-button-down ( gesture loc world -- )
233 dup update-click# hand-buttons get-global push
237 : send-button-up ( gesture loc world -- )
239 dup button-up-# hand-buttons get-global delete
243 : send-wheel ( direction loc world -- )
245 scroll-direction set-global
246 T{ mouse-scroll } hand-gadget get-global send-gesture
249 : world-focus ( world -- gadget )
250 dup gadget-focus [ world-focus ] [ ] ?if ;
252 : send-action ( world gesture -- )
253 swap world-focus send-gesture drop ;
255 : resend-button-down ( gesture world -- )
256 hand-loc get-global swap send-button-down ;
258 : resend-button-up ( gesture world -- )
259 hand-loc get-global swap send-button-up ;
261 GENERIC: gesture>string ( gesture -- string/f )
263 : modifiers>string ( modifiers -- string )
264 [ word-name ] map concat >string ;
266 M: key-down gesture>string
267 dup key-down-mods modifiers>string
268 swap key-down-sym append ;
270 M: button-up gesture>string
272 dup button-up-mods modifiers>string %
274 button-up-# [ " " % # ] when*
277 M: button-down gesture>string
279 dup button-down-mods modifiers>string %
281 button-down-# [ " " % # ] when*
284 M: left-action gesture>string drop "Swipe left" ;
286 M: right-action gesture>string drop "Swipe right" ;
288 M: up-action gesture>string drop "Swipe up" ;
290 M: down-action gesture>string drop "Swipe down" ;
292 M: zoom-in-action gesture>string drop "Zoom in" ;
294 M: zoom-out-action gesture>string drop "Zoom out (pinch)" ;
296 M: object gesture>string drop f ;