Skip to content

Commit

Permalink
Adding support for manipulating cursors (#114)
Browse files Browse the repository at this point in the history
Adding support for manipulating cursors
  • Loading branch information
dusk125 authored Aug 22, 2024
1 parent 8ed417f commit 2de4cad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
49 changes: 49 additions & 0 deletions backends/opengl/cursor.go
Original file line number Diff line number Diff line change
@@ -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
})
}
5 changes: 5 additions & 0 deletions backends/opengl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,6 +205,9 @@ func NewWindow(cfg WindowConfig) (*Window, error) {
})
}

w.cursor = CreateStandardCursor(ArrowCursor)
w.SetCursor(w.cursor)

w.SetVSync(cfg.VSync)

w.initInput()
Expand All @@ -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()
}
Expand Down

0 comments on commit 2de4cad

Please sign in to comment.