monogame: Add IsKeyPressed

Updates #1078
This commit is contained in:
Hajime Hoshi 2020-04-21 22:33:56 +09:00
parent ea99743e0d
commit 0e75540f8e
3 changed files with 9 additions and 1 deletions

View File

@ -114,6 +114,11 @@ func (g *Game) ResetDestination(viewportWidth, viewportHeight int) {
g.binding.Call("SetDestination", nil, viewportWidth, viewportHeight) g.binding.Call("SetDestination", nil, viewportWidth, viewportHeight)
} }
func (g *Game) IsKeyPressed(key driver.Key) bool {
// Pass a string of the key since both driver.Key value and XNA's key value are not reliable.
return g.binding.Call("IsKeyPressed", key.String()).Bool()
}
type RenderTarget2D struct { type RenderTarget2D struct {
v js.Value v js.Value
binding js.Value binding js.Value

View File

@ -18,9 +18,11 @@ package monogame
import ( import (
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/monogame"
) )
type Input struct { type Input struct {
game *monogame.Game
} }
func (i *Input) CursorPosition() (x, y int) { func (i *Input) CursorPosition() (x, y int) {
@ -56,7 +58,7 @@ func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool
} }
func (i *Input) IsKeyPressed(key driver.Key) bool { func (i *Input) IsKeyPressed(key driver.Key) bool {
return false return i.game.IsKeyPressed(key)
} }
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {

View File

@ -39,6 +39,7 @@ func (u *UI) Run(context driver.UIContext) error {
defer g.Dispose() defer g.Dispose()
u.game = g u.game = g
u.input.game = g
u.context = context u.context = context
u.Graphics().(*graphics.Graphics).SetGame(g) u.Graphics().(*graphics.Graphics).SetGame(g)
u.updateSize() u.updateSize()