From 0e75540f8e3c3744d926def7462cac78f861ec17 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 21 Apr 2020 22:33:56 +0900 Subject: [PATCH] monogame: Add IsKeyPressed Updates #1078 --- internal/monogame/monogame.go | 5 +++++ internal/uidriver/monogame/input.go | 4 +++- internal/uidriver/monogame/ui.go | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/monogame/monogame.go b/internal/monogame/monogame.go index 6bdf00cda..a1c4c9f00 100644 --- a/internal/monogame/monogame.go +++ b/internal/monogame/monogame.go @@ -114,6 +114,11 @@ func (g *Game) ResetDestination(viewportWidth, viewportHeight int) { 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 { v js.Value binding js.Value diff --git a/internal/uidriver/monogame/input.go b/internal/uidriver/monogame/input.go index c7ddf5733..c58797264 100644 --- a/internal/uidriver/monogame/input.go +++ b/internal/uidriver/monogame/input.go @@ -18,9 +18,11 @@ package monogame import ( "github.com/hajimehoshi/ebiten/internal/driver" + "github.com/hajimehoshi/ebiten/internal/monogame" ) type Input struct { + game *monogame.Game } 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 { - return false + return i.game.IsKeyPressed(key) } func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool { diff --git a/internal/uidriver/monogame/ui.go b/internal/uidriver/monogame/ui.go index b8e060442..f228b1ae6 100644 --- a/internal/uidriver/monogame/ui.go +++ b/internal/uidriver/monogame/ui.go @@ -39,6 +39,7 @@ func (u *UI) Run(context driver.UIContext) error { defer g.Dispose() u.game = g + u.input.game = g u.context = context u.Graphics().(*graphics.Graphics).SetGame(g) u.updateSize()