1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors colors colors.constants combinators kernel
4 math.rectangles math.vectors namespaces opengl opengl.capabilities
5 opengl.gl 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_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
37 GL_VERTEX_ARRAY glEnableClientState
38 GL_PACK_ALIGNMENT 1 glPixelStorei
39 GL_UNPACK_ALIGNMENT 1 glPixelStorei ;
41 : gl-draw-init ( world -- )
42 GL_SCISSOR_TEST glEnable
46 background-color>> >rgba-components glClearColor
47 GL_COLOR_BUFFER_BIT glClear
50 GENERIC: draw-gadget* ( gadget -- )
52 M: gadget draw-gadget* drop ;
56 { 0 0 } origin set-global
58 : visible-children ( gadget -- seq )
59 [ clip get origin get vneg offset-rect ] dip children-on ;
61 : translate ( rect/point -- ) loc>> origin [ v+ ] change ;
63 GENERIC: draw-children ( gadget -- )
65 ! For gadget selection
66 SYMBOL: selected-gadgets
68 SYMBOL: selection-background
70 GENERIC: selected-children ( gadget -- assoc/f selection-background )
72 M: gadget selected-children drop f f ;
79 GENERIC: gadget-background ( gadget -- color )
81 M: gadget gadget-background dup interior>> pen-background ;
83 GENERIC: gadget-foreground ( gadget -- color )
85 M: gadget gadget-foreground dup interior>> pen-foreground ;
89 : draw-selection-background ( gadget -- )
90 selection-background get background namespaces:set
91 selection-background get gl-color
92 [ { 0 0 } ] dip dim>> gl-fill-rect ;
94 : draw-standard-background ( object -- )
95 dup interior>> dup [ draw-interior ] [ 2drop ] if ;
97 : draw-background ( gadget -- )
100 dup selected-gadgets get in?
101 [ draw-selection-background ]
102 [ draw-standard-background ] if
103 ] [ draw-gadget* ] bi
106 : draw-border ( object -- )
108 origin get [ draw-boundary ] with-translation
113 : (draw-gadget) ( gadget -- )
114 dup loc>> origin get v+ origin [
115 [ draw-background ] [ draw-children ] [ draw-border ] tri
118 : >absolute ( rect -- rect )
119 origin get offset-rect ;
121 : change-clip ( gadget -- )
122 >absolute clip [ rect-intersect ] change ;
124 : with-clipping ( gadget quot -- )
125 clip get [ over change-clip do-clip call ] dip
126 clip namespaces:set do-clip ; inline
128 : draw-gadget ( gadget -- )
130 { [ dup visible?>> not ] [ drop ] }
131 { [ dup clipped?>> not ] [ (draw-gadget) ] }
132 [ [ (draw-gadget) ] with-clipping ]
135 M: gadget draw-children
139 [ selected-children ]
140 [ gadget-background ]
141 [ gadget-foreground ]
145 [ [ selected-gadgets namespaces:set ] when* ]
146 [ [ selection-background namespaces:set ] when* ]
147 [ [ background namespaces:set ] when* ]
148 [ [ foreground namespaces:set ] when* ]
154 CONSTANT: selection-color T{ rgba f 0.8 0.8 1.0 1.0 }
156 CONSTANT: panel-background-color
164 CONSTANT: focus-border-color COLOR: dark-gray