Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure a single click triggers at most one click event #442

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ type Game struct {

fs spxfs.Dir

inputs inputManager
sounds soundMgr
turtle turtleCanvas
typs map[string]reflect.Type // map: name => sprite type, for all sprites
sprs map[string]Sprite // map: name => sprite prototype, for loaded sprites
items []Shape // shapes on stage (in Zorder), not only sprites
destroyItems []Shape // shapes on stage (in Zorder), not only sprites
tempItems []Shape // temp items

tickMgr tickMgr
events chan event
Expand Down Expand Up @@ -361,6 +363,7 @@ func findObjPtr(v reflect.Value, name string, from int) interface{} {

func (p *Game) startLoad(fs spxfs.Dir, cfg *Config) {
p.sounds.init(p)
p.inputs.init(p)
p.events = make(chan event, 16)
p.fs = fs
p.windowWidth_ = cfg.Width
Expand Down Expand Up @@ -642,16 +645,15 @@ type clicker interface {

func (p *Game) doWhenLeftButtonDown(ev *eventLeftButtonDown) {
point := engine.NewVec2(float64(ev.X), float64(ev.Y))
// TOOD(tanjp) avoid new a array every frame
newItems := make([]Shape, len(p.items))
copy(newItems, p.items)
for _, item := range newItems {
tempItems := p.getTempShapes()
for _, item := range tempItems {
if o, ok := item.(clicker); ok {
proxy := o.getProxy()
if proxy != nil {
isClicked := engine.SyncSpriteCheckCollisionWithPoint(proxy.GetId(), point, true)
if isClicked {
if isClicked && p.inputs.canTriggerClickEvent(proxy.GetId()) {
o.doWhenClick(o)
return
}
}
}
Expand Down Expand Up @@ -690,23 +692,15 @@ func (p *Game) eventLoop(me coroutine.Thread) int {
}
}
func (p *Game) logicLoop(me coroutine.Thread) int {
newItems := make([]Shape, 50)
deltaTime := 0.03
for {
p.Wait(deltaTime)
// copy to temp array
if cap(newItems) < len(p.items) {
newItems = make([]Shape, len(p.items))
} else {
newItems = newItems[:len(p.items)]
}
copy(newItems, p.items)
for _, item := range newItems {
tempItems := p.getTempShapes()
for _, item := range tempItems {
if result, ok := item.(interface{ onUpdate(float64) }); ok {
result.onUpdate(deltaTime)
}
}
newItems = newItems[:0]
}
}

Expand Down Expand Up @@ -1319,6 +1313,25 @@ func (p *Game) getAllShapes() []Shape {
return p.items
}

func (p *Game) getTempShapes() []Shape {
p.tempItems = getTempShapes(p.tempItems, p.items)
return p.tempItems
}

func getTempShapes(dst []Shape, src []Shape) []Shape {
if dst == nil {
dst = make([]Shape, 50)
}
dst = dst[:0]
if cap(dst) < len(src) {
dst = make([]Shape, len(src))
} else {
dst = dst[:len(src)]
}
copy(dst, src)
return dst
}

// -----------------------------------------------------------------------------
// Widget

Expand Down
31 changes: 31 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package spx

import (
"time"

"github.com/realdream-ai/gdspx/pkg/engine"
)

Expand Down Expand Up @@ -264,3 +266,32 @@ type eventFirer interface {
}

// -------------------------------------------------------------------------------------

type inputManager struct {
tempItems []Shape
g *Game
id2Timer map[engine.Object]int64
}

const (
// minimum interval between two mouse click events
inputMouseClickIntervalMs = 50
)

func (p *inputManager) init(g *Game) {
p.tempItems = make([]Shape, 50)
p.id2Timer = make(map[engine.Object]int64)
p.g = g
}

func (p *inputManager) canTriggerClickEvent(id engine.Object) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user clicks and holds for a long time, will the click event trigger multiple times?

In SPX v1, it will only trigger once when pressed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user clicks and holds for a long time, will the click event trigger multiple times?
No, it would only trigger once

currentTime := time.Now()
milliseconds := currentTime.UnixNano() / int64(time.Millisecond)
if lastTime, ok := p.id2Timer[id]; ok {
if milliseconds-lastTime < inputMouseClickIntervalMs {
return false
}
}
p.id2Timer[id] = milliseconds
return true
}