Add input

This commit is contained in:
Hajime Hoshi 2014-12-07 22:10:04 +09:00
parent c039c13acd
commit 7fd54f6bb3
6 changed files with 27 additions and 26 deletions

View File

@ -3,7 +3,7 @@ package blocks
import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten/ui"
"github.com/hajimehoshi/ebiten/input"
"image/color"
"math/rand"
"time"
@ -64,26 +64,26 @@ func (s *GameScene) Update(state *GameState) {
y := s.currentPieceY
angle := s.currentPieceAngle
moved := false
if state.Input.StateForKey(ui.KeySpace) == 1 {
if state.Input.StateForKey(input.KeySpace) == 1 {
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
moved = angle != s.currentPieceAngle
}
if l := state.Input.StateForKey(ui.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
if l := state.Input.StateForKey(input.KeyLeft); l == 1 || (10 <= l && l%2 == 0) {
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
moved = x != s.currentPieceX
}
if r := state.Input.StateForKey(ui.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
if r := state.Input.StateForKey(input.KeyRight); r == 1 || (10 <= r && r%2 == 0) {
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
moved = y != s.currentPieceX
}
if d := state.Input.StateForKey(ui.KeyDown); (d-1)%2 == 0 {
if d := state.Input.StateForKey(input.KeyDown); (d-1)%2 == 0 {
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
moved = y != s.currentPieceY
}
if moved {
s.landingCount = 0
} else if !s.field.PieceDroppable(piece, x, y, angle) {
if 0 < state.Input.StateForKey(ui.KeyDown) {
if 0 < state.Input.StateForKey(input.KeyDown) {
s.landingCount += 10
} else {
s.landingCount++

View File

@ -1,16 +1,16 @@
package blocks
import (
"github.com/hajimehoshi/ebiten/ui"
"github.com/hajimehoshi/ebiten/input"
)
type Input struct {
states map[ui.Key]int
states map[input.Key]int
}
func NewInput() *Input {
states := map[ui.Key]int{}
for key := ui.Key(0); key < ui.KeyMax; key++ {
states := map[input.Key]int{}
for key := input.Key(0); key < input.KeyMax; key++ {
states[key] = 0
}
return &Input{
@ -18,13 +18,13 @@ func NewInput() *Input {
}
}
func (i *Input) StateForKey(key ui.Key) int {
func (i *Input) StateForKey(key input.Key) int {
return i.states[key]
}
func (i *Input) Update() {
for key := range i.states {
if !ui.IsKeyPressed(key) {
if !input.IsKeyPressed(key) {
i.states[key] = 0
continue
}

View File

@ -3,7 +3,7 @@ package blocks
import (
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten/ui"
"github.com/hajimehoshi/ebiten/input"
"image/color"
)
@ -21,7 +21,7 @@ func NewTitleScene() *TitleScene {
func (s *TitleScene) Update(state *GameState) {
s.count++
if state.Input.StateForKey(ui.KeySpace) == 1 {
if state.Input.StateForKey(input.KeySpace) == 1 {
state.SceneManager.GoTo(NewGameScene())
}
}

View File

@ -1,4 +1,4 @@
package ui
package input
type Key int

View File

@ -4,6 +4,7 @@ import (
glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/opengl"
"github.com/hajimehoshi/ebiten/input"
"github.com/hajimehoshi/ebiten/ui"
"image"
"runtime"
@ -29,7 +30,7 @@ func NewCanvas(width, height, scale int, title string) *Canvas {
funcsDone: make(chan struct{}),
}
ui.SetKeyboard(canvas.keyboard)
input.SetKeyboard(canvas.keyboard)
graphics.SetTextureFactory(canvas)
// For retina displays, recalculate the scale with the framebuffer size.

View File

@ -2,30 +2,30 @@ package glfw
import (
glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/ui"
"github.com/hajimehoshi/ebiten/input"
)
type Keyboard struct {
pressedKeys map[ui.Key]struct{}
pressedKeys map[input.Key]struct{}
}
func NewKeyboard() *Keyboard {
return &Keyboard{
pressedKeys: map[ui.Key]struct{}{},
pressedKeys: map[input.Key]struct{}{},
}
}
func (k *Keyboard) IsKeyPressed(key ui.Key) bool {
func (k *Keyboard) IsKeyPressed(key input.Key) bool {
_, ok := k.pressedKeys[key]
return ok
}
var glfwKeyCodeToKey = map[glfw.Key]ui.Key{
glfw.KeySpace: ui.KeySpace,
glfw.KeyLeft: ui.KeyLeft,
glfw.KeyRight: ui.KeyRight,
glfw.KeyUp: ui.KeyUp,
glfw.KeyDown: ui.KeyDown,
var glfwKeyCodeToKey = map[glfw.Key]input.Key{
glfw.KeySpace: input.KeySpace,
glfw.KeyLeft: input.KeyLeft,
glfw.KeyRight: input.KeyRight,
glfw.KeyUp: input.KeyUp,
glfw.KeyDown: input.KeyDown,
}
func (k *Keyboard) update(window *glfw.Window) {