forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
278 lines (226 loc) · 7.86 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package gl
import (
"image/color"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/widget"
"github.com/go-gl/gl/v3.2-core/gl"
)
func (c *glCanvas) walkObjects(obj fyne.CanvasObject, pos fyne.Position,
f func(object fyne.CanvasObject, pos fyne.Position)) {
switch co := obj.(type) {
case *fyne.Container:
offset := co.Position().Add(pos)
f(obj, offset)
for _, child := range co.Objects {
c.walkObjects(child, offset, f)
}
case *widget.ScrollContainer: // TODO should this be somehow not scroll container specific?
offset := co.Position().Add(pos)
scrollX := textureScaleInt(c, offset.X)
scrollY := textureScaleInt(c, offset.Y)
scrollWidth := textureScaleInt(c, co.Size().Width)
scrollHeight := textureScaleInt(c, co.Size().Height)
_, pixHeight := c.window.viewport.GetFramebufferSize()
gl.Scissor(int32(scrollX), int32(pixHeight-scrollY-scrollHeight), int32(scrollWidth), int32(scrollHeight))
gl.Enable(gl.SCISSOR_TEST)
f(obj, offset)
for _, child := range widget.Renderer(co).Objects() {
c.walkObjects(child, offset, f)
}
gl.Disable(gl.SCISSOR_TEST)
case fyne.Widget:
offset := co.Position().Add(pos)
f(obj, offset)
for _, child := range widget.Renderer(co).Objects() {
c.walkObjects(child, offset, f)
}
default:
f(obj, pos)
}
}
func rectInnerCoords(size fyne.Size, pos fyne.Position, fill canvas.ImageFill, aspect float32) (fyne.Size, fyne.Position) {
if fill == canvas.ImageFillContain || fill == canvas.ImageFillOriginal {
// change pos and size accordingly
viewAspect := float32(size.Width) / float32(size.Height)
newWidth, newHeight := size.Width, size.Height
widthPad, heightPad := 0, 0
if viewAspect > aspect {
newWidth = int(float32(size.Height) * aspect)
widthPad = (size.Width - newWidth) / 2
} else if viewAspect < aspect {
newHeight = int(float32(size.Width) / aspect)
heightPad = (size.Height - newHeight) / 2
}
return fyne.NewSize(newWidth, newHeight), fyne.NewPos(pos.X+widthPad, pos.Y+heightPad)
}
return size, pos
}
// rectCoords calculates the openGL coordinate space of a rectangle
func (c *glCanvas) rectCoords(size fyne.Size, pos fyne.Position, frame fyne.Size,
fill canvas.ImageFill, aspect float32, pad int) []float32 {
size, pos = rectInnerCoords(size, pos, fill, aspect)
xPos := float32(pos.X-pad) / float32(frame.Width)
x1 := -1 + xPos*2
x2Pos := float32(pos.X+size.Width+pad) / float32(frame.Width)
x2 := -1 + x2Pos*2
yPos := float32(pos.Y-pad) / float32(frame.Height)
y1 := 1 - yPos*2
y2Pos := float32(pos.Y+size.Height+pad) / float32(frame.Height)
y2 := 1 - y2Pos*2
points := []float32{
// coord x, y, x texture x, y
x1, y2, 0, 0.0, 1.0, // top left
x1, y1, 0, 0.0, 0.0, // bottom left
x2, y2, 0, 1.0, 1.0, // top right
x2, y1, 0, 1.0, 0.0, // bottom right
}
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
gl.EnableVertexAttribArray(0)
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, 4*len(points), gl.Ptr(points), gl.STATIC_DRAW)
textureUniform := gl.GetUniformLocation(c.program, gl.Str("tex\x00"))
gl.Uniform1i(textureUniform, 0)
vertAttrib := uint32(gl.GetAttribLocation(c.program, gl.Str("vert\x00")))
gl.EnableVertexAttribArray(vertAttrib)
gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))
texCoordAttrib := uint32(gl.GetAttribLocation(c.program, gl.Str("vertTexCoord\x00")))
gl.EnableVertexAttribArray(texCoordAttrib)
gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))
return points
}
func (c *glCanvas) drawTexture(texture uint32, points []float32) {
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.DrawArrays(gl.TRIANGLE_STRIP, 0, int32(len(points)/5))
}
func (c *glCanvas) drawWidget(box fyne.CanvasObject, pos fyne.Position, frame fyne.Size) {
backCol := widget.Renderer(box.(fyne.Widget)).BackgroundColor()
if !box.Visible() || backCol == color.Transparent {
return
}
points := c.rectCoords(box.Size(), pos, frame, canvas.ImageFillStretch, 0.0, 0)
texture := getTexture(box, c.newGlRectTexture)
gl.Disable(gl.BLEND)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawCircle(circle *canvas.Circle, pos fyne.Position, frame fyne.Size) {
if !circle.Visible() {
return
}
points := c.rectCoords(circle.Size(), pos, frame, canvas.ImageFillStretch, 0.0, vectorPad)
texture := getTexture(circle, c.newGlCircleTexture)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawLine(line *canvas.Line, pos fyne.Position, frame fyne.Size) {
if !line.Visible() {
return
}
points := c.rectCoords(line.Size(), pos, frame, canvas.ImageFillStretch, 0.0, vectorPad)
texture := getTexture(line, c.newGlLineTexture)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawImage(img *canvas.Image, pos fyne.Position, frame fyne.Size) {
if !img.Visible() {
return
}
texture := getTexture(img, c.newGlImageTexture)
if texture == 0 {
return
}
// here we have to choose between blending the image alpha or fading it...
// TODO find a way to support both
gl.Enable(gl.BLEND)
if img.Alpha() != 1 {
gl.BlendColor(0, 0, 0, float32(img.Alpha()))
gl.BlendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA)
} else {
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
}
aspect := aspects[img.Resource]
if aspect == 0 {
aspect = aspects[img]
}
points := c.rectCoords(img.Size(), pos, frame, img.FillMode, aspect, 0)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawRaster(img *canvas.Raster, pos fyne.Position, frame fyne.Size) {
if !img.Visible() {
return
}
texture := getTexture(img, c.newGlRasterTexture)
if texture == 0 {
return
}
// here we have to choose between blending the image alpha or fading it...
// TODO find a way to support both
gl.Enable(gl.BLEND)
if img.Alpha() != 1 {
gl.BlendColor(0, 0, 0, float32(img.Alpha()))
gl.BlendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA)
} else {
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
}
points := c.rectCoords(img.Size(), pos, frame, canvas.ImageFillStretch, 0.0, 0)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame fyne.Size) {
if !rect.Visible() {
return
}
points := c.rectCoords(rect.Size(), pos, frame, canvas.ImageFillStretch, 0.0, 0)
texture := getTexture(rect, c.newGlRectTexture)
gl.Enable(gl.BLEND) // enable translucency
c.drawTexture(texture, points)
}
func (c *glCanvas) drawText(text *canvas.Text, pos fyne.Position, frame fyne.Size) {
if !text.Visible() || text.Text == "" {
return
}
size := text.MinSize()
containerSize := text.Size()
switch text.Alignment {
case fyne.TextAlignTrailing:
pos = fyne.NewPos(pos.X+containerSize.Width-size.Width, pos.Y)
case fyne.TextAlignCenter:
pos = fyne.NewPos(pos.X+(containerSize.Width-size.Width)/2, pos.Y)
}
if text.Size().Height > text.MinSize().Height {
pos = fyne.NewPos(pos.X, pos.Y+(text.Size().Height-text.MinSize().Height)/2)
}
points := c.rectCoords(size, pos, frame, canvas.ImageFillStretch, 0.0, 0)
texture := getTexture(text, c.newGlTextTexture)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
c.drawTexture(texture, points)
}
func (c *glCanvas) drawObject(o fyne.CanvasObject, offset fyne.Position, frame fyne.Size) {
canvasMutex.Lock()
canvases[o] = c
canvasMutex.Unlock()
pos := o.Position().Add(offset)
switch obj := o.(type) {
case *canvas.Circle:
c.drawCircle(obj, pos, frame)
case *canvas.Line:
c.drawLine(obj, pos, frame)
case *canvas.Image:
c.drawImage(obj, pos, frame)
case *canvas.Raster:
c.drawRaster(obj, pos, frame)
case *canvas.Rectangle:
c.drawRectangle(obj, pos, frame)
case *canvas.Text:
c.drawText(obj, pos, frame)
case fyne.Widget:
c.drawWidget(obj, offset, frame)
}
}