Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input events #85

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions backends/opengl/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,35 @@ var keyButtonMapping = map[glfw.Key]pixel.Button{
glfw.KeyMenu: pixel.KeyMenu,
}

func (w *Window) SetButtonCallback(callback func(win *Window, button pixel.Button, action pixel.Action)) {
w.buttonCallback = callback
}

func (w *Window) SetCharCallback(callback func(win *Window, r rune)) {
w.charCallback = callback
}

func (w *Window) SetMouseEnteredCallback(callback func(win *Window, entered bool)) {
w.mouseEnteredCallback = callback
}

func (w *Window) SetMouseMovedCallback(callback func(win *Window, pos pixel.Vec)) {
w.mouseMovedCallback = callback
}

func (w *Window) SetScrollCallback(callback func(win *Window, scroll pixel.Vec)) {
w.scrollCallback = callback
}

func (w *Window) initInput() {
mainthread.Call(func() {
w.window.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
if b, buttonOk := mouseButtonMapping[button]; buttonOk {
if a, actionOk := actionMapping[action]; actionOk {
w.input.ButtonEvent(b, a)
if w.buttonCallback != nil {
w.buttonCallback(w, b, a)
}
}
}
})
Expand All @@ -231,29 +254,43 @@ func (w *Window) initInput() {
if b, buttonOk := keyButtonMapping[key]; buttonOk {
if a, actionOk := actionMapping[action]; actionOk {
w.input.ButtonEvent(b, a)
if w.buttonCallback != nil {
w.buttonCallback(w, b, a)
}
}
}
})

w.window.SetCursorEnterCallback(func(_ *glfw.Window, entered bool) {
w.cursorInsideWindow = entered
w.input.MouseEnteredEvent(entered)
if w.mouseEnteredCallback != nil {
w.mouseEnteredCallback(w, entered)
}
})

w.window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
w.input.MouseMoveEvent(
pixel.V(
x+w.bounds.Min.X,
(w.bounds.H()-y)+w.bounds.Min.Y,
),
pos := pixel.V(
x+w.bounds.Min.X,
(w.bounds.H()-y)+w.bounds.Min.Y,
)
w.input.MouseMoveEvent(pos)
if w.mouseMovedCallback != nil {
w.mouseMovedCallback(w, pos)
}
})

w.window.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.input.MouseScrollEvent(xoff, yoff)
if w.scrollCallback != nil {
w.scrollCallback(w, pixel.V(xoff, yoff))
}
})

w.window.SetCharCallback(func(_ *glfw.Window, r rune) {
w.input.CharEvent(r)
if w.charCallback != nil {
w.charCallback(w, r)
}
})
})
}
Expand Down
15 changes: 10 additions & 5 deletions backends/opengl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ type WindowConfig struct {
type Window struct {
window *glfw.Window

bounds pixel.Rect
canvas *Canvas
vsync bool
cursorVisible bool
cursorInsideWindow bool
bounds pixel.Rect
canvas *Canvas
vsync bool
cursorVisible bool

// need to save these to correctly restore a fullscreen window
restore struct {
Expand All @@ -99,6 +98,12 @@ type Window struct {

input *pixel.InputHandler
prevJoy, currJoy, tempJoy joystickState

buttonCallback func(win *Window, button pixel.Button, action pixel.Action)
charCallback func(win *Window, r rune)
mouseEnteredCallback func(win *Window, entered bool)
mouseMovedCallback func(win *Window, pos pixel.Vec)
scrollCallback func(win *Window, scroll pixel.Vec)
}

var currWin *Window
Expand Down
Loading