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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user