examples/gamepad: add vibrations

This commit is contained in:
Hajime Hoshi 2022-07-09 16:58:54 +09:00
parent 55c7a2df6c
commit be356390e0

View File

@ -23,6 +23,7 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -87,6 +88,28 @@ func (g *Game) Update() error {
for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ { for b := ebiten.StandardGamepadButton(0); b <= ebiten.StandardGamepadButtonMax; b++ {
// Log button events. // Log button events.
if inpututil.IsStandardGamepadButtonJustPressed(id, b) { if inpututil.IsStandardGamepadButtonJustPressed(id, b) {
var strong float64
var weak float64
switch b {
case ebiten.StandardGamepadButtonLeftTop,
ebiten.StandardGamepadButtonLeftLeft,
ebiten.StandardGamepadButtonLeftRight,
ebiten.StandardGamepadButtonLeftBottom:
weak = 0.5
case ebiten.StandardGamepadButtonRightTop,
ebiten.StandardGamepadButtonRightLeft,
ebiten.StandardGamepadButtonRightRight,
ebiten.StandardGamepadButtonRightBottom:
strong = 0.5
}
if strong > 0 || weak > 0 {
op := &ebiten.VibrateGamepadOptions{
Duration: 200 * time.Millisecond,
StrongMagnitude: strong,
WeakMagnitude: weak,
}
ebiten.VibrateGamepad(id, op)
}
log.Printf("standard button pressed: id: %d, button: %d", id, b) log.Printf("standard button pressed: id: %d, button: %d", id, b)
} }
if inpututil.IsStandardGamepadButtonJustReleased(id, b) { if inpututil.IsStandardGamepadButtonJustReleased(id, b) {