doodle/pkg/uix/canvas_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

78 lines
1.7 KiB
Go

package uix
import (
"git.kirsle.net/apps/doodle/pkg/cursor"
"git.kirsle.net/apps/doodle/pkg/drawtool"
"git.kirsle.net/apps/doodle/pkg/shmem"
"git.kirsle.net/go/render"
"git.kirsle.net/go/ui"
)
// IsCursorOver returns true if the mouse cursor is physically over top
// of the canvas's widget space.
func (w *Canvas) IsCursorOver() bool {
var (
P = ui.AbsolutePosition(w)
S = w.Size()
)
return shmem.Cursor.Inside(render.Rect{
X: P.X,
Y: P.Y,
W: S.W,
H: S.H,
})
}
// presentCursor draws something at the mouse cursor on the Canvas.
//
// This is currently used in Edit Mode when you're drawing a shape with a thick
// brush size, and draws a "preview rect" under the cursor of how big a click
// will be at that size.
func (w *Canvas) presentCursor(e render.Engine) {
// Are we to show a custom mouse cursor?
if w.FancyCursors {
switch w.Tool {
case drawtool.PencilTool:
w.cursor = cursor.NewPencil(e)
case drawtool.FloodTool:
w.cursor = cursor.NewFlood(e)
default:
w.cursor = nil
}
if w.IsCursorOver() && w.cursor != nil {
cursor.Current = w.cursor
} else {
cursor.Current = cursor.NewPointer(e)
}
}
if !w.IsCursorOver() {
return
}
// Are we editing with a thick brush?
if w.Tool == drawtool.LineTool || w.Tool == drawtool.RectTool ||
w.Tool == drawtool.PencilTool || w.Tool == drawtool.EllipseTool ||
w.Tool == drawtool.EraserTool {
// Draw a box where the brush size is.
if w.BrushSize > 0 {
var r = w.BrushSize
rect := render.Rect{
X: shmem.Cursor.X - r,
Y: shmem.Cursor.Y - r,
W: r * 2,
H: r * 2,
}
e.DrawRect(render.Black, rect)
rect.X++
rect.Y++
rect.W -= 2
rect.H -= 2
e.DrawRect(render.RGBA(153, 153, 153, 153), rect)
}
}
}