Skip to content

Commit

Permalink
Changes to resolve duplicate inputs when tps and
Browse files Browse the repository at this point in the history
fps are out of sync
  • Loading branch information
mcarpenter622 committed May 13, 2023
1 parent 6a06cc7 commit 507f5ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions _examples/demo/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"log"
"sort"

"golang.org/x/text/collate"
"golang.org/x/text/language"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"

"image/color"
_ "image/png"
Expand All @@ -32,6 +34,7 @@ func main() {
ebiten.SetWindowTitle("Ebiten UI Demo")
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
ebiten.SetScreenClearedEveryFrame(false)
ebiten.SetVsyncEnabled(false)

ui, closeUI, err := createUI()
if err != nil {
Expand Down Expand Up @@ -380,4 +383,6 @@ func (g *game) Update() error {

func (g *game) Draw(screen *ebiten.Image) {
g.ui.Draw(screen)

ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.ActualFPS()))
}
4 changes: 3 additions & 1 deletion _examples/widget_demos/cursor_updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ func (cu *cursor_updater) Update() {
cu.systemPosition = image.Point{X, Y}

}
func (cu *cursor_updater) Draw() {
func (cu *cursor_updater) Draw(screen *ebiten.Image) {
}
func (cu *cursor_updater) AfterDraw(screen *ebiten.Image) {
}

// MouseButtonPressed returns whether mouse button b is currently pressed.
Expand Down
9 changes: 6 additions & 3 deletions input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type CursorUpdater interface {
//Note that before this is called the current cursor shape is reset to DEFAULT every cycle
Update()
//Called at the beginning of every Draw call.
Draw()
Draw(screen *ebiten.Image)
//Called at the end of every Draw call
AfterDraw(screen *ebiten.Image)
// MouseButtonPressed returns whether mouse button b is currently pressed.
MouseButtonPressed(b ebiten.MouseButton) bool
// MouseButtonJustPressed returns whether mouse button b has just been pressed.
Expand Down Expand Up @@ -157,10 +159,10 @@ func Update() {

func Draw(screen *ebiten.Image) {
windowSize = screen.Bounds().Max
currentCursorUpdater.Draw()
currentCursorUpdater.Draw(screen)
}

func DrawAfter(screen *ebiten.Image) {
func AfterDraw(screen *ebiten.Image) {
posX, posY := currentCursorUpdater.CursorPosition()
if posX < 0 || posY < 0 || posX > windowSize.X || posY > windowSize.Y {
return
Expand Down Expand Up @@ -196,4 +198,5 @@ func DrawAfter(screen *ebiten.Image) {
ebiten.SetCursorMode(ebiten.CursorModeHidden)
}
}
currentCursorUpdater.AfterDraw(screen)
}
11 changes: 7 additions & 4 deletions internal/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ var InputHandler *DefaultInternalHandler = &DefaultInternalHandler{KeyPressed: m

// Update updates the input system. This is called by the UI.
func (handler *DefaultInternalHandler) Update() {
handler.InputChars = handler.InputChars[:0]
handler.WheelX, handler.WheelY = 0, 0

touches := ebiten.TouchIDs()
if len(touches) > 0 {
handler.isTouched = true
Expand Down Expand Up @@ -74,14 +71,20 @@ func (handler *DefaultInternalHandler) Update() {

}

func (handler *DefaultInternalHandler) Draw() {
func (handler *DefaultInternalHandler) Draw(screen *ebiten.Image) {
handler.LeftMouseButtonJustPressed = handler.LeftMouseButtonPressed && handler.LeftMouseButtonPressed != handler.LastLeftMouseButtonPressed
handler.MiddleMouseButtonJustPressed = handler.MiddleMouseButtonPressed && handler.MiddleMouseButtonPressed != handler.LastMiddleMouseButtonPressed
handler.RightMouseButtonJustPressed = handler.RightMouseButtonPressed && handler.RightMouseButtonPressed != handler.LastRightMouseButtonPressed

handler.LastLeftMouseButtonPressed = handler.LeftMouseButtonPressed
handler.LastMiddleMouseButtonPressed = handler.MiddleMouseButtonPressed
handler.LastRightMouseButtonPressed = handler.RightMouseButtonPressed

}

func (handler *DefaultInternalHandler) AfterDraw(screen *ebiten.Image) {
handler.InputChars = handler.InputChars[:0]
handler.WheelX, handler.WheelY = 0, 0
}

func (handler *DefaultInternalHandler) MouseButtonPressed(b ebiten.MouseButton) bool {
Expand Down
2 changes: 1 addition & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (u *UI) Update() {
func (u *UI) Draw(screen *ebiten.Image) {
event.ExecuteDeferred()
input.Draw(screen)
defer input.DrawAfter(screen)
defer input.AfterDraw(screen)
x, y := screen.Bounds().Dx(), screen.Bounds().Dy()
rect := image.Rect(0, 0, x, y)

Expand Down

0 comments on commit 507f5ce

Please sign in to comment.