@@ -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+
110200func newButtonTestEng (cursorPhy vgeo.Box [float32 ]) * Eng {
111201 eng := NewEng (& EngOpts {MaxSprs : 1 })
112202 eng .Layer (0 ).Clip = vgeo .NewBox [float32 ](0 , 0 , 100 , 100 )
0 commit comments