2009-01-21 20:34:42 -05:00
|
|
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-12 02:39:03 -05:00
|
|
|
USING: accessors alien alien.c-types arrays hashtables io io.pathnames
|
|
|
|
kernel math namespaces opengl opengl.gl opengl.glu sequences strings
|
|
|
|
vectors combinators math.vectors ui.gadgets ui.images colors fry
|
|
|
|
colors.constants math.order math.rectangles locals
|
|
|
|
specialized-arrays.float ;
|
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 ;
|
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: init-clip ( clip-rect -- )
|
|
|
|
[
|
|
|
|
dim>>
|
|
|
|
[ { 0 1 } v* viewport-translation set ]
|
|
|
|
[ [ { 0 0 } ] dip gl-viewport ]
|
|
|
|
[ [ 0 ] dip first2 0 gluOrtho2D ] tri
|
|
|
|
]
|
|
|
|
[ clip set ] bi
|
2007-09-20 18:09:08 -04:00
|
|
|
do-clip ;
|
|
|
|
|
2009-01-27 00:11:45 -05:00
|
|
|
: init-gl ( clip-rect -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
GL_SMOOTH glShadeModel
|
2009-01-27 00:11:45 -05:00
|
|
|
GL_SCISSOR_TEST glEnable
|
2007-09-20 18:09:08 -04:00
|
|
|
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
|
2009-02-05 23:17:15 -05:00
|
|
|
COLOR: white gl-color
|
2008-11-11 01:28:37 -05:00
|
|
|
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 -- )
|
|
|
|
|
2009-02-12 02:39:03 -05:00
|
|
|
GENERIC: pen-pref-dim ( gadget pen -- dim )
|
|
|
|
|
|
|
|
M: object pen-pref-dim 2drop { 0 0 } ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
SYMBOL: origin
|
|
|
|
|
|
|
|
{ 0 0 } origin set-global
|
|
|
|
|
|
|
|
: visible-children ( gadget -- seq )
|
2009-02-10 19:47:34 -05:00
|
|
|
[ clip get origin get vneg offset-rect ] dip children-on ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-01 21:31:42 -05:00
|
|
|
: translate ( rect/point -- ) loc>> origin [ v+ ] change ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
DEFER: draw-gadget
|
|
|
|
|
|
|
|
: (draw-gadget) ( gadget -- )
|
2009-02-10 19:47:34 -05:00
|
|
|
dup loc>> origin get v+ origin [
|
|
|
|
[
|
|
|
|
origin get [
|
|
|
|
[ dup interior>> dup [ draw-interior ] [ 2drop ] if ]
|
|
|
|
[ draw-gadget* ]
|
|
|
|
bi
|
|
|
|
] with-translation
|
|
|
|
]
|
|
|
|
[ visible-children [ draw-gadget ] each ]
|
|
|
|
[
|
|
|
|
dup boundary>> dup [
|
|
|
|
origin get [ draw-boundary ] with-translation
|
|
|
|
] [ 2drop ] if
|
|
|
|
] tri
|
|
|
|
] with-variable ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: >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
|
2009-01-26 17:30:17 -05:00
|
|
|
swap [ over v+ 2array ] curry map
|
2008-11-18 23:18:35 -05:00
|
|
|
concat concat >float-array ;
|
2008-11-11 01:28:37 -05:00
|
|
|
|
|
|
|
: gradient-colors ( colors -- seq )
|
2009-01-25 01:11:06 -05:00
|
|
|
[ >rgba-components 4array dup 2array ] map concat concat
|
2008-11-18 23:18:35 -05:00
|
|
|
>float-array ;
|
2008-11-11 01:28:37 -05:00
|
|
|
|
|
|
|
M: gradient recompute-pen ( gadget gradient -- )
|
2009-01-25 18:55:27 -05:00
|
|
|
[ nip ] [ [ [ orientation>> ] [ dim>> ] bi ] [ colors>> ] bi* ] 2bi
|
2008-11-11 01:28:37 -05:00
|
|
|
[ gradient-vertices >>last-vertices ]
|
2009-01-25 18:55:27 -05:00
|
|
|
[ gradient-colors >>last-colors ]
|
|
|
|
bi drop ;
|
2008-11-11 01:28:37 -05:00
|
|
|
|
|
|
|
: 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-29 06:14:49 -05:00
|
|
|
dup close-path [ [ concat >float-array ] [ length ] bi ] bi@
|
2008-11-26 02:41:13 -05:00
|
|
|
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
|
|
|
|
2009-02-12 02:39:03 -05:00
|
|
|
: theme-image ( name -- image-name )
|
|
|
|
"resource:basis/ui/gadgets/theme/" prepend-path ".tiff" append <image-name> ;
|
|
|
|
|
|
|
|
! Image pen
|
|
|
|
TUPLE: image-pen image fill? ;
|
|
|
|
|
|
|
|
: <image-pen> ( image -- pen ) f image-pen boa ;
|
|
|
|
|
|
|
|
M: image-pen draw-interior
|
|
|
|
[ dim>> ] [ [ image>> ] [ fill?>> ] bi ] bi*
|
|
|
|
[ draw-scaled-image ] [
|
|
|
|
[ image-dim [ - 2/ ] 2map ] keep
|
|
|
|
'[ _ draw-image ] with-translation
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
M: image-pen pen-pref-dim nip image>> image-dim ;
|
|
|
|
|
|
|
|
! Tile pen
|
|
|
|
TUPLE: tile-pen left center right ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-12 02:39:03 -05:00
|
|
|
: <tile-pen> ( left center right -- pen )
|
|
|
|
tile-pen boa ;
|
|
|
|
|
|
|
|
: >tile-pen< ( pen -- left center right )
|
|
|
|
[ left>> ] [ center>> ] [ right>> ] tri ; inline
|
|
|
|
|
|
|
|
M: tile-pen pen-pref-dim
|
|
|
|
swap [
|
|
|
|
>tile-pen< [ image-dim ] tri@
|
|
|
|
[ vmax vmax ] [ v+ v+ ] 3bi
|
|
|
|
] dip orientation>> set-axis ;
|
|
|
|
|
|
|
|
: compute-tile-xs ( gadget pen -- x1 x2 x3 )
|
|
|
|
[ 2drop { 0 0 } ]
|
|
|
|
[ nip left>> image-dim ]
|
|
|
|
[ [ dim>> ] [ right>> image-dim ] bi* v- ]
|
|
|
|
2tri ;
|
|
|
|
|
|
|
|
: compute-tile-widths ( gadget pen -- w1 w2 w3 )
|
|
|
|
[ nip left>> image-dim ]
|
|
|
|
[ [ dim>> ] [ [ left>> ] [ right>> ] bi [ image-dim ] bi@ ] bi* v+ v- ]
|
|
|
|
[ nip right>> image-dim ]
|
|
|
|
2tri ;
|
|
|
|
|
|
|
|
: render-tile ( tile x width gadget -- )
|
|
|
|
[ orientation>> '[ _ v* ] dip ] keep
|
|
|
|
'[
|
|
|
|
_ _ [ dim>> swap ] [ orientation>> ] bi set-axis
|
|
|
|
swap draw-scaled-image
|
|
|
|
] with-translation ;
|
|
|
|
|
|
|
|
M: tile-pen draw-interior ( gadget pen -- )
|
|
|
|
{
|
|
|
|
[ nip >tile-pen< ]
|
|
|
|
[ compute-tile-xs ]
|
|
|
|
[ compute-tile-widths ]
|
|
|
|
[ drop ]
|
|
|
|
} 2cleave
|
|
|
|
[ render-tile ] curry tri-curry@ tri-curry* tri* ;
|