main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2022 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/hajimehoshi/ebiten/v2"
  19. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  20. "github.com/hajimehoshi/ebiten/v2/inpututil"
  21. )
  22. const (
  23. screenWidth = 640
  24. screenHeight = 480
  25. )
  26. type pos struct {
  27. x int
  28. y int
  29. }
  30. type Game struct {
  31. releasedTouchIDs []ebiten.TouchID
  32. lastTouchPositions []pos
  33. }
  34. func (g *Game) Update() error {
  35. // In general, Append* function allocates a new slice if the given slice doesn't have enough capacity,
  36. // or otherwise, just extends the length of the given slice.
  37. //
  38. // This example passes an empty slice that might have a capacity in order to reduce the chances of slice allocations.
  39. // You can also pass 'nil' to AppendJustReleasedTouchIDs if you don't care the cost of creating slices.
  40. // In this case, AppendJustReleasedTouchIDs would always create a new slice.
  41. g.releasedTouchIDs = inpututil.AppendJustReleasedTouchIDs(g.releasedTouchIDs[:0])
  42. for _, id := range g.releasedTouchIDs {
  43. // Get the last position of the touch.
  44. // ebiten.TouchPosition would not work as the touch has already gone in this tick.
  45. x, y := inpututil.TouchPositionInPreviousTick(id)
  46. g.lastTouchPositions = append(g.lastTouchPositions, pos{x: x, y: y})
  47. }
  48. const n = 10
  49. if len(g.lastTouchPositions) > n {
  50. copy(g.lastTouchPositions, g.lastTouchPositions[len(g.lastTouchPositions)-n:])
  51. g.lastTouchPositions = g.lastTouchPositions[:n]
  52. }
  53. return nil
  54. }
  55. func (g *Game) Draw(screen *ebiten.Image) {
  56. msg := "Touch the screen and release your finger from it.\n\nLast Positions:\n"
  57. for _, p := range g.lastTouchPositions {
  58. msg += fmt.Sprintf(" (%d, %d)\n", p.x, p.y)
  59. }
  60. ebitenutil.DebugPrint(screen, msg)
  61. }
  62. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  63. return screenWidth, screenHeight
  64. }
  65. func main() {
  66. ebiten.SetWindowSize(screenWidth, screenHeight)
  67. ebiten.SetWindowTitle("Last Touch Positions (Ebitengine Demo)")
  68. if err := ebiten.RunGame(&Game{}); err != nil {
  69. log.Fatal(err)
  70. }
  71. }