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
|
2005-07-12 20:30:05 -04:00
|
|
|
USING: alien generic io kernel lists math matrices namespaces
|
|
|
|
prettyprint sdl sequences vectors ;
|
2005-02-01 20:14:03 -05:00
|
|
|
|
2005-07-16 22:16:18 -04:00
|
|
|
: (pick-up) ( point gadget -- gadget )
|
2005-07-16 23:01:51 -04:00
|
|
|
gadget-children reversed [ inside? ] find-with nip ;
|
2005-02-03 18:18:47 -05:00
|
|
|
|
2005-07-13 18:08:54 -04:00
|
|
|
: pick-up ( point gadget -- gadget )
|
2005-02-03 18:18:47 -05:00
|
|
|
#! 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-16 23:01:51 -04:00
|
|
|
[ translate ] keep 2dup
|
|
|
|
(pick-up) [ pick-up ] [ nip ] ?ifte
|
2005-02-03 18:18:47 -05:00
|
|
|
] [
|
|
|
|
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
|
|
|
|
2005-07-13 22:51:43 -04: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
|
2005-06-22 02:32:17 -04:00
|
|
|
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
|
|
|
|
2005-02-05 22:51:41 -05:00
|
|
|
: fire-leave ( hand gadget -- )
|
2005-07-12 20:30:05 -04:00
|
|
|
[ swap shape-loc swap screen-loc v- ] keep mouse-leave ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
|
|
|
: fire-enter ( oldpos hand -- )
|
2005-07-12 20:30:05 -04:00
|
|
|
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 ].
|
2005-06-23 15:53:54 -04:00
|
|
|
rot hand-buttons car add swap handle-gesture drop ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
|
|
|
: 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
|
2005-07-12 20:30:05 -04:00
|
|
|
dup hand-buttons
|
|
|
|
[ dup hand-clicked [ drag ] motion-gesture ] [ drop ] ifte ;
|
2005-02-05 11:52:24 -05:00
|
|
|
|
2005-07-12 20:30:05 -04:00
|
|
|
: move-hand ( loc hand -- )
|
|
|
|
dup shape-loc >r
|
|
|
|
[ set-shape-loc ] keep
|
2005-02-05 22:51:41 -05:00
|
|
|
dup hand-gadget >r
|
2005-02-05 11:52:24 -05:00
|
|
|
dup update-hand-gadget
|
2005-02-05 22:51:41 -05:00
|
|
|
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.
|
2005-07-12 20:30:05 -04:00
|
|
|
dup shape-loc swap move-hand ;
|
2005-02-15 18:05:28 -05:00
|
|
|
|
2005-07-04 18:36:07 -04:00
|
|
|
: request-focus ( gadget -- )
|
|
|
|
focusable-child
|
|
|
|
hand hand-focus
|
2005-02-10 23:58:28 -05:00
|
|
|
2dup lose-focus
|
2005-07-04 18:36:07 -04:00
|
|
|
swap dup hand set-hand-focus
|
2005-02-10 23:58:28 -05:00
|
|
|
gain-focus ;
|