ebiten/examples/blocks/blocks/gamepadscene.go

108 lines
2.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:13:31 +02:00
// +build example
2015-01-19 14:21:24 +01:00
package blocks
import (
"fmt"
"image/color"
"strings"
2016-02-15 17:13:04 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/common"
2015-01-19 14:21:24 +01:00
)
type GamepadScene struct {
currentIndex int
countAfterSetting int
buttonStates []string
}
func (s *GamepadScene) Update(state *GameState) error {
if s.currentIndex == 0 {
state.Input.gamepadConfig.Reset()
}
if state.Input.StateForKey(ebiten.KeyEscape) == 1 {
state.Input.gamepadConfig.Reset()
2018-01-22 07:12:17 +01:00
state.SceneManager.GoTo(&TitleScene{})
2015-01-19 14:21:24 +01:00
}
if s.buttonStates == nil {
2018-01-22 07:12:17 +01:00
s.buttonStates = make([]string, len(virtualGamepadButtons))
2015-01-19 14:21:24 +01:00
}
2018-01-22 07:12:17 +01:00
for i, b := range virtualGamepadButtons {
2015-01-19 14:21:24 +01:00
if i < s.currentIndex {
2018-01-22 07:12:17 +01:00
s.buttonStates[i] = strings.ToUpper(state.Input.gamepadConfig.ButtonName(b))
2015-01-19 14:21:24 +01:00
continue
}
if s.currentIndex == i {
s.buttonStates[i] = "_"
continue
}
s.buttonStates[i] = ""
}
if 0 < s.countAfterSetting {
s.countAfterSetting--
if s.countAfterSetting <= 0 {
2018-01-22 07:12:17 +01:00
state.SceneManager.GoTo(&TitleScene{})
2015-01-19 14:21:24 +01:00
}
return nil
}
2018-01-22 07:12:17 +01:00
b := virtualGamepadButtons[s.currentIndex]
const gamepadID = 0
if state.Input.gamepadConfig.Scan(gamepadID, b) {
2015-01-19 14:21:24 +01:00
s.currentIndex++
2018-01-22 07:12:17 +01:00
if s.currentIndex == len(virtualGamepadButtons) {
2016-03-12 20:48:13 +01:00
s.countAfterSetting = ebiten.FPS
2015-01-19 14:21:24 +01:00
}
}
return nil
}
func (s *GamepadScene) Draw(screen *ebiten.Image) {
screen.Fill(color.Black)
2015-01-19 14:21:24 +01:00
if s.buttonStates == nil {
return
2015-01-19 14:21:24 +01:00
}
f := `GAMEPAD CONFIGURATION
(PRESS ESC TO CANCEL)
MOVE LEFT: %s
MOVE RIGHT: %s
DROP: %s
ROTATE LEFT: %s
ROTATE RIGHT: %s
%s`
msg := ""
2018-01-22 07:12:17 +01:00
if s.currentIndex == len(virtualGamepadButtons) {
2015-01-19 14:21:24 +01:00
msg = "OK!"
}
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
common.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White)
2015-01-19 14:21:24 +01:00
}