A cross-cutting pass, each fix with a test that failed first. Veils: MyGo still hands a key to a base that turned off this frame, so every element key now goes through pixel.Shortcut, which an archtest enforces. Input handlers record and the build applies them only while enabled; the editor turns away a veil-frame letter or copy; value bases get a copy and keep only enabled turns. Popups give a held focus back to their opener when the veil lifts. Motion: S5 is enforced at the clock. Every Tick, Since, Step, Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps, fast animations step at 15, and drawn time is quantized to frames. Clocks stepping back: Since holds at zero without overflow, and k.Elapsed moves a stored start to now, so nothing goes negative, runs backward or indexes out of range. Also: logs count typed text instead of holding it, gallery names use glyphs the font has, removable rows are keyed, components take their own Options (archtest), and sweeps over all 508 demos check veiled keys, clicks, wheels, the clock stepping back and names.
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
package datetime
|
||
|
||
import (
|
||
"math"
|
||
"time"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||
)
|
||
|
||
type AnalogClockOptions struct {
|
||
// Size is the face's width in dots; zero means 48.
|
||
Size int
|
||
}
|
||
|
||
// AnalogClock ticks each second; its hands are Bresenham lines.
|
||
func AnalogClock(c *ui.Context, o AnalogClockOptions) *ui.Element {
|
||
k := pixel.K(c)
|
||
size := pixel.Or("AnalogClock Size", o.Size, 48)
|
||
// Wake on the next whole second.
|
||
now := k.Now()
|
||
k.At(c, now.Truncate(time.Second).Add(time.Second))
|
||
h, m, s := now.Clock()
|
||
// hand ends at turn (0–1 from twelve) and length r.
|
||
hand := func(cx, cy int, turn float64, r int) (int, int) {
|
||
a := turn * 2 * math.Pi
|
||
return cx + int(math.Round(float64(r)*math.Sin(a))), cy - int(math.Round(float64(r)*math.Cos(a)))
|
||
}
|
||
return ui.Box(c).Size(k.D(size), k.D(size)).Role(ui.RoleImage).Label(now.Format("15:04:05")).Draw(func(p *ui.Painter, r ui.Rect) {
|
||
g := k.Canvas(p, r)
|
||
cx, cy, rad := size/2, size/2, size/2-1
|
||
g.Disc(cx, cy, rad, pixel.Face)
|
||
g.Circle(cx, cy, rad, pixel.Edge)
|
||
for i := range 12 {
|
||
x, y := hand(cx, cy, float64(i)/12, rad-3)
|
||
g.Dot(x, y, pixel.Edge)
|
||
}
|
||
x, y := hand(cx, cy, (float64(h%12)+float64(m)/60)/12, rad/2)
|
||
g.Line(cx, cy, x, y, pixel.Ink)
|
||
x, y = hand(cx, cy, (float64(m)+float64(s)/60)/60, rad-5)
|
||
g.Line(cx, cy, x, y, pixel.Ink)
|
||
x, y = hand(cx, cy, float64(s)/60, rad-4)
|
||
g.Line(cx, cy, x, y, pixel.Accent)
|
||
g.Dot(cx, cy, pixel.Accent)
|
||
})
|
||
}
|