ebiten/examples/blocks/blocks/gamepad.go

155 lines
3.4 KiB
Go
Raw Normal View History

2015-01-19 14:21:24 +01:00
// Copyright 2015 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2016-10-15 17:09:03 +02:00
// +build example
2016-09-03 07:03:41 +02:00
package blocks
2015-01-19 14:21:24 +01:00
import (
"fmt"
2016-09-03 07:03:41 +02:00
2015-01-19 14:21:24 +01:00
"github.com/hajimehoshi/ebiten"
)
type abstractButton int
2015-01-19 14:21:24 +01:00
const (
abstractButtonLeft abstractButton = iota
abstractButtonRight
abstractButtonDown
abstractButtonButtonA
abstractButtonButtonB
2015-01-19 14:21:24 +01:00
)
const threshold = 0.75
type axis struct {
id int
positive bool
}
type gamepadConfig struct {
current abstractButton
buttons map[abstractButton]ebiten.GamepadButton
axes map[abstractButton]axis
2015-01-19 14:21:24 +01:00
assignedButtons map[ebiten.GamepadButton]struct{}
assignedAxes map[axis]struct{}
}
func (c *gamepadConfig) initializeIfNeeded() {
2015-01-19 14:21:24 +01:00
if c.buttons == nil {
c.buttons = map[abstractButton]ebiten.GamepadButton{}
2015-01-19 14:21:24 +01:00
}
if c.axes == nil {
c.axes = map[abstractButton]axis{}
2015-01-19 14:21:24 +01:00
}
if c.assignedButtons == nil {
c.assignedButtons = map[ebiten.GamepadButton]struct{}{}
}
if c.assignedAxes == nil {
c.assignedAxes = map[axis]struct{}{}
}
}
func (c *gamepadConfig) Reset() {
2015-01-19 14:21:24 +01:00
c.buttons = nil
c.axes = nil
c.assignedButtons = nil
c.assignedAxes = nil
}
func (c *gamepadConfig) Scan(index int, b abstractButton) bool {
2015-01-19 14:21:24 +01:00
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
// 1) there is a bug that a button returns an axis value wrongly
// and the value may be over 1.
// 2) just 1.0 or -1.0 values are ignored since PS4's L2/R2 keys take
// -1.0 by default.
if threshold <= v && v < 1.0 {
2015-01-19 14:21:24 +01:00
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 {
2015-01-19 14:21:24 +01:00
if _, ok := c.assignedAxes[axis{a, false}]; !ok {
c.axes[b] = axis{a, false}
c.assignedAxes[axis{a, false}] = struct{}{}
return true
}
}
}
return false
}
func (c *gamepadConfig) IsButtonPressed(id int, b abstractButton) bool {
2015-01-19 14:21:24 +01:00
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
}
func (c *gamepadConfig) Name(b abstractButton) string {
2015-01-19 14:21:24 +01:00
c.initializeIfNeeded()
bb, ok := c.buttons[b]
if ok {
return fmt.Sprintf("Button %d", bb)
}
a, ok := c.axes[b]
if ok {
if a.positive {
return fmt.Sprintf("Axis %d+", a.id)
} else {
return fmt.Sprintf("Axis %d-", a.id)
}
}
return ""
}