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{}
closeCallbacks = map[CloseCallback]glfw.CloseCallback{}
framebufferSizeCallbacks = map[FramebufferSizeCallback]glfw.FramebufferSizeCallback{}
monitorCallbacks = map[MonitorCallback]glfw.MonitorCallback{}
scrollCallbacks = map[ScrollCallback]glfw.ScrollCallback{}
sizeCallbacks = map[SizeCallback]glfw.SizeCallback{}
)
@ -65,6 +66,22 @@ func ToFramebufferSizeCallback(cb func(window *Window, width int, height int)) F
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 {
if cb == nil {
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 {
if cb == nil {
return 0

View File

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

View File

@ -22,8 +22,6 @@ import (
"runtime"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
type glfwImage struct {
@ -414,20 +412,10 @@ func PostEmptyEvent() {
panicError()
}
func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) {
var gcb uintptr
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)
func SetMonitorCallback(cbfun MonitorCallback) MonitorCallback {
glfwDLL.call("glfwSetMonitorCallback", uintptr(cbfun))
panicError()
return ToMonitorCallback(nil) // TODO
}
func SwapInterval(interval int) {

View File

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

View File

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