internal/ui: call updateInputState after layoutGame

The cursor position is affected by the current layout. Then, input
states should be updated after layoutGame is called.

Updates #2763
This commit is contained in:
Hajime Hoshi 2023-09-17 14:22:58 +09:00
parent a62b8a00e7
commit 8c25b07336
7 changed files with 35 additions and 13 deletions

View File

@ -118,6 +118,11 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
return nil
}
// Update the input state after the layout is updated as a cursor position is affected by the layout.
if err := ui.updateInputState(); err != nil {
return err
}
// Ensure that Update is called once before Draw so that Update can be used for initialization.
if !c.updateCalled && updateCount == 0 {
updateCount = 1

View File

@ -224,3 +224,8 @@ func (u *userInterfaceImpl) keyName(key Key) string {
func UpdateInputFromEvent(e js.Value) {
theUI.updateInputFromEvent(e)
}
func (u *userInterfaceImpl) updateInputState() error {
// TODO: Adjust cursor and touch positions based on the latest layout.
return nil
}

View File

@ -26,7 +26,7 @@ type TouchForInput struct {
Y float64
}
func (u *userInterfaceImpl) updateInputState(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
func (u *userInterfaceImpl) updateInputStateFromOutside(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
u.m.Lock()
defer u.m.Unlock()
@ -48,6 +48,11 @@ func (u *userInterfaceImpl) updateInputState(keys map[Key]struct{}, runes []rune
}
}
func (u *userInterfaceImpl) updateInputState() error {
// TODO: Adjust cursor and touch positions based on the latest layout.
return nil
}
func KeyName(key Key) string {
// TODO: Implement this.
return ""

View File

@ -25,7 +25,23 @@ package ui
// const int kScreenHeight = 1080;
import "C"
func (u *userInterfaceImpl) updateInputState() {
import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
)
func (u *userInterfaceImpl) updateInputState() error {
var ferr error
u.mainThread.Call(func() {
if err := gamepad.Update(); err != nil {
ferr = err
return
}
u.updateInputStateImpl()
})
return ferr
}
func (u *userInterfaceImpl) updateInputStateImpl() {
C.ebitengine_UpdateTouches()
u.nativeTouches = u.nativeTouches[:0]

View File

@ -1140,9 +1140,6 @@ func (u *userInterfaceImpl) update() (float64, float64, error) {
} else {
glfw.WaitEvents()
}
if err := u.updateInputState(); err != nil {
return 0, 0, err
}
// In the initial state on macOS, the window is not shown (#2620).
for u.window.GetAttrib(glfw.Visible) != 0 && !u.isRunnableOnUnfocused() && u.window.GetAttrib(glfw.Focused) == 0 && !u.window.ShouldClose() {

View File

@ -219,7 +219,7 @@ func (u *userInterfaceImpl) appMain(a app.App) {
for _, t := range touches {
ts = append(ts, t)
}
u.updateInputState(keys, runes, ts)
u.updateInputStateFromOutside(keys, runes, ts)
}
}
}
@ -451,7 +451,7 @@ func (u *userInterfaceImpl) Monitor() *Monitor {
}
func (u *userInterfaceImpl) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
u.updateInputState(keys, runes, touches)
u.updateInputStateFromOutside(keys, runes, touches)
if u.fpsMode == FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded()
}

View File

@ -28,7 +28,6 @@ import (
"golang.org/x/sync/errgroup"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
@ -117,11 +116,6 @@ func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
for {
recordProfilerHeartbeat()
u.mainThread.Call(func() {
gamepad.Update()
u.updateInputState()
})
if err := u.context.updateFrame(u.graphicsDriver, float64(C.kScreenWidth), float64(C.kScreenHeight), deviceScaleFactor, u, func() {
u.egl.swapBuffers()
}); err != nil {