From e2a8b07a94f34f1b499fb7a1ef4d05ee26971447 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 1 Dec 2013 21:23:03 +0900 Subject: [PATCH] Event handling --- ebiten.go | 9 ++++++--- example/game/input/input.go | 7 +++++-- example/game/monochrome/monochrome.go | 7 +++++-- example/game/rects/rects.go | 7 +++++-- example/game/rotating/rotating.go | 7 +++++-- example/game/sprites/sprites.go | 7 +++++-- example/main.go | 12 ++++++------ ui/cocoa/cocoa.go | 6 ++++-- 8 files changed, 41 insertions(+), 21 deletions(-) diff --git a/ebiten.go b/ebiten.go index acb63e372..f751aaa81 100644 --- a/ebiten.go +++ b/ebiten.go @@ -14,11 +14,14 @@ type InputStateUpdatedEvent struct { Y int } +type UIEvents interface { + ObserveScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent + ObserveInputStateUpdated() <-chan InputStateUpdatedEvent +} + type UI interface { PollEvents() InitTextures(func(graphics.TextureFactory)) Draw(func(graphics.Canvas)) - - ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent - InputStateUpdated() <-chan InputStateUpdatedEvent + UIEvents } diff --git a/example/game/input/input.go b/example/game/input/input.go index b5790da50..6336ea7c4 100644 --- a/example/game/input/input.go +++ b/example/game/input/input.go @@ -24,8 +24,11 @@ func New() *Input { } } -func (game *Input) InputStateUpdated() chan<- ebiten.InputStateUpdatedEvent { - return game.inputStateUpdatedCh +func (game *Input) OnInputStateUpdated(e ebiten.InputStateUpdatedEvent) { + go func() { + e := e + game.inputStateUpdatedCh <- e + }() } func (game *Input) InitTextures(tf graphics.TextureFactory) { diff --git a/example/game/monochrome/monochrome.go b/example/game/monochrome/monochrome.go index 3458588f2..291fdad5d 100644 --- a/example/game/monochrome/monochrome.go +++ b/example/game/monochrome/monochrome.go @@ -33,8 +33,11 @@ func New() *Monochrome { } } -func (game *Monochrome) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { - return game.screenSizeUpdatedCh +func (game *Monochrome) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { + go func() { + e := e + game.screenSizeUpdatedCh <- e + }() } func (game *Monochrome) InitTextures(tf graphics.TextureFactory) { diff --git a/example/game/rects/rects.go b/example/game/rects/rects.go index e897fecb5..66c4d3cf5 100644 --- a/example/game/rects/rects.go +++ b/example/game/rects/rects.go @@ -39,8 +39,11 @@ func New() *Rects { } } -func (game *Rects) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { - return game.screenSizeUpdatedCh +func (game *Rects) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { + go func() { + e := e + game.screenSizeUpdatedCh <- e + }() } func (game *Rects) InitTextures(tf graphics.TextureFactory) { diff --git a/example/game/rotating/rotating.go b/example/game/rotating/rotating.go index 12ebb82cc..58d94a316 100644 --- a/example/game/rotating/rotating.go +++ b/example/game/rotating/rotating.go @@ -30,8 +30,11 @@ func New() *Rotating { } } -func (game *Rotating) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { - return game.screenSizeUpdatedCh +func (game *Rotating) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { + go func() { + e := e + game.screenSizeUpdatedCh <- e + }() } func (game *Rotating) InitTextures(tf graphics.TextureFactory) { diff --git a/example/game/sprites/sprites.go b/example/game/sprites/sprites.go index 3b3fcfb2d..3d9c49528 100644 --- a/example/game/sprites/sprites.go +++ b/example/game/sprites/sprites.go @@ -77,8 +77,11 @@ func New() *Sprites { } } -func (game *Sprites) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { - return game.screenSizeUpdatedCh +func (game *Sprites) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { + go func() { + e := e + game.screenSizeUpdatedCh <- e + }() } func (game *Sprites) InitTextures(tf graphics.TextureFactory) { diff --git a/example/main.go b/example/main.go index f564e4eeb..30be6cfdf 100644 --- a/example/main.go +++ b/example/main.go @@ -74,8 +74,8 @@ func main() { } }() - inputStateUpdated := ui.InputStateUpdated() - screenSizeUpdated := ui.ScreenSizeUpdated() + inputStateUpdated := ui.ObserveInputStateUpdated() + screenSizeUpdated := ui.ObserveScreenSizeUpdated() for { ui.PollEvents() events: @@ -83,17 +83,17 @@ func main() { select { case e := <-screenSizeUpdated: type Handler interface { - ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent + OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) } if game2, ok := game.(Handler); ok { - game2.ScreenSizeUpdated() <- e + game2.OnScreenSizeUpdated(e) } case e := <-inputStateUpdated: type Handler interface { - InputStateUpdated() chan<- ebiten.InputStateUpdatedEvent + OnInputStateUpdated(ebiten.InputStateUpdatedEvent) } if game2, ok := game.(Handler); ok { - game2.InputStateUpdated() <- e + game2.OnInputStateUpdated(e) } default: break events diff --git a/ui/cocoa/cocoa.go b/ui/cocoa/cocoa.go index d92e24b60..7bb3529a9 100644 --- a/ui/cocoa/cocoa.go +++ b/ui/cocoa/cocoa.go @@ -122,7 +122,7 @@ func (ui *UI) Draw(f func(graphics.Canvas)) { C.EndDrawing(ui.window) } -func (ui *UI) InputStateUpdated() <-chan ebiten.InputStateUpdatedEvent { +func (ui *UI) ObserveInputStateUpdated() <-chan ebiten.InputStateUpdatedEvent { ch := make(chan ebiten.InputStateUpdatedEvent) go func() { ui.inputStateUpdatedChs <- ch @@ -132,11 +132,12 @@ func (ui *UI) InputStateUpdated() <-chan ebiten.InputStateUpdatedEvent { func (ui *UI) notifyInputStateUpdated(e ebiten.InputStateUpdatedEvent) { go func() { + e := e ui.inputStateUpdatedNotified <- e }() } -func (ui *UI) ScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent { +func (ui *UI) ObserveScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent { ch := make(chan ebiten.ScreenSizeUpdatedEvent) go func() { ui.screenSizeUpdatedChs <- ch @@ -146,6 +147,7 @@ func (ui *UI) ScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent { func (ui *UI) notifyScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { go func() { + e := e ui.screenSizeUpdatedNotified <- e }() }