internal/driver: remove Input

This commit is contained in:
Hajime Hoshi 2022-02-06 18:30:31 +09:00
parent 6f72b15912
commit 2fbfa5444b
17 changed files with 57 additions and 99 deletions

View File

@ -324,7 +324,7 @@ func UpdateStandardGamepadLayoutMappings(mappings string) (bool, error) {
} }
// TouchID represents a touch's identifier. // TouchID represents a touch's identifier.
type TouchID = driver.TouchID type TouchID = ui.TouchID
// AppendTouchIDs appends the current touch states to touches, and returns the extended buffer. // AppendTouchIDs appends the current touch states to touches, and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently. // Giving a slice that already has enough capacity works efficiently.

View File

@ -66,8 +66,6 @@ import (
"reflect" "reflect"
"time" "time"
"unsafe" "unsafe"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
) )
type Gamepad struct { type Gamepad struct {
@ -81,7 +79,7 @@ type Gamepad struct {
} }
type Touch struct { type Touch struct {
ID driver.TouchID ID ui.TouchID
X int X int
Y int Y int
} }
@ -153,7 +151,7 @@ func AppendTouches(touches []Touch) []Touch {
for _, t := range cTouches { for _, t := range cTouches {
touches = append(touches, Touch{ touches = append(touches, Touch{
ID: driver.TouchID(t.id), ID: ui.TouchID(t.id),
X: int(t.x), X: int(t.x),
Y: int(t.y), Y: int(t.y),
}) })

View File

@ -1,27 +0,0 @@
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package driver
type TouchID int
type Input interface {
AppendInputChars(runes []rune) []rune
AppendTouchIDs(touchIDs []TouchID) []TouchID
CursorPosition() (x, y int)
IsKeyPressed(key Key) bool
IsMouseButtonPressed(button MouseButton) bool
TouchPosition(id TouchID) (x, y int)
Wheel() (xoff, yoff float64)
}

View File

@ -1,23 +0,0 @@
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package driver
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
)

View File

@ -52,7 +52,7 @@ func (i *Input) AppendInputChars(runes []rune) []rune {
return nil return nil
} }
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
@ -70,11 +70,11 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
return false return false
} }
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
return false return false
} }
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { func (i *Input) TouchPosition(id TouchID) (x, y int) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()

View File

@ -35,7 +35,7 @@ type Input struct {
scrollY float64 scrollY float64
cursorX int cursorX int
cursorY int cursorY int
touches map[driver.TouchID]pos // TODO: Implement this (#417) touches map[TouchID]pos // TODO: Implement this (#417)
runeBuffer []rune runeBuffer []rune
ui *UserInterface ui *UserInterface
} }
@ -55,7 +55,7 @@ func (i *Input) CursorPosition() (x, y int) {
return i.cursorX, i.cursorY return i.cursorX, i.cursorY
} }
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return nil return nil
} }
@ -68,7 +68,7 @@ func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
return touchIDs return touchIDs
} }
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { func (i *Input) TouchPosition(id TouchID) (x, y int) {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return 0, 0 return 0, 0
} }
@ -118,7 +118,7 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
return ok && i.keyPressed[gk] return ok && i.keyPressed[gk]
} }
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
if !i.ui.isRunning() { if !i.ui.isRunning() {
return false return false
} }
@ -149,10 +149,10 @@ func (i *Input) Wheel() (xoff, yoff float64) {
return i.scrollX, i.scrollY return i.scrollX, i.scrollY
} }
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{ var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
glfw.MouseButtonLeft: driver.MouseButtonLeft, glfw.MouseButtonLeft: MouseButtonLeft,
glfw.MouseButtonRight: driver.MouseButtonRight, glfw.MouseButtonRight: MouseButtonRight,
glfw.MouseButtonMiddle: driver.MouseButtonMiddle, glfw.MouseButtonMiddle: MouseButtonMiddle,
} }
// update must be called from the main thread. // update must be called from the main thread.

View File

@ -68,7 +68,7 @@ type Input struct {
origCursorY int origCursorY int
wheelX float64 wheelX float64
wheelY float64 wheelY float64
touches map[driver.TouchID]pos touches map[TouchID]pos
runeBuffer []rune runeBuffer []rune
ui *UserInterface ui *UserInterface
} }
@ -81,14 +81,14 @@ func (i *Input) CursorPosition() (x, y int) {
return int(xf), int(yf) return int(xf), int(yf)
} }
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID {
for id := range i.touches { for id := range i.touches {
touchIDs = append(touchIDs, id) touchIDs = append(touchIDs, id)
} }
return touchIDs return touchIDs
} }
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { func (i *Input) TouchPosition(id TouchID) (x, y int) {
d := i.ui.DeviceScaleFactor() d := i.ui.DeviceScaleFactor()
for tid, pos := range i.touches { for tid, pos := range i.touches {
if id == tid { if id == tid {
@ -128,13 +128,13 @@ func (i *Input) IsKeyPressed(key driver.Key) bool {
return false return false
} }
var codeToMouseButton = map[int]driver.MouseButton{ var codeToMouseButton = map[int]MouseButton{
0: driver.MouseButtonLeft, 0: MouseButtonLeft,
1: driver.MouseButtonMiddle, 1: MouseButtonMiddle,
2: driver.MouseButtonRight, 2: MouseButtonRight,
} }
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
if i.mouseButtonPressed == nil { if i.mouseButtonPressed == nil {
i.mouseButtonPressed = map[int]bool{} i.mouseButtonPressed = map[int]bool{}
} }
@ -282,9 +282,9 @@ func (in *Input) updateTouchesFromEvent(e js.Value) {
} }
for i := 0; i < j.Length(); i++ { for i := 0; i < j.Length(); i++ {
jj := j.Call("item", i) jj := j.Call("item", i)
id := driver.TouchID(jj.Get("identifier").Int()) id := TouchID(jj.Get("identifier").Int())
if in.touches == nil { if in.touches == nil {
in.touches = map[driver.TouchID]pos{} in.touches = map[TouchID]pos{}
} }
in.touches[id] = pos{ in.touches[id] = pos{
X: jj.Get("clientX").Int(), X: jj.Get("clientX").Int(),
@ -307,9 +307,9 @@ func (i *Input) updateForGo2Cpp() {
x := go2cpp.Call("getTouchX", idx) x := go2cpp.Call("getTouchX", idx)
y := go2cpp.Call("getTouchY", idx) y := go2cpp.Call("getTouchY", idx)
if i.touches == nil { if i.touches == nil {
i.touches = map[driver.TouchID]pos{} i.touches = map[TouchID]pos{}
} }
i.touches[driver.TouchID(id.Int())] = pos{ i.touches[TouchID(id.Int())] = pos{
X: x.Int(), X: x.Int(),
Y: y.Int(), Y: y.Int(),
} }

View File

@ -33,7 +33,7 @@ func (i *Input) CursorPosition() (x, y int) {
return 0, 0 return 0, 0
} }
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID { func (i *Input) AppendTouchIDs(touchIDs []TouchID) []TouchID {
i.ui.m.RLock() i.ui.m.RLock()
defer i.ui.m.RUnlock() defer i.ui.m.RUnlock()
@ -43,7 +43,7 @@ func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
return touchIDs return touchIDs
} }
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) { func (i *Input) TouchPosition(id TouchID) (x, y int) {
i.ui.m.RLock() i.ui.m.RLock()
defer i.ui.m.RUnlock() defer i.ui.m.RUnlock()
@ -73,7 +73,7 @@ func (i *Input) Wheel() (xoff, yoff float64) {
return 0, 0 return 0, 0
} }
func (i *Input) IsMouseButtonPressed(key driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(key MouseButton) bool {
return false return false
} }

View File

@ -27,6 +27,16 @@ type Context interface {
AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64)
} }
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
)
type TouchID int
// RegularTermination represents a regular termination. // RegularTermination represents a regular termination.
// Run can return this error, and if this error is received, // Run can return this error, and if this error is received,
// the game loop should be terminated as soon as possible. // the game loop should be terminated as soon as possible.

View File

@ -128,7 +128,7 @@ func (*UserInterface) SetInitFocused(focused bool) {
func (*UserInterface) Vibrate(duration time.Duration, magnitude float64) { func (*UserInterface) Vibrate(duration time.Duration, magnitude float64) {
} }
func (*UserInterface) Input() driver.Input { func (*UserInterface) Input() *Input {
return &theUserInterface.input return &theUserInterface.input
} }

View File

@ -1431,7 +1431,7 @@ func (u *UserInterface) SetInitFocused(focused bool) {
u.setInitFocused(focused) u.setInitFocused(focused)
} }
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() *Input {
return &u.input return &u.input
} }

View File

@ -661,7 +661,7 @@ func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
} }
} }
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() *Input {
return &u.input return &u.input
} }

View File

@ -187,7 +187,7 @@ func (u *UserInterface) appMain(a app.App) {
x, y := float64(e.X)/s, float64(e.Y)/s x, y := float64(e.X)/s, float64(e.Y)/s
// TODO: Is it ok to cast from int64 to int here? // TODO: Is it ok to cast from int64 to int here?
touches[e.Sequence] = Touch{ touches[e.Sequence] = Touch{
ID: driver.TouchID(e.Sequence), ID: TouchID(e.Sequence),
X: int(x), X: int(x),
Y: int(y), Y: int(y),
} }
@ -446,7 +446,7 @@ func (u *UserInterface) SetInitFocused(focused bool) {
// Do nothing // Do nothing
} }
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() *Input {
return &u.input return &u.input
} }
@ -455,7 +455,7 @@ func (u *UserInterface) Window() driver.Window {
} }
type Touch struct { type Touch struct {
ID driver.TouchID ID TouchID
X int X int
Y int Y int
} }

View File

@ -30,7 +30,7 @@ type position struct {
var ( var (
keys = map[driver.Key]struct{}{} keys = map[driver.Key]struct{}{}
runes []rune runes []rune
touches = map[driver.TouchID]position{} touches = map[ui.TouchID]position{}
) )
var ( var (

View File

@ -20,8 +20,8 @@ import (
"math" "math"
"unicode" "unicode"
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
// https://developer.android.com/reference/android/view/KeyEvent // https://developer.android.com/reference/android/view/KeyEvent
@ -183,10 +183,10 @@ var androidAxisIDToHatID2 = map[int]int{
func UpdateTouchesOnAndroid(action int, id int, x, y int) { func UpdateTouchesOnAndroid(action int, id int, x, y int) {
switch action { switch action {
case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE case 0x00, 0x05, 0x02: // ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_MOVE
touches[driver.TouchID(id)] = position{x, y} touches[ui.TouchID(id)] = position{x, y}
updateInput() updateInput()
case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP case 0x01, 0x06: // ACTION_UP, ACTION_POINTER_UP
delete(touches, driver.TouchID(id)) delete(touches, ui.TouchID(id))
updateInput() updateInput()
} }
} }

View File

@ -20,7 +20,7 @@ package ebitenmobileview
import ( import (
"fmt" "fmt"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
// #cgo CFLAGS: -x objective-c // #cgo CFLAGS: -x objective-c
@ -50,12 +50,12 @@ func UpdateTouchesOnIOS(phase int, ptr int64, x, y int) {
switch phase { switch phase {
case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary: case C.UITouchPhaseBegan, C.UITouchPhaseMoved, C.UITouchPhaseStationary:
id := getIDFromPtr(ptr) id := getIDFromPtr(ptr)
touches[driver.TouchID(id)] = position{x, y} touches[ui.TouchID(id)] = position{x, y}
updateInput() updateInput()
case C.UITouchPhaseEnded, C.UITouchPhaseCancelled: case C.UITouchPhaseEnded, C.UITouchPhaseCancelled:
id := getIDFromPtr(ptr) id := getIDFromPtr(ptr)
delete(ptrToID, ptr) delete(ptrToID, ptr)
delete(touches, driver.TouchID(id)) delete(touches, ui.TouchID(id))
updateInput() updateInput()
default: default:
panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase)) panic(fmt.Sprintf("ebitenmobileview: invalid phase: %d", phase))

View File

@ -15,15 +15,15 @@
package ebiten package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
// A MouseButton represents a mouse button. // A MouseButton represents a mouse button.
type MouseButton = driver.MouseButton type MouseButton = ui.MouseButton
// MouseButtons // MouseButtons
const ( const (
MouseButtonLeft MouseButton = driver.MouseButtonLeft MouseButtonLeft MouseButton = ui.MouseButtonLeft
MouseButtonRight MouseButton = driver.MouseButtonRight MouseButtonRight MouseButton = ui.MouseButtonRight
MouseButtonMiddle MouseButton = driver.MouseButtonMiddle MouseButtonMiddle MouseButton = ui.MouseButtonMiddle
) )