diff options
| author | ewy <ewy0@protonmail.com> | 2026-08-13 15:25:59 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-08-13 15:25:59 +0200 |
| commit | bc3f852ea670e206fa3facc736a230f6b9d23920 (patch) | |
| tree | 198bdb2107108fe0cbe83836c8d14688128ba80c | |
| parent | 60f58e548ef745ec072482954ac37c35d88280d4 (diff) | |
clamp mous
| -rw-r--r-- | events/input.go | 13 | ||||
| -rw-r--r-- | events/poll.go | 7 | ||||
| -rw-r--r-- | input/input.go | 11 | ||||
| -rw-r--r-- | objs/cursor.go | 12 | ||||
| -rw-r--r-- | tree/tree.go | 15 |
5 files changed, 42 insertions, 16 deletions
diff --git a/events/input.go b/events/input.go new file mode 100644 index 0000000..8fc1591 --- /dev/null +++ b/events/input.go @@ -0,0 +1,13 @@ +package events + +type MouseMotionHandler interface { + ProcessMouseMotion(ev MouseMotion) +} + +type MousePressHandler interface { + ProcessMousePress(ev MousePress) +} + +type WindowChangeHandler interface { + ProcessWindowChange(ev WindowChange) +} diff --git a/events/poll.go b/events/poll.go index faa4151..c57621e 100644 --- a/events/poll.go +++ b/events/poll.go @@ -27,6 +27,13 @@ func Poll(delta uint64) ([]any, error) { Which: DeviceID(ev.Which), IsPressed: ev.Down, }) + case sdl.EVENT_WINDOW_SHOWN: + win := event.Window() + w, h, _ := win.Size() + evs = append(evs, WindowChange{ + Width: w, + Height: h, + }) case sdl.EVENT_WINDOW_RESIZED: ev := event.WindowEvent() evs = append(evs, WindowChange{ diff --git a/input/input.go b/input/input.go deleted file mode 100644 index 5a0dea7..0000000 --- a/input/input.go +++ /dev/null @@ -1,11 +0,0 @@ -package input - -import "ponger/events" - -type MouseMotionHandler interface { - ProcessMouseMotion(ev events.MouseMotion) -} - -type MousePressHandler interface { - ProcessMousePress(ev events.MousePress) -} diff --git a/objs/cursor.go b/objs/cursor.go index 817279b..2a676fe 100644 --- a/objs/cursor.go +++ b/objs/cursor.go @@ -8,6 +8,12 @@ import ( type Cursor struct { *ecs.GameObject + MaxX, MaxY float32 +} + +func (c *Cursor) ProcessWindowChange(ev events.WindowChange) { + c.MaxX = float32(ev.Width) + c.MaxY = float32(ev.Height) } func (c *Cursor) Render(renderer *sdl.Renderer) { @@ -31,6 +37,12 @@ func (c *Cursor) ProcessMouseMotion(ev events.MouseMotion) { if c.Position.Y < 0 { c.Position.Y = 0 } + if c.Position.X > c.MaxX { + c.Position.X = c.MaxX + } + if c.Position.Y > c.MaxY { + c.Position.Y = c.MaxY + } } func (c *Cursor) GO() *ecs.GameObject { diff --git a/tree/tree.go b/tree/tree.go index a48dbc0..d39d296 100644 --- a/tree/tree.go +++ b/tree/tree.go @@ -4,7 +4,6 @@ import ( "github.com/Zyko0/go-sdl3/sdl" "ponger/ecs" "ponger/events" - "ponger/input" "ponger/render" ) @@ -28,16 +27,22 @@ func ParseRecursive(current ecs.IsGameObject, delta uint64, renderer *sdl.Render } func ParseSingle(obj ecs.IsGameObject, delta uint64, renderer *sdl.Renderer, evs ...any) { - switch impl := obj.(type) { - case input.MouseMotionHandler: + if impl, ok := obj.(events.WindowChangeHandler); ok { + for _, e := range events.OfType[events.WindowChange](evs...) { + impl.ProcessWindowChange(e) + } + } + if impl, ok := obj.(events.MouseMotionHandler); ok { for _, e := range events.OfType[events.MouseMotion](evs...) { impl.ProcessMouseMotion(e) } - case input.MousePressHandler: + } + if impl, ok := obj.(events.MousePressHandler); ok { for _, e := range events.OfType[events.MousePress](evs...) { impl.ProcessMousePress(e) } - case render.Renders: + } + if impl, ok := obj.(render.Renders); ok { impl.Render(renderer) } } |
