Add shop components 436-450; Odometer exact at 15 fps

Fifteen shop components: PriceTag, Wallet, ShopItem, Cart, Receipt,
Checkout, CurrencyConvert, SaleBadge, Quantity, PricingTiers, Coupon,
DailyReward, BidTicker, TradeWindow and VendingMachine.

Receipt prints dot by dot with feed holes and a torn edge; Coupon cuts
real notches; SaleBadge is a sixteen-point starburst; VendingMachine
sells A1 to D4 by code and drops the snack into its tray. Sums are
checked against overflow, TradeWindow withdraws both confirms on any
change to an offer, and a disabled Checkout veils its inert content.

text.Odometer now counts digits as text, steps at 15 fps as S5 asks,
draws exact digits at rest and lands rolls that floats would stall.
This commit is contained in:
ZacharyZhang-NY committed 2026-10-12 11:15:07 -04:00
1 parent c8d058799d
commit 5605d785de
28 files changed
+2420 -9

No files matched your search

+15 -7
View File
@@ -31,8 +31,9 @@ func Odometer(c *ui.Context, value int, o OdometerOptions) *ui.Element {
panic(fmt.Sprintf("text: Odometer value %d is negative", value))
}
k := pixel.K(c)
// Counted as text, so the largest int needs no float.
digits := pixel.Or("Odometer Digits", o.Digits, len(strconv.Itoa(value)))
if value >= int(math.Pow10(digits)) {
if len(strconv.Itoa(value)) > digits {
panic(fmt.Sprintf("text: Odometer value %d needs more than %d digits", value, digits))
}
h := pixel.LineHeight + 2
@@ -43,20 +44,25 @@ func Odometer(c *ui.Context, value int, o OdometerOptions) *ui.Element {
case k.Reduce() || st.shown == target:
st.shown, st.last = target, -1
default:
tick := k.Tick(c, 30)
// S5 caps steps at 15 fps; two rolls a step.
tick := k.Tick(c, 15)
if st.last >= 0 {
for range tick - st.last {
for range 2 * (tick - st.last) {
st.shown = roll(st.shown, target)
}
}
st.last = tick
}
shown := st.shown
// At rest digits come from the int; floats would round.
shown, settled, exact := st.shown, st.last < 0, fmt.Sprintf("%0*d", digits, value)
return e.Draw(func(p *ui.Painter, r ui.Rect) {
g := k.Canvas(p, r)
for i := range digits {
x := (digits - 1 - i) * odoCell
d, frac := wheel(shown, i)
d, frac := int(exact[digits-1-i]-'0'), 0.0
if !settled {
d, frac = wheel(shown, i)
}
g.Frame(x, 0, odoCell-1, h, pixel.Sunken, pixel.Well)
g.Clip(x+2, 2, odoCell-5, h-4, func() {
y := 1 - int(frac*float64(h))
@@ -71,10 +77,12 @@ func Odometer(c *ui.Context, value int, o OdometerOptions) *ui.Element {
func roll(shown, target float64) float64 {
diff := target - shown
stride := max(0.25, math.Abs(diff)/6)
if math.Abs(diff) <= stride {
// Near 2^53 a step may not move; then land.
next := shown + math.Copysign(stride, diff)
if math.Abs(diff) <= stride || next == shown {
return target
}
return shown + math.Copysign(stride, diff)
return next
}
// wheel is digit i of shown and its roll.
+69
View File
@@ -0,0 +1,69 @@
package text_test
import (
"bytes"
"math"
"testing"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/text"
)
func TestOdometerHoldsMaxIntAndRollsWithinS5(t *testing.T) {
clk := newClock()
view(400, 100, clk, func(c *ui.Context) { text.Odometer(c, math.MaxInt, text.OdometerOptions{}) })
value := 0
tt := view(400, 100, clk, func(c *ui.Context) { text.Odometer(c, value, text.OdometerOptions{Digits: 3}) })
value = 999
tt.Frame()
// S5: a second of rolling changes at most 15 times.
changes, last := 0, append([]byte(nil), tt.Image().Pix...)
for range 30 {
clk.t = clk.t.Add(time.Second / 30)
tt.Frame()
if !bytes.Equal(last, tt.Image().Pix) {
changes++
last = append(last[:0], tt.Image().Pix...)
}
}
if changes > 15 || changes == 0 {
t.Fatalf("the odometer changed %d times in a second", changes)
}
}
func TestOdometerShowsBigIntsExactly(t *testing.T) {
clk := newClock()
cell := func(v, digits int) []byte {
tt := view(400, 100, clk, func(c *ui.Context) { text.Odometer(c, v, text.OdometerOptions{Digits: digits}) })
img := tt.Image()
// The last digit's 10-dot cell, at two pixels a dot.
var out []byte
for y := 0; y < 2*18; y++ {
x0 := 2 * 10 * (digits - 1)
out = append(out, img.Pix[y*img.Stride+4*x0:y*img.Stride+4*(x0+20)]...)
}
return out
}
// 2^53+1 ends in 3; a float rounds it away.
if !bytes.Equal(cell(9007199254740993, 16), cell(3, 1)) {
t.Fatal("9007199254740993 does not end in a drawn 3")
}
}
func TestOdometerRollsHomeOnBigInts(t *testing.T) {
clk := newClock()
value := 0
tt := view(400, 100, clk, func(c *ui.Context) { text.Odometer(c, value, text.OdometerOptions{Digits: 16}) })
value = 9007199254740993
for range 400 {
clk.add(time.Second / 15)
tt.Frame()
}
// Floats stall short of 2^53+1; the roll must still land.
ref := view(400, 100, clk, func(c *ui.Context) { text.Odometer(c, 9007199254740993, text.OdometerOptions{Digits: 16}) })
if !bytes.Equal(tt.Image().Pix, ref.Image().Pix) {
t.Fatal("the roll never landed on 9007199254740993")
}
}
+2 -1
View File
@@ -337,7 +337,8 @@ func TestOdometerRollsToValue(t *testing.T) {
five := digits()
value = 6
tt.Frame()
clk.add(34 * time.Millisecond)
// One step at 15 fps, S5's fastest.
clk.add(67 * time.Millisecond)
tt.Frame()
mid := digits()
clk.add(time.Second)