main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021 The Ebiten 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. "image"
  17. "image/color"
  18. "log"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  21. "github.com/hajimehoshi/ebiten/v2/inpututil"
  22. "github.com/hajimehoshi/ebiten/v2/vector"
  23. )
  24. const (
  25. screenWidth = 640
  26. screenHeight = 480
  27. )
  28. type Game struct {
  29. grids map[image.Rectangle]ebiten.CursorShapeType
  30. gridColors map[image.Rectangle]color.Color
  31. cursor ebiten.CursorShapeType
  32. }
  33. func (g *Game) Update() error {
  34. pt := image.Pt(ebiten.CursorPosition())
  35. cursor := ebiten.CursorShapeDefault
  36. for r, c := range g.grids {
  37. if pt.In(r) {
  38. cursor = c
  39. break
  40. }
  41. }
  42. // Call SetCursorShape only when this is changed to test Ebitengine remembers the current cursor correctly even when it is hidden.
  43. if g.cursor != cursor {
  44. ebiten.SetCursorShape(cursor)
  45. g.cursor = cursor
  46. }
  47. if inpututil.IsKeyJustPressed(ebiten.KeyC) {
  48. switch ebiten.CursorMode() {
  49. case ebiten.CursorModeVisible:
  50. ebiten.SetCursorMode(ebiten.CursorModeHidden)
  51. case ebiten.CursorModeHidden:
  52. ebiten.SetCursorMode(ebiten.CursorModeVisible)
  53. }
  54. }
  55. return nil
  56. }
  57. func (g *Game) Draw(screen *ebiten.Image) {
  58. for r, c := range g.gridColors {
  59. vector.DrawFilledRect(screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), c, false)
  60. }
  61. switch ebiten.CursorShape() {
  62. case ebiten.CursorShapeDefault:
  63. ebitenutil.DebugPrint(screen, "CursorShape: Default")
  64. case ebiten.CursorShapeText:
  65. ebitenutil.DebugPrint(screen, "CursorShape: Text")
  66. case ebiten.CursorShapeCrosshair:
  67. ebitenutil.DebugPrint(screen, "CursorShape: Crosshair")
  68. case ebiten.CursorShapePointer:
  69. ebitenutil.DebugPrint(screen, "CursorShape: Pointer")
  70. case ebiten.CursorShapeEWResize:
  71. ebitenutil.DebugPrint(screen, "CursorShape: EW Resize")
  72. case ebiten.CursorShapeNSResize:
  73. ebitenutil.DebugPrint(screen, "CursorShape: NS Resize")
  74. case ebiten.CursorShapeNESWResize:
  75. ebitenutil.DebugPrint(screen, "CursorShape: NESW Resize")
  76. case ebiten.CursorShapeNWSEResize:
  77. ebitenutil.DebugPrint(screen, "CursorShape: NWSE Resize")
  78. case ebiten.CursorShapeMove:
  79. ebitenutil.DebugPrint(screen, "CursorShape: Move")
  80. case ebiten.CursorShapeNotAllowed:
  81. ebitenutil.DebugPrint(screen, "CursorShape: Not Allowed")
  82. }
  83. }
  84. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  85. return screenWidth, screenHeight
  86. }
  87. func main() {
  88. g := &Game{
  89. grids: map[image.Rectangle]ebiten.CursorShapeType{
  90. image.Rect(100, 100, 200, 200): ebiten.CursorShapeDefault,
  91. image.Rect(200, 100, 300, 200): ebiten.CursorShapeText,
  92. image.Rect(300, 100, 400, 200): ebiten.CursorShapeCrosshair,
  93. image.Rect(400, 100, 500, 200): ebiten.CursorShapePointer,
  94. image.Rect(100, 200, 200, 300): ebiten.CursorShapeEWResize,
  95. image.Rect(200, 200, 300, 300): ebiten.CursorShapeNSResize,
  96. image.Rect(300, 200, 400, 300): ebiten.CursorShapeNESWResize,
  97. image.Rect(400, 200, 500, 300): ebiten.CursorShapeNWSEResize,
  98. image.Rect(100, 300, 200, 400): ebiten.CursorShapeMove,
  99. image.Rect(200, 300, 300, 400): ebiten.CursorShapeNotAllowed,
  100. },
  101. gridColors: map[image.Rectangle]color.Color{},
  102. }
  103. for rect, c := range g.grids {
  104. clr := color.RGBA{0x40, 0x40, 0x40, 0xff}
  105. switch c % 3 {
  106. case 0:
  107. clr.R = 0x80
  108. case 1:
  109. clr.G = 0x80
  110. case 2:
  111. clr.B = 0x80
  112. }
  113. g.gridColors[rect] = clr
  114. }
  115. ebiten.SetWindowSize(screenWidth, screenHeight)
  116. ebiten.SetWindowTitle("Cursor (Ebitengine Demo)")
  117. if err := ebiten.RunGame(g); err != nil {
  118. log.Fatal(err)
  119. }
  120. }