mobile: Remove EventDispatcher

This commit is contained in:
Hajime Hoshi 2016-06-25 15:50:03 +09:00
parent 59a21620dc
commit cc5fba729a
7 changed files with 80 additions and 73 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -22,14 +22,18 @@ package mobile
// #import <UIKit/UIKit.h>
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")
}

View File

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