Skip to content

Commit c4570b9

Browse files
committed
move input masking to call sites
1 parent 63a5e98 commit c4570b9

8 files changed

Lines changed: 365 additions & 107 deletions

File tree

‎.fat‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dist/demo/atlas.webp 1432 1457
22
dist/demo/index.css 69 94
3-
dist/demo/index.html 131367 64134
4-
dist/demo/index.js 126286 59779
5-
dist/demo/index.wasm 69448 37499
3+
dist/demo/index.html 131237 64302
4+
dist/demo/index.js 126156 59937
5+
dist/demo/index.wasm 69349 37404

‎src/internal/demo/game/cam_hooks.go‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,30 @@ func UpdateCam(gam *Game) veng.Status {
5252
stat |= veng.Loop
5353
lvlScaleChanged = true
5454
}
55-
if in.IsOnStart(vin.ButtonScaleReset) && gam.ResetLvlScaleAt(anchor) {
56-
stat |= veng.Loop
57-
lvlScaleChanged = true
55+
if in.IsOnStart(vin.ButtonScaleReset) {
56+
in.Mask |= vin.ButtonScaleReset
57+
if gam.ResetLvlScaleAt(anchor) {
58+
stat |= veng.Loop
59+
lvlScaleChanged = true
60+
}
5861
}
59-
if in.IsOnStart(vin.ButtonScaleDec) &&
60-
adjustLvlScaleAtKey(gam, anchor, -keyZoomDelta) {
61-
stat |= veng.Loop
62-
lvlScaleChanged = true
62+
if in.IsOnStart(vin.ButtonScaleDec) {
63+
in.Mask |= vin.ButtonScaleDec
64+
if adjustLvlScaleAtKey(gam, anchor, -keyZoomDelta) {
65+
stat |= veng.Loop
66+
lvlScaleChanged = true
67+
}
6368
}
64-
if in.IsOnStart(vin.ButtonScaleInc) &&
65-
adjustLvlScaleAtKey(gam, anchor, keyZoomDelta) {
66-
stat |= veng.Loop
67-
lvlScaleChanged = true
69+
if in.IsOnStart(vin.ButtonScaleInc) {
70+
in.Mask |= vin.ButtonScaleInc
71+
if adjustLvlScaleAtKey(gam, anchor, keyZoomDelta) {
72+
stat |= veng.Loop
73+
lvlScaleChanged = true
74+
}
6875
}
6976
d := camKeyVel * float32(gam.DeltaSecs()) * tiles.ScaleOrDefault()
70-
if in.IsOn(vin.ButtonC) {
77+
if dirOn && in.IsOn(vin.ButtonC) {
78+
in.Mask |= vin.ButtonC
7179
d *= 10
7280
}
7381
keyBy := vgeo.NewXY(float32(in.Dir.X)*d, float32(in.Dir.Y)*d)

‎src/internal/demo/game/cam_hooks_test.go‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,84 @@ func TestKeyZoomTarget(t *testing.T) {
9494
}
9595
}
9696

97+
func TestUpdateCamScaleMask(t *testing.T) {
98+
for _, test := range []struct {
99+
name string
100+
btn vin.Button
101+
scale, want float32
102+
}{
103+
{"reset", vin.ButtonScaleReset, 3, 4},
104+
{"decrease", vin.ButtonScaleDec, 3, 2.75},
105+
{"increase", vin.ButtonScaleInc, 3, 3.25},
106+
{"at limit", vin.ButtonScaleInc, lvlScaleMax, lvlScaleMax},
107+
} {
108+
t.Run(test.name, func(t *testing.T) {
109+
for _, masked := range []bool{false, true} {
110+
gam := NewGame()
111+
*gam.CanvasPhy() = vgeo.NewWH[uint16](2048, 1280)
112+
gam.LvlZoom = test.scale
113+
gam.UpdateLvlLayers()
114+
in := gam.In()
115+
in.On = test.btn | vin.ButtonA
116+
in.Mask = vin.ButtonB
117+
want := test.want
118+
if masked {
119+
in.Mask |= test.btn
120+
want = test.scale
121+
}
122+
for range 2 {
123+
UpdateCam(gam)
124+
if got := gam.LvlZoom; got != want {
125+
t.Errorf("masked=%v: scale = %v, want %v", masked, got, want)
126+
}
127+
if in.Mask != vin.ButtonB|test.btn {
128+
t.Errorf("masked=%v: Mask = %v, want only used buttons",
129+
masked, in.Mask)
130+
}
131+
}
132+
}
133+
})
134+
}
135+
}
136+
137+
func TestUpdateCamBoostMask(t *testing.T) {
138+
for _, test := range []struct {
139+
name string
140+
mask vin.Button
141+
kbdMode bool
142+
wantX float32
143+
claimed bool
144+
}{
145+
{"boost", vin.ButtonB, false, 40, true},
146+
{"masked", vin.ButtonC, false, 4, true},
147+
{"cursor keys", vin.ButtonB, true, 0, false},
148+
} {
149+
t.Run(test.name, func(t *testing.T) {
150+
gam := NewGame()
151+
*gam.Cam() = vgeo.XY[float32]{}
152+
gam.Layer(gfx.LayerTiles).Scale = 4
153+
gam.Poll().DeltaMillis = camKeyStepMillis()
154+
gam.SetCursor(&veng.CursorEnt{KbdEnabled: test.kbdMode})
155+
in := gam.In()
156+
in.Dir = vgeo.NewXY[int8](1, 0)
157+
in.DirOn = true
158+
in.On = vin.ButtonR | vin.ButtonC | vin.ButtonA
159+
in.Mask = test.mask
160+
UpdateCam(gam)
161+
if got := gam.Cam().X; got != test.wantX {
162+
t.Errorf("cam X = %v, want %v", got, test.wantX)
163+
}
164+
wantMask := test.mask
165+
if test.claimed {
166+
wantMask |= vin.ButtonC
167+
}
168+
if in.Mask != wantMask {
169+
t.Errorf("Mask = %v, want %v", in.Mask, wantMask)
170+
}
171+
})
172+
}
173+
}
174+
97175
func TestUpdateCamZoomEndSnapsNearInteger(t *testing.T) {
98176
tests := []struct {
99177
name string

‎src/void/veng/button_ent.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ func (this *ButtonEnt) Update(eng *Eng) Status {
8787
hi := layer.PhyToLayer(cursorPhy.Max)
8888
this.Focused = bounds.HitsBox(vgeo.Box[float32]{Min: lo, Max: hi})
8989
}
90-
offStart := this.Type == ButtonTypeToggle && this.Focused &&
91-
in.IsOffStart(vin.ButtonA)
90+
offStart := this.Focused && in.IsOffStart(vin.ButtonA)
9291
pressed := this.Focused && in.IsOn(vin.ButtonA)
9392
wasOn := this.On
9493
if this.Type == ButtonTypeToggle {
@@ -98,9 +97,12 @@ func (this *ButtonEnt) Update(eng *Eng) Status {
9897
} else {
9998
this.On = pressed
10099
}
101-
this.Start = wasOn != this.On
100+
this.Start = wasOn != this.On && (pressed || offStart)
101+
if pressed || this.Clicked() {
102+
in.Mask |= vin.ButtonA
103+
}
102104
loop := Pause
103-
if pressed {
105+
if pressed || wasOn != this.On {
104106
loop = Loop
105107
}
106108

‎src/void/veng/button_ent_test.go‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,96 @@ func TestButtonEnt_ToggleClicksOnOffStart(t *testing.T) {
107107
}
108108
}
109109

110+
func TestButtonEnt_MasksPrimary(t *testing.T) {
111+
for _, test := range []struct {
112+
name string
113+
typ ButtonType
114+
}{
115+
{"button", ButtonTypeButton},
116+
{"toggle", ButtonTypeToggle},
117+
} {
118+
t.Run(test.name, func(t *testing.T) {
119+
eng := newButtonTestEng(vgeo.XYWH[float32](12, 12, 1, 1))
120+
in := eng.In()
121+
in.MapKey(vin.KeyA, vin.ButtonA)
122+
ents := [2]ButtonEnt{{Type: test.typ}, {Type: test.typ}}
123+
clicks := [2]int{}
124+
for i := range ents {
125+
ents[i].XY = vgeo.NewXY[float32](10, 10)
126+
ents[i].WH = vgeo.NewWH[uint16](10, 10)
127+
ents[i].OnClick = func(*ButtonEnt) {
128+
if in.Mask&vin.ButtonA == 0 {
129+
t.Error("callback ran before claiming ButtonA")
130+
}
131+
clicks[i]++
132+
}
133+
}
134+
for now, keys := range []vin.Key{vin.KeyA, vin.KeyA, 0, 0} {
135+
in.Update(float64(now), &vin.InPoll{
136+
Kbd: vin.KeyboardPoll{Keys: keys},
137+
}, vgeo.Box[float32]{})
138+
for i := range ents {
139+
ents[i].Update(eng)
140+
}
141+
wantMask := vin.ButtonA
142+
if now == 3 {
143+
wantMask = 0
144+
}
145+
if in.Mask != wantMask {
146+
t.Errorf("update %d: Mask = %v, want %v", now, in.Mask, wantMask)
147+
}
148+
if ents[1].On || ents[1].Start {
149+
t.Errorf("update %d: second button acted on claimed input", now)
150+
}
151+
}
152+
if clicks != [2]int{1, 0} {
153+
t.Errorf("clicks = %v, want [1 0]", clicks)
154+
}
155+
})
156+
}
157+
}
158+
159+
func TestButtonEnt_MaskedInput(t *testing.T) {
160+
for _, typ := range []ButtonType{ButtonTypeButton, ButtonTypeToggle} {
161+
for _, test := range []struct {
162+
name string
163+
on bool
164+
held bool
165+
}{
166+
{"press", false, true},
167+
{"hold", true, true},
168+
{"release", true, false},
169+
} {
170+
t.Run(test.name, func(t *testing.T) {
171+
eng := newButtonTestEng(vgeo.XYWH[float32](12, 12, 1, 1))
172+
ent := ButtonEnt{Type: typ, On: test.on}
173+
ent.XY = vgeo.NewXY[float32](10, 10)
174+
ent.WH = vgeo.NewWH[uint16](10, 10)
175+
ent.OnClick = func(*ButtonEnt) { t.Error("masked input clicked") }
176+
in := eng.In()
177+
in.PrevOn = vin.ButtonA
178+
if test.held {
179+
in.On = vin.ButtonA
180+
}
181+
in.Mask = vin.ButtonA | vin.ButtonB
182+
stat := ent.Update(eng)
183+
if ent.Start || ent.Clicked() {
184+
t.Error("masked input reported a transition")
185+
}
186+
if typ == ButtonTypeButton && (ent.On || test.on && stat != Loop) {
187+
t.Error("masked input did not redraw the cancelled press")
188+
}
189+
if typ == ButtonTypeToggle && ent.On != test.on {
190+
t.Error("masked input changed toggle state")
191+
}
192+
if in.Mask != vin.ButtonA|vin.ButtonB {
193+
t.Errorf("Mask = %v, want ButtonA|ButtonB", in.Mask)
194+
}
195+
})
196+
}
197+
}
198+
}
199+
110200
func newButtonTestEng(cursorPhy vgeo.Box[float32]) *Eng {
111201
eng := NewEng(&EngOpts{MaxSprs: 1})
112202
eng.Layer(0).Clip = vgeo.NewBox[float32](0, 0, 100, 100)

‎src/void/veng/cursor_ent_test.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,37 @@ func TestUpdate_RestoresPointTag(t *testing.T) {
102102
t.Errorf("picked Tag = %d, want 2", got)
103103
}
104104
in.On = 0
105-
in.Mask = 0
106105
updateCursorEnt(&ent, in, &sprs, 0, &layer)
107106
if got := sprs[1].Tag(); got != 1 {
108107
t.Errorf("released Tag = %d, want 1", got)
109108
}
110109
}
111110

111+
func TestCursorEnt_DoesNotMaskInput(t *testing.T) {
112+
for _, mask := range []vin.Button{0, vin.ButtonA, vin.ButtonB} {
113+
eng := NewEng(&EngOpts{MaxSprs: 1})
114+
ent := NewCursorEnt(
115+
vboards.Spawn{Tag: 1}, 2, 10, vgeo.XYWH[uint16](0, 0, 1, 1),
116+
)
117+
ent.KbdEnabled = true
118+
in := eng.In()
119+
in.On = vin.ButtonA
120+
in.Mask = mask
121+
pressed := in.IsOnStart(vin.ButtonA)
122+
ent.Update(eng)
123+
wantTag := ent.pointTag
124+
if pressed {
125+
wantTag = ent.pickTag
126+
}
127+
if ent.Visible != pressed || ent.Spr.Tag() != wantTag {
128+
t.Errorf("Mask=%v: cursor did not reflect the available press", mask)
129+
}
130+
if in.Mask != mask || in.IsOnStart(vin.ButtonA) != pressed {
131+
t.Errorf("Mask=%v: cursor changed input state", mask)
132+
}
133+
}
134+
}
135+
112136
var defaultBounds = vgeo.XYWH[float32](-100, -100, 1000, 1000)
113137

114138
func moveCursorKey(

0 commit comments

Comments
 (0)