blinking cursor in UI

cvs
Slava Pestov 2005-08-23 22:16:42 +00:00
parent 0785188ba5
commit a21a630087
9 changed files with 77 additions and 15 deletions

View File

@ -1,5 +1,4 @@
- reader syntax for arrays, byte arrays, displaced aliens - reader syntax for arrays, byte arrays, displaced aliens
- sleep word
- fix infer hang - fix infer hang
+ ui: + ui:

View File

@ -31,7 +31,7 @@ namespaces parser sequences strings vectors words ;
: check-shape ( word slots -- ) : check-shape ( word slots -- )
#! If the new list of slots is different from the previous, #! If the new list of slots is different from the previous,
#! forget the old definition. #! forget the old definition.
>r "use" get search dup [ >r "in" get lookup dup [
dup "tuple-size" word-prop r> length 2 + = dup "tuple-size" word-prop r> length 2 + =
[ drop ] [ forget-tuple ] ifte [ drop ] [ forget-tuple ] ifte
] [ ] [

View File

@ -94,3 +94,11 @@ TUPLE: delegate-clone ;
! This must be the last test in the file! ! This must be the last test in the file!
[ "<constructor-test>" ] [ "<constructor-test>" ]
[ "TUPLE: constructor-test ; C: constructor-test ;" eval word word-name ] unit-test [ "TUPLE: constructor-test ; C: constructor-test ;" eval word word-name ] unit-test
! There was a typo in check-shape; it would unintern the wrong
! words!
[ "temporary-1" ]
[
"IN: temporary-1 SYMBOL: foobar IN: temporary TUPLE: foobar ;" eval
"foobar" [ "temporary-1" "temporary" ] search word-vocabulary
] unit-test

View File

@ -1,8 +1,28 @@
! Copyright (C) 2005 Slava Pestov. ! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license. ! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets IN: gadgets
USING: generic kernel line-editor math matrices namespaces USING: generic kernel math matrices namespaces sdl sequences
sdl sequences strings styles vectors ; strings styles vectors ;
! A blinking caret
TUPLE: caret ;
C: caret ( -- caret )
<plain-gadget> over set-delegate
dup red background set-paint-prop
500 over set-gadget-framerate ;
: toggle-visible ( gadget -- )
dup gadget-visible? not over set-gadget-visible?
relayout ;
M: caret tick* ( ms caret -- ) nip toggle-visible ;
: add-caret ( caret parent -- ) dupd add-gadget add-timer ;
: unparent-caret ( caret -- ) dup remove-timer unparent ;
USE: line-editor
! An editor gadget wraps a line editor object and passes ! An editor gadget wraps a line editor object and passes
! gestures to the line editor. ! gestures to the line editor.
@ -22,10 +42,10 @@ TUPLE: editor line caret ;
[ set-line-text ] with-editor ; [ set-line-text ] with-editor ;
: focus-editor ( editor -- ) : focus-editor ( editor -- )
dup editor-caret swap add-gadget ; dup editor-caret swap add-caret ;
: unfocus-editor ( editor -- ) : unfocus-editor ( editor -- )
editor-caret unparent ; editor-caret unparent-caret ;
: run-char-widths ( font str -- wlist ) : run-char-widths ( font str -- wlist )
#! List of x co-ordinates of each character. #! List of x co-ordinates of each character.
@ -58,9 +78,6 @@ TUPLE: editor line caret ;
[[ [ "END" ] [ [ end ] with-editor ] ]] [[ [ "END" ] [ [ end ] with-editor ] ]]
] swap add-actions ; ] swap add-actions ;
: <caret> ( -- caret )
<plain-gadget> dup red background set-paint-prop ;
C: editor ( text -- ) C: editor ( text -- )
<gadget> over set-delegate <gadget> over set-delegate
[ <line-editor> swap set-editor-line ] keep [ <line-editor> swap set-editor-line ] keep

View File

@ -33,7 +33,7 @@ M: rectangle inside? ( loc rect -- ? )
! A gadget is a rectangle, a paint, a mapping of gestures to ! A gadget is a rectangle, a paint, a mapping of gestures to
! actions, and a reference to the gadget's parent. ! actions, and a reference to the gadget's parent.
TUPLE: gadget TUPLE: gadget
paint gestures visible? relayout? root? paint gestures framerate visible? relayout? root?
parent children ; parent children ;
: gadget-child gadget-children first ; : gadget-child gadget-children first ;

View File

@ -6,8 +6,7 @@ sequences vectors ;
: remove-gadget ( gadget parent -- ) : remove-gadget ( gadget parent -- )
[ 2dup gadget-children remq swap set-gadget-children ] keep [ 2dup gadget-children remq swap set-gadget-children ] keep
relayout relayout f swap set-gadget-parent ;
f swap set-gadget-parent ;
: unparent ( gadget -- ) : unparent ( gadget -- )
[ [

View File

@ -10,6 +10,7 @@ USING: kernel parser sequences io ;
"/library/ui/borders.factor" "/library/ui/borders.factor"
"/library/ui/frames.factor" "/library/ui/frames.factor"
"/library/ui/world.factor" "/library/ui/world.factor"
"/library/ui/timer.factor"
"/library/ui/hand.factor" "/library/ui/hand.factor"
"/library/ui/labels.factor" "/library/ui/labels.factor"
"/library/ui/buttons.factor" "/library/ui/buttons.factor"

36
library/ui/timer.factor Normal file
View File

@ -0,0 +1,36 @@
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: gadgets
USING: hashtables kernel math namespaces sequences ;
TUPLE: timer gadget last ;
C: timer ( gadget -- timer )
[ set-timer-gadget ] keep
millis over set-timer-last ;
GENERIC: tick* ( ms gadget -- )
: next-time ( timer -- ms )
dup timer-gadget gadget-framerate swap timer-last + ;
: advance-timer ( ms timer -- delay )
#! Outputs the time since the last firing.
[ timer-last - 0 max ] 2keep set-timer-last ;
: do-timer ( ms timer -- )
#! Takes current time, and a timer. If the timer is set to
#! fire, calls its callback.
dup next-time pick <=
[ [ advance-timer ] keep timer-gadget tick* ] [ 2drop ] ifte ;
: timers ( -- hash ) world get world-timers ;
: add-timer ( gadget -- ) [ <timer> ] keep timers set-hash ;
: remove-timer ( gadget -- ) timers remove-hash ;
: do-timers ( -- )
millis timers hash-values [ do-timer ] each-with ;
M: gadget tick* ( ms gadget -- ) 2drop ;

View File

@ -9,15 +9,17 @@ vectors ;
! gadgets are contained in. The current world is stored in the ! gadgets are contained in. The current world is stored in the
! world variable. The invalid slot is a list of gadgets that ! world variable. The invalid slot is a list of gadgets that
! need to be layout. ! need to be layout.
TUPLE: world running? hand glass invalid ; TUPLE: world running? hand glass invalid timers ;
DEFER: <hand> DEFER: <hand>
DEFER: update-hand DEFER: update-hand
DEFER: do-timers
C: world ( -- world ) C: world ( -- world )
f <stack> over set-delegate f <stack> over set-delegate
t over set-gadget-root? t over set-gadget-root?
dup <hand> over set-world-hand ; dup <hand> over set-world-hand
<namespace> over set-world-timers ;
: add-invalid ( gadget -- ) : add-invalid ( gadget -- )
world get [ world-invalid cons ] keep set-world-invalid ; world get [ world-invalid cons ] keep set-world-invalid ;
@ -65,7 +67,7 @@ DEFER: handle-event
next-event [ next-event [
handle-event run-world handle-event run-world
] [ ] [
drop world-step drop world-step do-timers
world get world-running? [ 10 sleep run-world ] when world get world-running? [ 10 sleep run-world ] when
] ifte ; ] ifte ;