Refactoring

This commit is contained in:
Hajime Hoshi 2013-12-16 02:42:54 +09:00
parent b70884ca40
commit aeab5f3437
2 changed files with 26 additions and 30 deletions

View File

@ -137,7 +137,7 @@ func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType
if inputType == C.InputTypeMouseUp {
e := ui.MouseStateUpdatedEvent{-1, -1}
w.notifyInputStateUpdated(e)
w.notify(e)
return
}
@ -155,13 +155,13 @@ func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, inputType C.InputType
y = w.screenHeight - 1
}
e := ui.MouseStateUpdatedEvent{x, y}
w.notifyInputStateUpdated(e)
w.notify(e)
}
//export ebiten_WindowClosed
func ebiten_WindowClosed(nativeWindow unsafe.Pointer) {
w := windows[nativeWindow]
w.closed = true
w.notifyWindowClosed(ui.WindowClosedEvent{})
w.notify(ui.WindowClosedEvent{})
delete(windows, nativeWindow)
}

View File

@ -10,6 +10,29 @@ type windowEvents struct {
windowClosed chan ui.WindowClosedEvent // initialized lazily
}
func (w *windowEvents) notify(e interface{}) {
go func() {
w.doNotify(e)
}()
}
func (w *windowEvents) doNotify(e interface{}) {
switch e := e.(type) {
case ui.ScreenSizeUpdatedEvent:
if w.screenSizeUpdated != nil {
w.screenSizeUpdated <- e
}
case ui.MouseStateUpdatedEvent:
if w.mouseStateUpdated != nil {
w.mouseStateUpdated <- e
}
case ui.WindowClosedEvent:
if w.windowClosed != nil {
w.windowClosed <- e
}
}
}
func (w *windowEvents) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
if w.screenSizeUpdated != nil {
return w.screenSizeUpdated
@ -18,15 +41,6 @@ func (w *windowEvents) ScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
return w.screenSizeUpdated
}
func (w *windowEvents) notifyScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
if w.screenSizeUpdated == nil {
return
}
go func() {
w.screenSizeUpdated <- e
}()
}
func (w *windowEvents) MouseStateUpdated() <-chan ui.MouseStateUpdatedEvent {
if w.mouseStateUpdated != nil {
return w.mouseStateUpdated
@ -35,15 +49,6 @@ func (w *windowEvents) MouseStateUpdated() <-chan ui.MouseStateUpdatedEvent {
return w.mouseStateUpdated
}
func (w *windowEvents) notifyInputStateUpdated(e ui.MouseStateUpdatedEvent) {
if w.mouseStateUpdated == nil {
return
}
go func() {
w.mouseStateUpdated <- e
}()
}
func (w *windowEvents) WindowClosed() <-chan ui.WindowClosedEvent {
if w.windowClosed != nil {
return w.windowClosed
@ -51,12 +56,3 @@ func (w *windowEvents) WindowClosed() <-chan ui.WindowClosedEvent {
w.windowClosed = make(chan ui.WindowClosedEvent)
return w.windowClosed
}
func (w *windowEvents) notifyWindowClosed(e ui.WindowClosedEvent) {
if w.windowClosed == nil {
return
}
go func() {
w.windowClosed <- e
}()
}