main.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2024 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. "image"
  18. "image/color"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. "github.com/hajimehoshi/ebiten/v2/inpututil"
  23. "github.com/hajimehoshi/ebiten/v2/vector"
  24. )
  25. type Game struct {
  26. contentArea image.Rectangle
  27. // dragging indicates whether a pointing device is dragging the content.
  28. dragging bool
  29. // prevY is the previous Y value of a pointing device in the last frame.
  30. prevY int
  31. // startY is the initial Y value of a pointing device where dragging started.
  32. startY int
  33. // offsetY is the content offset in the Y axis.
  34. offsetY int
  35. // offsetStartY is the content offset in the Y axis when dragging started.
  36. offsetStartY int
  37. // velocityY is the scrolling velocity in the Y axis.
  38. velocityY int
  39. itemCount int
  40. itemHeight int
  41. touchIDs []ebiten.TouchID
  42. justReleasedTouchIDs []ebiten.TouchID
  43. }
  44. func (g *Game) updateInput() {
  45. g.touchIDs = ebiten.AppendTouchIDs(g.touchIDs[:0])
  46. g.justReleasedTouchIDs = inpututil.AppendJustReleasedTouchIDs(g.justReleasedTouchIDs[:0])
  47. }
  48. // pointingDevicePosition returns the position of a pointing device (a touch or a mouse).
  49. func (g *Game) pointingDevicePosition() (x, y int) {
  50. if len(g.touchIDs) > 0 {
  51. return ebiten.TouchPosition(g.touchIDs[0])
  52. }
  53. return ebiten.CursorPosition()
  54. }
  55. // isPointingDevicePressed reports whether a pointing device is pressed.
  56. func (g *Game) isPointingDevicePressed() bool {
  57. if len(g.touchIDs) > 0 {
  58. return true
  59. }
  60. return ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)
  61. }
  62. // isPointingDeviceJustReleased reports whether a pointing device is just released.
  63. func (g *Game) isPointingDeviceJustReleased() bool {
  64. if len(g.justReleasedTouchIDs) > 0 {
  65. return false
  66. }
  67. return inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft)
  68. }
  69. func (g *Game) Update() error {
  70. g.updateInput()
  71. x, y := g.pointingDevicePosition()
  72. defer func() {
  73. g.prevY = y
  74. }()
  75. hovering := image.Pt(x, y).In(g.contentArea)
  76. // If a pointing device is just released, start scrolling.
  77. if g.isPointingDeviceJustReleased() && g.dragging {
  78. g.dragging = false
  79. g.velocityY = y - g.prevY
  80. return nil
  81. }
  82. // Process a mouse wheel.
  83. if _, wheelY := ebiten.Wheel(); wheelY != 0 && !g.dragging && hovering {
  84. g.velocityY = int(wheelY)
  85. }
  86. // If a pointing device is NOT pressed, scroll by the inertia.
  87. if !g.isPointingDevicePressed() {
  88. g.dragging = false
  89. g.setOffsetY(g.offsetY + g.velocityY)
  90. if g.velocityY != 0 {
  91. g.velocityY = int(float64(g.velocityY) * 15.0 / 16.0)
  92. }
  93. return nil
  94. }
  95. // As a pointing device is pressed, stop the inertia.
  96. g.velocityY = 0
  97. if !g.dragging && hovering {
  98. g.dragging = true
  99. g.offsetStartY = g.offsetY
  100. g.startY = y
  101. }
  102. // If a pinting device is pressed, adjust the offset by the movement.
  103. if g.dragging {
  104. g.setOffsetY(g.offsetStartY + y - g.startY)
  105. }
  106. return nil
  107. }
  108. func (g *Game) setOffsetY(offsetY int) {
  109. g.offsetY = offsetY
  110. h := g.contentHeight()
  111. if g.offsetY < -h+g.contentArea.Dy() {
  112. g.offsetY = -h + g.contentArea.Dy()
  113. }
  114. if g.offsetY > 0 {
  115. g.offsetY = 0
  116. }
  117. }
  118. func (g *Game) contentHeight() int {
  119. return g.itemCount * g.itemHeight
  120. }
  121. func (g *Game) Draw(screen *ebiten.Image) {
  122. // Render the content. Use SubImage as a mask.
  123. screenContentArea := screen.SubImage(g.contentArea).(*ebiten.Image)
  124. for i := 0; i < g.itemCount; i++ {
  125. itemRegion := image.Rect(0, i*g.itemHeight, g.contentArea.Dx(), (i+1)*g.itemHeight)
  126. itemRegion = itemRegion.Add(image.Pt(g.contentArea.Min.X, g.contentArea.Min.Y))
  127. itemRegion = itemRegion.Add(image.Pt(0, g.offsetY))
  128. // Skip rendering if the item is out of the content area.
  129. if itemRegion.Intersect(g.contentArea).Empty() {
  130. continue
  131. }
  132. vector.DrawFilledRect(screenContentArea, float32(itemRegion.Min.X), float32(itemRegion.Min.Y), float32(itemRegion.Dx()), float32(itemRegion.Dy()), color.RGBA{byte(i), byte(i), byte(i), 0xff}, false)
  133. text := fmt.Sprintf("Item %d", i)
  134. if i == 0 {
  135. text += " (drag or touch to scroll)"
  136. }
  137. ebitenutil.DebugPrintAt(screenContentArea, text, itemRegion.Min.X, itemRegion.Min.Y)
  138. }
  139. // Render the content border line.
  140. vector.StrokeRect(screen, float32(g.contentArea.Min.X), float32(g.contentArea.Min.Y), float32(g.contentArea.Dx()), float32(g.contentArea.Dy()), 1, color.White, false)
  141. }
  142. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  143. const offset = 16
  144. g.contentArea = image.Rect(offset, offset, outsideWidth-offset, outsideHeight-offset)
  145. return outsideWidth, outsideHeight
  146. }
  147. func main() {
  148. ebiten.SetWindowTitle("Scroll (Ebitengine Demo)")
  149. ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
  150. ebiten.SetWindowSizeLimits(320, 240, -1, -1)
  151. if err := ebiten.RunGame(&Game{
  152. itemCount: 256,
  153. itemHeight: 24,
  154. }); err != nil {
  155. log.Fatal(err)
  156. }
  157. }