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
}
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
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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
}()
}