]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/worlds/worlds.factor
6f901c37ee4f787cbe0e754299ce456784c72279
[factor.git] / basis / ui / gadgets / worlds / worlds.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs continuations kernel math models
4 namespaces opengl sequences io combinators math.vectors
5 ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks
6 debugger math.geometry.rect ;
7 IN: ui.gadgets.worlds
8
9 TUPLE: world < track
10 active? focused?
11 glass
12 title status
13 fonts handle
14 window-loc ;
15
16 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
17
18 : show-status ( string/f gadget -- )
19     find-world dup [
20         status>> dup [ set-model ] [ 2drop ] if
21     ] [ 2drop ] if ;
22
23 : hide-status ( gadget -- ) f swap show-status ;
24
25 ERROR: no-world-found ;
26
27 : find-gl-context ( gadget -- )
28     find-world dup
29     [ handle>> select-gl-context ] [ no-world-found ] if ;
30
31 : (request-focus) ( child world ? -- )
32     pick parent>> pick eq? [
33         >r >r dup parent>> dup r> r>
34         [ (request-focus) ] keep
35     ] unless focus-child ;
36
37 M: world request-focus-on ( child gadget -- )
38     2dup eq?
39     [ 2drop ] [ dup focused?>> (request-focus) ] if ;
40
41 : <world> ( gadget title status -- world )
42     { 0 1 } world new-track
43         t >>root?
44         t >>active?
45         H{ } clone >>fonts
46         { 0 0 } >>window-loc
47         swap >>status
48         swap >>title
49         swap 1 track-add
50     dup request-focus ;
51
52 M: world layout*
53     dup call-next-method
54     dup glass>> [
55         >r dup rect-dim r> (>>dim)
56     ] when* drop ;
57
58 M: world focusable-child* gadget-child ;
59
60 M: world children-on nip children>> ;
61
62 : (draw-world) ( world -- )
63     dup handle>> [
64         [ dup init-gl ] keep draw-gadget
65     ] with-gl-context ;
66
67 : draw-world? ( world -- ? )
68     #! We don't draw deactivated worlds, or those with 0 size.
69     #! On Windows, the latter case results in GL errors.
70     dup active?>>
71     over handle>>
72     rot rect-dim [ 0 > ] all? and and ;
73
74 TUPLE: world-error error world ;
75
76 C: <world-error> world-error
77
78 SYMBOL: ui-error-hook
79
80 : ui-error ( error -- )
81     ui-error-hook get [ call ] [ print-error ] if* ;
82
83 [ rethrow ] ui-error-hook set-global
84
85 : draw-world ( world -- )
86     dup draw-world? [
87         dup world [
88             [
89                 (draw-world)
90             ] [
91                 over <world-error> ui-error
92                 f >>active? drop
93             ] recover
94         ] with-variable
95     ] [
96         drop
97     ] if ;
98
99 world H{
100     { T{ key-down f { C+ } "x" } [ T{ cut-action } send-action ] }
101     { T{ key-down f { C+ } "c" } [ T{ copy-action } send-action ] }
102     { T{ key-down f { C+ } "v" } [ T{ paste-action } send-action ] }
103     { T{ key-down f { C+ } "a" } [ T{ select-all-action } send-action ] }
104     { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
105     { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
106     { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
107     { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
108 } set-gestures
109
110 : close-global ( world global -- )
111     dup get-global find-world rot eq?
112     [ f swap set-global ] [ drop ] if ;