Пример #1
0
func update(screen *ebiten.Image) error {
	// TODO: API to get the available, lowest ID
	const gamepadID = 0
	axes := []string{}
	pressedButtons := []string{}

	maxAxis := ebiten.GamepadAxisNum(gamepadID)
	for a := 0; a < maxAxis; a++ {
		v := ebiten.GamepadAxis(gamepadID, a)
		axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v))
	}

	maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID))
	for b := ebiten.GamepadButton(gamepadID); b < maxButton; b++ {
		if ebiten.IsGamepadButtonPressed(gamepadID, b) {
			pressedButtons = append(pressedButtons, strconv.Itoa(int(b)))
		}
	}

	str := `Gamepad
  Axes:
    {{.Axes}}
  Pressed Buttons: {{.Buttons}}`
	str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n    "), -1)
	str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1)
	ebitenutil.DebugPrint(screen, str)
	return nil
}
Пример #2
0
func (c *gamepadConfig) IsButtonPressed(id int, b abstractButton) bool {
	c.initializeIfNeeded()

	bb, ok := c.buttons[b]
	if ok {
		return ebiten.IsGamepadButtonPressed(0, bb)
	}
	a, ok := c.axes[b]
	if ok {
		v := ebiten.GamepadAxis(0, a.id)
		if a.positive {
			return threshold <= v && v <= 1.0
		} else {
			return -1.0 <= v && v <= -threshold
		}
	}
	return false
}
Пример #3
0
func (c *gamepadConfig) Scan(index int, b abstractButton) bool {
	c.initializeIfNeeded()

	delete(c.buttons, b)
	delete(c.axes, b)

	ebn := ebiten.GamepadButton(ebiten.GamepadButtonNum(index))
	for eb := ebiten.GamepadButton(0); eb < ebn; eb++ {
		if _, ok := c.assignedButtons[eb]; ok {
			continue
		}
		if ebiten.IsGamepadButtonPressed(index, eb) {
			c.buttons[b] = eb
			c.assignedButtons[eb] = struct{}{}
			return true
		}
	}

	an := ebiten.GamepadAxisNum(index)
	for a := 0; a < an; a++ {
		v := ebiten.GamepadAxis(index, a)
		// Check v <= 1.0 because there is a bug that a button returns an axis value wrongly and the value may be over 1.
		if threshold <= v && v <= 1.0 {
			if _, ok := c.assignedAxes[axis{a, true}]; !ok {
				c.axes[b] = axis{a, true}
				c.assignedAxes[axis{a, true}] = struct{}{}
				return true
			}
		}
		if -1.0 <= v && v <= -threshold {
			if _, ok := c.assignedAxes[axis{a, false}]; !ok {
				c.axes[b] = axis{a, false}
				c.assignedAxes[axis{a, false}] = struct{}{}
				return true
			}
		}
	}

	return false
}