Enhance rendering functionality by adding cursor visibility check. Updated Render method to conditionally draw the crosshair based on cursor presence in the window. Modified Run method to include cursorInWindow state management.

This commit is contained in:
2025-10-26 20:17:39 +00:00
parent 16502b8e19
commit 73e6a8bd99
2 changed files with 18 additions and 13 deletions

View File

@@ -95,7 +95,7 @@ func (app *WidgetApp) Init() (err error) {
} }
// Render renders the widget tree // 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 // Set the clear color to black
gl.ClearColor(0.0, 0.0, 0.0, 1.0) gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
@@ -130,8 +130,10 @@ func (app *WidgetApp) Render(width, height int, mouseX, mouseY float64) (err err
return return
} }
// Draw crosshair at mouse cursor position // Draw crosshair at mouse cursor position only if cursor is in window
drawCrosshair(float32(mouseX), float32(height)-float32(mouseY), width, height) if cursorInWindow {
drawCrosshair(float32(mouseX), float32(height)-float32(mouseY), width, height)
}
return return
} }

View File

@@ -23,6 +23,7 @@ type Window struct {
resizeThreshold int resizeThreshold int
mouseX float64 mouseX float64
mouseY float64 mouseY float64
cursorInWindow bool
} }
func init() { 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 // 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) { if err = glfw.Init(); chk.E(err) {
return return
} }
@@ -103,6 +104,16 @@ func (w *Window) Run(renderFunc func(windowWidth, windowHeight int, mouseX, mous
log.D.Ln("Character input:", string(char)) 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 w.running = true
for !w.window.ShouldClose() && w.running { for !w.window.ShouldClose() && w.running {
// Get window size (logical size in screen coordinates) // 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 // 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 return
} }
@@ -144,11 +155,3 @@ func (w *Window) Stop() {
func (w *Window) GetWindow() *glfw.Window { func (w *Window) GetWindow() *glfw.Window {
return w.window return w.window
} }
// abs returns the absolute value of an integer
func abs(x int) int {
if x < 0 {
return -x
}
return x
}