factor/library/ui/hand.factor

96 lines
2.8 KiB
Factor
Raw Normal View History

2005-02-01 20:14:03 -05:00
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: alien generic io kernel lists math matrices namespaces
prettyprint sdl sequences vectors ;
2005-02-01 20:14:03 -05:00
DEFER: pick-up
2005-07-13 18:08:54 -04:00
: (pick-up) ( point list -- gadget )
dup [
2dup car pick-up dup
2005-07-13 18:08:54 -04:00
[ 2nip ] [ drop cdr (pick-up) ] ifte
] [
2drop f
] ifte ;
2005-07-13 18:08:54 -04:00
: pick-up ( point gadget -- gadget )
#! The logic is thus. If the point is definately outside the
#! box, return f. Otherwise, see if the point is contained
#! in any subgadget. If not, see if it is contained in the
#! box delegate.
2dup inside? [
2005-07-13 18:08:54 -04:00
[
[ translate ] keep
gadget-children reverse (pick-up) dup
] keep ?
] [
2drop f
] ifte ;
2005-02-01 20:14:03 -05:00
! 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
2005-02-10 23:58:28 -05:00
! its contents. Some comments on the slots:
! - hand-gadget is the gadget under the mouse position
! - hand-clicked is the most recently clicked gadget
! - hand-focus is the gadget holding keyboard focus
2005-07-13 18:08:54 -04:00
TUPLE: hand click-loc click-rel clicked buttons gadget focus ;
2005-02-01 20:14:03 -05:00
2005-02-01 21:47:10 -05:00
C: hand ( world -- hand )
2005-07-13 21:03:34 -04:00
<gadget> over set-delegate
2005-02-05 11:52:24 -05:00
[ set-gadget-parent ] 2keep
2005-03-06 19:46:29 -05:00
[ set-hand-gadget ] keep ;
2005-02-01 20:14:03 -05:00
: hand world get world-hand ;
2005-02-01 20:14:03 -05:00
: button/ ( n hand -- )
2005-02-05 11:52:24 -05:00
dup hand-gadget over set-hand-clicked
dup screen-loc over set-hand-click-loc
2005-03-06 19:46:29 -05:00
dup hand-gadget over relative over set-hand-click-rel
2005-02-01 20:14:03 -05:00
[ hand-buttons unique ] keep set-hand-buttons ;
: button\ ( n hand -- )
[ hand-buttons remove ] keep set-hand-buttons ;
2005-02-05 11:52:24 -05:00
: fire-leave ( hand gadget -- )
[ swap shape-loc swap screen-loc v- ] keep mouse-leave ;
2005-02-05 11:52:24 -05:00
: fire-enter ( oldpos hand -- )
hand-gadget [ screen-loc v- ] keep mouse-enter ;
2005-02-05 11:52:24 -05:00
2005-03-03 20:43:55 -05:00
: update-hand-gadget ( hand -- )
2005-07-13 18:08:54 -04:00
[ world get pick-up ] keep set-hand-gadget ;
2005-03-03 20:43:55 -05:00
: motion-gesture ( hand gadget gesture -- )
#! Send a gesture like [ drag 2 ].
rot hand-buttons car add swap handle-gesture drop ;
: fire-motion ( hand -- )
2005-02-26 02:11:25 -05:00
#! Fire a motion gesture to the gadget underneath the hand,
#! and if a mouse button is down, fire a drag gesture to the
#! gadget that was clicked.
[ motion ] over hand-gadget handle-gesture drop
dup hand-buttons
[ dup hand-clicked [ drag ] motion-gesture ] [ drop ] ifte ;
2005-02-05 11:52:24 -05:00
: move-hand ( loc hand -- )
dup shape-loc >r
[ set-shape-loc ] keep
dup hand-gadget >r
2005-02-05 11:52:24 -05:00
dup update-hand-gadget
dup r> fire-leave
dup fire-motion
2005-02-05 11:52:24 -05:00
r> swap fire-enter ;
2005-02-10 23:58:28 -05:00
2005-02-15 18:05:28 -05:00
: update-hand ( hand -- )
#! Called when a gadget is removed or added.
dup shape-loc swap move-hand ;
2005-02-15 18:05:28 -05:00
: request-focus ( gadget -- )
focusable-child
hand hand-focus
2005-02-10 23:58:28 -05:00
2dup lose-focus
swap dup hand set-hand-focus
2005-02-10 23:58:28 -05:00
gain-focus ;