go-tetris/engineKeyInput.go

162 lines
3.0 KiB
Go
Raw Normal View History

2019-12-01 15:03:27 -06:00
package main
import (
"runtime"
"github.com/gdamore/tcell"
)
// ProcessEventKey process the key input event
func (engine *Engine) ProcessEventKey(eventKey *tcell.EventKey) {
if eventKey.Key() == tcell.KeyCtrlL {
// Ctrl l (lower case L) to log stack trace
buffer := make([]byte, 1<<16)
length := runtime.Stack(buffer, true)
logger.Println("Stack trace")
logger.Println(string(buffer[:length]))
return
}
switch engine.mode {
// edit
case engineModeEdit:
if edit.boardSize {
switch eventKey.Key() {
case tcell.KeyUp:
edit.BoardHeightIncrement()
case tcell.KeyDown:
edit.BoardHeightDecrement()
case tcell.KeyLeft:
edit.BoardWidthDecrement()
case tcell.KeyRight:
edit.BoardWidthIncrement()
case tcell.KeyRune:
if eventKey.Rune() == 'q' {
edit.ChangeBoardSize()
}
}
} else {
switch eventKey.Key() {
case tcell.KeyUp:
edit.CursorUp()
case tcell.KeyDown:
edit.CursorDown()
case tcell.KeyLeft:
edit.CursorLeft()
case tcell.KeyRight:
edit.CursorRight()
case tcell.KeyCtrlB:
edit.BoardSizeMode()
case tcell.KeyCtrlS:
edit.SaveBoard()
case tcell.KeyCtrlN:
edit.SaveBoardNew()
case tcell.KeyCtrlK:
edit.DeleteBoard()
case tcell.KeyCtrlO:
edit.EmptyBoard()
case tcell.KeyCtrlQ, tcell.KeyCtrlC:
engine.DisableEditMode()
case tcell.KeyRune:
switch eventKey.Rune() {
case 'c':
edit.SetColor(colorCyan)
case 'b':
edit.SetColor(colorBlue)
case 'w':
edit.SetColor(colorWhite)
case 'e':
edit.SetColor(colorYellow)
case 'g':
edit.SetColor(colorGreen)
case 'a':
edit.SetColor(colorMagenta)
case 'r':
edit.SetColor(colorRed)
case 'f':
edit.SetColor(colorBlank)
case 'z':
edit.RotateLeft()
case 'x':
edit.RotateRight()
}
}
}
// game over
case engineModeGameOver, engineModePreview:
switch eventKey.Key() {
case tcell.KeyCtrlC:
engine.Stop()
case tcell.KeyLeft:
board.PreviousBoard()
case tcell.KeyRight:
board.NextBoard()
case tcell.KeyCtrlE:
engine.EnabledEditMode()
case tcell.KeyRune:
switch eventKey.Rune() {
case 'q':
engine.Stop()
case ' ':
engine.NewGame()
}
}
// paused
case engineModePaused:
switch eventKey.Rune() {
case 'q':
engine.Stop()
case 'p':
engine.UnPause()
}
// run with AI
case engineModeRunWithAI:
switch eventKey.Rune() {
case 'q':
engine.Stop()
case 'p':
engine.Pause()
case 'i':
engine.DisableAi()
}
// run
case engineModeRun:
switch eventKey.Key() {
case tcell.KeyUp:
board.MinoDrop()
case tcell.KeyDown:
board.MinoMoveDown()
case tcell.KeyLeft:
board.MinoMoveLeft()
case tcell.KeyRight:
board.MinoMoveRight()
case tcell.KeyRune:
switch eventKey.Rune() {
case 'q':
engine.Stop()
case ' ':
board.MinoDrop()
case 'z':
board.MinoRotateLeft()
case 'x':
board.MinoRotateRight()
case 'p':
engine.Pause()
case 'i':
engine.EnabledAi()
}
}
}
}