Example #1
0
func (i *Input) Update() {
	if i.keyStates == nil {
		i.keyStates = map[ebiten.Key]int{}
	}
	for key := ebiten.Key(0); key <= ebiten.KeyMax; key++ {
		if !ebiten.IsKeyPressed(ebiten.Key(key)) {
			i.keyStates[key] = 0
			continue
		}
		i.keyStates[key]++
	}

	const gamepadID = 0
	if i.gamepadButtonStates == nil {
		i.gamepadButtonStates = map[ebiten.GamepadButton]int{}
	}
	for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
		if !ebiten.IsGamepadButtonPressed(gamepadID, b) {
			i.gamepadButtonStates[b] = 0
			continue
		}
		i.gamepadButtonStates[b]++
	}

	if i.gamepadAbstractButtonStates == nil {
		i.gamepadAbstractButtonStates = map[abstractButton]int{}
	}
	for _, b := range gamepadAbstractButtons {
		if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
			i.gamepadAbstractButtonStates[b] = 0
			continue
		}
		i.gamepadAbstractButtonStates[b]++
	}
}
Example #2
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
}
Example #3
0
func (i *Input) Update() {
	for key := range i.keyStates {
		if !ebiten.IsKeyPressed(ebiten.Key(key)) {
			i.keyStates[key] = 0
			continue
		}
		i.keyStates[key]++
	}

	const gamepadID = 0
	for b := range i.gamepadButtonStates {
		if !ebiten.IsGamepadButtonPressed(gamepadID, ebiten.GamepadButton(b)) {
			i.gamepadButtonStates[b] = 0
			continue
		}
		i.gamepadButtonStates[b]++
	}

	for _, b := range gamepadStdButtons {
		if !i.gamepadConfig.IsButtonPressed(gamepadID, b) {
			i.gamepadStdButtonStates[b] = 0
			continue
		}
		i.gamepadStdButtonStates[b]++
	}
}
Example #4
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
}
Example #5
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
}