zntg/ui/spinner.go

100 lines
2.4 KiB
Go
Raw Normal View History

2018-08-03 06:46:10 +00:00
package ui
import (
"math"
"time"
"github.com/llgcode/draw2d/draw2dimg"
"opslag.de/schobers/zntg/allg5"
2018-08-03 06:46:10 +00:00
)
var _ Control = &Spinner{}
type Spinner struct {
ControlBase
Text string
spin float32
circs *allg5.Bitmap
2018-08-03 06:46:10 +00:00
}
func (s *Spinner) Created(ctx Context, p Container) error {
s.ControlBase.Created(ctx, p)
const row = 6
const numCircs = row * row
const width = 48
const center = width * .5
const full = 2 * math.Pi
s.circs = drawBitmap(row*width, row*width, func(gc *draw2dimg.GraphicContext) {
var start, a float64 = 0, .2 * math.Pi
var inc = true
gc.SetLineWidth(4)
gc.SetStrokeColor(ctx.Palette().Primary())
for i := 0; i < numCircs; i++ {
var left = float64(i%row) * width
var top = float64(i/row) * width
gc.ArcTo(left+center, top+center, center-8, center-8, start, a)
gc.Stroke()
if inc {
start += full / numCircs
a += 1.90 * full / numCircs
if a >= full {
inc = false
}
} else {
start += 2.90 * full / numCircs
a -= 1.90 * full / numCircs
// if a <= .1*math.Pi {
// inc = true
// }
}
}
})
for i := 0; i < numCircs; i++ {
var left = (i % row) * width
var top = (i / row) * width
s.circs.Sub(left, top, width, width)
2018-08-03 06:46:10 +00:00
}
return nil
}
func (s *Spinner) Destroyed(ctx Context) {
s.circs.Destroy()
2018-08-03 06:46:10 +00:00
}
func (s *Spinner) Update(ctx Context, dt time.Duration) {
var spin = float64(s.spin)
spin += dt.Seconds() * .5
2018-08-03 06:46:10 +00:00
for spin > 1. {
spin -= 1.
}
s.spin = float32(spin)
}
func (s *Spinner) Render(ctx Context) {
var disp = ctx.Display()
var fonts = ctx.Fonts()
var width = float32(disp.Width())
var height = float32(disp.Height())
var fnt = fonts.Get("default")
var textW = fnt.TextWidth(s.Text)
allg5.DrawFilledRectangle(0, 0, width, height, ctx.Palette().Disabled())
2018-08-03 06:46:10 +00:00
const marginH, marginV float32 = 64, 16
const textH float32 = 12
var rectW, rectH float32 = textW + 2*marginH, 3*marginV + textH + 32
var rectX, rectY = (width - rectW) * .5, (height - rectH) * .5
allg5.DrawFilledRectangle(rectX, rectY, rectX+rectW, rectY+rectH, ctx.Palette().Lightest())
2018-08-03 06:46:10 +00:00
DropShadow(rectX, rectY, rectX+rectW, rectY+rectH)
fnt.Draw(rectX+marginH, rectY+marginV, ctx.Palette().Darkest(), allg5.AlignLeft, s.Text)
2018-08-03 06:46:10 +00:00
const numCircs = 36
2018-08-03 06:46:10 +00:00
var i = int(math.Floor(float64(s.spin) * numCircs))
s.circs.Subs()[i].DrawOptions(width*.5, rectY+2*marginV+2*textH, allg5.DrawOptions{Center: true})
2018-08-03 06:46:10 +00:00
s.ControlBase.Render(ctx)
}