2022-05-05 05:38:26 +00:00
|
|
|
// Package cursor handles custom mouse cursor sprite images.
|
|
|
|
package cursor
|
|
|
|
|
|
|
|
import (
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2022-09-25 00:45:54 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/native"
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/sprites"
|
2022-05-05 05:38:26 +00:00
|
|
|
"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{}
|
|
|
|
|
2022-09-25 00:45:54 +00:00
|
|
|
// Draw the cursor on screen. NOTE: Does not draw on touchscreen devices.
|
2022-05-05 05:38:26 +00:00
|
|
|
func Draw(e render.Engine) {
|
2024-04-20 05:42:47 +00:00
|
|
|
if native.IsTouchScreenMode() {
|
2022-09-25 00:45:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-05 05:38:26 +00:00
|
|
|
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
|
|
|
|
)
|