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.
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
// Package media plays, shows and records sound and pictures.
|
||
package media
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||
)
|
||
|
||
// Track is a song: title, artist and length.
|
||
type Track struct {
|
||
Title, Artist string
|
||
Length time.Duration
|
||
}
|
||
|
||
// mmss prints d as m:ss, or h:mm:ss past an hour.
|
||
func mmss(d time.Duration) string {
|
||
s := int(d / time.Second)
|
||
if s >= 3600 {
|
||
return fmt.Sprintf("%d:%02d:%02d", s/3600, s/60%60, s%60)
|
||
}
|
||
return fmt.Sprintf("%d:%02d", s/60, s%60)
|
||
}
|
||
|
||
// prev is Next turned around.
|
||
var prev = icon.Next.Mirror()
|
||
|
||
// scale is s, or def for zero; above 4 panics.
|
||
func scale(name string, s, def int) int {
|
||
s = pixel.Or(name+" Scale", s, def)
|
||
if s > 4 {
|
||
panic(fmt.Sprintf("media: %s Scale %d outside 1–4", name, s))
|
||
}
|
||
return s
|
||
}
|
||
|
||
// art is a named picture w by h dots.
|
||
func art(c *ui.Context, k pixel.Kit, label string, w, h int) *ui.Element {
|
||
if label == "" {
|
||
panic("media: a picture needs a name")
|
||
}
|
||
return ui.Box(c).Size(k.D(w), k.D(h)).Label(label).Role(ui.RoleImage)
|
||
}
|
||
|
||
// dark is the darkest slot and a line on it.
|
||
func dark(k pixel.Kit) (body, line pixel.Slot) {
|
||
if k.Palette().Night {
|
||
return pixel.Well, pixel.Face
|
||
}
|
||
return pixel.Ink, pixel.Faint
|
||
}
|
||
|
||
// sameSize panics unless every sprite matches the first.
|
||
func sameSize(name string, sps []*pixel.Sprite) {
|
||
if len(sps) == 0 {
|
||
panic("media: " + name + " needs pictures")
|
||
}
|
||
for _, sp := range sps {
|
||
if sp.W != sps[0].W || sp.H != sps[0].H {
|
||
panic(fmt.Sprintf("media: %s mixes %d×%d and %d×%d pictures", name, sps[0].W, sps[0].H, sp.W, sp.H))
|
||
}
|
||
}
|
||
}
|
||
|
||
type turnKey struct{}
|
||
|
||
// turn is steps taken, their first frame, and play.
|
||
type turn struct {
|
||
held, from int
|
||
playing bool
|
||
}
|
||
|
||
// turned counts steps while playing; stopped, it holds still.
|
||
func turned(c *ui.Context, k pixel.Kit, e *ui.Element, playing bool, fps int) int {
|
||
s := ui.Local(e, turnKey{}, func() turn { return turn{} })
|
||
on := playing && !k.Reduce()
|
||
if on {
|
||
n := k.Tick(c, fps)
|
||
// A clock stepping back rebases, so the count never falls.
|
||
if !s.playing || n-s.from < s.held {
|
||
s.from = n - s.held
|
||
}
|
||
s.held = n - s.from
|
||
}
|
||
s.playing = on
|
||
return s.held
|
||
}
|