2005-02-03 22:21:51 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-08-31 21:06:13 -04:00
|
|
|
IN: gadgets-buttons
|
2005-09-28 23:29:00 -04:00
|
|
|
USING: gadgets gadgets-borders gadgets-layouts gadgets-theme
|
|
|
|
generic io kernel lists math namespaces sdl sequences sequences
|
|
|
|
styles threads ;
|
2005-02-03 22:21:51 -05:00
|
|
|
|
2005-10-07 20:26:21 -04:00
|
|
|
: button-down? ( n -- ? ) hand get hand-buttons member? ;
|
2005-02-19 23:25:21 -05:00
|
|
|
|
2005-10-07 20:26:21 -04:00
|
|
|
: mouse-over? ( gadget -- ? ) hand get hand-gadget child? ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
|
|
|
: button-pressed? ( button -- ? )
|
2005-02-19 22:55:45 -05:00
|
|
|
#! Return true if the mouse was clicked on the button, and
|
|
|
|
#! is currently over the button.
|
2005-09-27 00:24:42 -04:00
|
|
|
dup mouse-over? 1 button-down? and
|
2005-10-07 20:26:21 -04:00
|
|
|
[ hand get hand-clicked child? ] [ drop f ] if ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
|
|
|
: button-update ( button -- )
|
2005-06-27 16:50:21 -04:00
|
|
|
dup dup mouse-over? rollover set-paint-prop
|
2005-03-06 19:46:29 -05:00
|
|
|
dup dup button-pressed? reverse-video set-paint-prop
|
2005-09-25 20:41:49 -04:00
|
|
|
relayout-1 ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
|
|
|
: button-clicked ( button -- )
|
|
|
|
#! If the mouse is released while still inside the button,
|
|
|
|
#! fire an action gesture.
|
2005-08-27 15:12:37 -04:00
|
|
|
dup button-update dup mouse-over?
|
|
|
|
[ [ action ] swap handle-gesture ] when drop ;
|
2005-02-05 22:51:41 -05:00
|
|
|
|
2005-05-03 19:00:52 -04:00
|
|
|
: button-action ( action -- quot )
|
2005-09-24 15:21:17 -04:00
|
|
|
[ [ swap handle-gesture drop ] cons ] [ [ drop ] ] if* ;
|
2005-05-03 19:00:52 -04:00
|
|
|
|
|
|
|
: button-gestures ( button quot -- )
|
2005-02-05 22:51:41 -05:00
|
|
|
dupd [ action ] set-action
|
2005-08-27 15:12:37 -04:00
|
|
|
dup [ button-clicked ] [ button-up 1 ] set-action
|
2005-02-05 22:51:41 -05:00
|
|
|
dup [ button-update ] [ button-down 1 ] set-action
|
|
|
|
dup [ button-update ] [ mouse-leave ] set-action
|
2005-08-27 15:12:37 -04:00
|
|
|
[ button-update ] [ mouse-enter ] set-action ;
|
2005-07-17 02:49:07 -04:00
|
|
|
|
2005-08-27 15:12:37 -04:00
|
|
|
TUPLE: button ;
|
|
|
|
|
|
|
|
C: button ( gadget quot -- button )
|
2005-10-09 21:27:14 -04:00
|
|
|
rot bevel-border over set-gadget-delegate
|
|
|
|
[ swap button-gestures ] keep ;
|
2005-08-26 00:55:56 -04:00
|
|
|
|
2005-08-26 21:42:43 -04:00
|
|
|
: <roll-button> ( gadget quot -- button )
|
2005-09-27 00:44:38 -04:00
|
|
|
>r dup roll-button-theme dup r> button-gestures ;
|
|
|
|
|
|
|
|
: <highlight-button> ( gadget quot -- button )
|
|
|
|
dupd button-gestures ;
|
2005-08-27 15:12:37 -04:00
|
|
|
|
|
|
|
: repeat-button-down ( button -- )
|
|
|
|
dup 100 add-timer button-clicked ;
|
|
|
|
|
|
|
|
: repeat-button-up ( button -- )
|
|
|
|
dup button-update remove-timer ;
|
|
|
|
|
|
|
|
: repeat-actions ( button -- )
|
|
|
|
dup [ repeat-button-down ] [ button-down 1 ] set-action
|
|
|
|
[ repeat-button-up ] [ button-up 1 ] set-action ;
|
|
|
|
|
|
|
|
: <repeat-button> ( gadget quot -- button )
|
|
|
|
#! Button that calls the quotation every 100ms as long as
|
|
|
|
#! the mouse is held down.
|
|
|
|
<button> dup repeat-actions ;
|
|
|
|
|
|
|
|
M: button tick ( ms object -- ) nip button-clicked ;
|