forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.go
416 lines (351 loc) · 13.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package gl
import (
"image/color"
"math"
"github.com/williambrode/fyne/v2"
"github.com/williambrode/fyne/v2/canvas"
paint "github.com/williambrode/fyne/v2/internal/painter"
)
func (p *painter) createBuffer(points []float32) Buffer {
vbo := p.ctx.CreateBuffer()
p.logError()
p.ctx.BindBuffer(arrayBuffer, vbo)
p.logError()
p.ctx.BufferData(arrayBuffer, points, staticDraw)
p.logError()
return vbo
}
func (p *painter) defineVertexArray(prog Program, name string, size, stride, offset int) {
vertAttrib := p.ctx.GetAttribLocation(prog, name)
p.ctx.EnableVertexAttribArray(vertAttrib)
p.ctx.VertexAttribPointerWithOffset(vertAttrib, size, float, false, stride*floatSize, offset*floatSize)
p.logError()
}
func (p *painter) drawCircle(circle *canvas.Circle, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(circle, p.newGlCircleTexture, pos, circle.Size(), frame, canvas.ImageFillStretch,
1.0, paint.VectorPad(circle))
}
func (p *painter) drawGradient(o fyne.CanvasObject, texCreator func(fyne.CanvasObject) Texture, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(o, texCreator, pos, o.Size(), frame, canvas.ImageFillStretch, 1.0, 0)
}
func (p *painter) drawImage(img *canvas.Image, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(img, p.newGlImageTexture, pos, img.Size(), frame, img.FillMode, float32(img.Alpha()), 0)
}
func (p *painter) drawLine(line *canvas.Line, pos fyne.Position, frame fyne.Size) {
if line.StrokeColor == color.Transparent || line.StrokeColor == nil || line.StrokeWidth == 0 {
return
}
points, halfWidth, feather := p.lineCoords(pos, line.Position1, line.Position2, line.StrokeWidth, 0.5, frame)
p.ctx.UseProgram(p.lineProgram)
vbo := p.createBuffer(points)
p.defineVertexArray(p.lineProgram, "vert", 2, 4, 0)
p.defineVertexArray(p.lineProgram, "normal", 2, 4, 2)
p.ctx.BlendFunc(srcAlpha, oneMinusSrcAlpha)
p.logError()
colorUniform := p.ctx.GetUniformLocation(p.lineProgram, "color")
r, g, b, a := getFragmentColor(line.StrokeColor)
p.ctx.Uniform4f(colorUniform, r, g, b, a)
lineWidthUniform := p.ctx.GetUniformLocation(p.lineProgram, "lineWidth")
p.ctx.Uniform1f(lineWidthUniform, halfWidth)
featherUniform := p.ctx.GetUniformLocation(p.lineProgram, "feather")
p.ctx.Uniform1f(featherUniform, feather)
p.ctx.DrawArrays(triangles, 0, 6)
p.logError()
p.freeBuffer(vbo)
}
func (p *painter) drawObject(o fyne.CanvasObject, pos fyne.Position, frame fyne.Size) {
switch obj := o.(type) {
case *canvas.Circle:
p.drawCircle(obj, pos, frame)
case *canvas.Line:
p.drawLine(obj, pos, frame)
case *canvas.Image:
p.drawImage(obj, pos, frame)
case *canvas.Raster:
p.drawRaster(obj, pos, frame)
case *canvas.Rectangle:
p.drawRectangle(obj, pos, frame)
case *canvas.Text:
p.drawText(obj, pos, frame)
case *canvas.LinearGradient:
p.drawGradient(obj, p.newGlLinearGradientTexture, pos, frame)
case *canvas.RadialGradient:
p.drawGradient(obj, p.newGlRadialGradientTexture, pos, frame)
}
}
func (p *painter) drawRaster(img *canvas.Raster, pos fyne.Position, frame fyne.Size) {
p.drawTextureWithDetails(img, p.newGlRasterTexture, pos, img.Size(), frame, canvas.ImageFillStretch, float32(img.Alpha()), 0)
}
func (p *painter) drawRectangle(rect *canvas.Rectangle, pos fyne.Position, frame fyne.Size) {
if (rect.FillColor == color.Transparent || rect.FillColor == nil) && (rect.StrokeColor == color.Transparent || rect.StrokeColor == nil || rect.StrokeWidth == 0) {
return
}
roundedCorners := rect.CornerRadius != 0
var program Program
if roundedCorners {
program = p.roundRectangleProgram
} else {
program = p.rectangleProgram
}
// Vertex: BEG
bounds, points := p.vecRectCoords(pos, rect, frame)
p.ctx.UseProgram(program)
vbo := p.createBuffer(points)
p.defineVertexArray(program, "vert", 2, 4, 0)
p.defineVertexArray(program, "normal", 2, 4, 2)
p.ctx.BlendFunc(srcAlpha, oneMinusSrcAlpha)
p.logError()
// Vertex: END
// Fragment: BEG
frameSizeUniform := p.ctx.GetUniformLocation(program, "frame_size")
frameWidthScaled, frameHeightScaled := p.scaleFrameSize(frame)
p.ctx.Uniform2f(frameSizeUniform, frameWidthScaled, frameHeightScaled)
rectCoordsUniform := p.ctx.GetUniformLocation(program, "rect_coords")
x1Scaled, x2Scaled, y1Scaled, y2Scaled := p.scaleRectCoords(bounds[0], bounds[2], bounds[1], bounds[3])
p.ctx.Uniform4f(rectCoordsUniform, x1Scaled, x2Scaled, y1Scaled, y2Scaled)
strokeWidthScaled := roundToPixel(rect.StrokeWidth*p.pixScale, 1.0)
if roundedCorners {
strokeUniform := p.ctx.GetUniformLocation(program, "stroke_width_half")
p.ctx.Uniform1f(strokeUniform, strokeWidthScaled*0.5)
rectSizeUniform := p.ctx.GetUniformLocation(program, "rect_size_half")
rectSizeWidthScaled := x2Scaled - x1Scaled - strokeWidthScaled
rectSizeHeightScaled := y2Scaled - y1Scaled - strokeWidthScaled
p.ctx.Uniform2f(rectSizeUniform, rectSizeWidthScaled*0.5, rectSizeHeightScaled*0.5)
radiusUniform := p.ctx.GetUniformLocation(program, "radius")
radiusScaled := roundToPixel(rect.CornerRadius*p.pixScale, 1.0)
p.ctx.Uniform1f(radiusUniform, radiusScaled)
} else {
strokeUniform := p.ctx.GetUniformLocation(program, "stroke_width")
p.ctx.Uniform1f(strokeUniform, strokeWidthScaled)
}
var r, g, b, a float32
fillColorUniform := p.ctx.GetUniformLocation(program, "fill_color")
r, g, b, a = getFragmentColor(rect.FillColor)
p.ctx.Uniform4f(fillColorUniform, r, g, b, a)
strokeColorUniform := p.ctx.GetUniformLocation(program, "stroke_color")
strokeColor := rect.StrokeColor
if strokeColor == nil {
strokeColor = color.Transparent
}
r, g, b, a = getFragmentColor(strokeColor)
p.ctx.Uniform4f(strokeColorUniform, r, g, b, a)
p.logError()
// Fragment: END
p.ctx.DrawArrays(triangleStrip, 0, 4)
p.logError()
p.freeBuffer(vbo)
}
func (p *painter) drawText(text *canvas.Text, pos fyne.Position, frame fyne.Size) {
if text.Text == "" || 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 containerSize.Height > size.Height {
pos = fyne.NewPos(pos.X, pos.Y+(containerSize.Height-size.Height)/2)
}
// text size is sensitive to position on screen
size, _ = roundToPixelCoords(size, text.Position(), p.pixScale)
size.Width += roundToPixel(paint.VectorPad(text), p.pixScale)
p.drawTextureWithDetails(text, p.newGlTextTexture, pos, size, frame, canvas.ImageFillStretch, 1.0, 0)
}
func (p *painter) drawTextureWithDetails(o fyne.CanvasObject, creator func(canvasObject fyne.CanvasObject) Texture,
pos fyne.Position, size, frame fyne.Size, fill canvas.ImageFill, alpha float32, pad float32) {
texture, err := p.getTexture(o, creator)
if err != nil {
return
}
aspect := float32(0)
if img, ok := o.(*canvas.Image); ok {
aspect = img.Aspect()
if aspect == 0 {
aspect = 1 // fallback, should not occur - normally an image load error
}
}
points := p.rectCoords(size, pos, frame, fill, aspect, pad)
p.ctx.UseProgram(p.program)
vbo := p.createBuffer(points)
p.defineVertexArray(p.program, "vert", 3, 5, 0)
p.defineVertexArray(p.program, "vertTexCoord", 2, 5, 3)
// here we have to choose between blending the image alpha or fading it...
// TODO find a way to support both
if alpha != 1.0 {
p.ctx.BlendColor(0, 0, 0, alpha)
p.ctx.BlendFunc(constantAlpha, oneMinusConstantAlpha)
} else {
p.ctx.BlendFunc(one, oneMinusSrcAlpha)
}
p.logError()
p.ctx.ActiveTexture(texture0)
p.ctx.BindTexture(texture2D, texture)
p.logError()
p.ctx.DrawArrays(triangleStrip, 0, 4)
p.logError()
p.freeBuffer(vbo)
}
func (p *painter) freeBuffer(vbo Buffer) {
p.ctx.BindBuffer(arrayBuffer, noBuffer)
p.logError()
p.ctx.DeleteBuffer(vbo)
p.logError()
}
func (p *painter) lineCoords(pos, pos1, pos2 fyne.Position, lineWidth, feather float32, frame fyne.Size) ([]float32, float32, float32) {
// Shift line coordinates so that they match the target position.
xPosDiff := pos.X - fyne.Min(pos1.X, pos2.X)
yPosDiff := pos.Y - fyne.Min(pos1.Y, pos2.Y)
pos1.X = roundToPixel(pos1.X+xPosDiff, p.pixScale)
pos1.Y = roundToPixel(pos1.Y+yPosDiff, p.pixScale)
pos2.X = roundToPixel(pos2.X+xPosDiff, p.pixScale)
pos2.Y = roundToPixel(pos2.Y+yPosDiff, p.pixScale)
if lineWidth <= 1 {
offset := float32(0.5) // adjust location for lines < 1pt on regular display
if lineWidth <= 0.5 && p.pixScale > 1 { // and for 1px drawing on HiDPI (width 0.5)
offset = 0.25
}
if pos1.X == pos2.X {
pos1.X -= offset
pos2.X -= offset
}
if pos1.Y == pos2.Y {
pos1.Y -= offset
pos2.Y -= offset
}
}
x1Pos := pos1.X / frame.Width
x1 := -1 + x1Pos*2
y1Pos := pos1.Y / frame.Height
y1 := 1 - y1Pos*2
x2Pos := pos2.X / frame.Width
x2 := -1 + x2Pos*2
y2Pos := pos2.Y / frame.Height
y2 := 1 - y2Pos*2
normalX := (pos2.Y - pos1.Y) / frame.Width
normalY := (pos2.X - pos1.X) / frame.Height
dirLength := float32(math.Sqrt(float64(normalX*normalX + normalY*normalY)))
normalX /= dirLength
normalY /= dirLength
normalObjX := normalX * 0.5 * frame.Width
normalObjY := normalY * 0.5 * frame.Height
widthMultiplier := float32(math.Sqrt(float64(normalObjX*normalObjX + normalObjY*normalObjY)))
halfWidth := (roundToPixel(lineWidth+feather, p.pixScale) * 0.5) / widthMultiplier
featherWidth := feather / widthMultiplier
return []float32{
// coord x, y normal x, y
x1, y1, normalX, normalY,
x2, y2, normalX, normalY,
x2, y2, -normalX, -normalY,
x2, y2, -normalX, -normalY,
x1, y1, normalX, normalY,
x1, y1, -normalX, -normalY,
}, halfWidth, featherWidth
}
// rectCoords calculates the openGL coordinate space of a rectangle
func (p *painter) rectCoords(size fyne.Size, pos fyne.Position, frame fyne.Size,
fill canvas.ImageFill, aspect float32, pad float32) []float32 {
size, pos = rectInnerCoords(size, pos, fill, aspect)
size, pos = roundToPixelCoords(size, pos, p.pixScale)
xPos := (pos.X - pad) / frame.Width
x1 := -1 + xPos*2
x2Pos := (pos.X + size.Width + pad) / frame.Width
x2 := -1 + x2Pos*2
yPos := (pos.Y - pad) / frame.Height
y1 := 1 - yPos*2
y2Pos := (pos.Y + size.Height + pad) / frame.Height
y2 := 1 - y2Pos*2
return []float32{
// coord x, y, z 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
}
}
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 := size.Width / size.Height
newWidth, newHeight := size.Width, size.Height
widthPad, heightPad := float32(0), float32(0)
if viewAspect > aspect {
newWidth = size.Height * aspect
widthPad = (size.Width - newWidth) / 2
} else if viewAspect < aspect {
newHeight = size.Width / aspect
heightPad = (size.Height - newHeight) / 2
}
return fyne.NewSize(newWidth, newHeight), fyne.NewPos(pos.X+widthPad, pos.Y+heightPad)
}
return size, pos
}
func (p *painter) vecRectCoords(pos fyne.Position, rect *canvas.Rectangle, frame fyne.Size) ([4]float32, []float32) {
size := rect.Size()
pos1 := rect.Position()
xPosDiff := pos.X - pos1.X
yPosDiff := pos.Y - pos1.Y
pos1.X = roundToPixel(pos1.X+xPosDiff, p.pixScale)
pos1.Y = roundToPixel(pos1.Y+yPosDiff, p.pixScale)
size.Width = roundToPixel(size.Width, p.pixScale)
size.Height = roundToPixel(size.Height, p.pixScale)
x1Pos := pos1.X
x1Norm := -1 + x1Pos*2/frame.Width
x2Pos := pos1.X + size.Width
x2Norm := -1 + x2Pos*2/frame.Width
y1Pos := pos1.Y
y1Norm := 1 - y1Pos*2/frame.Height
y2Pos := pos1.Y + size.Height
y2Norm := 1 - y2Pos*2/frame.Height
// output a norm for the fill and the vert is unused, but we pass 0 to avoid optimisation issues
coords := []float32{
0, 0, x1Norm, y1Norm, // first triangle
0, 0, x2Norm, y1Norm, // second triangle
0, 0, x1Norm, y2Norm,
0, 0, x2Norm, y2Norm}
return [4]float32{x1Pos, y1Pos, x2Pos, y2Pos}, coords
}
func roundToPixel(v float32, pixScale float32) float32 {
if pixScale == 1.0 {
return float32(math.Round(float64(v)))
}
return float32(math.Round(float64(v*pixScale))) / pixScale
}
func roundToPixelCoords(size fyne.Size, pos fyne.Position, pixScale float32) (fyne.Size, fyne.Position) {
end := pos.Add(size)
end.X = roundToPixel(end.X, pixScale)
end.Y = roundToPixel(end.Y, pixScale)
pos.X = roundToPixel(pos.X, pixScale)
pos.Y = roundToPixel(pos.Y, pixScale)
size.Width = end.X - pos.X
size.Height = end.Y - pos.Y
return size, pos
}
// Returns FragmentColor(red,green,blue,alpha) from fyne.Color
func getFragmentColor(col color.Color) (float32, float32, float32, float32) {
if col == nil {
return 0, 0, 0, 0
}
r, g, b, a := col.RGBA()
if a == 0 {
return 0, 0, 0, 0
}
alpha := float32(a)
return float32(r) / alpha, float32(g) / alpha, float32(b) / alpha, alpha / 0xffff
}
func (p *painter) scaleFrameSize(frame fyne.Size) (float32, float32) {
frameWidthScaled := roundToPixel(frame.Width*p.pixScale, 1.0)
frameHeightScaled := roundToPixel(frame.Height*p.pixScale, 1.0)
return frameWidthScaled, frameHeightScaled
}
// Returns scaled RectCoords(x1,x2,y1,y2) in same order
func (p *painter) scaleRectCoords(x1, x2, y1, y2 float32) (float32, float32, float32, float32) {
x1Scaled := roundToPixel(x1*p.pixScale, 1.0)
x2Scaled := roundToPixel(x2*p.pixScale, 1.0)
y1Scaled := roundToPixel(y1*p.pixScale, 1.0)
y2Scaled := roundToPixel(y2*p.pixScale, 1.0)
return x1Scaled, x2Scaled, y1Scaled, y2Scaled
}