154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
package testutils
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"gitea.boner.be/bdnugget/goonscape/game/mock"
|
|
rl "github.com/gen2brain/raylib-go/raylib"
|
|
)
|
|
|
|
var (
|
|
mockInput struct {
|
|
sync.Mutex
|
|
keyPressed map[int32]bool
|
|
keyDown map[int32]bool
|
|
mousePressed map[int32]bool
|
|
mousePosition rl.Vector2
|
|
mouseRay rl.Ray
|
|
mouseWheel float32
|
|
charPressed rune
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
ResetMockInput()
|
|
SetupMockFunctions()
|
|
}
|
|
|
|
// SetupMockFunctions initializes mock functions
|
|
func SetupMockFunctions() {
|
|
mock.IsKeyPressed = MockIsKeyPressed
|
|
mock.IsKeyDown = MockIsKeyDown
|
|
mock.IsMouseButtonPressed = MockIsMouseButtonPressed
|
|
mock.GetMousePosition = MockGetMousePosition
|
|
mock.GetMouseRay = MockGetMouseRay
|
|
mock.GetMouseWheelMove = MockGetMouseWheelMove
|
|
mock.GetCharPressed = MockGetCharPressed
|
|
}
|
|
|
|
// ResetMockInput resets all mock input states
|
|
func ResetMockInput() {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
mockInput.keyPressed = make(map[int32]bool)
|
|
mockInput.keyDown = make(map[int32]bool)
|
|
mockInput.mousePressed = make(map[int32]bool)
|
|
mockInput.mousePosition = rl.Vector2{}
|
|
mockInput.mouseRay = rl.Ray{}
|
|
mockInput.mouseWheel = 0
|
|
mockInput.charPressed = 0
|
|
}
|
|
|
|
// SimulateKeyPress simulates a key press
|
|
func SimulateKeyPress(key int32) {
|
|
mockInput.Lock()
|
|
mockInput.keyPressed[key] = true
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateKeyDown simulates holding a key down
|
|
func SimulateKeyDown(key int32, isDown bool) {
|
|
mockInput.Lock()
|
|
mockInput.keyDown[key] = isDown
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateMouseButton simulates a mouse button press
|
|
func SimulateMouseButton(button int32, isPressed bool) {
|
|
mockInput.Lock()
|
|
mockInput.mousePressed[button] = isPressed
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateMousePosition simulates mouse movement
|
|
func SimulateMousePosition(x, y float32) {
|
|
mockInput.Lock()
|
|
mockInput.mousePosition = rl.Vector2{X: x, Y: y}
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateMouseClick simulates a mouse click at the given position
|
|
func SimulateMouseClick(x, y float32) {
|
|
SimulateMousePosition(x, y)
|
|
SimulateMouseButton(ToInt32(rl.MouseLeftButton), true)
|
|
}
|
|
|
|
// SimulateMouseRay simulates a mouse ray
|
|
func SimulateMouseRay(ray rl.Ray) {
|
|
mockInput.Lock()
|
|
mockInput.mouseRay = ray
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateMouseWheel simulates mouse wheel movement
|
|
func SimulateMouseWheel(move float32) {
|
|
mockInput.Lock()
|
|
mockInput.mouseWheel = move
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// SimulateCharInput simulates character input
|
|
func SimulateCharInput(char rune) {
|
|
mockInput.Lock()
|
|
mockInput.charPressed = char
|
|
mockInput.Unlock()
|
|
}
|
|
|
|
// Mock raylib functions
|
|
func MockIsKeyPressed(key int32) bool {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.keyPressed[key]
|
|
}
|
|
|
|
func MockIsKeyDown(key int32) bool {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.keyDown[key]
|
|
}
|
|
|
|
func MockIsMouseButtonPressed(button int32) bool {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.mousePressed[button]
|
|
}
|
|
|
|
func MockGetMousePosition() rl.Vector2 {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.mousePosition
|
|
}
|
|
|
|
func MockGetMouseRay(mousePos rl.Vector2, camera rl.Camera3D) rl.Ray {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.mouseRay
|
|
}
|
|
|
|
func MockGetMouseWheelMove() float32 {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.mouseWheel
|
|
}
|
|
|
|
func MockGetCharPressed() rune {
|
|
mockInput.Lock()
|
|
defer mockInput.Unlock()
|
|
return mockInput.charPressed
|
|
}
|
|
|
|
// ToInt32 converts MouseButton to int32
|
|
func ToInt32(button rl.MouseButton) int32 {
|
|
return int32(button)
|
|
}
|