Event handling

This commit is contained in:
Hajime Hoshi 2013-12-01 21:23:03 +09:00
parent da30a83491
commit e2a8b07a94
8 changed files with 41 additions and 21 deletions

View File

@ -14,11 +14,14 @@ type InputStateUpdatedEvent struct {
Y int Y int
} }
type UIEvents interface {
ObserveScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
ObserveInputStateUpdated() <-chan InputStateUpdatedEvent
}
type UI interface { type UI interface {
PollEvents() PollEvents()
InitTextures(func(graphics.TextureFactory)) InitTextures(func(graphics.TextureFactory))
Draw(func(graphics.Canvas)) Draw(func(graphics.Canvas))
UIEvents
ScreenSizeUpdated() <-chan ScreenSizeUpdatedEvent
InputStateUpdated() <-chan InputStateUpdatedEvent
} }

View File

@ -24,8 +24,11 @@ func New() *Input {
} }
} }
func (game *Input) InputStateUpdated() chan<- ebiten.InputStateUpdatedEvent { func (game *Input) OnInputStateUpdated(e ebiten.InputStateUpdatedEvent) {
return game.inputStateUpdatedCh go func() {
e := e
game.inputStateUpdatedCh <- e
}()
} }
func (game *Input) InitTextures(tf graphics.TextureFactory) { func (game *Input) InitTextures(tf graphics.TextureFactory) {

View File

@ -33,8 +33,11 @@ func New() *Monochrome {
} }
} }
func (game *Monochrome) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { func (game *Monochrome) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
return game.screenSizeUpdatedCh go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
} }
func (game *Monochrome) InitTextures(tf graphics.TextureFactory) { func (game *Monochrome) InitTextures(tf graphics.TextureFactory) {

View File

@ -39,8 +39,11 @@ func New() *Rects {
} }
} }
func (game *Rects) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { func (game *Rects) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
return game.screenSizeUpdatedCh go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
} }
func (game *Rects) InitTextures(tf graphics.TextureFactory) { func (game *Rects) InitTextures(tf graphics.TextureFactory) {

View File

@ -30,8 +30,11 @@ func New() *Rotating {
} }
} }
func (game *Rotating) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { func (game *Rotating) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
return game.screenSizeUpdatedCh go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
} }
func (game *Rotating) InitTextures(tf graphics.TextureFactory) { func (game *Rotating) InitTextures(tf graphics.TextureFactory) {

View File

@ -77,8 +77,11 @@ func New() *Sprites {
} }
} }
func (game *Sprites) ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent { func (game *Sprites) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
return game.screenSizeUpdatedCh go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
} }
func (game *Sprites) InitTextures(tf graphics.TextureFactory) { func (game *Sprites) InitTextures(tf graphics.TextureFactory) {

View File

@ -74,8 +74,8 @@ func main() {
} }
}() }()
inputStateUpdated := ui.InputStateUpdated() inputStateUpdated := ui.ObserveInputStateUpdated()
screenSizeUpdated := ui.ScreenSizeUpdated() screenSizeUpdated := ui.ObserveScreenSizeUpdated()
for { for {
ui.PollEvents() ui.PollEvents()
events: events:
@ -83,17 +83,17 @@ func main() {
select { select {
case e := <-screenSizeUpdated: case e := <-screenSizeUpdated:
type Handler interface { type Handler interface {
ScreenSizeUpdated() chan<- ebiten.ScreenSizeUpdatedEvent OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent)
} }
if game2, ok := game.(Handler); ok { if game2, ok := game.(Handler); ok {
game2.ScreenSizeUpdated() <- e game2.OnScreenSizeUpdated(e)
} }
case e := <-inputStateUpdated: case e := <-inputStateUpdated:
type Handler interface { type Handler interface {
InputStateUpdated() chan<- ebiten.InputStateUpdatedEvent OnInputStateUpdated(ebiten.InputStateUpdatedEvent)
} }
if game2, ok := game.(Handler); ok { if game2, ok := game.(Handler); ok {
game2.InputStateUpdated() <- e game2.OnInputStateUpdated(e)
} }
default: default:
break events break events

View File

@ -122,7 +122,7 @@ func (ui *UI) Draw(f func(graphics.Canvas)) {
C.EndDrawing(ui.window) C.EndDrawing(ui.window)
} }
func (ui *UI) InputStateUpdated() <-chan ebiten.InputStateUpdatedEvent { func (ui *UI) ObserveInputStateUpdated() <-chan ebiten.InputStateUpdatedEvent {
ch := make(chan ebiten.InputStateUpdatedEvent) ch := make(chan ebiten.InputStateUpdatedEvent)
go func() { go func() {
ui.inputStateUpdatedChs <- ch ui.inputStateUpdatedChs <- ch
@ -132,11 +132,12 @@ func (ui *UI) InputStateUpdated() <-chan ebiten.InputStateUpdatedEvent {
func (ui *UI) notifyInputStateUpdated(e ebiten.InputStateUpdatedEvent) { func (ui *UI) notifyInputStateUpdated(e ebiten.InputStateUpdatedEvent) {
go func() { go func() {
e := e
ui.inputStateUpdatedNotified <- e ui.inputStateUpdatedNotified <- e
}() }()
} }
func (ui *UI) ScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent { func (ui *UI) ObserveScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent {
ch := make(chan ebiten.ScreenSizeUpdatedEvent) ch := make(chan ebiten.ScreenSizeUpdatedEvent)
go func() { go func() {
ui.screenSizeUpdatedChs <- ch ui.screenSizeUpdatedChs <- ch
@ -146,6 +147,7 @@ func (ui *UI) ScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent {
func (ui *UI) notifyScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) { func (ui *UI) notifyScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
go func() { go func() {
e := e
ui.screenSizeUpdatedNotified <- e ui.screenSizeUpdatedNotified <- e
}() }()
} }