]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/render/render.factor
disambiguate namespaces:set and sets:set.
[factor.git] / basis / ui / render / render.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors colors colors.constants combinators kernel
4 math.rectangles math.vectors namespaces opengl opengl.capabilities
5 opengl.gl opengl.textures sequences sets ui.gadgets ui.pens ;
6 IN: ui.render
7
8 SYMBOL: clip
9
10 SYMBOL: viewport-translation
11
12 : flip-rect ( rect -- loc dim )
13     rect-bounds [
14         [ { 1 -1 } v* ] dip { 0 -1 } v* v+
15         viewport-translation get v+
16     ] keep ;
17
18 : do-clip ( -- ) clip get flip-rect gl-set-clip ;
19
20 : init-clip ( gadget -- )
21     [
22         dim>>
23         [ { 0 1 } v* viewport-translation namespaces:set ]
24         [ [ { 0 0 } ] dip gl-viewport ]
25         [ [ 0 ] dip first2 0 1 -1 glOrtho ] tri
26     ]
27     [ clip namespaces:set ] bi
28     do-clip ;
29
30 SLOT: background-color
31
32 : gl-init ( -- )
33     check-extensions "1.0" require-gl-version
34     GL_SMOOTH glShadeModel
35     GL_BLEND glEnable
36     GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
37     GL_VERTEX_ARRAY glEnableClientState
38     GL_PACK_ALIGNMENT 1 glPixelStorei
39     GL_UNPACK_ALIGNMENT 1 glPixelStorei ;
40
41 : gl-draw-init ( world -- )
42     GL_SCISSOR_TEST glEnable
43     init-matrices
44     [ init-clip ]
45     [
46         background-color>> >rgba-components glClearColor
47         GL_COLOR_BUFFER_BIT glClear
48     ] bi ;
49
50 GENERIC: draw-gadget* ( gadget -- )
51
52 M: gadget draw-gadget* drop ;
53
54 SYMBOL: origin
55
56 { 0 0 } origin set-global
57
58 : visible-children ( gadget -- seq )
59     [ clip get origin get vneg offset-rect ] dip children-on ;
60
61 : translate ( rect/point -- ) loc>> origin [ v+ ] change ;
62
63 GENERIC: draw-children ( gadget -- )
64
65 ! For gadget selection
66 SYMBOL: selected-gadgets
67
68 SYMBOL: selection-background
69
70 GENERIC: selected-children ( gadget -- assoc/f selection-background )
71
72 M: gadget selected-children drop f f ;
73
74 ! For text rendering
75 SYMBOL: background
76
77 SYMBOL: foreground
78
79 GENERIC: gadget-background ( gadget -- color )
80
81 M: gadget gadget-background dup interior>> pen-background ;
82
83 GENERIC: gadget-foreground ( gadget -- color )
84
85 M: gadget gadget-foreground dup interior>> pen-foreground ;
86
87 <PRIVATE
88
89 : draw-selection-background ( gadget -- )
90     selection-background get background namespaces:set
91     selection-background get gl-color
92     [ { 0 0 } ] dip dim>> gl-fill-rect ;
93
94 : draw-standard-background ( object -- )
95     dup interior>> dup [ draw-interior ] [ 2drop ] if ;
96
97 : draw-background ( gadget -- )
98     origin get [
99         [
100             dup selected-gadgets get in?
101             [ draw-selection-background ]
102             [ draw-standard-background ] if
103         ] [ draw-gadget* ] bi
104     ] with-translation ;
105
106 : draw-border ( object -- )
107     dup boundary>> dup [
108         origin get [ draw-boundary ] with-translation
109     ] [ 2drop ] if ;
110
111 PRIVATE>
112
113 : (draw-gadget) ( gadget -- )
114     dup loc>> origin get v+ origin [
115         [ draw-background ] [ draw-children ] [ draw-border ] tri
116     ] with-variable ;
117
118 : >absolute ( rect -- rect )
119     origin get offset-rect ;
120
121 : change-clip ( gadget -- )
122     >absolute clip [ rect-intersect ] change ;
123
124 : with-clipping ( gadget quot -- )
125     clip get [ over change-clip do-clip call ] dip
126     clip namespaces:set do-clip ; inline
127
128 : draw-gadget ( gadget -- )
129     {
130         { [ dup visible?>> not ] [ drop ] }
131         { [ dup clipped?>> not ] [ (draw-gadget) ] }
132         [ [ (draw-gadget) ] with-clipping ]
133     } cond ;
134
135 M: gadget draw-children
136     dup children>> [
137         {
138             [ visible-children ]
139             [ selected-children ]
140             [ gadget-background ]
141             [ gadget-foreground ]
142         } cleave [
143
144             {
145                 [ [ selected-gadgets namespaces:set ] when* ]
146                 [ [ selection-background namespaces:set ] when* ]
147                 [ [ background namespaces:set ] when* ]
148                 [ [ foreground namespaces:set ] when* ]
149             } spread
150             [ draw-gadget ] each
151         ] with-scope
152     ] [ drop ] if ;
153
154 CONSTANT: selection-color T{ rgba f 0.8 0.8 1.0 1.0 }
155
156 CONSTANT: panel-background-color
157     T{ rgba f
158         0.7843137254901961
159         0.7686274509803922
160         0.7176470588235294
161         1.0
162     }
163
164 CONSTANT: focus-border-color COLOR: dark-gray