1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors colors combinators kernel math.rectangles
4 math.vectors namespaces opengl opengl.capabilities opengl.gl
5 opengl.textures sequences sets ui.gadgets ui.pens ;
10 SYMBOL: viewport-translation
12 : flip-rect ( rect -- loc dim )
14 [ { 1 -1 } v* ] dip { 0 -1 } v* v+
15 viewport-translation get v+
18 : do-clip ( -- ) clip get flip-rect gl-set-clip ;
20 : init-clip ( gadget -- )
23 [ { 0 1 } v* viewport-translation namespaces:set ]
24 [ [ { 0 0 } ] dip gl-viewport ]
25 [ [ 0 ] dip first2 0 1 -1 glOrtho ] tri
27 [ clip namespaces:set ] bi
30 SLOT: background-color
33 check-extensions "1.0" require-gl-version
34 GL_SMOOTH glShadeModel
36 GL_VERTEX_ARRAY glEnableClientState
37 GL_PACK_ALIGNMENT 1 glPixelStorei
38 GL_UNPACK_ALIGNMENT 1 glPixelStorei ;
40 : gl-draw-init ( world -- )
41 GL_SCISSOR_TEST glEnable
42 GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
44 [ init-clip ] [ background-color>> gl-clear ] bi ;
46 GENERIC: draw-gadget* ( gadget -- )
48 M: gadget draw-gadget* drop ;
52 { 0 0 } origin set-global
54 : visible-children ( gadget -- seq )
55 [ clip get origin get vneg offset-rect ] dip children-on ;
57 : translate ( rect/point -- ) loc>> origin [ v+ ] change ;
59 GENERIC: draw-children ( gadget -- )
61 ! For gadget selection
62 SYMBOL: selected-gadgets
64 SYMBOL: selection-background
66 GENERIC: selected-children ( gadget -- assoc/f selection-background )
68 M: gadget selected-children drop f f ;
75 GENERIC: gadget-background ( gadget -- color )
77 M: gadget gadget-background dup interior>> pen-background ;
79 GENERIC: gadget-foreground ( gadget -- color )
81 M: gadget gadget-foreground dup interior>> pen-foreground ;
85 : draw-selection-background ( gadget -- )
86 selection-background get background namespaces:set
87 selection-background get gl-color
88 [ { 0 0 } ] dip dim>> gl-fill-rect ;
90 : draw-standard-background ( object -- )
91 dup interior>> [ draw-interior ] [ drop ] if* ;
93 : draw-background ( gadget -- )
96 dup selected-gadgets get in?
97 [ draw-selection-background ]
98 [ draw-standard-background ] if
102 : draw-border ( object -- )
104 origin get [ draw-boundary ] with-translation
109 : (draw-gadget) ( gadget -- )
110 dup loc>> origin get v+ origin [
111 [ draw-background ] [ draw-children ] [ draw-border ] tri
114 : >absolute ( rect -- rect )
115 origin get offset-rect ;
117 : change-clip ( gadget -- )
118 >absolute clip [ rect-intersect ] change ;
120 : with-clipping ( gadget quot -- )
121 clip get [ over change-clip do-clip call ] dip
122 clip namespaces:set do-clip ; inline
124 : draw-gadget ( gadget -- )
126 { [ dup visible?>> not ] [ drop ] }
127 { [ dup clipped?>> not ] [ (draw-gadget) ] }
128 [ [ (draw-gadget) ] with-clipping ]
131 M: gadget draw-children
135 [ selected-children ]
136 [ gadget-background ]
137 [ gadget-foreground ]
141 [ [ selected-gadgets namespaces:set ] when* ]
142 [ [ selection-background namespaces:set ] when* ]
143 [ [ background namespaces:set ] when* ]
144 [ [ foreground namespaces:set ] when* ]