diff --git a/cmd/hello/main.go b/cmd/hello/main.go index e627286..8318278 100644 --- a/cmd/hello/main.go +++ b/cmd/hello/main.go @@ -95,7 +95,7 @@ func (app *WidgetApp) Init() (err error) { } // Render renders the widget tree -func (app *WidgetApp) Render(width, height int, mouseX, mouseY float64) (err error) { +func (app *WidgetApp) Render(width, height int, mouseX, mouseY float64, cursorInWindow bool) (err error) { // Set the clear color to black gl.ClearColor(0.0, 0.0, 0.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) @@ -130,8 +130,10 @@ func (app *WidgetApp) Render(width, height int, mouseX, mouseY float64) (err err return } - // Draw crosshair at mouse cursor position - drawCrosshair(float32(mouseX), float32(height)-float32(mouseY), width, height) + // Draw crosshair at mouse cursor position only if cursor is in window + if cursorInWindow { + drawCrosshair(float32(mouseX), float32(height)-float32(mouseY), width, height) + } return } diff --git a/pkg/window/window.go b/pkg/window/window.go index 894e1b2..c755743 100644 --- a/pkg/window/window.go +++ b/pkg/window/window.go @@ -23,6 +23,7 @@ type Window struct { resizeThreshold int mouseX float64 mouseY float64 + cursorInWindow bool } func init() { @@ -44,7 +45,7 @@ func New(width, height int, title string) (w *Window, err error) { } // Run starts the window and runs the application main loop -func (w *Window) Run(renderFunc func(windowWidth, windowHeight int, mouseX, mouseY float64) error) (err error) { +func (w *Window) Run(renderFunc func(windowWidth, windowHeight int, mouseX, mouseY float64, cursorInWindow bool) error) (err error) { if err = glfw.Init(); chk.E(err) { return } @@ -103,6 +104,16 @@ func (w *Window) Run(renderFunc func(windowWidth, windowHeight int, mouseX, mous log.D.Ln("Character input:", string(char)) }) + // Set cursor enter/leave callback + w.window.SetCursorEnterCallback(func(window *glfw.Window, entered bool) { + w.cursorInWindow = entered + if entered { + log.D.Ln("Cursor entered window") + } else { + log.D.Ln("Cursor left window") + } + }) + w.running = true for !w.window.ShouldClose() && w.running { // Get window size (logical size in screen coordinates) @@ -122,7 +133,7 @@ func (w *Window) Run(renderFunc func(windowWidth, windowHeight int, mouseX, mous } // Render with window dimensions and mouse position - if err = renderFunc(windowWidth, windowHeight, w.mouseX, w.mouseY); chk.E(err) { + if err = renderFunc(windowWidth, windowHeight, w.mouseX, w.mouseY, w.cursorInWindow); chk.E(err) { return } @@ -144,11 +155,3 @@ func (w *Window) Stop() { func (w *Window) GetWindow() *glfw.Window { return w.window } - -// abs returns the absolute value of an integer -func abs(x int) int { - if x < 0 { - return -x - } - return x -}