From df716082f310212c24868d704470312eb5a6028c Mon Sep 17 00:00:00 2001 From: Allen Ray Date: Mon, 22 Jun 2020 23:37:25 -0400 Subject: [PATCH] Broke out ui input handling --- input.go | 228 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui.go | 221 +---------------------------------------------------- 2 files changed, 230 insertions(+), 219 deletions(-) create mode 100644 input.go diff --git a/input.go b/input.go new file mode 100644 index 0000000..bb0d8d6 --- /dev/null +++ b/input.go @@ -0,0 +1,228 @@ +package pixelui + +import ( + "github.com/faiface/pixel/pixelgl" + "github.com/inkyblackness/imgui-go" +) + +func (ui *UI) prepareIO() { + ui.io.SetDisplaySize(pixelVecToimguiVec(ui.win.Bounds().Size())) + + ui.io.AddMouseWheelDelta(float32(ui.win.MouseScroll().X), float32(ui.win.MouseScroll().Y)) + mouse := ui.matrix.Unproject(ui.win.MousePosition()) + ui.io.SetMousePosition(imgui.Vec2{X: float32(mouse.X), Y: float32(mouse.Y)}) + + ui.io.SetMouseButtonDown(0, ui.win.Pressed(pixelgl.MouseButtonLeft)) + ui.io.SetMouseButtonDown(1, ui.win.Pressed(pixelgl.MouseButtonRight)) + ui.io.SetMouseButtonDown(2, ui.win.Pressed(pixelgl.MouseButtonMiddle)) + + for _, key := range keys { + if ui.win.Pressed(key) { + ui.io.KeyPress(int(key)) + } else { + ui.io.KeyRelease(int(key)) + } + ui.updateKeyMod() + } + ui.io.AddInputCharacters(ui.win.Typed()) +} + +func (ui *UI) mapModifier(lKey pixelgl.Button, rKey pixelgl.Button) (lResult int, rResult int) { + if ui.win.Pressed(lKey) { + lResult = int(lKey) + } + if ui.win.Pressed(rKey) { + rResult = int(rKey) + } + return +} + +func (ui *UI) updateKeyMod() { + ui.io.KeyCtrl(ui.mapModifier(pixelgl.KeyLeftControl, pixelgl.KeyRightControl)) + ui.io.KeyShift(ui.mapModifier(pixelgl.KeyLeftShift, pixelgl.KeyRightShift)) + ui.io.KeyAlt(ui.mapModifier(pixelgl.KeyLeftAlt, pixelgl.KeyRightAlt)) + ui.io.KeySuper(ui.mapModifier(pixelgl.KeyLeftSuper, pixelgl.KeyRightSuper)) +} + +// inputWant is a helper for determining what type a button is: keyboard/mouse +func (ui *UI) inputWant(button pixelgl.Button) bool { + switch button { + case pixelgl.MouseButton1, pixelgl.MouseButton2, pixelgl.MouseButton3, pixelgl.MouseButton4, pixelgl.MouseButton5, pixelgl.MouseButton6, pixelgl.MouseButton7, pixelgl.MouseButton8: + return ui.io.WantCaptureMouse() + } + return ui.io.WantCaptureKeyboard() +} + +// JustPressed returns true if imgui hasn't handled the button and the button was just pressed +func (ui *UI) JustPressed(button pixelgl.Button) bool { + return !ui.inputWant(button) && ui.win.JustPressed(button) +} + +// JustPressed returns true if imgui hasn't handled the button and the button was just released +func (ui *UI) JustReleased(button pixelgl.Button) bool { + return !ui.inputWant(button) && ui.win.JustReleased(button) +} + +// JustPressed returns true if imgui hasn't handled the button and the button is pressed +func (ui *UI) Pressed(button pixelgl.Button) bool { + return !ui.inputWant(button) && ui.win.Pressed(button) +} + +// Repeated returns true if imgui hasn't handled the button and the button was repeated +func (ui *UI) Repeated(button pixelgl.Button) bool { + return !ui.inputWant(button) && ui.win.Repeated(button) +} + +func (ui *UI) setKeyMapping() { + keys := map[int]pixelgl.Button{ + imgui.KeyTab: pixelgl.KeyTab, + imgui.KeyLeftArrow: pixelgl.KeyLeft, + imgui.KeyRightArrow: pixelgl.KeyRight, + imgui.KeyUpArrow: pixelgl.KeyUp, + imgui.KeyDownArrow: pixelgl.KeyDown, + imgui.KeyPageUp: pixelgl.KeyPageUp, + imgui.KeyPageDown: pixelgl.KeyPageDown, + imgui.KeyHome: pixelgl.KeyHome, + imgui.KeyEnd: pixelgl.KeyEnd, + imgui.KeyInsert: pixelgl.KeyInsert, + imgui.KeyDelete: pixelgl.KeyDelete, + imgui.KeyBackspace: pixelgl.KeyBackspace, + imgui.KeySpace: pixelgl.KeySpace, + imgui.KeyEnter: pixelgl.KeyEnter, + imgui.KeyEscape: pixelgl.KeyEscape, + imgui.KeyA: pixelgl.KeyA, + imgui.KeyC: pixelgl.KeyC, + imgui.KeyV: pixelgl.KeyV, + imgui.KeyX: pixelgl.KeyX, + imgui.KeyY: pixelgl.KeyY, + imgui.KeyZ: pixelgl.KeyZ, + } + + // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array. + for imguiKey, nativeKey := range keys { + ui.io.KeyMap(imguiKey, int(nativeKey)) + } +} + +var keys = []pixelgl.Button{ + pixelgl.KeySpace, + pixelgl.KeyApostrophe, + pixelgl.KeyComma, + pixelgl.KeyMinus, + pixelgl.KeyPeriod, + pixelgl.KeySlash, + pixelgl.Key0, + pixelgl.Key1, + pixelgl.Key2, + pixelgl.Key3, + pixelgl.Key4, + pixelgl.Key5, + pixelgl.Key6, + pixelgl.Key7, + pixelgl.Key8, + pixelgl.Key9, + pixelgl.KeySemicolon, + pixelgl.KeyEqual, + pixelgl.KeyA, + pixelgl.KeyB, + pixelgl.KeyC, + pixelgl.KeyD, + pixelgl.KeyE, + pixelgl.KeyF, + pixelgl.KeyG, + pixelgl.KeyH, + pixelgl.KeyI, + pixelgl.KeyJ, + pixelgl.KeyK, + pixelgl.KeyL, + pixelgl.KeyM, + pixelgl.KeyN, + pixelgl.KeyO, + pixelgl.KeyP, + pixelgl.KeyQ, + pixelgl.KeyR, + pixelgl.KeyS, + pixelgl.KeyT, + pixelgl.KeyU, + pixelgl.KeyV, + pixelgl.KeyW, + pixelgl.KeyX, + pixelgl.KeyY, + pixelgl.KeyZ, + pixelgl.KeyLeftBracket, + pixelgl.KeyBackslash, + pixelgl.KeyRightBracket, + pixelgl.KeyGraveAccent, + pixelgl.KeyWorld1, + pixelgl.KeyWorld2, + pixelgl.KeyEscape, + pixelgl.KeyEnter, + pixelgl.KeyTab, + pixelgl.KeyBackspace, + pixelgl.KeyInsert, + pixelgl.KeyDelete, + pixelgl.KeyRight, + pixelgl.KeyLeft, + pixelgl.KeyDown, + pixelgl.KeyUp, + pixelgl.KeyPageUp, + pixelgl.KeyPageDown, + pixelgl.KeyHome, + pixelgl.KeyEnd, + pixelgl.KeyCapsLock, + pixelgl.KeyScrollLock, + pixelgl.KeyNumLock, + pixelgl.KeyPrintScreen, + pixelgl.KeyPause, + pixelgl.KeyF1, + pixelgl.KeyF2, + pixelgl.KeyF3, + pixelgl.KeyF4, + pixelgl.KeyF5, + pixelgl.KeyF6, + pixelgl.KeyF7, + pixelgl.KeyF8, + pixelgl.KeyF9, + pixelgl.KeyF10, + pixelgl.KeyF11, + pixelgl.KeyF12, + pixelgl.KeyF13, + pixelgl.KeyF14, + pixelgl.KeyF15, + pixelgl.KeyF16, + pixelgl.KeyF17, + pixelgl.KeyF18, + pixelgl.KeyF19, + pixelgl.KeyF20, + pixelgl.KeyF21, + pixelgl.KeyF22, + pixelgl.KeyF23, + pixelgl.KeyF24, + pixelgl.KeyF25, + pixelgl.KeyKP0, + pixelgl.KeyKP1, + pixelgl.KeyKP2, + pixelgl.KeyKP3, + pixelgl.KeyKP4, + pixelgl.KeyKP5, + pixelgl.KeyKP6, + pixelgl.KeyKP7, + pixelgl.KeyKP8, + pixelgl.KeyKP9, + pixelgl.KeyKPDecimal, + pixelgl.KeyKPDivide, + pixelgl.KeyKPMultiply, + pixelgl.KeyKPSubtract, + pixelgl.KeyKPAdd, + pixelgl.KeyKPEnter, + pixelgl.KeyKPEqual, + pixelgl.KeyLeftShift, + pixelgl.KeyLeftControl, + pixelgl.KeyLeftAlt, + pixelgl.KeyLeftSuper, + pixelgl.KeyRightShift, + pixelgl.KeyRightControl, + pixelgl.KeyRightAlt, + pixelgl.KeyRightSuper, + pixelgl.KeyMenu, +} diff --git a/ui.go b/ui.go index fe9ba92..a722f92 100644 --- a/ui.go +++ b/ui.go @@ -94,80 +94,17 @@ func (ui *UI) Destroy() { func (ui *UI) NewFrame() { ui.timer = time.Now() - ui.io.SetDisplaySize(pixelVecToimguiVec(ui.win.Bounds().Size())) - - ui.io.AddMouseWheelDelta(float32(ui.win.MouseScroll().X), float32(ui.win.MouseScroll().Y)) - mouse := ui.matrix.Unproject(ui.win.MousePosition()) - ui.io.SetMousePosition(imgui.Vec2{X: float32(mouse.X), Y: float32(mouse.Y)}) - - ui.io.SetMouseButtonDown(0, ui.win.Pressed(pixelgl.MouseButtonLeft)) - ui.io.SetMouseButtonDown(1, ui.win.Pressed(pixelgl.MouseButtonRight)) - ui.io.SetMouseButtonDown(2, ui.win.Pressed(pixelgl.MouseButtonMiddle)) - - for _, key := range keys { - if ui.win.Pressed(key) { - ui.io.KeyPress(int(key)) - } else { - ui.io.KeyRelease(int(key)) - } - ui.updateKeyMod() - } - ui.io.AddInputCharacters(ui.win.Typed()) + // imgui requires that io be set before calling NewFrame + ui.prepareIO() imgui.NewFrame() } -func (ui *UI) mapModifier(lKey pixelgl.Button, rKey pixelgl.Button) (lResult int, rResult int) { - if ui.win.Pressed(lKey) { - lResult = int(lKey) - } - if ui.win.Pressed(rKey) { - rResult = int(rKey) - } - return -} - -func (ui *UI) updateKeyMod() { - ui.io.KeyCtrl(ui.mapModifier(pixelgl.KeyLeftControl, pixelgl.KeyRightControl)) - ui.io.KeyShift(ui.mapModifier(pixelgl.KeyLeftShift, pixelgl.KeyRightShift)) - ui.io.KeyAlt(ui.mapModifier(pixelgl.KeyLeftAlt, pixelgl.KeyRightAlt)) - ui.io.KeySuper(ui.mapModifier(pixelgl.KeyLeftSuper, pixelgl.KeyRightSuper)) -} - // update Handles general update type things and handle inputs. Called from ui.Draw. func (ui *UI) update() { ui.io.SetDeltaTime(float32(time.Since(ui.timer).Seconds())) } -// inputWant is a helper for determining what type a button is: keyboard/mouse -func (ui *UI) inputWant(button pixelgl.Button) bool { - switch button { - case pixelgl.MouseButton1, pixelgl.MouseButton2, pixelgl.MouseButton3, pixelgl.MouseButton4, pixelgl.MouseButton5, pixelgl.MouseButton6, pixelgl.MouseButton7, pixelgl.MouseButton8: - return ui.io.WantCaptureMouse() - } - return ui.io.WantCaptureKeyboard() -} - -// JustPressed returns true if imgui hasn't handled the button and the button was just pressed -func (ui *UI) JustPressed(button pixelgl.Button) bool { - return !ui.inputWant(button) && ui.win.JustPressed(button) -} - -// JustPressed returns true if imgui hasn't handled the button and the button was just released -func (ui *UI) JustReleased(button pixelgl.Button) bool { - return !ui.inputWant(button) && ui.win.JustReleased(button) -} - -// JustPressed returns true if imgui hasn't handled the button and the button is pressed -func (ui *UI) Pressed(button pixelgl.Button) bool { - return !ui.inputWant(button) && ui.win.Pressed(button) -} - -// Repeated returns true if imgui hasn't handled the button and the button was repeated -func (ui *UI) Repeated(button pixelgl.Button) bool { - return !ui.inputWant(button) && ui.win.Repeated(button) -} - // Draw Draws the imgui UI to the Pixel Window func (ui *UI) Draw(win *pixelgl.Window) { win.SetComposeMethod(pixel.ComposeOver) @@ -253,157 +190,3 @@ func imguiRectToPixelRect(r imgui.Vec4) pixel.Rect { func pixelVecToimguiVec(v pixel.Vec) imgui.Vec2 { return imgui.Vec2{X: float32(v.X), Y: float32(v.Y)} } - -func (ui *UI) setKeyMapping() { - keys := map[int]pixelgl.Button{ - imgui.KeyTab: pixelgl.KeyTab, - imgui.KeyLeftArrow: pixelgl.KeyLeft, - imgui.KeyRightArrow: pixelgl.KeyRight, - imgui.KeyUpArrow: pixelgl.KeyUp, - imgui.KeyDownArrow: pixelgl.KeyDown, - imgui.KeyPageUp: pixelgl.KeyPageUp, - imgui.KeyPageDown: pixelgl.KeyPageDown, - imgui.KeyHome: pixelgl.KeyHome, - imgui.KeyEnd: pixelgl.KeyEnd, - imgui.KeyInsert: pixelgl.KeyInsert, - imgui.KeyDelete: pixelgl.KeyDelete, - imgui.KeyBackspace: pixelgl.KeyBackspace, - imgui.KeySpace: pixelgl.KeySpace, - imgui.KeyEnter: pixelgl.KeyEnter, - imgui.KeyEscape: pixelgl.KeyEscape, - imgui.KeyA: pixelgl.KeyA, - imgui.KeyC: pixelgl.KeyC, - imgui.KeyV: pixelgl.KeyV, - imgui.KeyX: pixelgl.KeyX, - imgui.KeyY: pixelgl.KeyY, - imgui.KeyZ: pixelgl.KeyZ, - } - - // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array. - for imguiKey, nativeKey := range keys { - ui.io.KeyMap(imguiKey, int(nativeKey)) - } -} - -var keys = []pixelgl.Button{ - pixelgl.KeySpace, - pixelgl.KeyApostrophe, - pixelgl.KeyComma, - pixelgl.KeyMinus, - pixelgl.KeyPeriod, - pixelgl.KeySlash, - pixelgl.Key0, - pixelgl.Key1, - pixelgl.Key2, - pixelgl.Key3, - pixelgl.Key4, - pixelgl.Key5, - pixelgl.Key6, - pixelgl.Key7, - pixelgl.Key8, - pixelgl.Key9, - pixelgl.KeySemicolon, - pixelgl.KeyEqual, - pixelgl.KeyA, - pixelgl.KeyB, - pixelgl.KeyC, - pixelgl.KeyD, - pixelgl.KeyE, - pixelgl.KeyF, - pixelgl.KeyG, - pixelgl.KeyH, - pixelgl.KeyI, - pixelgl.KeyJ, - pixelgl.KeyK, - pixelgl.KeyL, - pixelgl.KeyM, - pixelgl.KeyN, - pixelgl.KeyO, - pixelgl.KeyP, - pixelgl.KeyQ, - pixelgl.KeyR, - pixelgl.KeyS, - pixelgl.KeyT, - pixelgl.KeyU, - pixelgl.KeyV, - pixelgl.KeyW, - pixelgl.KeyX, - pixelgl.KeyY, - pixelgl.KeyZ, - pixelgl.KeyLeftBracket, - pixelgl.KeyBackslash, - pixelgl.KeyRightBracket, - pixelgl.KeyGraveAccent, - pixelgl.KeyWorld1, - pixelgl.KeyWorld2, - pixelgl.KeyEscape, - pixelgl.KeyEnter, - pixelgl.KeyTab, - pixelgl.KeyBackspace, - pixelgl.KeyInsert, - pixelgl.KeyDelete, - pixelgl.KeyRight, - pixelgl.KeyLeft, - pixelgl.KeyDown, - pixelgl.KeyUp, - pixelgl.KeyPageUp, - pixelgl.KeyPageDown, - pixelgl.KeyHome, - pixelgl.KeyEnd, - pixelgl.KeyCapsLock, - pixelgl.KeyScrollLock, - pixelgl.KeyNumLock, - pixelgl.KeyPrintScreen, - pixelgl.KeyPause, - pixelgl.KeyF1, - pixelgl.KeyF2, - pixelgl.KeyF3, - pixelgl.KeyF4, - pixelgl.KeyF5, - pixelgl.KeyF6, - pixelgl.KeyF7, - pixelgl.KeyF8, - pixelgl.KeyF9, - pixelgl.KeyF10, - pixelgl.KeyF11, - pixelgl.KeyF12, - pixelgl.KeyF13, - pixelgl.KeyF14, - pixelgl.KeyF15, - pixelgl.KeyF16, - pixelgl.KeyF17, - pixelgl.KeyF18, - pixelgl.KeyF19, - pixelgl.KeyF20, - pixelgl.KeyF21, - pixelgl.KeyF22, - pixelgl.KeyF23, - pixelgl.KeyF24, - pixelgl.KeyF25, - pixelgl.KeyKP0, - pixelgl.KeyKP1, - pixelgl.KeyKP2, - pixelgl.KeyKP3, - pixelgl.KeyKP4, - pixelgl.KeyKP5, - pixelgl.KeyKP6, - pixelgl.KeyKP7, - pixelgl.KeyKP8, - pixelgl.KeyKP9, - pixelgl.KeyKPDecimal, - pixelgl.KeyKPDivide, - pixelgl.KeyKPMultiply, - pixelgl.KeyKPSubtract, - pixelgl.KeyKPAdd, - pixelgl.KeyKPEnter, - pixelgl.KeyKPEqual, - pixelgl.KeyLeftShift, - pixelgl.KeyLeftControl, - pixelgl.KeyLeftAlt, - pixelgl.KeyLeftSuper, - pixelgl.KeyRightShift, - pixelgl.KeyRightControl, - pixelgl.KeyRightAlt, - pixelgl.KeyRightSuper, - pixelgl.KeyMenu, -}