mobile: Introduce EventDispatcher to reduce glue code

This commit is contained in:
Hajime Hoshi 2016-05-23 02:44:28 +09:00
parent f251ae8b49
commit 2b477eef5a
2 changed files with 24 additions and 26 deletions

View File

@ -29,25 +29,10 @@ import (
"github.com/hajimehoshi/ebiten/mobile" "github.com/hajimehoshi/ebiten/mobile"
) )
func Start() error { // EventDispacher must be redeclared and exported so that this is available on the Java/Objective-C side.
mobile.Start(example.Update, example.ScreenWidth, example.ScreenHeight, 2, "Mobile (Ebiten Demo)")
return nil
}
func Render() error { type EventDispatcher mobile.EventDispatcher
return mobile.Render()
}
// TODO: So many glue codes: Can I reduce those? func Start() (EventDispatcher, error) {
return mobile.Start(example.Update, example.ScreenWidth, example.ScreenHeight, 2, "Mobile (Ebiten Demo)")
func TouchDown(x, y int) {
mobile.TouchDown(x, y)
}
func TouchUp(x, y int) {
mobile.TouchUp(x, y)
}
func TouchMove(x, y int) {
mobile.TouchMove(x, y)
} }

View File

@ -26,22 +26,35 @@ import (
var chError <-chan error var chError <-chan error
type EventDispatcher interface {
SetScreenSize(width, height int)
SetScreenScale(scale int)
Render() error
TouchDown(x, y int)
TouchUp(x, y int)
TouchMove(x, y int)
}
// Start starts the game and returns immediately. // Start starts the game and returns immediately.
// //
// Different from ebiten.Run, this invokes only the game loop and not the main (UI) loop. // Different from ebiten.Run, this invokes only the game loop and not the main (UI) loop.
func Start(f func(*ebiten.Image) error, width, height, scale int, title string) { func Start(f func(*ebiten.Image) error, width, height, scale int, title string) (EventDispatcher, error) {
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title) chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
return &eventDispatcher{}, nil
} }
func SetScreenSize(width, height int) { type eventDispatcher struct {
}
func (e *eventDispatcher) SetScreenSize(width, height int) {
ui.CurrentUI().SetScreenSize(width, height) ui.CurrentUI().SetScreenSize(width, height)
} }
func SetScreenScale(scale int) { func (e *eventDispatcher) SetScreenScale(scale int) {
ui.CurrentUI().SetScreenScale(scale) ui.CurrentUI().SetScreenScale(scale)
} }
func Render() error { func (e *eventDispatcher) Render() error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -51,14 +64,14 @@ func Render() error {
return ui.Render(chError) return ui.Render(chError)
} }
func TouchDown(x, y int) { func (e *eventDispatcher) TouchDown(x, y int) {
ui.TouchDown(x, y) ui.TouchDown(x, y)
} }
func TouchUp(x, y int) { func (e *eventDispatcher) TouchUp(x, y int) {
ui.TouchUp(x, y) ui.TouchUp(x, y)
} }
func TouchMove(x, y int) { func (e *eventDispatcher) TouchMove(x, y int) {
ui.TouchMove(x, y) ui.TouchMove(x, y)
} }