internal/glfw: refactoring: make callback handlers consistent

This commit is contained in:
Hajime Hoshi 2022-05-19 01:49:21 +09:00
parent 7189c7f649
commit 8d2b08dd14
6 changed files with 40 additions and 29 deletions

View File

@ -25,6 +25,7 @@ var (
charModsCallbacks = map[CharModsCallback]glfw.CharModsCallback{} charModsCallbacks = map[CharModsCallback]glfw.CharModsCallback{}
closeCallbacks = map[CloseCallback]glfw.CloseCallback{} closeCallbacks = map[CloseCallback]glfw.CloseCallback{}
framebufferSizeCallbacks = map[FramebufferSizeCallback]glfw.FramebufferSizeCallback{} framebufferSizeCallbacks = map[FramebufferSizeCallback]glfw.FramebufferSizeCallback{}
monitorCallbacks = map[MonitorCallback]glfw.MonitorCallback{}
scrollCallbacks = map[ScrollCallback]glfw.ScrollCallback{} scrollCallbacks = map[ScrollCallback]glfw.ScrollCallback{}
sizeCallbacks = map[SizeCallback]glfw.SizeCallback{} sizeCallbacks = map[SizeCallback]glfw.SizeCallback{}
) )
@ -65,6 +66,22 @@ func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) F
return id return id
} }
func ToMonitorCallback(cb func(monitor *Monitor, event PeripheralEvent)) MonitorCallback {
if cb == nil {
return 0
}
id := MonitorCallback(len(monitorCallbacks) + 1)
var gcb glfw.MonitorCallback = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
var m *Monitor
if monitor != nil {
m = &Monitor{monitor}
}
cb(m, PeripheralEvent(event))
}
monitorCallbacks[id] = gcb
return id
}
func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback { func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback {
if cb == nil { if cb == nil {
return 0 return 0

View File

@ -48,6 +48,20 @@ func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) F
})) }))
} }
func ToMonitorCallback(cb func(monitor *Monitor, event PeripheralEvent)) MonitorCallback {
if cb == nil {
return 0
}
return MonitorCallback(windows.NewCallbackCDecl(func(monitor uintptr, event PeripheralEvent) uintptr {
var m *Monitor
if monitor != 0 {
m = &Monitor{monitor}
}
cb(m, event)
return 0
}))
}
func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback { func ToScrollCallback(cb func(window *Window, xoff float64, yoff float64)) ScrollCallback {
if cb == nil { if cb == nil {
return 0 return 0

View File

@ -301,18 +301,9 @@ func PostEmptyEvent() {
glfw.PostEmptyEvent() glfw.PostEmptyEvent()
} }
func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) { func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
var gcb func(monitor *glfw.Monitor, event glfw.PeripheralEvent) glfw.SetMonitorCallback(monitorCallbacks[cbfun])
if cbfun != nil { return ToMonitorCallback(nil)
gcb = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
var m *Monitor
if monitor != nil {
m = &Monitor{monitor}
}
cbfun(m, PeripheralEvent(event))
}
}
glfw.SetMonitorCallback(gcb)
} }
func SwapInterval(interval int) { func SwapInterval(interval int) {

View File

@ -22,8 +22,6 @@ import (
"runtime" "runtime"
"sync" "sync"
"unsafe" "unsafe"
"golang.org/x/sys/windows"
) )
type glfwImage struct { type glfwImage struct {
@ -414,20 +412,10 @@ func PostEmptyEvent() {
panicError() panicError()
} }
func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) { func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
var gcb uintptr glfwDLL.call("glfwSetMonitorCallback", uintptr(cbfun))
if cbfun != nil {
gcb = windows.NewCallbackCDecl(func(monitor uintptr, event PeripheralEvent) uintptr {
var m *Monitor
if monitor != 0 {
m = &Monitor{monitor}
}
cbfun(m, event)
return 0
})
}
glfwDLL.call("glfwSetMonitorCallback", gcb)
panicError() panicError()
return ToMonitorCallback(nil) // TODO
} }
func SwapInterval(interval int) { func SwapInterval(interval int) {

View File

@ -21,6 +21,7 @@ type (
CharModsCallback uintptr CharModsCallback uintptr
CloseCallback uintptr CloseCallback uintptr
FramebufferSizeCallback uintptr FramebufferSizeCallback uintptr
MonitorCallback uintptr
ScrollCallback uintptr ScrollCallback uintptr
SizeCallback uintptr SizeCallback uintptr
) )

View File

@ -150,9 +150,9 @@ func init() {
if err := initialize(); err != nil { if err := initialize(); err != nil {
panic(err) panic(err)
} }
glfw.SetMonitorCallback(func(monitor *glfw.Monitor, event glfw.PeripheralEvent) { glfw.SetMonitorCallback(glfw.ToMonitorCallback(func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
updateMonitors() updateMonitors()
}) }))
updateMonitors() updateMonitors()
} }