| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | ! Copyright (C) 2005, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | USING: accessors alien alien.c-types arrays hashtables io kernel | 
					
						
							|  |  |  | math namespaces opengl opengl.gl opengl.glu sequences strings | 
					
						
							|  |  |  | io.styles vectors combinators math.vectors ui.gadgets colors | 
					
						
							|  |  |  | math.order math.geometry.rect locals ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: ui.render | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: clip | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: viewport-translation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : flip-rect ( rect -- loc dim )
 | 
					
						
							|  |  |  |     rect-bounds [ | 
					
						
							| 
									
										
										
										
											2008-11-28 01:02:02 -05:00
										 |  |  |         [ { 1 -1 } v* ] dip { 0 -1 } v* v+ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         viewport-translation get v+ | 
					
						
							|  |  |  |     ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : do-clip ( -- ) clip get flip-rect gl-set-clip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : init-clip ( clip-rect rect -- )
 | 
					
						
							|  |  |  |     GL_SCISSOR_TEST glEnable | 
					
						
							|  |  |  |     [ rect-intersect ] keep
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     dim>> dup { 0 1 } v* viewport-translation set
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     { 0 0 } over gl-viewport | 
					
						
							| 
									
										
										
										
											2008-11-17 06:16:34 -05:00
										 |  |  |     0 swap first2 0 gluOrtho2D | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     clip set
 | 
					
						
							|  |  |  |     do-clip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : init-gl ( clip-rect rect -- )
 | 
					
						
							|  |  |  |     GL_SMOOTH glShadeModel | 
					
						
							|  |  |  |     GL_BLEND glEnable | 
					
						
							|  |  |  |     GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     GL_VERTEX_ARRAY glEnableClientState | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     init-matrices | 
					
						
							|  |  |  |     init-clip | 
					
						
							|  |  |  |     ! white gl-clear is broken w.r.t window resizing | 
					
						
							|  |  |  |     ! Linux/PPC Radeon 9200 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     white gl-color | 
					
						
							|  |  |  |     clip get dim>> gl-fill-rect ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | GENERIC: draw-gadget* ( gadget -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: gadget draw-gadget* drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: draw-interior ( gadget interior -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: draw-boundary ( gadget boundary -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: origin | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | { 0 0 } origin set-global
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : visible-children ( gadget -- seq )
 | 
					
						
							|  |  |  |     clip get origin get vneg offset-rect swap children-on ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : translate ( rect/point -- ) rect-loc origin [ v+ ] change ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: draw-gadget | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (draw-gadget) ( gadget -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup translate | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |         dup interior>> [ | 
					
						
							|  |  |  |             origin get [ dupd draw-interior ] with-translation | 
					
						
							|  |  |  |         ] when*
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         dup draw-gadget* | 
					
						
							|  |  |  |         dup visible-children [ draw-gadget ] each
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |         dup boundary>> [ | 
					
						
							|  |  |  |             origin get [ dupd draw-boundary ] with-translation | 
					
						
							|  |  |  |         ] when*
 | 
					
						
							|  |  |  |         drop
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] with-scope ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >absolute ( rect -- rect )
 | 
					
						
							|  |  |  |     origin get offset-rect ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : change-clip ( gadget -- )
 | 
					
						
							|  |  |  |     >absolute clip [ rect-intersect ] change ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-clipping ( gadget quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-28 01:02:02 -05:00
										 |  |  |     clip get [ over change-clip do-clip call ] dip clip set do-clip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : draw-gadget ( gadget -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-08-29 19:44:19 -04:00
										 |  |  |         { [ dup visible?>> not ] [ drop ] } | 
					
						
							|  |  |  |         { [ dup clipped?>> not ] [ (draw-gadget) ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:54:33 -04:00
										 |  |  |         [ [ (draw-gadget) ] with-clipping ] | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | ! A pen that caches vertex arrays, etc | 
					
						
							|  |  |  | TUPLE: caching-pen last-dim ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: recompute-pen ( gadget pen -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : compute-pen ( gadget pen -- )
 | 
					
						
							|  |  |  |     2dup [ dim>> ] [ last-dim>> ] bi* = [ | 
					
						
							|  |  |  |         2drop
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ swap dim>> >>last-dim drop ] [ recompute-pen ] 2bi
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Solid fill/border | 
					
						
							| 
									
										
										
										
											2008-11-11 03:35:27 -05:00
										 |  |  | TUPLE: solid < caching-pen color interior-vertices boundary-vertices ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <solid> ( color -- solid ) solid new swap >>color ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | M: solid recompute-pen | 
					
						
							| 
									
										
										
										
											2008-11-11 03:35:27 -05:00
										 |  |  |     swap dim>> | 
					
						
							|  |  |  |     [ (fill-rect-vertices) >>interior-vertices ] | 
					
						
							|  |  |  |     [ (rect-vertices) >>boundary-vertices ] | 
					
						
							|  |  |  |     bi drop ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Solid pen | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | : (solid) ( gadget pen -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:35:27 -05:00
										 |  |  |     [ compute-pen ] [ color>> gl-color ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:35:27 -05:00
										 |  |  | M: solid draw-interior | 
					
						
							|  |  |  |     [ (solid) ] [ interior-vertices>> gl-vertex-pointer ] bi
 | 
					
						
							|  |  |  |     (gl-fill-rect) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:35:27 -05:00
										 |  |  | M: solid draw-boundary | 
					
						
							|  |  |  |     [ (solid) ] [ boundary-vertices>> gl-vertex-pointer ] bi
 | 
					
						
							|  |  |  |     (gl-rect) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Gradient pen | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | TUPLE: gradient < caching-pen colors last-vertices last-colors ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <gradient> ( colors -- gradient ) gradient new swap >>colors ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: gradient-vertices ( direction dim colors -- seq )
 | 
					
						
							|  |  |  |     direction dim v* dim over v- swap
 | 
					
						
							|  |  |  |     colors length dup 1- v/n [ v*n ] with map
 | 
					
						
							|  |  |  |     [ dup rot v+ 2array ] with map
 | 
					
						
							|  |  |  |     concat concat >c-float-array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gradient-colors ( colors -- seq )
 | 
					
						
							|  |  |  |     [ color>raw 4array dup 2array ] map concat concat >c-float-array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: gradient recompute-pen ( gadget gradient -- )
 | 
					
						
							|  |  |  |     tuck | 
					
						
							|  |  |  |     [ [ orientation>> ] [ dim>> ] bi ] [ colors>> ] bi*
 | 
					
						
							|  |  |  |     [ gradient-vertices >>last-vertices ] | 
					
						
							|  |  |  |     [ gradient-colors >>last-colors ] bi
 | 
					
						
							|  |  |  |     drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : draw-gradient ( colors -- )
 | 
					
						
							|  |  |  |     GL_COLOR_ARRAY [ | 
					
						
							|  |  |  |         [ GL_QUAD_STRIP 0 ] dip length 2 * glDrawArrays | 
					
						
							|  |  |  |     ] do-enabled-client-state ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: gradient draw-interior | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         [ compute-pen ] | 
					
						
							|  |  |  |         [ last-vertices>> gl-vertex-pointer ] | 
					
						
							|  |  |  |         [ last-colors>> gl-color-pointer ] | 
					
						
							|  |  |  |         [ colors>> draw-gradient ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Polygon pen | 
					
						
							| 
									
										
										
										
											2008-11-26 02:41:13 -05:00
										 |  |  | TUPLE: polygon color | 
					
						
							|  |  |  | interior-vertices | 
					
						
							|  |  |  | interior-count | 
					
						
							|  |  |  | boundary-vertices | 
					
						
							|  |  |  | boundary-count ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | : <polygon> ( color points -- polygon )
 | 
					
						
							| 
									
										
										
										
											2008-11-26 02:41:13 -05:00
										 |  |  |     dup close-path [ [ concat >c-float-array ] [ length ] bi ] bi@
 | 
					
						
							|  |  |  |     polygon boa ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-26 02:41:13 -05:00
										 |  |  | M: polygon draw-boundary | 
					
						
							|  |  |  |     nip
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     [ color>> gl-color ] | 
					
						
							| 
									
										
										
										
											2008-11-26 02:41:13 -05:00
										 |  |  |     [ boundary-vertices>> gl-vertex-pointer ] | 
					
						
							|  |  |  |     [ [ GL_LINE_STRIP 0 ] dip boundary-count>> glDrawArrays ] | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     tri ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: polygon draw-interior | 
					
						
							| 
									
										
										
										
											2008-11-26 02:41:13 -05:00
										 |  |  |     nip
 | 
					
						
							|  |  |  |     [ color>> gl-color ] | 
					
						
							|  |  |  |     [ interior-vertices>> gl-vertex-pointer ] | 
					
						
							|  |  |  |     [ [ GL_POLYGON 0 ] dip interior-count>> glDrawArrays ] | 
					
						
							|  |  |  |     tri ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : arrow-up    { { 3 0 } { 6 6 } { 0 6 } } ;
 | 
					
						
							|  |  |  | : arrow-right { { 0 0 } { 6 3 } { 0 6 } } ;
 | 
					
						
							|  |  |  | : arrow-down  { { 0 0 } { 6 0 } { 3 6 } } ;
 | 
					
						
							|  |  |  | : arrow-left  { { 0 3 } { 6 0 } { 6 6 } } ;
 | 
					
						
							|  |  |  | : close-box   { { 0 0 } { 6 0 } { 6 6 } { 0 6 } } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <polygon-gadget> ( color points -- gadget )
 | 
					
						
							|  |  |  |     dup max-dim | 
					
						
							| 
									
										
										
										
											2008-11-28 01:02:02 -05:00
										 |  |  |     [ <polygon> <gadget> ] dip >>dim | 
					
						
							| 
									
										
										
										
											2008-09-27 15:36:04 -04:00
										 |  |  |     swap >>interior ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-31 01:04:54 -04:00
										 |  |  | ! Font rendering | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | SYMBOL: font-renderer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: open-font font-renderer ( font -- open-font )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: string-width font-renderer ( open-font string -- w )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: string-height font-renderer ( open-font string -- h )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: draw-string font-renderer ( font string loc -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: x>offset font-renderer ( x open-font string -- n )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | HOOK: free-fonts font-renderer ( world -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : text-height ( open-font text -- n )
 | 
					
						
							|  |  |  |     dup string? [ | 
					
						
							|  |  |  |         string-height | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |         [ string-height ] with map sum
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : text-width ( open-font text -- n )
 | 
					
						
							|  |  |  |     dup string? [ | 
					
						
							|  |  |  |         string-width | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-11-30 18:47:29 -05:00
										 |  |  |         [ 0 ] 2dip [ string-width max ] with each
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : text-dim ( open-font text -- dim )
 | 
					
						
							|  |  |  |     [ text-width ] 2keep text-height 2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : draw-text ( font text loc -- )
 | 
					
						
							|  |  |  |     over string? [ | 
					
						
							|  |  |  |         draw-string | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 2dup { 0 0 } draw-string | 
					
						
							| 
									
										
										
										
											2008-11-28 01:02:02 -05:00
										 |  |  |                 [ open-font ] dip string-height | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |                 0.0 swap 0.0 glTranslated | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |             ] with each
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         ] with-translation | 
					
						
							|  |  |  |     ] if ;
 |