First pass at custom cursor display

This commit is contained in:
2020-04-10 19:55:16 +01:00
parent d99a5b9ec3
commit bb3ddc4896
5 changed files with 146 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package ui
import (
"flag"
"fmt"
"image"
"log"
"os"
"runtime/debug"
@@ -23,11 +24,16 @@ var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
)
// TODO: move all scaling into Window, so drivers only need to cope with one
// coordinate space. This will allow us to draw custom mouse cursors in the
// window, rather than in the driver.
type Window struct {
Title string
KeyUpHandlers map[ebiten.Key]func()
MouseWheelHandler func(float64, float64)
cursor *ebiten.Image
// Allow the "game" to be switched out at any time
game Game
@@ -68,6 +74,17 @@ func (w *Window) Layout(_, _ int) (int, int) {
return w.xRes, w.yRes
}
func (w *Window) SetCursor(cursor *ebiten.Image) {
w.cursor = cursor
// Hide the system cursor if we have a custom one
if cursor == nil {
ebiten.SetCursorMode(ebiten.CursorModeVisible)
} else {
ebiten.SetCursorMode(ebiten.CursorModeHidden)
}
}
func (w *Window) Update(screen *ebiten.Image) (outErr error) {
// Ebiten does not like it if we panic inside its main loop
defer func() {
@@ -108,6 +125,16 @@ func (w *Window) Update(screen *ebiten.Image) (outErr error) {
return err
}
if w.cursor != nil {
// TODO: account for scaling, including the hotspot.
pos := image.Pt(ebiten.CursorPosition())
op := ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(pos.X), float64(pos.Y))
if err := screen.DrawImage(w.cursor, &op); err != nil {
return err
}
}
if w.debug {
// Draw FPS, etc, to the screen
msg := fmt.Sprintf("tps=%0.2f fps=%0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())