mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 12:32:05 +01:00
mobile: Remove EventDispatcher
This commit is contained in:
parent
59a21620dc
commit
cc5fba729a
@ -26,6 +26,6 @@ func render() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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 nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,7 @@ func render() error {
|
|||||||
return ui.Render(chError)
|
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)
|
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, title)
|
||||||
return &eventDispatcher{
|
return nil
|
||||||
touches: map[int]position{},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
@ -16,75 +16,44 @@ package mobile
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten"
|
"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.
|
// 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 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)
|
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 {
|
func Render() error {
|
||||||
return render()
|
return render()
|
||||||
}
|
}
|
||||||
|
|
||||||
type position struct {
|
// UpdateTouchesOnAndroid updates the touch state on Android.
|
||||||
x int
|
//
|
||||||
y int
|
// 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 {
|
func UpdateTouchesOnIOS(phase int, ptr int, x, y int) {
|
||||||
touches map[int]position
|
updateTouchesOnIOSImpl(phase, ptr, x, y)
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,17 @@
|
|||||||
|
|
||||||
package mobile
|
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")
|
panic("not reach")
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@
|
|||||||
|
|
||||||
package mobile
|
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) {
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,18 @@ package mobile
|
|||||||
// #import <UIKit/UIKit.h>
|
// #import <UIKit/UIKit.h>
|
||||||
import "C"
|
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 {
|
switch phase {
|
||||||
case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary:
|
case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary:
|
||||||
e.touches[ptr] = position{x, y}
|
touches[ptr] = position{x, y}
|
||||||
e.updateTouches()
|
updateTouches()
|
||||||
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
|
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
|
||||||
delete(e.touches, ptr)
|
delete(touches, ptr)
|
||||||
e.updateTouches()
|
updateTouches()
|
||||||
default:
|
default:
|
||||||
panic("not reach")
|
panic("not reach")
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,34 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"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{}
|
ts := []ui.Touch{}
|
||||||
for id, position := range e.touches {
|
for id, position := range touches {
|
||||||
ts = append(ts, touch{id, position})
|
ts = append(ts, touch{id, position})
|
||||||
}
|
}
|
||||||
ui.UpdateTouches(ts)
|
ui.UpdateTouches(ts)
|
||||||
|
Loading…
Reference in New Issue
Block a user