examples/paint: refactoring: avoid calling input functions in Draw

This commit is contained in:
Hajime Hoshi 2022-08-16 01:06:22 +09:00
parent 5b182efe7e
commit e21c881644

View File

@ -56,8 +56,19 @@ func init() {
}) })
} }
type touch struct {
id ebiten.TouchID
pos pos
}
type pos struct {
x int
y int
}
type Game struct { type Game struct {
touches []ebiten.TouchID cursor pos
touches []touch
count int count int
canvasImage *ebiten.Image canvasImage *ebiten.Image
@ -80,12 +91,23 @@ func (g *Game) Update() error {
g.paint(g.canvasImage, mx, my) g.paint(g.canvasImage, mx, my)
drawn = true drawn = true
} }
g.cursor = pos{
x: mx,
y: my,
}
// Paint the brush by touches // Paint the brush by touches
g.touches = ebiten.AppendTouchIDs(g.touches[:0]) g.touches = g.touches[:0]
for _, t := range g.touches { for _, id := range ebiten.AppendTouchIDs(nil) {
x, y := ebiten.TouchPosition(t) x, y := ebiten.TouchPosition(id)
g.paint(g.canvasImage, x, y) g.paint(g.canvasImage, x, y)
g.touches = append(g.touches, touch{
id: id,
pos: pos{
x: x,
y: y,
},
})
drawn = true drawn = true
} }
if drawn { if drawn {
@ -109,11 +131,9 @@ func (g *Game) paint(canvas *ebiten.Image, x, y int) {
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(g.canvasImage, nil) screen.DrawImage(g.canvasImage, nil)
mx, my := ebiten.CursorPosition() msg := fmt.Sprintf("(%d, %d)", g.cursor.x, g.cursor.y)
msg := fmt.Sprintf("(%d, %d)", mx, my)
for _, t := range g.touches { for _, t := range g.touches {
x, y := ebiten.TouchPosition(t) msg += fmt.Sprintf("\n(%d, %d) touch %d", t.pos.x, t.pos.y, t.id)
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t)
} }
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
} }