package ui import ( "math" "time" "github.com/llgcode/draw2d/draw2dimg" "opslag.de/schobers/galleg/allegro5" ) var _ Control = &Spinner{} type Spinner struct { ControlBase Text string spin float32 circs *allegro5.Bitmap } 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) } return nil } func (s *Spinner) Destroyed(ctx Context) { s.circs.Destroy() } func (s *Spinner) Update(ctx Context, dt time.Duration) { var spin = float64(s.spin) spin += dt.Seconds() * .5 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) allegro5.DrawFilledRectangle(0, 0, width, height, ctx.Palette().Disabled()) 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 allegro5.DrawFilledRectangle(rectX, rectY, rectX+rectW, rectY+rectH, ctx.Palette().Lightest()) DropShadow(rectX, rectY, rectX+rectW, rectY+rectH) fnt.Draw(rectX+marginH, rectY+marginV, ctx.Palette().Darkest(), allegro5.AlignLeft, s.Text) const numCircs = 36 var i = int(math.Floor(float64(s.spin) * numCircs)) s.circs.Subs()[i].DrawOptions(width*.5, rectY+2*marginV+2*textH, allegro5.DrawOptions{Center: true}) s.ControlBase.Render(ctx) }