doodle/pkg/cursor/cursor.go
Noah Petherbridge 5b3121171e Fix touchscreen mode detection
* Touchscreen mode used to be detected based on SDL2 GetNumTouchDevices
  but on a Macbook, the trackpad registers as a touch device - worse,
  GetNumTouchDevices will only start returning 1 the first time some
  devices are touched.
* The result was that on macOS the custom mouse cursor was drawn by
  default, but on the first trackpad touch, would disappear in favor of
  assuming the game is running on a touch screen device (which is not
  the case).
* New method: the render engine has an IsFingerDown boolean which will
  be true as long as at least one finger has registered a FingerDown
  event, but not yet a FingerUp event.
* So as long as one finger is down, the mouse cursor can disappear and
  then it comes back on release. This isn't perfectly ideal for pure
  touch devices (ideally the cursor remains hidden until a mouse
  movement without touch occurs).
2024-04-19 22:01:33 -07:00

92 lines
1.8 KiB
Go

// Package cursor handles custom mouse cursor sprite images.
package cursor
import (
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
"git.kirsle.net/SketchyMaze/doodle/pkg/native"
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
"git.kirsle.net/SketchyMaze/doodle/pkg/sprites"
"git.kirsle.net/go/render"
"git.kirsle.net/go/ui"
)
type Cursor struct {
Filename string
Sprite *ui.Image
Offset render.Point
}
// Current selected cursor to draw on screen.
var Current *Cursor
// NoCursor hides the cursor entirely.
var NoCursor = &Cursor{}
// Draw the cursor on screen. NOTE: Does not draw on touchscreen devices.
func Draw(e render.Engine) {
if native.IsTouchScreenMode {
return
}
if Current == nil {
Current = NewPointer(e)
}
if Current.Sprite != nil {
Current.Sprite.Present(e, shmem.Cursor)
}
}
// NewPointer initializes the default pointer cursor.
func NewPointer(e render.Engine) *Cursor {
if pointer != nil {
return pointer
}
img, err := sprites.LoadImage(e, balance.CursorIcon)
if err != nil {
log.Error("NewPointer: %s", err)
}
return &Cursor{
Filename: balance.CursorIcon,
Sprite: img,
}
}
// NewPencil initializes the pencil cursor.
func NewPencil(e render.Engine) *Cursor {
if pencil != nil {
return pencil
}
img, err := sprites.LoadImage(e, balance.PencilIcon)
if err != nil {
log.Error("NewPencil: %s", err)
}
return &Cursor{
Filename: balance.PencilIcon,
Sprite: img,
}
}
// NewFlood initializes the Flood cursor.
func NewFlood(e render.Engine) *Cursor {
if pencil != nil {
return pencil
}
img, err := sprites.LoadImage(e, balance.FloodCursor)
if err != nil {
log.Error("NewFlood: %s", err)
}
return &Cursor{
Filename: balance.FloodCursor,
Sprite: img,
}
}
// Cached singletons of the cursors.
var (
pointer *Cursor
pencil *Cursor
flood *Cursor
)