mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/glfw: refactoring: make callback handlers consistent
This commit is contained in:
parent
7189c7f649
commit
8d2b08dd14
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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
|
||||||
)
|
)
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user