mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Correctly handle imgui mouse/keyboard capture
This commit is contained in:
@ -15,6 +15,10 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
isInited = false
|
isInited = false
|
||||||
|
|
||||||
|
isSdlButtonLeftDown = false
|
||||||
|
isSdlButtonMiddleDown = false
|
||||||
|
isSdlButtonRightDown = false
|
||||||
)
|
)
|
||||||
|
|
||||||
type Window struct {
|
type Window struct {
|
||||||
@ -29,7 +33,9 @@ func (w *Window) handleInputs() {
|
|||||||
input.EventLoopStart()
|
input.EventLoopStart()
|
||||||
imIo := imgui.CurrentIO()
|
imIo := imgui.CurrentIO()
|
||||||
|
|
||||||
// @TODO: Would be nice to have imgui package process its own events via a callback instead of it being part of engine code
|
imguiCaptureMouse := imIo.WantCaptureMouse()
|
||||||
|
imguiCaptureKeyboard := imIo.WantCaptureKeyboard()
|
||||||
|
|
||||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
|
|
||||||
//Fire callbacks
|
//Fire callbacks
|
||||||
@ -42,14 +48,18 @@ func (w *Window) handleInputs() {
|
|||||||
|
|
||||||
case *sdl.MouseWheelEvent:
|
case *sdl.MouseWheelEvent:
|
||||||
|
|
||||||
|
if !imguiCaptureMouse {
|
||||||
input.HandleMouseWheelEvent(e)
|
input.HandleMouseWheelEvent(e)
|
||||||
|
}
|
||||||
|
|
||||||
xDelta, yDelta := input.GetMouseWheelMotion()
|
imIo.AddMouseWheelDelta(float32(e.X), float32(e.Y))
|
||||||
imIo.AddMouseWheelDelta(float32(xDelta), float32(yDelta))
|
|
||||||
|
|
||||||
case *sdl.KeyboardEvent:
|
case *sdl.KeyboardEvent:
|
||||||
|
|
||||||
|
if !imguiCaptureKeyboard {
|
||||||
input.HandleKeyboardEvent(e)
|
input.HandleKeyboardEvent(e)
|
||||||
|
}
|
||||||
|
|
||||||
imIo.AddKeyEvent(nmageimgui.SdlScancodeToImGuiKey(e.Keysym.Scancode), e.Type == sdl.KEYDOWN)
|
imIo.AddKeyEvent(nmageimgui.SdlScancodeToImGuiKey(e.Keysym.Scancode), e.Type == sdl.KEYDOWN)
|
||||||
|
|
||||||
// Send modifier key updates to imgui
|
// Send modifier key updates to imgui
|
||||||
@ -73,12 +83,29 @@ func (w *Window) handleInputs() {
|
|||||||
imIo.AddInputCharactersUTF8(e.GetText())
|
imIo.AddInputCharactersUTF8(e.GetText())
|
||||||
|
|
||||||
case *sdl.MouseButtonEvent:
|
case *sdl.MouseButtonEvent:
|
||||||
|
|
||||||
|
if !imguiCaptureMouse {
|
||||||
input.HandleMouseBtnEvent(e)
|
input.HandleMouseBtnEvent(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
isPressed := e.State == sdl.PRESSED
|
||||||
|
|
||||||
|
if e.Button == sdl.BUTTON_LEFT {
|
||||||
|
isSdlButtonLeftDown = isPressed
|
||||||
|
} else if e.Button == sdl.BUTTON_MIDDLE {
|
||||||
|
isSdlButtonMiddleDown = isPressed
|
||||||
|
} else if e.Button == sdl.BUTTON_RIGHT {
|
||||||
|
isSdlButtonRightDown = isPressed
|
||||||
|
}
|
||||||
|
|
||||||
case *sdl.MouseMotionEvent:
|
case *sdl.MouseMotionEvent:
|
||||||
|
|
||||||
|
if !imguiCaptureMouse {
|
||||||
input.HandleMouseMotionEvent(e)
|
input.HandleMouseMotionEvent(e)
|
||||||
|
}
|
||||||
|
|
||||||
case *sdl.WindowEvent:
|
case *sdl.WindowEvent:
|
||||||
|
|
||||||
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
|
||||||
w.handleWindowResize()
|
w.handleWindowResize()
|
||||||
}
|
}
|
||||||
@ -92,9 +119,9 @@ func (w *Window) handleInputs() {
|
|||||||
x, y, _ := sdl.GetMouseState()
|
x, y, _ := sdl.GetMouseState()
|
||||||
imIo.SetMousePos(imgui.Vec2{X: float32(x), Y: float32(y)})
|
imIo.SetMousePos(imgui.Vec2{X: float32(x), Y: float32(y)})
|
||||||
|
|
||||||
imIo.SetMouseButtonDown(0, input.MouseDown(sdl.BUTTON_LEFT))
|
imIo.SetMouseButtonDown(0, isSdlButtonLeftDown)
|
||||||
imIo.SetMouseButtonDown(1, input.MouseDown(sdl.BUTTON_RIGHT))
|
imIo.SetMouseButtonDown(1, isSdlButtonRightDown)
|
||||||
imIo.SetMouseButtonDown(2, input.MouseDown(sdl.BUTTON_MIDDLE))
|
imIo.SetMouseButtonDown(2, isSdlButtonMiddleDown)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Window) handleWindowResize() {
|
func (w *Window) handleWindowResize() {
|
||||||
|
|||||||
@ -31,8 +31,8 @@ type mouseWheelState struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
keyMap = make(map[sdl.Keycode]*keyState)
|
keyMap = make(map[sdl.Keycode]keyState)
|
||||||
mouseBtnMap = make(map[int]*mouseBtnState)
|
mouseBtnMap = make(map[int]mouseBtnState)
|
||||||
mouseMotion = mouseMotionState{}
|
mouseMotion = mouseMotionState{}
|
||||||
mouseWheel = mouseWheelState{}
|
mouseWheel = mouseWheelState{}
|
||||||
quitRequested bool
|
quitRequested bool
|
||||||
@ -70,29 +70,31 @@ func IsQuitClicked() bool {
|
|||||||
|
|
||||||
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
func HandleKeyboardEvent(e *sdl.KeyboardEvent) {
|
||||||
|
|
||||||
ks := keyMap[e.Keysym.Sym]
|
ks, ok := keyMap[e.Keysym.Sym]
|
||||||
if ks == nil {
|
if !ok {
|
||||||
ks = &keyState{Key: e.Keysym.Sym}
|
ks = keyState{Key: e.Keysym.Sym}
|
||||||
keyMap[ks.Key] = ks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ks.State = int(e.State)
|
ks.State = int(e.State)
|
||||||
ks.IsPressedThisFrame = e.State == sdl.PRESSED && e.Repeat == 0
|
ks.IsPressedThisFrame = e.State == sdl.PRESSED && e.Repeat == 0
|
||||||
ks.IsReleasedThisFrame = e.State == sdl.RELEASED && e.Repeat == 0
|
ks.IsReleasedThisFrame = e.State == sdl.RELEASED && e.Repeat == 0
|
||||||
|
|
||||||
|
keyMap[ks.Key] = ks
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMouseBtnEvent(e *sdl.MouseButtonEvent) {
|
func HandleMouseBtnEvent(e *sdl.MouseButtonEvent) {
|
||||||
|
|
||||||
mb := mouseBtnMap[int(e.Button)]
|
mb, ok := mouseBtnMap[int(e.Button)]
|
||||||
if mb == nil {
|
if !ok {
|
||||||
mb = &mouseBtnState{Btn: int(e.Button)}
|
mb = mouseBtnState{Btn: int(e.Button)}
|
||||||
mouseBtnMap[int(e.Button)] = mb
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mb.State = int(e.State)
|
mb.State = int(e.State)
|
||||||
mb.IsDoubleClicked = e.Clicks == 2 && e.State == sdl.PRESSED
|
mb.IsDoubleClicked = e.Clicks == 2 && e.State == sdl.PRESSED
|
||||||
mb.IsPressedThisFrame = e.State == sdl.PRESSED
|
mb.IsPressedThisFrame = e.State == sdl.PRESSED
|
||||||
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
mb.IsReleasedThisFrame = e.State == sdl.RELEASED
|
||||||
|
|
||||||
|
mouseBtnMap[int(e.Button)] = mb
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleMouseMotionEvent(e *sdl.MouseMotionEvent) {
|
func HandleMouseMotionEvent(e *sdl.MouseMotionEvent) {
|
||||||
@ -167,8 +169,8 @@ func GetMouseWheelYNorm() int32 {
|
|||||||
|
|
||||||
func KeyClicked(kc sdl.Keycode) bool {
|
func KeyClicked(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks, ok := keyMap[kc]
|
||||||
if ks == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,8 +179,8 @@ func KeyClicked(kc sdl.Keycode) bool {
|
|||||||
|
|
||||||
func KeyReleased(kc sdl.Keycode) bool {
|
func KeyReleased(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks, ok := keyMap[kc]
|
||||||
if ks == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,8 +189,8 @@ func KeyReleased(kc sdl.Keycode) bool {
|
|||||||
|
|
||||||
func KeyDown(kc sdl.Keycode) bool {
|
func KeyDown(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks, ok := keyMap[kc]
|
||||||
if ks == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +199,8 @@ func KeyDown(kc sdl.Keycode) bool {
|
|||||||
|
|
||||||
func KeyUp(kc sdl.Keycode) bool {
|
func KeyUp(kc sdl.Keycode) bool {
|
||||||
|
|
||||||
ks := keyMap[kc]
|
ks, ok := keyMap[kc]
|
||||||
if ks == nil {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,8 +209,8 @@ func KeyUp(kc sdl.Keycode) bool {
|
|||||||
|
|
||||||
func MouseClicked(mb int) bool {
|
func MouseClicked(mb int) bool {
|
||||||
|
|
||||||
btn := mouseBtnMap[mb]
|
btn, ok := mouseBtnMap[mb]
|
||||||
if btn == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,8 +219,8 @@ func MouseClicked(mb int) bool {
|
|||||||
|
|
||||||
func MouseDoubleClicked(mb int) bool {
|
func MouseDoubleClicked(mb int) bool {
|
||||||
|
|
||||||
btn := mouseBtnMap[mb]
|
btn, ok := mouseBtnMap[mb]
|
||||||
if btn == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,8 +228,8 @@ func MouseDoubleClicked(mb int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MouseReleased(mb int) bool {
|
func MouseReleased(mb int) bool {
|
||||||
btn := mouseBtnMap[mb]
|
btn, ok := mouseBtnMap[mb]
|
||||||
if btn == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,8 +238,8 @@ func MouseReleased(mb int) bool {
|
|||||||
|
|
||||||
func MouseDown(mb int) bool {
|
func MouseDown(mb int) bool {
|
||||||
|
|
||||||
btn := mouseBtnMap[mb]
|
btn, ok := mouseBtnMap[mb]
|
||||||
if btn == nil {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,8 +248,8 @@ func MouseDown(mb int) bool {
|
|||||||
|
|
||||||
func MouseUp(mb int) bool {
|
func MouseUp(mb int) bool {
|
||||||
|
|
||||||
btn := mouseBtnMap[mb]
|
btn, ok := mouseBtnMap[mb]
|
||||||
if btn == nil {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user