2014-12-24 03:04:10 +01:00
|
|
|
// Copyright 2014 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.
|
2014-12-09 15:16:04 +01:00
|
|
|
|
2014-12-08 17:35:35 +01:00
|
|
|
package blocks
|
2013-12-18 19:21:25 +01:00
|
|
|
|
|
|
|
import (
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2013-12-18 19:21:25 +01:00
|
|
|
)
|
|
|
|
|
2018-01-22 12:57:54 +01:00
|
|
|
// Input manages the input state including gamepads and keyboards.
|
2015-01-19 14:21:24 +01:00
|
|
|
type Input struct {
|
2018-01-22 07:12:17 +01:00
|
|
|
virtualGamepadButtonStates map[virtualGamepadButton]int
|
|
|
|
gamepadConfig gamepadConfig
|
2013-12-18 19:21:25 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 09:25:33 +02:00
|
|
|
// GamepadIDButtonPressed returns a gamepad ID where at least one button is pressed.
|
|
|
|
// If no button is pressed, GamepadIDButtonPressed returns -1.
|
2020-10-07 18:08:30 +02:00
|
|
|
func (i *Input) GamepadIDButtonPressed() ebiten.GamepadID {
|
2018-10-13 09:25:33 +02:00
|
|
|
for _, id := range ebiten.GamepadIDs() {
|
|
|
|
for b := ebiten.GamepadButton(0); b <= ebiten.GamepadButtonMax; b++ {
|
|
|
|
if ebiten.IsGamepadButtonPressed(id, b) {
|
|
|
|
return id
|
|
|
|
}
|
2018-02-04 15:43:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 09:25:33 +02:00
|
|
|
return -1
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
func (i *Input) stateForVirtualGamepadButton(b virtualGamepadButton) int {
|
|
|
|
if i.virtualGamepadButtonStates == nil {
|
2016-09-03 07:40:40 +02:00
|
|
|
return 0
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.virtualGamepadButtonStates[b]
|
2013-12-18 19:21:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 09:28:19 +01:00
|
|
|
func (i *Input) Update() {
|
2018-10-13 09:25:33 +02:00
|
|
|
if !i.gamepadConfig.IsInitialized() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
if i.virtualGamepadButtonStates == nil {
|
|
|
|
i.virtualGamepadButtonStates = map[virtualGamepadButton]int{}
|
2016-09-03 07:40:40 +02:00
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
for _, b := range virtualGamepadButtons {
|
|
|
|
if !i.gamepadConfig.IsButtonPressed(b) {
|
|
|
|
i.virtualGamepadButtonStates[b] = 0
|
2015-01-19 14:21:24 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
i.virtualGamepadButtonStates[b]++
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
func (i *Input) IsRotateRightJustPressed() bool {
|
2018-02-04 15:35:09 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyX) {
|
2015-01-19 14:21:24 +01:00
|
|
|
return true
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonB) == 1
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 07:12:17 +01:00
|
|
|
func (i *Input) IsRotateLeftJustPressed() bool {
|
2018-02-04 15:35:09 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyZ) {
|
2015-01-19 14:21:24 +01:00
|
|
|
return true
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonButtonA) == 1
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) StateForLeft() int {
|
2018-02-04 15:35:09 +01:00
|
|
|
if v := inpututil.KeyPressDuration(ebiten.KeyLeft); 0 < v {
|
2015-01-19 14:21:24 +01:00
|
|
|
return v
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonLeft)
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) StateForRight() int {
|
2018-02-04 15:35:09 +01:00
|
|
|
if v := inpututil.KeyPressDuration(ebiten.KeyRight); 0 < v {
|
2015-01-19 14:21:24 +01:00
|
|
|
return v
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonRight)
|
2015-01-19 14:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) StateForDown() int {
|
2018-02-04 15:35:09 +01:00
|
|
|
if v := inpututil.KeyPressDuration(ebiten.KeyDown); 0 < v {
|
2015-01-19 14:21:24 +01:00
|
|
|
return v
|
|
|
|
}
|
2018-01-22 07:12:17 +01:00
|
|
|
return i.stateForVirtualGamepadButton(virtualGamepadButtonDown)
|
2013-12-18 19:21:25 +01:00
|
|
|
}
|