From cc5fba729a932dba347ddb8f034ee7cc5beb8e4c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 25 Jun 2016 15:50:03 +0900 Subject: [PATCH] mobile: Remove EventDispatcher --- mobile/impl_empty.go | 4 +- mobile/impl_mobile.go | 6 +-- mobile/mobile.go | 83 ++++++++++++--------------------------- mobile/touches_android.go | 13 +++++- mobile/touches_empty.go | 4 +- mobile/touches_ios.go | 14 ++++--- mobile/touches_mobile.go | 29 +++++++++++++- 7 files changed, 80 insertions(+), 73 deletions(-) diff --git a/mobile/impl_empty.go b/mobile/impl_empty.go index c5801134d..0d751907b 100644 --- a/mobile/impl_empty.go +++ b/mobile/impl_empty.go @@ -26,6 +26,6 @@ func render() error { return nil } -func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) (EventDispatcher, error) { - return nil, nil +func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error { + return nil } diff --git a/mobile/impl_mobile.go b/mobile/impl_mobile.go index 83538b275..5d3d30925 100644 --- a/mobile/impl_mobile.go +++ b/mobile/impl_mobile.go @@ -32,9 +32,7 @@ func render() error { return ui.Render(chError) } -func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) (EventDispatcher, error) { +func start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error { chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title) - return &eventDispatcher{ - touches: map[int]position{}, - }, nil + return nil } diff --git a/mobile/mobile.go b/mobile/mobile.go index a62127361..0aeb0bcc0 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -16,75 +16,44 @@ package mobile import ( "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/internal/ui" ) -type EventDispatcher interface { - // UpdateTouchesOnAndroid updates the touch state on Android. - // - // This should be called with onTouchEvent of GLSurfaceView like this: - // - // @Override - // public boolean onTouchEvent(MotionEvent e) { - // for (int i = 0; i < e.getPointerCount(); i++) { - // int id = e.getPointerId(i); - // int x = (int)e.getX(i); - // int y = (int)e.getY(i); - // YourGame.CurrentEventDispatcher().UpdateTouchesOnAndroid(e.getActionMasked(), id, x, y); - // } - // return true; - // } - UpdateTouchesOnAndroid(action int, id int, x, y int) - UpdateTouchesOnIOS(phase int, ptr int, 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 int, scale float64, title string) (EventDispatcher, error) { +func Start(f func(*ebiten.Image) error, width, height int, scale float64, title string) error { return start(f, width, height, scale, title) } +// Render updates and renders the game. +// +// This should be called on every frame. +// +// On Android, this should be called at onDrawFrame of Renderer (used by GLSurfaceView). +// +// On iOS, this should be called at glkView:drawInRect: of GLKViewDelegate. func Render() error { return render() } -type position struct { - x int - y int +// UpdateTouchesOnAndroid updates the touch state on Android. +// +// This should be called with onTouchEvent of GLSurfaceView like this: +// +// @Override +// public boolean onTouchEvent(MotionEvent e) { +// for (int i = 0; i < e.getPointerCount(); i++) { +// int id = e.getPointerId(i); +// int x = (int)e.getX(i); +// int y = (int)e.getY(i); +// YourGame.CurrentEventDispatcher().UpdateTouchesOnAndroid(e.getActionMasked(), id, x, y); +// } +// return true; +// } +func UpdateTouchesOnAndroid(action int, id int, x, y int) { + updateTouchesOnAndroid(action, id, x, y) } -type eventDispatcher struct { - touches map[int]position -} - -// touch implements ui.Touch. -type touch struct { - id int - position position -} - -func (t touch) ID() int { - return t.id -} - -func (t touch) Position() (int, int) { - // TODO: Is this OK to adjust the position here? - return int(float64(t.position.x) / ui.CurrentUI().ScreenScale()), - int(float64(t.position.y) / ui.CurrentUI().ScreenScale()) -} - -func (e *eventDispatcher) UpdateTouchesOnAndroid(action int, id int, x, y int) { - switch action { - case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE - e.touches[id] = position{x, y} - e.updateTouches() - case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP - delete(e.touches, id) - e.updateTouches() - } -} - -func (e *eventDispatcher) UpdateTouchesOnIOS(phase int, ptr int, x, y int) { - e.updateTouchesOnIOSImpl(phase, ptr, x, y) +func UpdateTouchesOnIOS(phase int, ptr int, x, y int) { + updateTouchesOnIOSImpl(phase, ptr, x, y) } diff --git a/mobile/touches_android.go b/mobile/touches_android.go index 50aeaf7e1..68081d20c 100644 --- a/mobile/touches_android.go +++ b/mobile/touches_android.go @@ -14,6 +14,17 @@ package mobile -func (e *eventDispatcher) updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { +func updateTouchesOnAndroid(action int, id int, x, y int) { + switch action { + case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE + touches[id] = position{x, y} + updateTouches() + case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP + delete(touches, id) + updateTouches() + } +} + +func updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { panic("not reach") } diff --git a/mobile/touches_empty.go b/mobile/touches_empty.go index 565ac0aa2..f2af52c78 100644 --- a/mobile/touches_empty.go +++ b/mobile/touches_empty.go @@ -18,8 +18,8 @@ package mobile -func (e *eventDispatcher) updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { +func updateTouchesOnAndroid(action int, id int, x, y int) { } -func (e *eventDispatcher) updateTouches() { +func updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { } diff --git a/mobile/touches_ios.go b/mobile/touches_ios.go index 6be6b87db..17d50c0a7 100644 --- a/mobile/touches_ios.go +++ b/mobile/touches_ios.go @@ -22,14 +22,18 @@ package mobile // #import import "C" -func (e *eventDispatcher) updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { +func updateTouchesOnAndroid(action int, id int, x, y int) { + panic("not reach") +} + +func updateTouchesOnIOSImpl(phase int, ptr int, x, y int) { switch phase { case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary: - e.touches[ptr] = position{x, y} - e.updateTouches() + touches[ptr] = position{x, y} + updateTouches() case C.UITouchPhaseEnded, C.UITouchPhaseCancelled: - delete(e.touches, ptr) - e.updateTouches() + delete(touches, ptr) + updateTouches() default: panic("not reach") } diff --git a/mobile/touches_mobile.go b/mobile/touches_mobile.go index 3210f87a6..174dadff7 100644 --- a/mobile/touches_mobile.go +++ b/mobile/touches_mobile.go @@ -20,9 +20,34 @@ import ( "github.com/hajimehoshi/ebiten/internal/ui" ) -func (e *eventDispatcher) updateTouches() { +type position struct { + x int + y int +} + +var ( + touches = map[int]position{} +) + +// touch implements ui.Touch. +type touch struct { + id int + position position +} + +func (t touch) ID() int { + return t.id +} + +func (t touch) Position() (int, int) { + // TODO: Is this OK to adjust the position here? + return int(float64(t.position.x) / ui.CurrentUI().ScreenScale()), + int(float64(t.position.y) / ui.CurrentUI().ScreenScale()) +} + +func updateTouches() { ts := []ui.Touch{} - for id, position := range e.touches { + for id, position := range touches { ts = append(ts, touch{id, position}) } ui.UpdateTouches(ts)