public void KeyDown(object sender, KeyEventArgs e) { // NOTE: The only way for cursor key events (up,down,left,right) // to make it to this function is for the main form to implement // the following: // // protected override bool ProcessDialogKey ( Keys keyData ) // // and explicitly invoke this KeyDown() method with the // an appropriately formed KeyEventArgs instance. // ALSO, the KeyPreview property of the main Form must be set to // 'true' for that override method to be called. STUserInterface.HandleKeyPress ( STEngine.GetMainForm( ).mGRControl.GetGR( ), STEngine.GetMainForm( ).Handle, 0, 0, STEngine.GetGame( ), e.KeyCode, e.Shift, e.Control ); }
public static void HandleKeyPress ( GR gr, IntPtr hwnd, int wParam, int lParam, STGame game, Keys keyCode, bool shiftKeyState, bool controlKeyState ) { STGameState gameState = game.GetGameState( ); // Priority of key press handling: // (1) Instructions; ESCAPE exits instructions; // (2) File Menu; ESCAPE exits file menu; // (3) Calibrate; ESCAPE cancels calibrate mode; // (4) Video Capture; ESCAPE quits application; // (5) Normal; ESCAPE quits application; // INSTRUCTIONS if (0 != game.InstructionGetState( )) { switch (keyCode) { case Keys.Down: case Keys.Next: // Page-Down case Keys.Right: { // Next page game.InstructionsNextPage( ); } break; case Keys.Up: case Keys.Prior: // Page-Up case Keys.Left: { // Previous page game.InstructionsPreviousPage( ); } break; default: { // User hit a key, but it wasn't relevant, so exit menu. game.InstructionsHide( ); // NOTE: Don't resume! : game.InputEventResume(); } break; } return; } else if (keyCode == Keys.I) { game.InstructionsShow( ); game.InputEventPause( ); return; } // FILE LIST if (true == gameState.mShowFileList) { switch (keyCode) { case Keys.Next: // Page-Down { // Next page gameState.mFirstItem += 20; } break; case Keys.Prior: // Page-Up { // Previous page gameState.mFirstItem -= 20; } break; case Keys.Down: { // Next Item gameState.mRelativeItem++; if (gameState.mRelativeItem > 19) { gameState.mFirstItem++; gameState.mRelativeItem = 19; } } break; case Keys.Up: { // Previous Item gameState.mRelativeItem--; if (gameState.mRelativeItem < 0) { gameState.mFirstItem--; gameState.mRelativeItem = 0; } } break; case Keys.Return: { // Load item gameState.mLoadFlag = true; } break; default: { // User hit a key, but it wasn't relevant, so exit menu. gameState.mShowFileList = false; // NOTE: Don't resume. : game.InputEvent_Resume(); } break; } return; } else if ((keyCode == Keys.L) && (true == shiftKeyState)) { // SHIFT-L will read a text file in to the game state. game.InputEventPause( ); gameState.mShowFileList = true; gameState.mFirstItem = 0; gameState.mRelativeItem = 0; gameState.mLoadFlag = false; STEngine.GetFileList().ScanDirectory(STEngine.GetApplicationPath( )); return; } // Calibrate Mode // (NOTE: See how normal mode enters calibrate mode by pressing 'C'.) if (true == game.GetCalibrationModeFlagValue( )) { if ((Keys.Escape == keyCode) || (Keys.C == keyCode)) { game.SetCalibrationModeFlagValue(false); game.InputEventResume( ); return; } if (keyCode == Keys.V) { if (false == game.GameIsSpawnFromVideoCapture( )) { // Set up sane conditions game.InputEventReset( ); game.InputEventShowNextPieceOff( ); game.InputEventAutoRestartOff( ); // Initialize Video Capture STEngine.GetVideoProcessing( ).Initialize(gr, hwnd); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.InputEventVideoStart( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); } else { STEngine.GetVideoProcessing( ).Terminate( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.InputEventVideoStop( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); } return; } switch (keyCode) { // training mode piece selection case Keys.D0: game.SetCalibrationModeShapeCode(0); break; case Keys.D1: game.SetCalibrationModeShapeCode(1); break; case Keys.D2: game.SetCalibrationModeShapeCode(2); break; case Keys.D3: game.SetCalibrationModeShapeCode(3); break; case Keys.D4: game.SetCalibrationModeShapeCode(4); break; case Keys.D5: game.SetCalibrationModeShapeCode(5); break; case Keys.D6: game.SetCalibrationModeShapeCode(6); break; case Keys.D7: game.SetCalibrationModeShapeCode(7); break; case Keys.D8: game.SetCalibrationModeShapeCode(0); break; case Keys.D9: game.SetCalibrationModeShapeCode(0); break; } return; } // Video Capture // The following is not mutually-exclusive with normal game play. if (true == game.GameIsSpawnFromVideoCapture( )) { if (keyCode == Keys.Return) { STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.ClearPreviousClassification( ); game.InputEventReset( ); System.Threading.Thread.Sleep(200); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.ClearPreviousClassification( ); game.InputEventReset( ); System.Threading.Thread.Sleep(200); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.ClearPreviousClassification( ); game.InputEventReset( ); System.Threading.Thread.Sleep(200); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.ClearPreviousClassification( ); game.InputEventReset( ); System.Threading.Thread.Sleep(200); } if (keyCode == Keys.V) { if (false == game.GameIsSpawnFromVideoCapture( )) { // Set up sane conditions game.InputEventReset( ); game.InputEventShowNextPieceOff( ); game.InputEventAutoRestartOff( ); // Initialize Video Capture STEngine.GetVideoProcessing( ).Initialize(gr, hwnd); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.InputEventVideoStart( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); } else { STEngine.GetVideoProcessing( ).Terminate( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.InputEventVideoStop( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); } return; } } // Console Mode if (true == gameState.mShowConsole) { if (keyCode == Keys.Delete) { STEngine.GetConsole( ).ClearAllLines( ); } else { // Any key other than delete or P (pause) exits console mode. if (keyCode != Keys.P) { gameState.mShowConsole = false; } } } else { if ((keyCode == Keys.Q) && (true == shiftKeyState)) { // SHIFT-Q : Console gameState.mShowConsole = true; } } // Normal Game Play // QUIT KEY: ESCAPE if (keyCode == Keys.Escape) { Form form = (Form)STEngine.GetMainForm( ); form.Close( ); return; } // Enter Calibrate Mode if (keyCode == Keys.C) { game.SetCalibrationModeFlagValue(true); game.SetCalibrationModeShapeCode(1); game.InputEventPause( ); } // Enable Video Capture if (keyCode == Keys.V) { if (false == game.GameIsSpawnFromVideoCapture( )) { // Set up sane conditions game.InputEventReset( ); game.InputEventShowNextPieceOff( ); game.InputEventAutoRestartOff( ); // Initialize Video Capture STEngine.GetVideoProcessing( ).Initialize(gr, hwnd); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); game.InputEventVideoStart( ); STEngine.GetVideoProcessing( ).ClearRegionStatus( ); } } // Reset Game if (keyCode == Keys.Return) { if (true == shiftKeyState) { game.InputEventHardReset( ); } else { game.InputEventReset( ); } } if (keyCode == Keys.P) { if (true == game.GameIsPaused( )) { game.InputEventResume( ); } else { game.InputEventPause( ); } } if (keyCode == Keys.A) { if (true == shiftKeyState) { STStrategyManager.SelectNextStrategy( ); } else { if (true == game.GameIsAI( )) { game.InputEventAIStop( ); } else { game.InputEventAIStart( ); } } } if (keyCode == Keys.T) { if (game.GameIsOutputToRS232( )) { game.InputEventRS232Stop( ); STRS232.TerminatePort( ); } else { STRS232.InitializePort( ); game.InputEventRS232Start( ); } } if ((keyCode == Keys.Subtract) || (keyCode == Keys.OemMinus)) // 0xbd { if (false == shiftKeyState) { game.InputEventGameSpeedDecrease( ); } } if ((keyCode == Keys.Add) || (keyCode == Keys.Oemplus)) // 0xbb { if (false == shiftKeyState) { game.InputEventGameSpeedIncrease( ); } } if ((keyCode == Keys.W) && (true == shiftKeyState)) { // SHIFT-W will write out a text file (c:\tetris_state.txt) game.InputEventGameStateWriteToFile( ); } if (keyCode == Keys.Next) // Page-Down { // Page-Down: Decrease Board Size game.InputEventGameBoardDecrease( ); } if (keyCode == Keys.Prior) // Page-Up { // Page-Up: Increase Board Size game.InputEventGameBoardIncrease( ); } if (true == controlKeyState) { if (keyCode == Keys.Up) { game.InputEventGameBoardIncreaseHeight( ); } if (keyCode == Keys.Left) { game.InputEventGameBoardDecreaseWidth( ); } if (keyCode == Keys.Right) { game.InputEventGameBoardIncreaseWidth( ); } if (keyCode == Keys.Down) { game.InputEventGameBoardDecreaseHeight( ); } } // COLOR SCHEME if ((keyCode == Keys.K) && (true == shiftKeyState)) { if (false == game.GetGameState( ).mMonochromeColorMode) { game.GetGameState( ).mMonochromeColorMode = true; } else { game.GetGameState( ).mMonochromeColorMode = false; } } // Non Video-Capture Options if (false == game.GameIsSpawnFromVideoCapture( )) { // Only respond to user piece-control input if AI is not active. if (false == game.GameIsAI( )) { if (false == controlKeyState) { if (keyCode == Keys.Up) { game.InputEventRotate( ); } if (keyCode == Keys.Left) { game.InputEventLeft( ); } if (keyCode == Keys.Right) { game.InputEventRight( ); } if (keyCode == Keys.Down) { game.InputEventDrop( ); } if (keyCode == Keys.Space) { game.InputEventDrop( ); } } } if (keyCode == Keys.Z) { if ((STPieceSequence.STPieceSelectionSource.AlternatingSAndZ) == game.GetPieceSequenceSourceType( )) { // Since we're in S/Z mode, stop. game.InputEventSZPieceModeStop( ); } else { // Start S/Z mode. game.InputEventSZPieceModeStart( ); } } if (keyCode == Keys.S) { // S will cycle the shadow mode. game.InputEventShadowModeCycle( ); } if ((keyCode == Keys.J) && (true == shiftKeyState)) { // SHIFT-J : Add line of random junk to bottom of the pile. game.InputEventAddRowOfJunk( ); } if ((keyCode == Keys.H) && (true == shiftKeyState)) { // SHIFT-H : Hint Mode if (true == game.GameIsHintMode( )) { game.InputEventHintModeStop( ); } else { game.InputEventHintModeStart( ); } } if (keyCode == Keys.N) { if (true == game.GameIsShowNextPiece( )) { game.InputEventShowNextPieceOff( ); } else { game.InputEventShowNextPieceOn( ); } } if (keyCode == Keys.X) { game.InputEventToggleMoveAnimation( ); } if (keyCode == Keys.U) { if (true == game.GameIsAutoRestart( )) { game.InputEventAutoRestartOff( ); } else { game.InputEventAutoRestartOn( ); } } if (keyCode == Keys.F) { game.InputEventToggleAutoWriteFile( ); } if ((keyCode == Keys.R) && (true == shiftKeyState)) { // SHIFT-R : Soft reset (game goes back to same random seed) game.InputEventSoftReset( ); } } }