func (i *input) update(window *glfw.Window, scale int) error { for g, e := range glfwKeyCodeToKey { i.keyPressed[e] = window.GetKey(g) == glfw.Press } for g, e := range glfwMouseButtonToMouseButton { i.mouseButtonPressed[e] = window.GetMouseButton(g) == glfw.Press } x, y := window.GetCursorPos() i.cursorX = int(math.Floor(x)) / scale i.cursorY = int(math.Floor(y)) / scale for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ { if !glfw.JoystickPresent(id) { continue } axes32 := glfw.GetJoystickAxes(id) i.gamepads[id].axisNum = len(axes32) for a := 0; a < len(i.gamepads[id].axes); a++ { if len(axes32) <= a { i.gamepads[id].axes[a] = 0 continue } i.gamepads[id].axes[a] = float64(axes32[a]) } buttons := glfw.GetJoystickButtons(id) i.gamepads[id].buttonNum = len(buttons) for b := 0; b < len(i.gamepads[id].buttonPressed); b++ { if len(buttons) <= b { i.gamepads[id].buttonPressed[b] = false continue } i.gamepads[id].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press } } return nil }
func readJoystick(joy glfw.Joystick, turbo bool) [8]bool { var result [8]bool if !glfw.JoystickPresent(joy) { return result } axes := glfw.GetJoystickAxes(joy) buttons := glfw.GetJoystickButtons(joy) result[nes.ButtonA] = buttons[0] == 1 || (turbo && buttons[2] == 1) result[nes.ButtonB] = buttons[1] == 1 || (turbo && buttons[3] == 1) result[nes.ButtonSelect] = buttons[6] == 1 result[nes.ButtonStart] = buttons[7] == 1 result[nes.ButtonUp] = axes[1] < -0.5 result[nes.ButtonDown] = axes[1] > 0.5 result[nes.ButtonLeft] = axes[0] < -0.5 result[nes.ButtonRight] = axes[0] > 0.5 return result }
// inputUpdate handles input actions that are dependent on the delta between frames. // (e.g. how much you move depends on the time delta since the last update) func inputUpdate(w *glfw.Window, delta float32) { for key, cb := range activeKeyboard.keyBindings { if w.GetKey(key) == glfw.Press && cb != nil { cb(delta) } } // if the joystick is still connected, then we do the joystick polling // TODO: for now, we're just working with Joystick1 as hardcoded if glfw.JoystickPresent(glfw.Joystick1) { buttons := glfw.GetJoystickButtons(glfw.Joystick1) axes := glfw.GetJoystickAxes(glfw.Joystick1) //groggy.Logsf("DEBUG", "inputUpdate: %d %d %d %d %d %d %d %d %d %d %d %d %d %d ", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4], // buttons[5], buttons[6], buttons[7], buttons[8], buttons[9], // buttons[10], buttons[11], buttons[12], buttons[13]) // process the buttons for buttonId, cb := range activeJoystick.buttonBindings { if buttons[buttonId] > 0 && cb != nil { cb(delta) } } // process the axis values for _, mapping := range activeJoystick.axisBindings { if mapping.Callback == nil { continue } v := axes[mapping.Id] if v >= mapping.Min && v <= mapping.Max { scale := mapping.Max - mapping.Min if mapping.NegativeMapping { // use the Max value here since NegativeMapping implies a negative ranage for the mapping v = (float32(math.Abs(float64(v))) - float32(math.Abs(float64(mapping.Max)))) / scale } else { v = (v - mapping.Min) / scale } mapping.Callback(delta * v) } } // axisBindings } }
func (e *EventHandler) JoystickAxes(joystick Joystick) []float32 { return glfw.GetJoystickAxes(glfw.Joystick(joystick)) }