Skip to content

Commit

Permalink
Add touch to room placement; unify getting cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
kettek committed Jun 30, 2024
1 parent 3a6d8f4 commit b62f818
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
18 changes: 12 additions & 6 deletions internal/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,8 @@ func (g *Game) DrawTower(screen *ebiten.Image) {
}

func (g *Game) CheckUI() (bool, UICheckKind) {
mx, my := IntToFloat2(ebiten.CursorPosition())
mx, my := g.CursorPosition()

if len(g.releasedTouchIDs) > 0 && inpututil.IsTouchJustReleased(g.releasedTouchIDs[0]) {
mx, my = IntToFloat2(inpututil.TouchPositionInPreviousTick(g.releasedTouchIDs[0]))
} else if len(g.touchIDs) > 0 {
mx, my = IntToFloat2(ebiten.TouchPosition(g.touchIDs[0]))
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) || len(g.releasedTouchIDs) > 0 {
if g.ui.Check(mx, my, UICheckClick) {
return true, UICheckClick
Expand Down Expand Up @@ -256,6 +251,17 @@ func (g *Game) CheckUI() (bool, UICheckKind) {
return false, UICheckNone
}

func (g *Game) CursorPosition() (float64, float64) {
mx, my := IntToFloat2(ebiten.CursorPosition())

if len(g.releasedTouchIDs) > 0 && inpututil.IsTouchJustReleased(g.releasedTouchIDs[0]) {
mx, my = IntToFloat2(inpututil.TouchPositionInPreviousTick(g.releasedTouchIDs[0]))
} else if len(g.touchIDs) > 0 {
mx, my = IntToFloat2(ebiten.TouchPosition(g.touchIDs[0]))
}
return mx, my
}

func (g *Game) UpdateInfo() {
if g.tower.targetStories == -1 {
g.ui.gameInfoPanel.storyText.SetText(fmt.Sprintf("Stories: %d", len(g.tower.Stories)-1))
Expand Down
2 changes: 1 addition & 1 deletion internal/game/gamebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (s *GameStateBuild) Update(g *Game) GameState {
if handled, kind := g.CheckUI(); !handled {
// Check for mouse hover and click.
if kind == UICheckHover {
mx, my := IntToFloat2(ebiten.CursorPosition())
mx, my := g.CursorPosition()

// Center of screen.
cx := float64(g.lastWidth) / 2
Expand Down
5 changes: 1 addition & 4 deletions internal/game/gamepre.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,11 @@ func (s *GameStatePre) Update(g *Game) GameState {
s.info.Layout(nil, &g.uiOptions)
s.info.SetPosition(w/2-s.info.Width()/2, y)

mx, my := IntToFloat2(ebiten.CursorPosition())
click := ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
if len(g.releasedTouchIDs) > 0 && inpututil.IsTouchJustReleased(g.releasedTouchIDs[0]) {
click = true
mx, my = IntToFloat2(inpututil.TouchPositionInPreviousTick(g.releasedTouchIDs[0]))
} else if len(g.touchIDs) > 0 {
mx, my = IntToFloat2(ebiten.TouchPosition(g.touchIDs[0]))
}
mx, my := g.CursorPosition()
if s.short.Check(mx, my, UICheckHover) {
if click {
s.short.Check(mx, my, UICheckClick)
Expand Down

0 comments on commit b62f818

Please sign in to comment.