From 2b477eef5a43d4cb077f2fc0abd9d29457aafc4c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 23 May 2016 02:44:28 +0900 Subject: [PATCH] mobile: Introduce EventDispatcher to reduce glue code --- examples/mobile/main_mobile.go | 23 ++++------------------- mobile/run.go | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/examples/mobile/main_mobile.go b/examples/mobile/main_mobile.go index 4f4bf3bd7..acd1d566a 100644 --- a/examples/mobile/main_mobile.go +++ b/examples/mobile/main_mobile.go @@ -29,25 +29,10 @@ import ( "github.com/hajimehoshi/ebiten/mobile" ) -func Start() error { - mobile.Start(example.Update, example.ScreenWidth, example.ScreenHeight, 2, "Mobile (Ebiten Demo)") - return nil -} +// EventDispacher must be redeclared and exported so that this is available on the Java/Objective-C side. -func Render() error { - return mobile.Render() -} +type EventDispatcher mobile.EventDispatcher -// TODO: So many glue codes: Can I reduce those? - -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) +func Start() (EventDispatcher, error) { + return mobile.Start(example.Update, example.ScreenWidth, example.ScreenHeight, 2, "Mobile (Ebiten Demo)") } diff --git a/mobile/run.go b/mobile/run.go index 6141b00b7..8ece510a9 100644 --- a/mobile/run.go +++ b/mobile/run.go @@ -26,22 +26,35 @@ import ( 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. // // 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) + return &eventDispatcher{}, nil } -func SetScreenSize(width, height int) { +type eventDispatcher struct { +} + +func (e *eventDispatcher) SetScreenSize(width, height int) { ui.CurrentUI().SetScreenSize(width, height) } -func SetScreenScale(scale int) { +func (e *eventDispatcher) SetScreenScale(scale int) { ui.CurrentUI().SetScreenScale(scale) } -func Render() error { +func (e *eventDispatcher) Render() error { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -51,14 +64,14 @@ func Render() error { return ui.Render(chError) } -func TouchDown(x, y int) { +func (e *eventDispatcher) TouchDown(x, y int) { ui.TouchDown(x, y) } -func TouchUp(x, y int) { +func (e *eventDispatcher) TouchUp(x, y int) { ui.TouchUp(x, y) } -func TouchMove(x, y int) { +func (e *eventDispatcher) TouchMove(x, y int) { ui.TouchMove(x, y) }