summaryrefslogtreecommitdiff
path: root/input
diff options
context:
space:
mode:
Diffstat (limited to 'input')
-rw-r--r--input/input.go30
-rw-r--r--input/sdl.go34
2 files changed, 64 insertions, 0 deletions
diff --git a/input/input.go b/input/input.go
new file mode 100644
index 0000000..94f4e56
--- /dev/null
+++ b/input/input.go
@@ -0,0 +1,30 @@
+package input
+
+import "ponger/ecs"
+
+type DeviceID int
+
+type Event struct {
+ ecs.Event
+ Which DeviceID
+ Dx, Dy float32
+ JustPressed, JustReleased, IsPressed bool
+}
+
+type Handler interface {
+ Process(ev Event)
+}
+
+func ProcessTree(root any, ev Event) {
+ if inp, ok := root.(Handler); ok {
+ inp.Process(ev)
+ }
+ if g, ok := root.(*ecs.GameObject); ok {
+ for _, c := range g.Children {
+ if c == nil {
+ continue
+ }
+ ProcessTree(c, ev)
+ }
+ }
+}
diff --git a/input/sdl.go b/input/sdl.go
new file mode 100644
index 0000000..6b88f35
--- /dev/null
+++ b/input/sdl.go
@@ -0,0 +1,34 @@
+package input
+
+import (
+ "github.com/Zyko0/go-sdl3/sdl"
+ "ponger/ecs"
+)
+
+func Poll(root ecs.IsGameObject, delta uint64) error {
+ e := Event{
+ Event: ecs.Event{
+ Delta: delta,
+ },
+ }
+ var event sdl.Event
+ for sdl.PollEvent(&event) {
+ switch event.Type {
+ case sdl.EVENT_QUIT:
+ return sdl.EndLoop
+ case sdl.EVENT_MOUSE_MOTION:
+ ev := event.MouseMotionEvent()
+ e.Which = DeviceID(ev.Which)
+ e.Dx = ev.Xrel
+ e.Dy = ev.Yrel
+ case sdl.EVENT_MOUSE_BUTTON_DOWN, sdl.EVENT_MOUSE_BUTTON_UP:
+ ev := event.MouseButtonEvent()
+ e.Which = DeviceID(ev.Which)
+ e.JustPressed = ev.Down
+ e.JustReleased = !ev.Down
+ e.IsPressed = ev.Down
+ }
+ }
+ ProcessTree(root, e)
+ return nil
+}