]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/render/render.factor
factor: don't need FROM: namespaces => set or namespaces:set anymore
[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: math.rectangles math.vectors namespaces kernel accessors
4 assocs combinators sequences sets opengl opengl.gl colors
5 colors.constants 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 ( clip-rect -- )
21     [
22         dim>>
23         [ { 0 1 } v* viewport-translation set ]
24         [ [ { 0 0 } ] dip gl-viewport ]
25         [ [ 0 ] dip first2 0 1 -1 glOrtho ] tri
26     ]
27     [ clip set ] bi
28     do-clip ;
29
30 SLOT: background-color
31
32 : init-gl ( world -- )
33     GL_SMOOTH glShadeModel
34     GL_SCISSOR_TEST glEnable
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     init-matrices
41     [ init-clip ]
42     [
43         background-color>> >rgba-components glClearColor
44         GL_COLOR_BUFFER_BIT glClear
45     ] bi ;
46
47 GENERIC: draw-gadget* ( gadget -- )
48
49 M: gadget draw-gadget* drop ;
50
51 SYMBOL: origin
52
53 { 0 0 } origin set-global
54
55 : visible-children ( gadget -- seq )
56     [ clip get origin get vneg offset-rect ] dip children-on ;
57
58 : translate ( rect/point -- ) loc>> origin [ v+ ] change ;
59
60 GENERIC: draw-children ( gadget -- )
61
62 ! For gadget selection
63 SYMBOL: selected-gadgets
64
65 SYMBOL: selection-background
66
67 GENERIC: selected-children ( gadget -- assoc/f selection-background )
68
69 M: gadget selected-children drop f f ;
70
71 ! For text rendering
72 SYMBOL: background
73
74 SYMBOL: foreground
75
76 GENERIC: gadget-background ( gadget -- color )
77
78 M: gadget gadget-background dup interior>> pen-background ;
79
80 GENERIC: gadget-foreground ( gadget -- color )
81
82 M: gadget gadget-foreground dup interior>> pen-foreground ;
83
84 <PRIVATE
85
86 : draw-selection-background ( gadget -- )
87     selection-background get background set
88     selection-background get gl-color
89     [ { 0 0 } ] dip dim>> gl-fill-rect ;
90
91 : draw-standard-background ( object -- )
92     dup interior>> dup [ draw-interior ] [ 2drop ] if ;
93
94 : draw-background ( gadget -- )
95     origin get [
96         [
97             dup selected-gadgets get in?
98             [ draw-selection-background ]
99             [ draw-standard-background ] if
100         ] [ draw-gadget* ] bi
101     ] with-translation ;
102
103 : draw-border ( object -- )
104     dup boundary>> dup [
105         origin get [ draw-boundary ] with-translation
106     ] [ 2drop ] if ;
107
108 PRIVATE>
109
110 : (draw-gadget) ( gadget -- )
111     dup loc>> origin get v+ origin [
112         [ draw-background ] [ draw-children ] [ draw-border ] tri
113     ] with-variable ;
114
115 : >absolute ( rect -- rect )
116     origin get offset-rect ;
117
118 : change-clip ( gadget -- )
119     >absolute clip [ rect-intersect ] change ;
120
121 : with-clipping ( gadget quot -- )
122     clip get [ over change-clip do-clip call ] dip clip set do-clip ; inline
123
124 : draw-gadget ( gadget -- )
125     {
126         { [ dup visible?>> not ] [ drop ] }
127         { [ dup clipped?>> not ] [ (draw-gadget) ] }
128         [ [ (draw-gadget) ] with-clipping ]
129     } cond ;
130
131 M: gadget draw-children
132     dup children>> [
133         {
134             [ visible-children ]
135             [ selected-children ]
136             [ gadget-background ]
137             [ gadget-foreground ]
138         } cleave [
139
140             {
141                 [ [ selected-gadgets set ] when* ]
142                 [ [ selection-background set ] when* ]
143                 [ [ background set ] when* ]
144                 [ [ foreground set ] when* ]
145             } spread
146             [ draw-gadget ] each
147         ] with-scope
148     ] [ drop ] if ;
149
150 CONSTANT: selection-color T{ rgba f 0.8 0.8 1.0 1.0 }
151
152 CONSTANT: panel-background-color
153     T{ rgba f
154         0.7843137254901961
155         0.7686274509803922
156         0.7176470588235294
157         1.0
158     }
159
160 CONSTANT: focus-border-color COLOR: dark-gray