1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types arrays hashtables io kernel
4 math namespaces opengl opengl.gl opengl.glu sequences strings
5 io.styles vectors combinators math.vectors ui.gadgets colors
6 math.order math.geometry.rect locals specialized-arrays.float ;
11 SYMBOL: viewport-translation
13 : flip-rect ( rect -- loc dim )
15 [ { 1 -1 } v* ] dip { 0 -1 } v* v+
16 viewport-translation get v+
19 : do-clip ( -- ) clip get flip-rect gl-set-clip ;
21 : init-clip ( clip-rect rect -- )
22 GL_SCISSOR_TEST glEnable
23 [ rect-intersect ] keep
24 dim>> dup { 0 1 } v* viewport-translation set
25 { 0 0 } over gl-viewport
26 0 swap first2 0 gluOrtho2D
30 : init-gl ( clip-rect rect -- )
31 GL_SMOOTH glShadeModel
33 GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
34 GL_VERTEX_ARRAY glEnableClientState
37 ! white gl-clear is broken w.r.t window resizing
38 ! Linux/PPC Radeon 9200
40 clip get dim>> gl-fill-rect ;
42 GENERIC: draw-gadget* ( gadget -- )
44 M: gadget draw-gadget* drop ;
46 GENERIC: draw-interior ( gadget interior -- )
48 GENERIC: draw-boundary ( gadget boundary -- )
52 { 0 0 } origin set-global
54 : visible-children ( gadget -- seq )
55 clip get origin get vneg offset-rect swap children-on ;
57 : translate ( rect/point -- ) rect-loc origin [ v+ ] change ;
61 : (draw-gadget) ( gadget -- )
65 origin get [ dupd draw-interior ] with-translation
68 dup visible-children [ draw-gadget ] each
70 origin get [ dupd draw-boundary ] with-translation
75 : >absolute ( rect -- rect )
76 origin get offset-rect ;
78 : change-clip ( gadget -- )
79 >absolute clip [ rect-intersect ] change ;
81 : with-clipping ( gadget quot -- )
82 clip get [ over change-clip do-clip call ] dip clip set do-clip ; inline
84 : draw-gadget ( gadget -- )
86 { [ dup visible?>> not ] [ drop ] }
87 { [ dup clipped?>> not ] [ (draw-gadget) ] }
88 [ [ (draw-gadget) ] with-clipping ]
91 ! A pen that caches vertex arrays, etc
92 TUPLE: caching-pen last-dim ;
94 GENERIC: recompute-pen ( gadget pen -- )
96 : compute-pen ( gadget pen -- )
97 2dup [ dim>> ] [ last-dim>> ] bi* = [
100 [ swap dim>> >>last-dim drop ] [ recompute-pen ] 2bi
104 TUPLE: solid < caching-pen color interior-vertices boundary-vertices ;
106 : <solid> ( color -- solid ) solid new swap >>color ;
108 M: solid recompute-pen
110 [ (fill-rect-vertices) >>interior-vertices ]
111 [ (rect-vertices) >>boundary-vertices ]
117 : (solid) ( gadget pen -- )
118 [ compute-pen ] [ color>> gl-color ] bi ;
122 M: solid draw-interior
123 [ (solid) ] [ interior-vertices>> gl-vertex-pointer ] bi
126 M: solid draw-boundary
127 [ (solid) ] [ boundary-vertices>> gl-vertex-pointer ] bi
131 TUPLE: gradient < caching-pen colors last-vertices last-colors ;
133 : <gradient> ( colors -- gradient ) gradient new swap >>colors ;
137 :: gradient-vertices ( direction dim colors -- seq )
138 direction dim v* dim over v- swap
139 colors length dup 1- v/n [ v*n ] with map
140 [ dup rot v+ 2array ] with map
141 concat concat >float-array ;
143 : gradient-colors ( colors -- seq )
144 [ color>raw 4array dup 2array ] map concat concat
147 M: gradient recompute-pen ( gadget gradient -- )
149 [ [ orientation>> ] [ dim>> ] bi ] [ colors>> ] bi*
150 [ gradient-vertices >>last-vertices ]
151 [ gradient-colors >>last-colors ] bi
154 : draw-gradient ( colors -- )
156 [ GL_QUAD_STRIP 0 ] dip length 2 * glDrawArrays
157 ] do-enabled-client-state ;
161 M: gradient draw-interior
164 [ last-vertices>> gl-vertex-pointer ]
165 [ last-colors>> gl-color-pointer ]
166 [ colors>> draw-gradient ]
176 : <polygon> ( color points -- polygon )
177 dup close-path [ [ concat >float-array ] [ length ] bi ] bi@
180 M: polygon draw-boundary
183 [ boundary-vertices>> gl-vertex-pointer ]
184 [ [ GL_LINE_STRIP 0 ] dip boundary-count>> glDrawArrays ]
187 M: polygon draw-interior
190 [ interior-vertices>> gl-vertex-pointer ]
191 [ [ GL_POLYGON 0 ] dip interior-count>> glDrawArrays ]
194 CONSTANT: arrow-up { { 3 0 } { 6 6 } { 0 6 } }
195 CONSTANT: arrow-right { { 0 0 } { 6 3 } { 0 6 } }
196 CONSTANT: arrow-down { { 0 0 } { 6 0 } { 3 6 } }
197 CONSTANT: arrow-left { { 0 3 } { 6 0 } { 6 6 } }
198 CONSTANT: close-box { { 0 0 } { 6 0 } { 6 6 } { 0 6 } }
200 : <polygon-gadget> ( color points -- gadget )
202 [ <polygon> <gadget> ] dip >>dim
206 SYMBOL: font-renderer
208 HOOK: open-font font-renderer ( font -- open-font )
210 HOOK: string-width font-renderer ( open-font string -- w )
212 HOOK: string-height font-renderer ( open-font string -- h )
214 HOOK: draw-string font-renderer ( font string loc -- )
216 HOOK: x>offset font-renderer ( x open-font string -- n )
218 HOOK: free-fonts font-renderer ( world -- )
220 : text-height ( open-font text -- n )
224 [ string-height ] with map sum
227 : text-width ( open-font text -- n )
231 [ 0 ] 2dip [ string-width max ] with each
234 : text-dim ( open-font text -- dim )
235 [ text-width ] 2keep text-height 2array ;
237 : draw-text ( font text loc -- )
243 2dup { 0 0 } draw-string
244 [ open-font ] dip string-height
245 0.0 swap 0.0 glTranslated