From 2de4cad73af096531d505fa7b65aade040420d6b Mon Sep 17 00:00:00 2001 From: Allen Ray Date: Thu, 22 Aug 2024 10:45:06 -0400 Subject: [PATCH] Adding support for manipulating cursors (#114) Adding support for manipulating cursors --- backends/opengl/cursor.go | 49 +++++++++++++++++++++++++++++++++++++++ backends/opengl/window.go | 5 ++++ 2 files changed, 54 insertions(+) create mode 100644 backends/opengl/cursor.go diff --git a/backends/opengl/cursor.go b/backends/opengl/cursor.go new file mode 100644 index 0000000..0cab3e4 --- /dev/null +++ b/backends/opengl/cursor.go @@ -0,0 +1,49 @@ +package opengl + +import ( + "image" + "runtime" + + "github.com/go-gl/glfw/v3.3/glfw" + "github.com/gopxl/mainthread/v2" + "github.com/gopxl/pixel/v2" +) + +type StandardCursor int + +const ( + ArrowCursor = StandardCursor(glfw.ArrowCursor) + IBeamCursor = StandardCursor(glfw.IBeamCursor) + CrosshairCursor = StandardCursor(glfw.CrosshairCursor) + HandCursor = StandardCursor(glfw.HandCursor) + HResizeCursor = StandardCursor(glfw.HResizeCursor) + VResizeCursor = StandardCursor(glfw.VResizeCursor) +) + +type Cursor = glfw.Cursor + +// CreateStandardCursor creates a new standard cursor. +func CreateStandardCursor(cursorId StandardCursor) *Cursor { + c := mainthread.CallVal(func() *Cursor { + return glfw.CreateStandardCursor(glfw.StandardCursor(cursorId)) + }) + runtime.SetFinalizer(c, (*Cursor).Destroy) + return c +} + +// CreateCursorImage creates a new cursor from an image with the specified hotspot (where the click is registered). +func CreateCursorImage(img image.Image, hot pixel.Vec) *Cursor { + c := mainthread.CallVal(func() *Cursor { + return glfw.CreateCursor(img, int(hot.X), int(hot.Y)) + }) + runtime.SetFinalizer(c, (*Cursor).Destroy) + return c +} + +// SetCursor sets the cursor for the window. +func (w *Window) SetCursor(cursor *Cursor) { + mainthread.Call(func() { + w.window.SetCursor(cursor) + w.cursor = cursor + }) +} diff --git a/backends/opengl/window.go b/backends/opengl/window.go index 8e37645..1e023fa 100644 --- a/backends/opengl/window.go +++ b/backends/opengl/window.go @@ -109,6 +109,7 @@ type Window struct { mouseEnteredCallback func(win *Window, entered bool) mouseMovedCallback func(win *Window, pos pixel.Vec) scrollCallback func(win *Window, scroll pixel.Vec) + cursor *Cursor } var currWin *Window @@ -204,6 +205,9 @@ func NewWindow(cfg WindowConfig) (*Window, error) { }) } + w.cursor = CreateStandardCursor(ArrowCursor) + w.SetCursor(w.cursor) + w.SetVSync(cfg.VSync) w.initInput() @@ -226,6 +230,7 @@ func (w *Window) Destroy() { // Update swaps buffers and polls events. Call this method at the end of each frame. func (w *Window) Update() { + w.SetCursor(w.cursor) w.SwapBuffers() w.UpdateInput() }