Skip to content

Commit 712c7c9

Browse files
committed
refactor: move all files handling input to a separate package
1 parent 1cac0b9 commit 712c7c9

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

piebiten/internal/ebitengame.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package internal
66
import (
77
"github.com/elgopher/pi/piaudio"
88
"github.com/elgopher/pi/piebiten/internal/audio"
9+
"github.com/elgopher/pi/piebiten/internal/input"
910
ebitenaudio "github.com/hajimehoshi/ebiten/v2/audio"
1011
"math"
1112
"time"
@@ -31,6 +32,12 @@ func RunEbitenGame() *EbitenGame {
3132
drawScreenOpts: &ebiten.DrawImageOptions{},
3233
audioBackend: theAudioBackend,
3334
}
35+
game.inputBackend = &input.Backend{
36+
Paused: &game.paused,
37+
LeftPos: &game.left,
38+
TopPos: &game.top,
39+
Scale: &game.scale,
40+
}
3441

3542
pidebug.Target().SubscribeAll(game.onPidebugEvent)
3643

@@ -59,8 +66,6 @@ type EbitenGame struct {
5966
piScreen pi.Canvas
6067
ebitenScreen *ebiten.Image
6168
drawScreenOpts *ebiten.DrawImageOptions
62-
keys []ebiten.Key
63-
mousePosition pi.Position
6469
cachedMonitor cachedMonitor
6570
started bool
6671

@@ -77,9 +82,9 @@ type EbitenGame struct {
7782

7883
paused bool
7984

80-
gamepads ebitenGamepads
8185
windowState windowState
8286
audioBackend *audio.Backend
87+
inputBackend *input.Backend
8388

8489
ebitenFrame int // frame incremented on each Ebiten tick
8590
}
@@ -111,9 +116,7 @@ func (g *EbitenGame) Update() error {
111116
piloop.DebugTarget().Publish(piloop.EventFrameStart)
112117
}
113118

114-
g.updateMouse()
115-
g.updateKeyboard()
116-
g.gamepads.update()
119+
g.inputBackend.Update()
117120

118121
if g.ebitenFrame%(ebitenTPS/pi.TPS()) == (ebitenTPS/pi.TPS())-1 {
119122
if !g.paused {

piebiten/internal/input/input.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package input
5+
6+
import (
7+
"github.com/elgopher/pi"
8+
"github.com/hajimehoshi/ebiten/v2"
9+
)
10+
11+
type Backend struct {
12+
Paused *bool
13+
LeftPos *float64
14+
TopPos *float64
15+
Scale *float64
16+
17+
keys []ebiten.Key
18+
mousePosition pi.Position
19+
gamepads ebitenGamepads
20+
}
21+
22+
func (g *Backend) Update() {
23+
g.updateMouse()
24+
g.updateKeyboard()
25+
g.gamepads.update()
26+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 Jacek Olszak
22
// This code is licensed under MIT license (see LICENSE for details)
33

4-
package internal
4+
package input
55

66
import (
77
"github.com/hajimehoshi/ebiten/v2"
@@ -10,11 +10,11 @@ import (
1010
"github.com/elgopher/pi/pikey"
1111
)
1212

13-
func (g *EbitenGame) updateKeyboard() {
13+
func (g *Backend) updateKeyboard() {
1414
g.keys = inpututil.AppendJustPressedKeys(g.keys[:0])
1515
for _, key := range g.keys {
1616
event := pikey.Event{Type: pikey.EventDown, Key: keyMap[key]}
17-
if !g.paused {
17+
if !*g.Paused {
1818
pikey.Target().Publish(event)
1919
}
2020
pikey.DebugTarget().Publish(event)
@@ -23,7 +23,7 @@ func (g *EbitenGame) updateKeyboard() {
2323
g.keys = inpututil.AppendJustReleasedKeys(g.keys[:0])
2424
for _, key := range g.keys {
2525
event := pikey.Event{Type: pikey.EventUp, Key: keyMap[key]}
26-
if !g.paused {
26+
if !*g.Paused {
2727
pikey.Target().Publish(event)
2828
}
2929
pikey.DebugTarget().Publish(event)
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 Jacek Olszak
22
// This code is licensed under MIT license (see LICENSE for details)
33

4-
package internal
4+
package input
55

66
import (
77
"github.com/hajimehoshi/ebiten/v2"
@@ -10,7 +10,7 @@ import (
1010
"github.com/elgopher/pi/pimouse"
1111
)
1212

13-
func (g *EbitenGame) updateMouse() {
13+
func (g *Backend) updateMouse() {
1414
g.publishEventDown(ebiten.MouseButtonLeft, pimouse.Left)
1515
g.publishEventDown(ebiten.MouseButtonMiddle, "Middle")
1616
g.publishEventDown(ebiten.MouseButtonRight, pimouse.Right)
@@ -25,8 +25,8 @@ func (g *EbitenGame) updateMouse() {
2525

2626
x, y := ebiten.CursorPosition()
2727
prev := g.mousePosition
28-
g.mousePosition.X = int((float64(x) - g.left) / g.scale)
29-
g.mousePosition.Y = int((float64(y) - g.top) / g.scale)
28+
g.mousePosition.X = int((float64(x) - *g.LeftPos) / *g.Scale)
29+
g.mousePosition.Y = int((float64(y) - *g.TopPos) / *g.Scale)
3030

3131
mouseMovementDelta := g.mousePosition.Subtract(prev)
3232

@@ -35,33 +35,33 @@ func (g *EbitenGame) updateMouse() {
3535
Position: g.mousePosition,
3636
Previous: prev,
3737
}
38-
if !g.paused {
38+
if !*g.Paused {
3939
pimouse.MoveTarget().Publish(event)
4040
}
4141
pimouse.MoveDebugTarget().Publish(event)
4242
}
4343
}
4444

45-
func (g *EbitenGame) publishEventDown(button ebiten.MouseButton, key pimouse.Button) {
45+
func (g *Backend) publishEventDown(button ebiten.MouseButton, key pimouse.Button) {
4646
if inpututil.IsMouseButtonJustPressed(button) {
4747
event := pimouse.EventButton{
4848
Type: pimouse.EventButtonDown,
4949
Button: key,
5050
}
51-
if !g.paused {
51+
if !*g.Paused {
5252
pimouse.ButtonTarget().Publish(event)
5353
}
5454
pimouse.ButtonDebugTarget().Publish(event)
5555
}
5656
}
5757

58-
func (g *EbitenGame) publishEventUp(button ebiten.MouseButton, key pimouse.Button) {
58+
func (g *Backend) publishEventUp(button ebiten.MouseButton, key pimouse.Button) {
5959
if inpututil.IsMouseButtonJustReleased(button) {
6060
event := pimouse.EventButton{
6161
Type: pimouse.EventButtonUp,
6262
Button: key,
6363
}
64-
if !g.paused {
64+
if !*g.Paused {
6565
pimouse.ButtonTarget().Publish(event)
6666
}
6767
pimouse.ButtonDebugTarget().Publish(event)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 Jacek Olszak
22
// This code is licensed under MIT license (see LICENSE for details)
33

4-
package internal
4+
package input
55

66
import (
77
"github.com/hajimehoshi/ebiten/v2"

0 commit comments

Comments
 (0)