forked from Noofbiz/engoBox2dSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_test.go
708 lines (584 loc) · 18.2 KB
/
mouse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package engoBox2dSystem
import (
"bytes"
"log"
"strings"
"testing"
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
"engo.io/engo/math"
"engo.io/gl"
"github.com/ByteArena/box2d"
)
type MouseTestScene struct {
entityCount int
}
func (*MouseTestScene) Preload() {}
func (s *MouseTestScene) Setup(u engo.Updater) {
w, _ := u.(*ecs.World)
// Add systems to the world
sys = &MouseSystem{}
w.AddSystem(&common.CameraSystem{})
w.AddSystem(sys)
//Add some entities
basics = make([]ecs.BasicEntity, 0)
for i := 0; i < s.entityCount; i++ {
basic := ecs.NewBasic()
basics = append(basics, basic)
entity := mouseEntity{&basic, &MouseComponent{}, &common.SpaceComponent{}, nil, &Box2dComponent{}}
entity.SpaceComponent = &common.SpaceComponent{
Position: engo.Point{X: float32(i * 20), Y: 0},
Width: 10,
Height: 10,
}
entityBodyDef := box2d.NewB2BodyDef()
entityBodyDef.Type = box2d.B2BodyType.B2_dynamicBody
entityBodyDef.Position = Conv.ToBox2d2Vec(entity.SpaceComponent.Center())
entityBodyDef.Angle = Conv.DegToRad(entity.SpaceComponent.Rotation)
entity.Box2dComponent.Body = World.CreateBody(entityBodyDef)
var entityShape box2d.B2PolygonShape
entityShape.SetAsBox(Conv.PxToMeters(entity.SpaceComponent.Width/2),
Conv.PxToMeters(entity.SpaceComponent.Height/2))
entityFixtureDef := box2d.B2FixtureDef{
Shape: &entityShape,
Density: 1,
Friction: 1,
}
entity.Box2dComponent.Body.CreateFixtureFromDef(&entityFixtureDef)
for _, system := range w.Systems() {
switch sys := system.(type) {
case *MouseSystem:
sys.Add(entity.BasicEntity, entity.MouseComponent, entity.SpaceComponent, nil, entity.Box2dComponent)
}
}
}
}
func (*MouseTestScene) Type() string { return "MouseTestScene" }
var (
sys *MouseSystem
basics []ecs.BasicEntity
)
// Should be able to add entities to the system
func TestMouseSystemAdd(t *testing.T) {
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{5})
if len(sys.entities) != 5 {
t.Errorf("Entity count does not match number added, have: %d, want: %d", len(sys.entities), 5)
}
}
// Should be able to remove entities from the systems
func TestMouseSystemRemove(t *testing.T) {
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{5})
sys.Remove(basics[1])
sys.Remove(basics[2])
if len(sys.entities) != 3 {
t.Errorf("Entity count does not match number removed, have %d, want %d", len(sys.entities), 3)
}
// removing an entity not in the system should do nothing
sys.Remove(basics[2])
if len(sys.entities) != 3 {
t.Errorf("Removing an entity not in the system changed the count, have %d, want %d", len(sys.entities), 3)
}
// make sure only the IDs that were removed were the ones removed
ids := make(map[uint64]struct{})
ids[basics[0].ID()] = struct{}{}
ids[basics[3].ID()] = struct{}{}
ids[basics[4].ID()] = struct{}{}
for _, e := range sys.entities {
_, ok := ids[e.ID()]
if !ok {
t.Errorf("Entity in system has ID other than ones that should remain")
}
}
}
// when the system is added before a CameraSystem is, an error is logged
func TestMouseSystemNewNoCamera(t *testing.T) {
var str bytes.Buffer
log.SetOutput(&str)
expected := "ERROR: CameraSystem not found - have you added the `RenderSystem` before the `MouseSystem`?\n"
w := ecs.World{}
w.AddSystem(&MouseSystem{})
if !strings.HasSuffix(str.String(), expected) {
t.Errorf("Log did not recieve correct message suffix, have: %v, wanted (suffix): %v", str.String(), expected)
}
}
// Test hovering
func TestMouseSystemUpdateHoverint(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{2})
for i := 0; i < 2; i++ {
//Place mouse inside entity
engo.Input.Mouse.X = float32(5 + (20 * i))
engo.Input.Mouse.Y = 5
//Update the systems
sys.Update(updateTime)
//The one inside should be Entered for the first frame
for _, e := range sys.entities {
if e.ID() == basics[i].ID() {
if !e.MouseComponent.Enter {
t.Errorf("mouse component not updated to enter on frame mouse entered it; entity: %v", i)
}
if !e.MouseComponent.Hovered {
t.Errorf("mouse component not updated to hover on frame mouse entered it; entity: %v", i)
}
} else {
if e.MouseComponent.Enter {
t.Errorf("mouse component says entered when it should not; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component says hovered when it should not; entity: %v", i)
}
}
}
//Update again, this should remove the entered but keep Hovered
sys.Update(updateTime)
for _, e := range sys.entities {
if e.ID() == basics[i].ID() {
if e.MouseComponent.Enter {
t.Errorf("mouse component not updated to not enter on second frame since mouse entered it; entity: %v", i)
}
if !e.MouseComponent.Hovered {
t.Errorf("mouse component not updated to hover on second frame mouse since entered it; entity: %v", i)
}
} else {
if e.MouseComponent.Enter {
t.Errorf("mouse component says entered when it should not; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component says hovered when it should not; entity: %v", i)
}
}
}
//Move mouse out
engo.Input.Mouse.X = 20
engo.Input.Mouse.Y = 20
//Update the system, this should give exit signal
sys.Update(updateTime)
for _, e := range sys.entities {
if e.ID() == basics[i].ID() {
if !e.MouseComponent.Leave {
t.Errorf("mouse component not updated to leave on frame mouse left it; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component not updated to not hover on frame mouse left it; entity: %v", i)
}
} else {
if e.MouseComponent.Leave {
t.Errorf("mouse component says leave when it should not; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component says hovered when it should not; entity: %v", i)
}
}
}
//One more update, should remove leavee too
sys.Update(updateTime)
for _, e := range sys.entities {
if e.ID() == basics[i].ID() {
if e.MouseComponent.Leave {
t.Errorf("mouse component not updated to not leave on second frame since mouse left it; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component not updated to not hover by second frame since mouse left it; entity: %v", i)
}
} else {
if e.MouseComponent.Leave {
t.Errorf("mouse component says leave when it should not; entity: %v", i)
}
if e.MouseComponent.Hovered {
t.Errorf("mouse component says hovered when it should not; entity: %v", i)
}
}
}
}
}
// Camera movements
func TestMouseSystemCameraMove(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{1})
shifts := [][]float32{
[]float32{5, 5, 0, 0, 0, 15},
[]float32{5, 5, 5, 0, 0, 0},
[]float32{5, 5, 0, 5, 0, 0},
[]float32{5, 5, 0, 0, 2, 0},
[]float32{5, 5, 5, 5, 0, 15},
[]float32{5, 5, 5, 0, 2, 0},
[]float32{5, 5, 0, 5, 2, 0},
[]float32{5, 5, 5, 5, 2, 0},
[]float32{5, 5, 5, 5, 2, 15},
[]float32{5, 5, 5, 5, 2, 15},
[]float32{5, 5, 5, 5, 2, 15},
}
for i := 0; i < len(shifts); i++ {
//To test the backend
if i == len(shifts)-3 {
engo.CurrentBackEnd = engo.BackEndMobile
} else if i == len(shifts)-2 {
engo.CurrentBackEnd = engo.BackEndWeb
} else {
engo.CurrentBackEnd = engo.BackEndGLFW
}
expX, expY := cameraShift(shifts[i][0], shifts[i][1], shifts[i][2], shifts[i][3], shifts[i][4], shifts[i][5])
sys.camera.Update(updateTime)
sys.Update(updateTime)
if expX != sys.mouseX {
t.Errorf("mouse X did not match expected X, want %v, have %v, test case %d", expX, sys.mouseX, i)
}
if expY != sys.mouseY {
t.Errorf("mouse Y did not match expected Y, want %v, have %v, test case %d", expY, sys.mouseY, i)
}
resetCamera()
}
}
func cameraShift(origX, origY, transX, transY, transZ, rotation float32) (expectedX, expectedY float32) {
engo.Input.Mouse.X = origX
engo.Input.Mouse.Y = origY
expectedX = origX
expectedY = origY
if transZ != 0 {
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.ZAxis,
Value: transZ,
})
expectedX = expectedX*sys.camera.Z() - (engo.GameWidth() * (sys.camera.Z() - 1) / 2)
expectedY = expectedY*sys.camera.Z() - (engo.GameHeight() * (sys.camera.Z() - 1) / 2)
}
if transX != 0 {
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.XAxis,
Value: transX,
Incremental: true,
})
expectedX += transX
}
if transY != 0 {
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.YAxis,
Value: transY,
Incremental: true,
})
expectedY += transY
}
if rotation != 0 {
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.Angle,
Value: rotation,
})
sin, cos := math.Sincos(rotation * math.Pi / 180)
expectedX, expectedY = expectedX*cos+expectedY*sin, expectedY*cos-expectedX*sin
}
return
}
func resetCamera() {
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.XAxis,
Value: common.CameraBounds.Max.X / 2,
})
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.YAxis,
Value: common.CameraBounds.Max.Y / 2,
})
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.ZAxis,
Value: 1,
})
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.Angle,
Value: 0,
})
}
//Tracking should update the mouseEntity whenever the mouse moves
func TestMouseSystemTracking(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{3})
//add tracking to two entities
sys.entities[0].Track = true
sys.entities[2].Track = true
moves := []float32{
15, 15,
5, 5,
25, 5,
45, 5,
}
for i := 0; i < len(moves); i += 2 {
//move the mouse
engo.Input.Mouse.X = moves[i]
engo.Input.Mouse.Y = moves[i+1]
//update
sys.Update(updateTime)
//check X of each entity
for _, e := range sys.entities {
if e.Track {
if e.MouseX != engo.Input.Mouse.X {
t.Errorf("Tracking mouse X doesn't match input, tracking: %v, mouse: %v", e.MouseX, engo.Input.Mouse.X)
}
if e.MouseY != engo.Input.Mouse.Y {
t.Errorf("Tracking mouse Y doesn't match input, tracking: %v, mouse: %v", e.MouseY, engo.Input.Mouse.Y)
}
} else {
if e.MouseComponent.Hovered {
if e.MouseX != engo.Input.Mouse.X {
t.Errorf("While hovering, mouse should be tracked. for X, tracking: %v, mouse: %v", e.MouseX, engo.Input.Mouse.X)
}
if e.MouseY != engo.Input.Mouse.Y {
t.Errorf("While hovering, mouse should be tracked. for Y, tracking: %v, mouse: %v", e.MouseY, engo.Input.Mouse.Y)
}
} else {
if e.MouseX == engo.Input.Mouse.X {
t.Errorf("Mouse should not be tracked, for X, tracking: %v, mouse: %v", e.MouseX, engo.Input.Mouse.X)
}
if e.MouseY == engo.Input.Mouse.Y {
t.Errorf("Mouse should not be tracked, for Y, tracking: %v, mouse: %v", e.MouseX, engo.Input.Mouse.Y)
}
}
}
}
}
}
// If there's no SpaceComponent, the mouse should not updated
func TestMouseSystemSpaceAndBoxComponentNil(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{3})
// set components to nil
sys.entities[0].SpaceComponent = nil
sys.entities[2].SpaceComponent = nil
sys.entities[1].Box2dComponent = nil
sys.entities[2].Box2dComponent = nil
// hover over them and check for component being updated
for i, e := range sys.entities {
// input setup
engo.Input.Mouse.X = float32(i*20 + 5)
engo.Input.Mouse.Y = float32(i*20 + 5)
// update
sys.Update(updateTime)
// Check
if e.Hovered {
t.Errorf("updated to hovering even though there is no required component, entity: %d", i)
}
}
}
// testDrawable implements the common.Drawable interface without actually using
// the gl context or gpu for testing headless
type testDrawable struct {
width, height float32
}
func (d *testDrawable) Close() {}
func (d *testDrawable) Width() float32 { return d.width }
func (d *testDrawable) Height() float32 { return d.height }
func (d *testDrawable) Texture() *gl.Texture { return nil }
func (d *testDrawable) View() (float32, float32, float32, float32) { return 0, 0, 1, 1 }
func TestMouseSystemRenderComponent(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{3})
drawable := &testDrawable{width: 10, height: 10}
sys.entities[0].RenderComponent = &common.RenderComponent{
Drawable: drawable,
}
engo.Input.Mouse.X = 5
engo.Input.Mouse.Y = 5
sys.Update(updateTime)
if sys.entities[0].Hovered != true {
t.Error("render component test, entity 0 should be hovered but was not")
}
//Hidden
sys.entities[1].RenderComponent = &common.RenderComponent{
Drawable: drawable,
Hidden: true,
}
engo.Input.Mouse.X = 25
engo.Input.Mouse.Y = 5
sys.Update(updateTime)
if sys.entities[1].Hovered == true {
t.Error("render component test, entity 1 should not be hovered but was")
}
//hud
sys.entities[2].RenderComponent = &common.RenderComponent{
Drawable: drawable,
}
sys.entities[2].MouseComponent.IsHUDShader = true
engo.Input.Mouse.X = 45
engo.Input.Mouse.Y = 5
engo.Mailbox.Dispatch(common.CameraMessage{
Axis: common.XAxis,
Value: 25,
Incremental: true,
})
sys.Update(updateTime)
if sys.entities[2].Hovered != true {
t.Error("render component test, entity 2 should be hovered but was not")
}
}
// test mouse button presses
func TestMouseSystemButtonPresses(t *testing.T) {
updateTime := float32(1.0 / 60.0)
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{1})
//Place cursor outside entity
engo.Input.Mouse.X = 15
engo.Input.Mouse.Y = 15
//Left click
engo.Input.Mouse.Button = engo.MouseButtonLeft
sys.Update(updateTime)
//Check that it didn't click it
if sys.entities[0].Clicked {
t.Error("Entity was left clicked even though the cursor was not over it")
}
//Click on the inside of the entity
engo.Input.Mouse.X = 5
engo.Input.Mouse.Y = 5
engo.Input.Mouse.Button = engo.MouseButtonLeft
engo.Input.Mouse.Action = engo.Press
sys.Update(updateTime)
if !sys.entities[0].Clicked {
t.Error("Entity was not left clicked with cursor over it")
}
if !sys.entities[0].startedDragging {
t.Error("Entity was not started dragging when left clicked with cursor over it")
}
if !sys.mouseDown {
t.Error("System did not change to mousedown when mouse left clicked")
}
//Move mouse
engo.Input.Mouse.X = 8
engo.Input.Mouse.Y = 8
engo.Input.Mouse.Action = engo.Move
sys.Update(updateTime)
if !sys.entities[0].Dragged {
t.Error("Entity was not left dragged")
}
//Release
engo.Input.Mouse.Action = engo.Release
sys.Update(updateTime)
if !sys.entities[0].Released {
t.Error("Entity was not left released when mouse was released")
}
if sys.entities[0].Dragged {
t.Error("Entity was still dragged when mouse was released")
}
if sys.entities[0].startedDragging {
t.Error("Entity was still startedDragging when mouse was released")
}
if sys.mouseDown {
t.Error("System was still mouseDown when mouse was released")
}
//Place cursor outside entity
engo.Input.Mouse.X = 15
engo.Input.Mouse.Y = 15
//Right click
engo.Input.Mouse.Button = engo.MouseButtonRight
sys.Update(updateTime)
//Check that it didn't click it
if sys.entities[0].RightClicked {
t.Error("Entity was right clicked even though the cursor was not over it")
}
//Click on the inside of the entity
engo.Input.Mouse.X = 5
engo.Input.Mouse.Y = 5
engo.Input.Mouse.Button = engo.MouseButtonRight
engo.Input.Mouse.Action = engo.Press
sys.Update(updateTime)
if !sys.entities[0].RightClicked {
t.Error("Entity was not right clicked with cursor over it")
}
if !sys.entities[0].rightStartedDragging {
t.Error("Entity was not right started dragging when right clicked with cursor over it")
}
if !sys.rightMouseDown {
t.Error("System did not change to rightMousedown when mouse right clicked")
}
//Move mouse
engo.Input.Mouse.X = 8
engo.Input.Mouse.Y = 8
engo.Input.Mouse.Action = engo.Move
sys.Update(updateTime)
if !sys.entities[0].RightDragged {
t.Error("Entity was not right dragged")
}
//Release
engo.Input.Mouse.Action = engo.Release
sys.Update(updateTime)
if !sys.entities[0].RightReleased {
t.Error("Entity was not right released when mouse was released")
}
if sys.entities[0].RightDragged {
t.Error("Entity was still right dragged when mouse was released")
}
if sys.entities[0].rightStartedDragging {
t.Error("Entity was still rightStartedDragging when mouse was released")
}
if sys.rightMouseDown {
t.Error("System was still rightMouseDown when mouse was released")
}
}
func TestMouseSystemAddByInterface(t *testing.T) {
engo.Run(engo.RunOptions{
Width: 100,
Height: 100,
NoRun: true,
HeadlessMode: true,
}, &MouseTestScene{0})
basic := ecs.NewBasic()
entity := mouseEntity{&basic, &MouseComponent{}, &common.SpaceComponent{}, nil, &Box2dComponent{}}
entity.SpaceComponent = &common.SpaceComponent{
Position: engo.Point{X: 0, Y: 0},
Width: 10,
Height: 10,
}
entityBodyDef := box2d.NewB2BodyDef()
entityBodyDef.Type = box2d.B2BodyType.B2_dynamicBody
entityBodyDef.Position = Conv.ToBox2d2Vec(entity.SpaceComponent.Center())
entityBodyDef.Angle = Conv.DegToRad(entity.SpaceComponent.Rotation)
entity.Box2dComponent.Body = World.CreateBody(entityBodyDef)
var entityShape box2d.B2PolygonShape
entityShape.SetAsBox(Conv.PxToMeters(entity.SpaceComponent.Width/2),
Conv.PxToMeters(entity.SpaceComponent.Height/2))
entityFixtureDef := box2d.B2FixtureDef{
Shape: &entityShape,
Density: 1,
Friction: 1,
}
entity.Box2dComponent.Body.CreateFixtureFromDef(&entityFixtureDef)
sys.AddByInterface(entity)
if len(sys.entities) != 1 {
t.Errorf("AddByInterface for MouseSystem failed; wanted %d, have %d entities", 1, len(sys.entities))
}
}