2009-01-27 00:11:45 -05:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-15 05:33:43 -05:00
|
|
|
USING: accessors arrays assocs continuations kernel math models
|
|
|
|
call namespaces opengl sequences io combinators
|
2009-02-10 19:47:34 -05:00
|
|
|
combinators.short-circuit fry math.vectors math.rectangles cache
|
2009-02-15 05:33:43 -05:00
|
|
|
ui.gadgets ui.gestures ui.render ui.text ui.text.private
|
|
|
|
ui.backend ui.gadgets.tracks ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.gadgets.worlds
|
|
|
|
|
2008-07-10 21:32:17 -04:00
|
|
|
TUPLE: world < track
|
2007-09-20 18:09:08 -04:00
|
|
|
active? focused?
|
2009-02-18 21:59:51 -05:00
|
|
|
layers
|
2009-02-19 19:01:26 -05:00
|
|
|
title status status-owner
|
2009-02-10 19:47:34 -05:00
|
|
|
text-handle handle images
|
2008-07-10 21:32:17 -04:00
|
|
|
window-loc ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-01 23:45:40 -04:00
|
|
|
: find-world ( gadget -- world/f ) [ world? ] find-parent ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: show-status ( string/f gadget -- )
|
2009-02-19 19:01:26 -05:00
|
|
|
dup find-world dup [
|
|
|
|
dup status>> [
|
|
|
|
[ (>>status-owner) ] [ status>> set-model ] bi
|
|
|
|
] [ 3drop ] if
|
|
|
|
] [ 3drop ] if ;
|
|
|
|
|
|
|
|
: hide-status ( gadget -- )
|
|
|
|
dup find-world dup [
|
|
|
|
[ status-owner>> eq? ] keep
|
|
|
|
'[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
|
2008-09-03 05:02:18 -04:00
|
|
|
] [ 2drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-09-05 20:29:14 -04:00
|
|
|
ERROR: no-world-found ;
|
|
|
|
|
|
|
|
: find-gl-context ( gadget -- )
|
|
|
|
find-world dup
|
|
|
|
[ handle>> select-gl-context ] [ no-world-found ] if ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: (request-focus) ( child world ? -- )
|
2008-08-29 19:44:19 -04:00
|
|
|
pick parent>> pick eq? [
|
2008-11-19 22:57:59 -05:00
|
|
|
[ dup parent>> dup ] 2dip
|
2007-09-20 18:09:08 -04:00
|
|
|
[ (request-focus) ] keep
|
|
|
|
] unless focus-child ;
|
|
|
|
|
|
|
|
M: world request-focus-on ( child gadget -- )
|
|
|
|
2dup eq?
|
2008-08-31 02:42:30 -04:00
|
|
|
[ 2drop ] [ dup focused?>> (request-focus) ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-09 12:22:23 -05:00
|
|
|
: new-world ( gadget title status class -- world )
|
2009-02-02 01:02:55 -05:00
|
|
|
vertical swap new-track
|
2008-07-10 21:32:17 -04:00
|
|
|
t >>root?
|
|
|
|
t >>active?
|
|
|
|
{ 0 0 } >>window-loc
|
|
|
|
swap >>status
|
|
|
|
swap >>title
|
2008-07-24 17:16:13 -04:00
|
|
|
swap 1 track-add
|
2009-02-10 03:45:43 -05:00
|
|
|
dup init-text-rendering
|
2007-09-20 18:09:08 -04:00
|
|
|
dup request-focus ;
|
|
|
|
|
2008-12-09 12:22:23 -05:00
|
|
|
: <world> ( gadget title status -- world )
|
|
|
|
world new-world ;
|
|
|
|
|
2009-02-18 21:59:51 -05:00
|
|
|
: as-big-as-possible ( world gadget -- )
|
|
|
|
dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: world layout*
|
2009-02-18 21:59:51 -05:00
|
|
|
[ call-next-method ]
|
|
|
|
[ dup layers>> [ as-big-as-possible ] with each ] bi ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: world focusable-child* gadget-child ;
|
|
|
|
|
2008-08-29 19:44:19 -04:00
|
|
|
M: world children-on nip children>> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-18 20:25:34 -05:00
|
|
|
M: world remove-gadget
|
2009-02-18 21:59:51 -05:00
|
|
|
2dup layers>> memq?
|
|
|
|
[ layers>> delq ] [ call-next-method ] if ;
|
2009-02-18 20:25:34 -05:00
|
|
|
|
2007-10-21 18:10:27 -04:00
|
|
|
: (draw-world) ( world -- )
|
2008-08-31 02:42:30 -04:00
|
|
|
dup handle>> [
|
2009-02-10 19:47:34 -05:00
|
|
|
{
|
|
|
|
[ init-gl ]
|
|
|
|
[ draw-gadget ]
|
|
|
|
[ finish-text-rendering ]
|
2009-02-15 05:33:43 -05:00
|
|
|
[ images>> [ purge-cache ] when* ]
|
2009-02-10 19:47:34 -05:00
|
|
|
} cleave
|
2007-09-20 18:09:08 -04:00
|
|
|
] with-gl-context ;
|
|
|
|
|
|
|
|
: draw-world? ( world -- ? )
|
|
|
|
#! We don't draw deactivated worlds, or those with 0 size.
|
|
|
|
#! On Windows, the latter case results in GL errors.
|
2009-01-27 00:11:45 -05:00
|
|
|
{ [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-07-11 01:01:22 -04:00
|
|
|
TUPLE: world-error error world ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-07-11 01:01:22 -04:00
|
|
|
C: <world-error> world-error
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
SYMBOL: ui-error-hook
|
|
|
|
|
2008-06-25 23:06:34 -04:00
|
|
|
: ui-error ( error -- )
|
2009-02-09 01:49:48 -05:00
|
|
|
ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-10 17:16:12 -05:00
|
|
|
ui-error-hook [ [ rethrow ] ] initialize
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-10-21 18:10:27 -04:00
|
|
|
: draw-world ( world -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
dup draw-world? [
|
|
|
|
dup world [
|
2009-01-27 00:11:45 -05:00
|
|
|
[ (draw-world) ] [
|
2007-09-20 18:09:08 -04:00
|
|
|
over <world-error> ui-error
|
2008-09-27 17:45:20 -04:00
|
|
|
f >>active? drop
|
2007-09-20 18:09:08 -04:00
|
|
|
] recover
|
|
|
|
] with-variable
|
2009-01-27 00:11:45 -05:00
|
|
|
] [ drop ] if ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
world H{
|
2009-01-28 01:30:57 -05:00
|
|
|
{ T{ key-down f { C+ } "z" } [ undo-action send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "Z" } [ redo-action send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "x" } [ cut-action send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "c" } [ copy-action send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "v" } [ paste-action send-action ] }
|
|
|
|
{ T{ key-down f { C+ } "a" } [ select-all-action send-action ] }
|
2008-10-01 00:38:11 -04:00
|
|
|
{ T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
|
|
|
|
{ T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
|
2008-11-22 02:42:16 -05:00
|
|
|
{ T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
|
2008-10-01 00:38:11 -04:00
|
|
|
{ T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
|
|
|
|
{ T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
|
2008-11-22 02:42:16 -05:00
|
|
|
{ T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
|
2007-09-20 18:09:08 -04:00
|
|
|
} set-gestures
|
|
|
|
|
2008-11-21 23:03:14 -05:00
|
|
|
PREDICATE: specific-button-up < button-up #>> ;
|
|
|
|
PREDICATE: specific-button-down < button-down #>> ;
|
2008-11-22 02:42:16 -05:00
|
|
|
PREDICATE: specific-drag < drag #>> ;
|
2008-11-21 23:03:14 -05:00
|
|
|
|
|
|
|
: generalize-gesture ( gesture -- )
|
|
|
|
clone f >># button-gesture ;
|
|
|
|
|
|
|
|
M: world handle-gesture ( gesture gadget -- ? )
|
2008-11-22 03:23:43 -05:00
|
|
|
2dup call-next-method [
|
|
|
|
{
|
|
|
|
{ [ over specific-button-up? ] [ drop generalize-gesture f ] }
|
|
|
|
{ [ over specific-button-down? ] [ drop generalize-gesture f ] }
|
|
|
|
{ [ over specific-drag? ] [ drop generalize-gesture f ] }
|
|
|
|
[ 2drop t ]
|
|
|
|
} cond
|
|
|
|
] [ 2drop f ] if ;
|
2008-11-21 23:03:14 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: close-global ( world global -- )
|
2008-11-30 18:47:29 -05:00
|
|
|
[ get-global find-world eq? ] keep '[ f _ set-global ] when ;
|