doodle/pkg/cursor/cursor.go
Noah Petherbridge 4efa8d00fc Fancy Mouse Cursors
The gamepad mouse cursor has become THE mouse cursor. It is always visible and your
real cursor is hidden, and this way the game can swap out other cursors for certain
scenarios:

* The Pencil Tool in the editor will use a pencil cursor over the level canvas.
* The Flood Tool has a custom Flood cursor so you don't forget it's selected!

Other improvements:

* The Palette buttons in the editor now render using their swatch's pattern
  instead of only using its color.
* If you have an ultra HD monitor and open a Bounded level in the editor which
  is too small to fill your screen, the editor canvas limits its size to fit
  the level (preferable over showing parts of the level you can't actually play
  as it's out of bounds).
* The "brush size" box is only drawn around the cursor when a relevant tool is
  selected (Pencil, Line, Rect, Ellipse, Eraser)
2022-05-04 22:38:26 -07:00

87 lines
1.7 KiB
Go

// Package cursor handles custom mouse cursor sprite images.
package cursor
import (
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/apps/doodle/pkg/shmem"
"git.kirsle.net/apps/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.
func Draw(e render.Engine) {
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
)