factor/basis/ui/render/render.factor

153 lines
3.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors colors colors.constants combinators kernel
math.rectangles math.vectors namespaces opengl opengl.capabilities
opengl.gl opengl.textures sequences sets ui.gadgets ui.pens ;
2007-09-20 18:09:08 -04:00
IN: ui.render
SYMBOL: clip
SYMBOL: viewport-translation
: flip-rect ( rect -- loc dim )
rect-bounds [
[ { 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 ( gadget -- )
2009-01-27 00:11:45 -05:00
[
dim>>
[ { 0 1 } v* viewport-translation namespaces:set ]
2009-01-27 00:11:45 -05:00
[ [ { 0 0 } ] dip gl-viewport ]
[ [ 0 ] dip first2 0 1 -1 glOrtho ] tri
2009-01-27 00:11:45 -05:00
]
[ clip namespaces:set ] bi
2007-09-20 18:09:08 -04:00
do-clip ;
2009-09-25 10:42:09 -04:00
SLOT: background-color
: gl-init ( -- )
check-extensions "1.0" require-gl-version
2007-09-20 18:09:08 -04:00
GL_SMOOTH glShadeModel
GL_BLEND glEnable
GL_VERTEX_ARRAY glEnableClientState
GL_PACK_ALIGNMENT 1 glPixelStorei
GL_UNPACK_ALIGNMENT 1 glPixelStorei ;
: gl-draw-init ( world -- )
GL_SCISSOR_TEST glEnable
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
2007-09-20 18:09:08 -04:00
init-matrices
2009-09-25 10:42:09 -04:00
[ init-clip ]
[
2009-09-25 10:42:09 -04:00
background-color>> >rgba-components glClearColor
GL_COLOR_BUFFER_BIT glClear
2009-09-25 10:42:09 -04:00
] bi ;
2007-09-20 18:09:08 -04:00
GENERIC: draw-gadget* ( gadget -- )
M: gadget draw-gadget* drop ;
SYMBOL: origin
{ 0 0 } origin set-global
: visible-children ( gadget -- seq )
[ clip get origin get vneg offset-rect ] dip children-on ;
2007-09-20 18:09:08 -04:00
: translate ( rect/point -- ) loc>> origin [ v+ ] change ;
2007-09-20 18:09:08 -04:00
2009-02-14 22:53:39 -05:00
GENERIC: draw-children ( gadget -- )
2007-09-20 18:09:08 -04:00
2009-04-02 10:09:09 -04:00
! For gadget selection
SYMBOL: selected-gadgets
SYMBOL: selection-background
GENERIC: selected-children ( gadget -- assoc/f selection-background )
M: gadget selected-children drop f f ;
! For text rendering
SYMBOL: background
SYMBOL: foreground
GENERIC: gadget-background ( gadget -- color )
M: gadget gadget-background dup interior>> pen-background ;
GENERIC: gadget-foreground ( gadget -- color )
M: gadget gadget-foreground dup interior>> pen-foreground ;
<PRIVATE
: draw-selection-background ( gadget -- )
selection-background get background namespaces:set
2009-04-02 10:09:09 -04:00
selection-background get gl-color
[ { 0 0 } ] dip dim>> gl-fill-rect ;
: draw-standard-background ( object -- )
dup interior>> [ draw-interior ] [ drop ] if* ;
2009-04-02 10:09:09 -04:00
: draw-background ( gadget -- )
origin get [
[
dup selected-gadgets get in?
2009-04-02 10:09:09 -04:00
[ draw-selection-background ]
[ draw-standard-background ] if
] [ draw-gadget* ] bi
] with-translation ;
: draw-border ( object -- )
2017-07-01 01:51:10 -04:00
dup boundary>> [
2009-04-02 10:09:09 -04:00
origin get [ draw-boundary ] with-translation
2017-07-01 01:51:10 -04:00
] [ drop ] if* ;
2009-04-02 10:09:09 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: (draw-gadget) ( gadget -- )
dup loc>> origin get v+ origin [
2009-04-02 10:09:09 -04:00
[ draw-background ] [ draw-children ] [ draw-border ] 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 -- )
clip get [ over change-clip do-clip call ] dip
clip namespaces: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 ]
2009-02-14 20:50:22 -05:00
} cond ;
2009-02-14 22:53:39 -05:00
M: gadget draw-children
2009-04-02 10:09:09 -04:00
dup children>> [
{
[ visible-children ]
[ selected-children ]
[ gadget-background ]
[ gadget-foreground ]
} cleave [
2012-07-19 16:55:34 -04:00
2009-04-02 10:09:09 -04:00
{
[ [ selected-gadgets namespaces:set ] when* ]
[ [ selection-background namespaces:set ] when* ]
[ [ background namespaces:set ] when* ]
[ [ foreground namespaces:set ] when* ]
2009-04-02 10:09:09 -04:00
} spread
[ draw-gadget ] each
] with-scope
] [ drop ] if ;