Skip to content

Commit

Permalink
Merge pull request hybridgroup#555 from imathno/fix-DPAD
Browse files Browse the repository at this point in the history
Fix joystick driver to detect dpad input for xbox controllers
  • Loading branch information
deadprogram authored May 25, 2018
2 parents 5c6e47a + 7beb0ec commit ed867b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions platforms/joystick/joystick_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (j *Driver) Start() (err error) {
j.AddEvent(value.Name)
}
for _, value := range j.config.Hats {
j.AddEvent(value.Name)
j.AddEvent(fmt.Sprintf("%s_press", value.Name))
j.AddEvent(fmt.Sprintf("%s_release", value.Name))
}

go func() {
Expand All @@ -144,6 +145,8 @@ func (j *Driver) Halt() (err error) {
return
}

var previousHat = ""

// HandleEvent publishes an specific event according to data received
func (j *Driver) handleEvent(event sdl.Event) error {
switch data := event.(type) {
Expand Down Expand Up @@ -172,8 +175,13 @@ func (j *Driver) handleEvent(event sdl.Event) error {
hat := j.findHatName(data.Value, data.Hat, j.config.Hats)
if hat == "" {
return fmt.Errorf("Unknown Hat: %v %v", data.Hat, data.Value)
} else if hat == "released" {
hat = previousHat
j.Publish(j.Event(fmt.Sprintf("%s_release", hat)), true)
} else {
previousHat = hat
j.Publish(j.Event(fmt.Sprintf("%s_press", hat)), true)
}
j.Publish(j.Event(hat), true)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion platforms/joystick/joystick_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestDriverHandleEvent(t *testing.T) {
}

// down button press
d.On(d.Event("down"), func(data interface{}) {
d.On(d.Event("down_press"), func(data interface{}) {
sem <- true
})
d.handleEvent(&sdl.JoyHatEvent{
Expand Down

0 comments on commit ed867b9

Please sign in to comment.