draw-gadget* ( gadget -- )
Factor documentation > Factor handbook > UI framework > Implementing new gadgets > Customizing gadget appearance > Implementing custom drawing logic
Next:find-gl-context ( gadget -- )


Vocabulary
ui.render

Inputs and outputs
gadgeta gadget


Generic word contract
Draws the gadget by making OpenGL calls. The top-left corner of the gadget should be drawn at the location stored in the origin variable.

Notes
This word should not be called directly. To force a gadget to redraw, call relayout-1.

Definition
IN: ui.render

GENERIC: draw-gadget* ( gadget -- )


Methods
USING: kernel ui.gadgets.debug ui.render ;

M: bad-gadget draw-gadget* "Lulz" throw ;


USING: accessors ui.gadgets.tables.private ui.render ;

M: column-headers draw-gadget* table>> draw-column-titles ;


USING: kernel namespaces ui.gadgets.editors
ui.gadgets.editors.private ui.gadgets.line-support ui.render ;

M: editor draw-gadget*
dup compute-selection selected-lines
[ [ draw-lines ] [ draw-caret ] bi ] with-variable ;


USING: kernel ui.gadgets ui.render ;

M: gadget draw-gadget* drop ;


USING: generic images.viewer images.viewer.private kernel
ui.render ;

M: image-control draw-gadget*
[ ?update-texture ]
[ M\ image-control draw-gadget* (call-next-method) ] bi ;


USING: accessors images.viewer images.viewer.private kernel
opengl.textures ui.render ;

M: image-gadget draw-gadget*
dup image>> [
[ dim>> ] [ image-gadget-texture ] bi
draw-scaled-texture
] [ drop ] if ;


USING: accessors kernel opengl.gl space-invaders ui.render ;

M: invaders-gadget draw-gadget*
0 0 glRasterPos2i 1.0 -1.0 glPixelZoom
[ 224 256 GL_RGB GL_UNSIGNED_BYTE ] dip
cpu>> bitmap>> glDrawPixels ;


USING: fonts kernel namespaces ui.gadgets.labels ui.render
ui.text ;

M: label draw-gadget*
>label< [
background get [ font-with-background ] when*
foreground get [ font-with-foreground ] when*
] dip draw-text ;


USING: accessors kernel math.rectangles namespaces opengl
ui.gadgets.lists ui.render ;

M: list draw-gadget*
origin get [
dup color>> gl-color selected-rect
[ rect-bounds gl-fill-rect ] when*
] with-translation ;


USING: maze ui.gadgets.canvas ui.render ;

M: maze draw-gadget* [ n draw-maze ] draw-canvas ;


USING: kernel math nehe.2 opengl.demo-support opengl.gl
opengl.glu ui.render ;

M: nehe2-gadget draw-gadget*
drop GL_PROJECTION glMatrixMode
glLoadIdentity 45.0 width height /
>float 0.1 100.0 gluPerspective GL_MODELVIEW glMatrixMode
glLoadIdentity GL_SMOOTH glShadeModel
0.0 0.0 0.0 0.0 glClearColor 1.0 glClearDepth
GL_DEPTH_TEST glEnable GL_LEQUAL glDepthFunc
GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST glHint
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
glLoadIdentity -1.5 0.0 -6.0 glTranslatef GL_TRIANGLES [
0.0 1.0 0.0 glVertex3f -1.0 -1.0 0.0 glVertex3f
1.0 -1.0 0.0 glVertex3f
] do-state 3.0 0.0 0.0 glTranslatef GL_QUADS [
-1.0 1.0 0.0 glVertex3f 1.0 1.0 0.0 glVertex3f
1.0 -1.0 0.0 glVertex3f -1.0 -1.0 0.0 glVertex3f
] do-state ;


USING: kernel math nehe.3 opengl.demo-support opengl.gl
opengl.glu ui.render ;

M: nehe3-gadget draw-gadget*
drop GL_PROJECTION glMatrixMode
glLoadIdentity 45.0 width height /
>float 0.1 100.0 gluPerspective GL_MODELVIEW glMatrixMode
glLoadIdentity GL_SMOOTH glShadeModel
0.0 0.0 0.0 0.0 glClearColor 1.0 glClearDepth
GL_DEPTH_TEST glEnable GL_LEQUAL glDepthFunc
GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST glHint
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
glLoadIdentity -1.5 0.0 -6.0 glTranslatef GL_TRIANGLES [
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 1.0 0.0 glColor3f -1.0 -1.0 0.0 glVertex3f
0.0 0.0 1.0 glColor3f 1.0 -1.0 0.0 glVertex3f
] do-state 3.0 0.0 0.0 glTranslatef 0.5 0.5 1.0 glColor3f
GL_QUADS [
-1.0 1.0 0.0 glVertex3f 1.0 1.0 0.0 glVertex3f
1.0 -1.0 0.0 glVertex3f -1.0 -1.0 0.0 glVertex3f
] do-state ;


USING: accessors kernel math nehe.4 opengl.demo-support
opengl.gl opengl.glu ui.render ;

M: nehe4-gadget draw-gadget*
GL_PROJECTION glMatrixMode
glLoadIdentity 45.0 width height /
>float 0.1 100.0 gluPerspective GL_MODELVIEW glMatrixMode
glLoadIdentity GL_SMOOTH glShadeModel
0.0 0.0 0.0 0.0 glClearColor 1.0 glClearDepth
GL_DEPTH_TEST glEnable GL_LEQUAL glDepthFunc
GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST glHint
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
glLoadIdentity -1.5 0.0 -6.0 glTranslatef
dup rtri>> 0.0 1.0 0.0 glRotatef GL_TRIANGLES [
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 1.0 0.0 glColor3f -1.0 -1.0 0.0 glVertex3f
0.0 0.0 1.0 glColor3f 1.0 -1.0 0.0 glVertex3f
] do-state glLoadIdentity 1.5 0.0 -6.0 glTranslatef
dup rquad>> 1.0 0.0 0.0 glRotatef 0.5 0.5 1.0 glColor3f
GL_QUADS [
-1.0 1.0 0.0 glVertex3f 1.0 1.0 0.0 glVertex3f
1.0 -1.0 0.0 glVertex3f -1.0 -1.0 0.0 glVertex3f
] do-state [ 0.2 + ] change-rtri [ 0.15 - ] change-rquad
drop ;


USING: accessors kernel math nehe.5 opengl.demo-support
opengl.gl opengl.glu ui.render ;

M: nehe5-gadget draw-gadget*
GL_PROJECTION glMatrixMode
glLoadIdentity 45.0 width height /
>float 0.1 100.0 gluPerspective GL_MODELVIEW glMatrixMode
glLoadIdentity GL_SMOOTH glShadeModel
0.0 0.0 0.0 0.0 glClearColor 1.0 glClearDepth
GL_DEPTH_TEST glEnable GL_LEQUAL glDepthFunc
GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST glHint
GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
glLoadIdentity -1.5 0.0 -6.0 glTranslatef
dup rtri>> 0.0 1.0 0.0 glRotatef GL_TRIANGLES [
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 1.0 0.0 glColor3f -1.0 -1.0 1.0 glVertex3f
0.0 0.0 1.0 glColor3f 1.0 -1.0 1.0 glVertex3f
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 0.0 1.0 glColor3f 1.0 -1.0 1.0 glVertex3f
0.0 1.0 0.0 glColor3f 1.0 -1.0 -1.0 glVertex3f
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 1.0 0.0 glColor3f 1.0 -1.0 -1.0 glVertex3f
0.0 0.0 1.0 glColor3f -1.0 -1.0 -1.0 glVertex3f
1.0 0.0 0.0 glColor3f 0.0 1.0 0.0 glVertex3f
0.0 0.0 1.0 glColor3f -1.0 -1.0 -1.0 glVertex3f
0.0 1.0 0.0 glColor3f -1.0 -1.0 1.0 glVertex3f
] do-state glLoadIdentity 1.5 0.0 -7.0 glTranslatef
dup rquad>> 1.0 0.0 0.0 glRotatef GL_QUADS [
0.0 1.0 0.0 glColor3f 1.0 1.0 -1.0 glVertex3f
-1.0 1.0 -1.0 glVertex3f -1.0 1.0 1.0 glVertex3f
1.0 1.0 1.0 glVertex3f 1.0 0.5 0.0 glColor3f
1.0 -1.0 1.0 glVertex3f -1.0 -1.0 1.0 glVertex3f
-1.0 -1.0 -1.0 glVertex3f 1.0 -1.0 -1.0 glVertex3f
1.0 0.0 0.0 glColor3f 1.0 1.0 1.0 glVertex3f
-1.0 1.0 1.0 glVertex3f -1.0 -1.0 1.0 glVertex3f
1.0 -1.0 1.0 glVertex3f 1.0 1.0 0.0 glColor3f
1.0 -1.0 -1.0 glVertex3f -1.0 -1.0 -1.0 glVertex3f
-1.0 1.0 -1.0 glVertex3f 1.0 1.0 -1.0 glVertex3f
0.0 0.0 1.0 glColor3f -1.0 1.0 1.0 glVertex3f
-1.0 1.0 -1.0 glVertex3f -1.0 -1.0 -1.0 glVertex3f
-1.0 -1.0 1.0 glVertex3f ~20 more~
] do-state [ 0.2 + ] change-rtri [ 0.15 - ] change-rquad
drop ;


USING: accessors arrays kernel locals math math.order
math.splines.viewer math.splines.viewer.private opengl.gl
sequences ui.render ;

M:: spline-gadget draw-gadget* ( gadget -- )
0 0 0 glColor3f gadget [ polynomials>> ] [ steps>> ] bi
eval-polynomials :> pts pts [ first ] [ max ] map-reduce
:> x-max pts [ first ] [ min ] map-reduce :> x-min pts
[ second ] [ max ] map-reduce :> y-max pts
[ second ] [ min ] map-reduce :> y-min pts [
[
first x-min - x-max x-min - /
gadget spline-dim>> first *
] [
second y-min - y-max y-min - /
gadget spline-dim>> second *
] bi 2array
] map :> pts GL_LINE_STRIP glBegin pts
[ first2 neg gadget spline-dim>> second + glVertex2f ] each
glEnd ;


USING: combinators kernel namespaces sequences ui.gadgets
ui.gadgets.line-support ui.gadgets.tables
ui.gadgets.tables.private ui.render ;

M: table draw-gadget*
dup control-value empty?
[ drop ] [
dup line-height \ line-height [
{
[ draw-selected-row ]
[ draw-lines ]
[ draw-column-lines ]
[ draw-focused-row ]
[ draw-moused-row ]
} cleave
] with-variable
] if ;


USING: accessors kernel sequences tetris tetris.gl ui.render ;

M: tetris-gadget draw-gadget*
[ [ dim>> first2 ] [ tetris>> ] bi draw-tetris ] keep
update-status ;


USING: kernel ui.gadgets.paragraphs ui.render ;

M: word-break-gadget draw-gadget* drop ;