mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
input: Refactoring: Remove input dependencies from ui
This commit is contained in:
parent
a980de8326
commit
6cdcd1ee62
@ -186,87 +186,68 @@ func (i *Input) UpdateGamepads() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnKeyDown(e js.Value) {
|
func (i *Input) Update(e js.Value) {
|
||||||
c := e.Get("code")
|
switch e.Get("type").String() {
|
||||||
if c == js.Undefined() {
|
case "keydown":
|
||||||
code := e.Get("keyCode").Int()
|
c := e.Get("code")
|
||||||
if keyCodeToKeyEdge[code] == driver.KeyUp ||
|
if c == js.Undefined() {
|
||||||
keyCodeToKeyEdge[code] == driver.KeyDown ||
|
code := e.Get("keyCode").Int()
|
||||||
keyCodeToKeyEdge[code] == driver.KeyLeft ||
|
if keyCodeToKeyEdge[code] == driver.KeyUp ||
|
||||||
keyCodeToKeyEdge[code] == driver.KeyRight ||
|
keyCodeToKeyEdge[code] == driver.KeyDown ||
|
||||||
keyCodeToKeyEdge[code] == driver.KeyBackspace ||
|
keyCodeToKeyEdge[code] == driver.KeyLeft ||
|
||||||
keyCodeToKeyEdge[code] == driver.KeyTab {
|
keyCodeToKeyEdge[code] == driver.KeyRight ||
|
||||||
|
keyCodeToKeyEdge[code] == driver.KeyBackspace ||
|
||||||
|
keyCodeToKeyEdge[code] == driver.KeyTab {
|
||||||
|
e.Call("preventDefault")
|
||||||
|
}
|
||||||
|
i.keyDownEdge(code)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cs := c.String()
|
||||||
|
if cs == keyToCodes[driver.KeyUp][0] ||
|
||||||
|
cs == keyToCodes[driver.KeyDown][0] ||
|
||||||
|
cs == keyToCodes[driver.KeyLeft][0] ||
|
||||||
|
cs == keyToCodes[driver.KeyRight][0] ||
|
||||||
|
cs == keyToCodes[driver.KeyBackspace][0] ||
|
||||||
|
cs == keyToCodes[driver.KeyTab][0] {
|
||||||
e.Call("preventDefault")
|
e.Call("preventDefault")
|
||||||
}
|
}
|
||||||
theInput.keyDownEdge(code)
|
i.keyDown(cs)
|
||||||
return
|
case "keypress":
|
||||||
}
|
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
|
||||||
cs := c.String()
|
i.runeBuffer = append(i.runeBuffer, r)
|
||||||
if cs == keyToCodes[driver.KeyUp][0] ||
|
}
|
||||||
cs == keyToCodes[driver.KeyDown][0] ||
|
case "keyup":
|
||||||
cs == keyToCodes[driver.KeyLeft][0] ||
|
if e.Get("code") == js.Undefined() {
|
||||||
cs == keyToCodes[driver.KeyRight][0] ||
|
// Assume that UA is Edge.
|
||||||
cs == keyToCodes[driver.KeyBackspace][0] ||
|
code := e.Get("keyCode").Int()
|
||||||
cs == keyToCodes[driver.KeyTab][0] {
|
i.keyUpEdge(code)
|
||||||
e.Call("preventDefault")
|
return
|
||||||
}
|
}
|
||||||
theInput.keyDown(cs)
|
code := e.Get("code").String()
|
||||||
}
|
i.keyUp(code)
|
||||||
|
case "mousedown":
|
||||||
func OnKeyPress(e js.Value) {
|
button := e.Get("button").Int()
|
||||||
if r := rune(e.Get("charCode").Int()); unicode.IsPrint(r) {
|
i.mouseDown(button)
|
||||||
theInput.runeBuffer = append(theInput.runeBuffer, r)
|
i.setMouseCursorFromEvent(e)
|
||||||
|
case "mouseup":
|
||||||
|
button := e.Get("button").Int()
|
||||||
|
i.mouseUp(button)
|
||||||
|
i.setMouseCursorFromEvent(e)
|
||||||
|
case "mousemove":
|
||||||
|
i.setMouseCursorFromEvent(e)
|
||||||
|
case "wheel":
|
||||||
|
// TODO: What if e.deltaMode is not DOM_DELTA_PIXEL?
|
||||||
|
i.wheelX = -e.Get("deltaX").Float()
|
||||||
|
i.wheelY = -e.Get("deltaY").Float()
|
||||||
|
case "touchstart", "touchend", "touchmove":
|
||||||
|
i.updateTouches(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnKeyUp(e js.Value) {
|
func (i *Input) setMouseCursorFromEvent(e js.Value) {
|
||||||
if e.Get("code") == js.Undefined() {
|
|
||||||
// Assume that UA is Edge.
|
|
||||||
code := e.Get("keyCode").Int()
|
|
||||||
theInput.keyUpEdge(code)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
code := e.Get("code").String()
|
|
||||||
theInput.keyUp(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnMouseDown(e js.Value) {
|
|
||||||
button := e.Get("button").Int()
|
|
||||||
theInput.mouseDown(button)
|
|
||||||
setMouseCursorFromEvent(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnMouseUp(e js.Value) {
|
|
||||||
button := e.Get("button").Int()
|
|
||||||
theInput.mouseUp(button)
|
|
||||||
setMouseCursorFromEvent(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnMouseMove(e js.Value) {
|
|
||||||
setMouseCursorFromEvent(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnWheel(e js.Value) {
|
|
||||||
// TODO: What if e.deltaMode is not DOM_DELTA_PIXEL?
|
|
||||||
theInput.wheelX = -e.Get("deltaX").Float()
|
|
||||||
theInput.wheelY = -e.Get("deltaY").Float()
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnTouchStart(e js.Value) {
|
|
||||||
theInput.updateTouches(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnTouchEnd(e js.Value) {
|
|
||||||
theInput.updateTouches(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func OnTouchMove(e js.Value) {
|
|
||||||
theInput.updateTouches(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setMouseCursorFromEvent(e js.Value) {
|
|
||||||
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
x, y := e.Get("clientX").Int(), e.Get("clientY").Int()
|
||||||
theInput.setMouseCursor(x, y)
|
i.setMouseCursor(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) updateTouches(e js.Value) {
|
func (i *Input) updateTouches(e js.Value) {
|
||||||
|
@ -27,7 +27,6 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
"github.com/hajimehoshi/ebiten/internal/devicescale"
|
||||||
"github.com/hajimehoshi/ebiten/internal/driver"
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
||||||
"github.com/hajimehoshi/ebiten/internal/hooks"
|
"github.com/hajimehoshi/ebiten/internal/hooks"
|
||||||
"github.com/hajimehoshi/ebiten/internal/input"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var canvas js.Value
|
var canvas js.Value
|
||||||
@ -35,6 +34,7 @@ var canvas js.Value
|
|||||||
type inputDriver interface {
|
type inputDriver interface {
|
||||||
driver.Input
|
driver.Input
|
||||||
|
|
||||||
|
Update(e js.Value)
|
||||||
UpdateGamepads()
|
UpdateGamepads()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -328,20 +328,40 @@ func init() {
|
|||||||
|
|
||||||
// Keyboard
|
// Keyboard
|
||||||
// Don't 'preventDefault' on keydown events or keypress events wouldn't work (#715).
|
// Don't 'preventDefault' on keydown events or keypress events wouldn't work (#715).
|
||||||
canvas.Call("addEventListener", "keydown", js.NewEventCallback(0, input.OnKeyDown))
|
canvas.Call("addEventListener", "keydown", js.NewEventCallback(0, func(e js.Value) {
|
||||||
canvas.Call("addEventListener", "keypress", js.NewEventCallback(js.PreventDefault, input.OnKeyPress))
|
currentUI.input.Update(e)
|
||||||
canvas.Call("addEventListener", "keyup", js.NewEventCallback(js.PreventDefault, input.OnKeyUp))
|
}))
|
||||||
|
canvas.Call("addEventListener", "keypress", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
canvas.Call("addEventListener", "keyup", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
|
||||||
// Mouse
|
// Mouse
|
||||||
canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, input.OnMouseDown))
|
canvas.Call("addEventListener", "mousedown", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, input.OnMouseUp))
|
currentUI.input.Update(e)
|
||||||
canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, input.OnMouseMove))
|
}))
|
||||||
canvas.Call("addEventListener", "wheel", js.NewEventCallback(js.PreventDefault, input.OnWheel))
|
canvas.Call("addEventListener", "mouseup", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
canvas.Call("addEventListener", "mousemove", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
canvas.Call("addEventListener", "wheel", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
|
||||||
// Touch
|
// Touch
|
||||||
canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, input.OnTouchStart))
|
canvas.Call("addEventListener", "touchstart", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
canvas.Call("addEventListener", "touchend", js.NewEventCallback(js.PreventDefault, input.OnTouchEnd))
|
currentUI.input.Update(e)
|
||||||
canvas.Call("addEventListener", "touchmove", js.NewEventCallback(js.PreventDefault, input.OnTouchMove))
|
}))
|
||||||
|
canvas.Call("addEventListener", "touchend", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
canvas.Call("addEventListener", "touchmove", js.NewEventCallback(js.PreventDefault, func(e js.Value) {
|
||||||
|
currentUI.input.Update(e)
|
||||||
|
}))
|
||||||
|
|
||||||
// Gamepad
|
// Gamepad
|
||||||
window.Call("addEventListener", "gamepadconnected", js.NewCallback(func(e []js.Value) {
|
window.Call("addEventListener", "gamepadconnected", js.NewCallback(func(e []js.Value) {
|
||||||
|
Loading…
Reference in New Issue
Block a user