main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2023 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/color"
  18. "log"
  19. "runtime"
  20. "sync"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. "github.com/hajimehoshi/ebiten/v2/inpututil"
  24. )
  25. var pointerImage = ebiten.NewImage(8, 8)
  26. func init() {
  27. pointerImage.Fill(color.RGBA{0xff, 0, 0, 0xff})
  28. }
  29. const (
  30. screenWidth = 640
  31. screenHeight = 480
  32. )
  33. type Game struct {
  34. initOnce sync.Once
  35. mouseX int
  36. mouseY int
  37. x int
  38. y int
  39. }
  40. func (g *Game) Update() error {
  41. g.initOnce.Do(func() {
  42. // initialize cursor position on first update
  43. g.mouseX, g.mouseY = ebiten.CursorPosition()
  44. })
  45. if ebiten.CursorMode() == ebiten.CursorModeCaptured {
  46. cursorX, cursorY := ebiten.CursorPosition()
  47. deltaX, deltaY := cursorX-g.mouseX, cursorY-g.mouseY
  48. g.mouseX, g.mouseY = cursorX, cursorY
  49. if deltaX != 0 {
  50. g.x += deltaX
  51. }
  52. if deltaY != 0 {
  53. g.y += deltaY
  54. }
  55. // Constrain red dot within screen view.
  56. if g.x < 0 {
  57. g.x = 0
  58. } else if g.x > screenWidth-pointerImage.Bounds().Dx() {
  59. g.x = screenWidth - pointerImage.Bounds().Dx()
  60. }
  61. if g.y < 0 {
  62. g.y = 0
  63. } else if g.y > screenHeight-pointerImage.Bounds().Dy() {
  64. g.y = screenHeight - pointerImage.Bounds().Dy()
  65. }
  66. }
  67. if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
  68. if ebiten.CursorMode() == ebiten.CursorModeCaptured {
  69. // Release mouse cursor capture.
  70. ebiten.SetCursorMode(ebiten.CursorModeVisible)
  71. } else {
  72. // Recapture mouse cursor.
  73. ebiten.SetCursorMode(ebiten.CursorModeCaptured)
  74. // Reset mouse cursor position for its return.
  75. g.mouseX, g.mouseY = ebiten.CursorPosition()
  76. }
  77. }
  78. return nil
  79. }
  80. func (g *Game) Draw(screen *ebiten.Image) {
  81. op := &ebiten.DrawImageOptions{}
  82. op.GeoM.Translate(float64(g.x), float64(g.y))
  83. screen.DrawImage(pointerImage, op)
  84. var message string
  85. if ebiten.CursorMode() == ebiten.CursorModeCaptured {
  86. message = fmt.Sprintf("Move the red point with mouse captured\nPress Space to release mouse capture\n(%d, %d)", g.x, g.y)
  87. } else {
  88. message = fmt.Sprintf("The red point can only move when mouse captured\nPress Space to capture mouse\n(%d, %d)", g.x, g.y)
  89. }
  90. ebitenutil.DebugPrint(screen, message)
  91. }
  92. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  93. return screenWidth, screenHeight
  94. }
  95. func main() {
  96. ebiten.SetWindowTitle("Mouse Capture (Ebitengine Demo)")
  97. ebiten.SetWindowSize(screenWidth, screenHeight)
  98. // Web browsers allow cursor mode capture only with user interaction.
  99. // Start without cursor captured with message indicating how to capture.
  100. if runtime.GOOS != "js" {
  101. ebiten.SetCursorMode(ebiten.CursorModeCaptured)
  102. }
  103. g := &Game{
  104. x: screenWidth / 2,
  105. y: screenHeight / 2,
  106. }
  107. if err := ebiten.RunGame(g); err != nil {
  108. log.Fatal(err)
  109. }
  110. }