]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/gadgets/worlds/worlds.factor
ccfa83334b202079c81c1500535c27135a7fb13c
[factor.git] / basis / ui / gadgets / worlds / worlds.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs continuations kernel math models
4 call namespaces opengl sequences io combinators
5 combinators.short-circuit fry math.vectors math.rectangles cache
6 ui.gadgets ui.gestures ui.render ui.text ui.text.private
7 ui.backend ui.gadgets.tracks ui.commands ;
8 IN: ui.gadgets.worlds
9
10 TUPLE: world < track
11 active? focused?
12 layers
13 title status status-owner
14 text-handle handle images
15 window-loc ;
16
17 : find-world ( gadget -- world/f ) [ world? ] find-parent ;
18
19 : show-status ( string/f gadget -- )
20     dup find-world dup [
21         dup status>> [
22             [ (>>status-owner) ] [ status>> set-model ] bi
23         ] [ 3drop ] if
24     ] [ 3drop ] if ;
25
26 : hide-status ( gadget -- )
27     dup find-world dup [
28         [ status-owner>> eq? ] keep
29         '[ f _ [ (>>status-owner) ] [ status>> set-model ] 2bi ] when
30     ] [ 2drop ] if ;
31
32 ERROR: no-world-found ;
33
34 : find-gl-context ( gadget -- )
35     find-world dup
36     [ handle>> select-gl-context ] [ no-world-found ] if ;
37
38 : (request-focus) ( child world ? -- )
39     pick parent>> pick eq? [
40         [ dup parent>> dup ] 2dip
41         [ (request-focus) ] keep
42     ] unless focus-child ;
43
44 M: world request-focus-on ( child gadget -- )
45     2dup eq?
46     [ 2drop ] [ dup focused?>> (request-focus) ] if ;
47
48 : new-world ( gadget title status class -- world )
49     vertical swap new-track
50         t >>root?
51         t >>active?
52         { 0 0 } >>window-loc
53         swap >>status
54         swap >>title
55         swap 1 track-add
56     dup init-text-rendering
57     dup request-focus ;
58
59 : <world> ( gadget title status -- world )
60     world new-world ;
61
62 : as-big-as-possible ( world gadget -- )
63     dup [ { 0 0 } >>loc over dim>> >>dim ] when 2drop ; inline
64
65 M: world layout*
66     [ call-next-method ]
67     [ dup layers>> [ as-big-as-possible ] with each ] bi ;
68
69 M: world focusable-child* gadget-child ;
70
71 M: world children-on nip children>> ;
72
73 M: world remove-gadget
74     2dup layers>> memq?
75     [ layers>> delq ] [ call-next-method ] if ;
76
77 : (draw-world) ( world -- )
78     dup handle>> [
79         {
80             [ init-gl ]
81             [ draw-gadget ]
82             [ finish-text-rendering ]
83             [ images>> [ purge-cache ] when* ]
84         } cleave
85     ] with-gl-context ;
86
87 : draw-world? ( world -- ? )
88     #! We don't draw deactivated worlds, or those with 0 size.
89     #! On Windows, the latter case results in GL errors.
90     { [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
91
92 TUPLE: world-error error world ;
93
94 C: <world-error> world-error
95
96 SYMBOL: ui-error-hook
97
98 : ui-error ( error -- )
99     ui-error-hook get [ call( error -- ) ] [ die drop ] if* ;
100
101 ui-error-hook [ [ rethrow ] ] initialize
102
103 : draw-world ( world -- )
104     dup draw-world? [
105         dup world [
106             [ (draw-world) ] [
107                 over <world-error> ui-error
108                 f >>active? drop
109             ] recover
110         ] with-variable
111     ] [ drop ] if ;
112
113 world
114 action-gestures [
115     [ [ { C+ } ] dip f <key-down> ]
116     [ '[ _ send-action ] ]
117     bi*
118 ] H{ } assoc-map-as
119 H{
120     { T{ button-down f { C+ } 1 } [ drop T{ button-down f f 3 } button-gesture ] }
121     { T{ button-down f { A+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
122     { T{ button-down f { M+ } 1 } [ drop T{ button-down f f 2 } button-gesture ] }
123     { T{ button-up f { C+ } 1 } [ drop T{ button-up f f 3 } button-gesture ] }
124     { T{ button-up f { A+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
125     { T{ button-up f { M+ } 1 } [ drop T{ button-up f f 2 } button-gesture ] }
126 } assoc-union set-gestures
127
128 PREDICATE: specific-button-up < button-up #>> ;
129 PREDICATE: specific-button-down < button-down #>> ;
130 PREDICATE: specific-drag < drag #>> ;
131
132 : generalize-gesture ( gesture -- )
133     clone f >># button-gesture ;
134
135 M: world handle-gesture ( gesture gadget -- ? )
136     2dup call-next-method [
137         {
138             { [ over specific-button-up? ] [ drop generalize-gesture f ] }
139             { [ over specific-button-down? ] [ drop generalize-gesture f ] }
140             { [ over specific-drag? ] [ drop generalize-gesture f ] }
141             [ 2drop t ]
142         } cond
143     ] [ 2drop f ] if ;
144
145 : close-global ( world global -- )
146     [ get-global find-world eq? ] keep '[ f _ set-global ] when ;