factor/basis/ui/gadgets/buttons/buttons.factor

230 lines
5.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays kernel math models namespaces sequences
2008-07-13 17:27:11 -04:00
strings quotations assocs combinators classes colors
classes.tuple opengl math.vectors
ui.commands ui.gadgets ui.gadgets.borders
ui.gadgets.labels ui.gadgets.theme
ui.gadgets.tracks ui.gadgets.packs ui.gadgets.worlds ui.gestures
ui.render math.geometry.rect ;
2007-09-20 18:09:08 -04:00
IN: ui.gadgets.buttons
2008-07-11 01:46:15 -04:00
TUPLE: button < border pressed? selected? quot ;
2007-09-20 18:09:08 -04:00
: buttons-down? ( -- ? )
hand-buttons get-global empty? not ;
: button-rollover? ( button -- ? )
hand-gadget get-global child? ;
: mouse-clicked? ( gadget -- ? )
hand-clicked get-global child? ;
: button-update ( button -- )
dup mouse-clicked?
over button-rollover? and
buttons-down? and
over set-button-pressed?
relayout-1 ;
: if-clicked ( button quot -- )
>r dup button-update dup button-rollover? r> [ drop ] if ;
: button-clicked ( button -- )
dup button-quot if-clicked ;
button H{
{ T{ button-up } [ button-clicked ] }
{ T{ button-down } [ button-update ] }
{ T{ mouse-leave } [ button-update ] }
{ T{ mouse-enter } [ button-update ] }
} set-gestures
: new-button ( label quot class -- button )
2008-07-11 01:46:15 -04:00
[ swap >label ] dip new-border swap >>quot ; inline
2008-07-11 01:46:15 -04:00
: <button> ( label quot -- button )
button new-button ;
2007-09-20 18:09:08 -04:00
TUPLE: button-paint plain rollover pressed selected ;
C: <button-paint> button-paint
2008-06-08 16:32:55 -04:00
: find-button ( gadget -- button )
[ [ button? ] is? ] find-parent ;
2007-10-31 01:04:54 -04:00
2007-09-20 18:09:08 -04:00
: button-paint ( button paint -- button paint )
2007-10-31 01:04:54 -04:00
over find-button {
2008-06-18 23:30:54 -04:00
{ [ dup pressed?>> ] [ drop pressed>> ] }
{ [ dup selected?>> ] [ drop selected>> ] }
{ [ dup button-rollover? ] [ drop rollover>> ] }
[ drop plain>> ]
2007-09-20 18:09:08 -04:00
} cond ;
M: button-paint draw-interior
button-paint draw-interior ;
M: button-paint draw-boundary
button-paint draw-boundary ;
2008-06-18 23:30:54 -04:00
: roll-button-theme ( button -- button )
f black <solid> dup f <button-paint> >>boundary
{ 0 1/2 } >>align ; inline
2007-09-20 18:09:08 -04:00
: <roll-button> ( label quot -- button )
<button> roll-button-theme ;
2007-09-20 18:09:08 -04:00
2008-06-18 23:30:54 -04:00
: <bevel-button-paint> ( -- paint )
2007-09-20 18:09:08 -04:00
plain-gradient
rollover-gradient
pressed-gradient
selected-gradient
2008-06-18 23:30:54 -04:00
<button-paint> ;
: bevel-button-theme ( gadget -- gadget )
<bevel-button-paint> >>interior
2008-07-11 01:46:15 -04:00
{ 5 5 } >>size
2008-06-18 23:30:54 -04:00
faint-boundary ; inline
2007-09-20 18:09:08 -04:00
: <bevel-button> ( label quot -- button )
2008-07-11 01:46:15 -04:00
<button> bevel-button-theme ;
2007-09-20 18:09:08 -04:00
TUPLE: repeat-button < button ;
2007-09-20 18:09:08 -04:00
repeat-button H{
{ T{ drag } [ button-clicked ] }
} set-gestures
: <repeat-button> ( label quot -- button )
#! Button that calls the quotation every 100ms as long as
#! the mouse is held down.
2008-07-11 01:46:15 -04:00
repeat-button new-button bevel-button-theme ;
2007-09-20 18:09:08 -04:00
TUPLE: checkmark-paint color ;
C: <checkmark-paint> checkmark-paint
M: checkmark-paint draw-interior
checkmark-paint-color set-color
origin get [
rect-dim
{ 0 0 } over gl-line
dup { 0 1 } v* swap { 1 0 } v* gl-line
] with-translation ;
2007-10-31 01:04:54 -04:00
: checkmark-theme ( gadget -- )
f
f
black <solid>
black <checkmark-paint>
<button-paint>
over set-gadget-interior
black <solid>
swap set-gadget-boundary ;
2007-09-20 18:09:08 -04:00
2007-10-31 01:04:54 -04:00
: <checkmark> ( -- gadget )
<gadget>
dup checkmark-theme
{ 14 14 } over (>>dim) ;
2007-10-31 01:04:54 -04:00
: toggle-model ( model -- )
[ not ] change-model ;
: checkbox-theme ( gadget -- gadget )
2008-06-08 16:32:55 -04:00
f >>interior
{ 5 5 } >>gap
1/2 >>align ; inline
2007-10-31 01:04:54 -04:00
TUPLE: checkbox < button ;
2007-11-13 18:51:10 -05:00
2007-10-31 01:04:54 -04:00
: <checkbox> ( model label -- checkbox )
<checkmark> label-on-right checkbox-theme
[ model>> toggle-model ]
checkbox new-button
swap >>model ;
2007-10-31 01:04:54 -04:00
2007-11-13 18:51:10 -05:00
M: checkbox model-changed
swap model-value over set-button-selected? relayout-1 ;
2007-11-13 18:51:10 -05:00
TUPLE: radio-paint color ;
C: <radio-paint> radio-paint
M: radio-paint draw-interior
radio-paint-color set-color
origin get { 4 4 } v+ swap rect-dim { 8 8 } v- 12 gl-fill-circle ;
M: radio-paint draw-boundary
radio-paint-color set-color
origin get { 1 1 } v+ swap rect-dim { 2 2 } v- 12 gl-circle ;
2007-10-31 01:04:54 -04:00
: radio-knob-theme ( gadget -- )
f
f
black <radio-paint>
black <radio-paint>
<button-paint>
over set-gadget-interior
black <radio-paint>
swap set-gadget-boundary ;
: <radio-knob> ( -- gadget )
<gadget>
dup radio-knob-theme
{ 16 16 } over (>>dim) ;
2007-10-31 01:04:54 -04:00
TUPLE: radio-control < button value ;
2007-11-13 18:51:10 -05:00
: <radio-control> ( value model label -- control )
[ [ value>> ] keep set-control-value ]
radio-control new-button
swap >>model
swap >>value ; inline
2007-11-13 18:51:10 -05:00
M: radio-control model-changed
swap model-value
2007-11-13 18:51:10 -05:00
over radio-control-value =
over set-button-selected?
relayout-1 ;
2007-10-31 01:04:54 -04:00
: <radio-controls> ( parent model assoc quot -- parent )
#! quot has stack effect ( value model label -- )
swapd [ swapd call add-gadget ] 2curry assoc-each ; inline
2007-10-31 01:04:54 -04:00
: radio-button-theme ( gadget -- gadget )
2008-06-08 16:32:55 -04:00
{ 5 5 } >>gap
1/2 >>align ; inline
2007-10-31 01:04:54 -04:00
2007-11-14 16:35:17 -05:00
: <radio-button> ( value model label -- gadget )
<radio-knob> label-on-right radio-button-theme <radio-control> ;
2007-10-31 01:04:54 -04:00
: <radio-buttons> ( model assoc -- gadget )
<filled-pile>
-rot
[ <radio-button> ] <radio-controls>
{ 5 5 } >>gap ;
2007-10-31 01:04:54 -04:00
2007-11-14 16:35:17 -05:00
: <toggle-button> ( value model label -- gadget )
2008-07-11 01:46:15 -04:00
<radio-control> bevel-button-theme ;
2007-10-31 01:04:54 -04:00
: <toggle-buttons> ( model assoc -- gadget )
<shelf>
-rot
[ <toggle-button> ] <radio-controls> ;
2007-09-20 18:09:08 -04:00
: command-button-quot ( target command -- quot )
[ invoke-command drop ] 2curry ;
: <command-button> ( target gesture command -- button )
[ command-string ] keep
swapd
command-button-quot
<bevel-button> ;
: <toolbar> ( target -- toolbar )
<shelf>
swap
"toolbar" over class command-map commands>> swap
[ -rot <command-button> add-gadget ] curry assoc-each ;