📄 kmahjongg.cpp
字号:
break; case ID_GAME_DEMO: if( bDemoModeActive ) { bw->stopDemoMode(); } else { // we assume demo mode removes tiles so we can // disbale redo here. bw->Game.allow_redo=false; bw->startDemoMode(); } break; case ID_GAME_PAUSE: case ID_GAME_CONTINUE: is_paused = !is_paused; demoModeChanged(false); gameTimer->pause(); bw->pause(); break; case ID_GAME_SHUFFLE: bw->shuffle(); break; case ID_GAME_SHOW: bShowMatchingTiles = ! bShowMatchingTiles; bw->setShowMatch( bShowMatchingTiles ); pMenuBar->setItemChecked( ID_GAME_SHOW, bShowMatchingTiles ); break; case ID_FILE_LOAD_BOARD: previewLoad->initialise(Preview::board, EXT_LAYOUT); previewLoad->exec(); break; case ID_FILE_LOAD_BACKGND: previewLoad->initialise(Preview::background, EXT_BACKGROUND); previewLoad->exec(); break; case ID_FILE_LOAD_TILESET: previewLoad->initialise(Preview::tileset, EXT_TILESET); previewLoad->exec(); break; case ID_FILE_LOAD_THEME: previewLoad->initialise(Preview::theme, EXT_THEME); previewLoad->exec(); break; case ID_EDIT_BOARD_EDIT: if (!boardEditor) boardEditor = new Editor(this); boardEditor->exec(); break; case ID_GAME_SHOW_HISCORE: theHighScores->exec(bw->getLayoutName()); break; case ID_FILE_LOAD_GAME: loadGame(); break; case ID_FILE_SAVE_GAME: saveGame(); break; case ID_FILE_SAVE_THEME: previewLoad->initialise(Preview::theme, EXT_THEME); previewLoad->saveTheme(); break; }}//----------------------------------------------------------// signalled from the prieview dialog to generate a new game// we don't make startNewGame a slot because it has a default// param.void KMahjonggWidget::newGame(void) { startNewGame();}// ---------------------------------------------------------void KMahjonggWidget::startNewGame( int item ){ if( ! bDemoModeActive ) { bw->calculateNewGame(item); // initialise button states bw->Game.allow_redo = bw->Game.allow_undo = 0; timerReset(); // update the initial enabled/disabled state for // the menu and the tool bar. demoModeChanged(false); }}// ---------------------------------------------------------void KMahjonggWidget::timerReset(void) { // initialise the scoring system gameElapsedTime = 0; // start the game timer gameTimer->start();}// ---------------------------------------------------------void KMahjonggWidget::gameOver( unsigned short numRemoved, unsigned short cheats){ int time; int score; int elapsed = gameTimer->toInt(); time = score = 0; // get the time in milli secs // subtract from 20 minutes to get bonus. if longer than 20 then ignore time = (60*20) - gameTimer->toInt(); if (time <0) time =0; // conv back to secs (max bonus = 60*20 = 1200 // points per removed tile bonus (for deragon max = 144*10 = 1440 score += (numRemoved * 20); // time bonus one point per second under one hour score += time; // points per cheat penalty (max penalty = 1440 for dragon) score -= (cheats *20); if (score < 0) score = 0; theHighScores->checkHighScore(score, elapsed, bw->getGameNum(), bw->getBoardName());}// ---------------------------------------------------------void KMahjonggWidget::closeEvent( QCloseEvent* e ){ kapp->quit(); e->accept();}// ---------------------------------------------------------void KMahjonggWidget::showStatusText( const QString &msg, long board ){ statusLabel->setText(msg); QString str = i18n("Game Number: %1").arg(board); gameNumLabel->setText(str);}// ---------------------------------------------------------void KMahjonggWidget::showMessage( const QString &msg ){ KMessageBox::information( this, msg );}// ---------------------------------------------------------void KMahjonggWidget::showTileNumber( int iMaximum, int iCurrent ){ QString szBuffer = i18n("Removed: %1/%2").arg(iCurrent).arg(iMaximum); tilesLeftLabel->setText(szBuffer); // Update here since undo allow is effected by demo mode // removal. However we only change the enabled state of the // items when not in demo mode bw->Game.allow_undo = iMaximum != iCurrent; // update undo menu item, if demomode is inactive if( ! bDemoModeActive && !is_paused) { pMenuBar->setItemEnabled( ID_EDIT_UNDO, bw->Game.allow_undo); toolBar->setItemEnabled( ID_EDIT_UNDO, bw->Game.allow_undo); }}// ---------------------------------------------------------// Set a menu button and tool bar items enabled state. Saves// a lot of redundant code for menu's and toolbars.void KMahjonggWidget::enableItem(int item, bool state){ pMenuBar->setItemEnabled( item, state); toolBar->setItemEnabled( item, state);}// ---------------------------------------------------------void KMahjonggWidget::demoModeChanged( bool bActive){ bDemoModeActive = bActive; if (bActive || is_paused) { enableItem( ID_EDIT_UNDO, false); enableItem( ID_EDIT_REDO, false); if (!is_paused) { enableItem( ID_GAME_PAUSE, false); enableItem( ID_GAME_CONTINUE, false); enableItem( ID_GAME_DEMO, true); } else { enableItem( ID_GAME_PAUSE, !is_paused); enableItem( ID_GAME_CONTINUE, is_paused); enableItem( ID_GAME_DEMO, false); } } else { enableItem( ID_EDIT_UNDO, bw->Game.allow_undo); enableItem( ID_EDIT_REDO, bw->Game.allow_redo); enableItem( ID_GAME_PAUSE, !is_paused); enableItem( ID_GAME_CONTINUE, is_paused); enableItem( ID_GAME_DEMO, true); } enableItem( ID_FILE_LOAD_TILESET, !bActive && !is_paused ); enableItem( ID_FILE_NEW, !bActive && !is_paused); enableItem( ID_FILE_LOAD_BACKGND, !bActive && !is_paused ); enableItem( ID_FILE_LOAD_THEME, !bActive && !is_paused); enableItem( ID_FILE_LOAD_GAME, !bActive && !is_paused); enableItem( ID_FILE_SAVE_GAME, !bActive && !is_paused); enableItem( ID_EDIT_BOARD_EDIT, !bActive && !is_paused); enableItem( ID_EDIT_PREFS, !bActive && !is_paused); enableItem( ID_GAME_HELP, !bActive && !is_paused); enableItem( ID_GAME_SHOW, !bActive && !is_paused); enableItem( ID_GAME_SHOW_HISCORE, !bActive && !is_paused); enableItem( ID_FILE_LOAD_BOARD, !bActive && !is_paused);}void KMahjonggWidget::statusBarMode(int onOff) { updateStatusbar(onOff);}void BoardWidget::getFileOrDefault(QString filename, QString type, QString &res){ QString picsPos = "pics/"; picsPos += "default."; picsPos += type; QFile test; test.setName(filename); if (test.exists()) { res = filename; } else { res = locate("appdata", picsPos); } if (res.isEmpty()) { KMessageBox::error(this, i18n("Kmahjongg could not locate the file: ") + filename + i18n("\nOr the default file of type: ")+ type + i18n("\nKmahjongg will now terminate") ); exit(0); }} void KMahjonggWidget::backgroundMode(void) { bw->updateScaleMode();}void KMahjonggWidget::loadBoardLayout(const QString &file) { bw->loadBoardLayout(file);}void KMahjonggWidget::tileSizeChanged(void) { bw->tileSizeChanged(); setDisplayedWidth();}void KMahjonggWidget::loadGame(void) { GAMEDATA in; char buffer[1024]; QString fname; // Get the name of the file to load KURL url = KFileDialog::getOpenURL( NULL, "*.kmgame", this, i18n("Load game." ) ); if ( url.isEmpty() ) return; KIO::NetAccess::download( url, fname ); // open the file for reading FILE *outFile = fopen( QFile::encodeName(fname), "r"); if (outFile == NULL) { KMessageBox::sorry(this, i18n("Could not read from file. Aborting.")); return; } // verify the magic fscanf(outFile, "%s\n", buffer); if (buffer != gameMagic) { KMessageBox::sorry(this, i18n("File format not recognised.")); fclose(outFile); return; } //ed the elapsed time fscanf(outFile, "%s\n", buffer); gameTimer->fromString(buffer); // suck out all the game data fread(&in, sizeof(GAMEDATA), 1, outFile); memcpy(&bw->Game, &in, sizeof(GAMEDATA)); // close the file before exit fclose(outFile); KIO::NetAccess::removeTempFile( fname ); // refresh the board bw->drawBoard();}void KMahjonggWidget::saveGame(void) { // Get the name of the file to save KURL url = KFileDialog::getSaveURL( NULL, "*.kmgame", this, i18n("Save game." ) ); if ( url.isEmpty() ) return; if( !url.isLocalFile() ) { KMessageBox::sorry( this, i18n( "Only saving to local files currently supported." ) ); return; } FILE *outFile = fopen( QFile::encodeName(url.path()), "w"); if (outFile == NULL) { KMessageBox::sorry(this, i18n("Could not write to file. Aborting.")); return; } // stick in the magic id string fprintf(outFile, "%s\n", gameMagic); // Now stick in the elapsed time for the game fprintf(outFile, "%s\n", gameTimer->toString().utf8().data()); // chuck in all the game data fwrite(&bw->Game, sizeof(GAMEDATA), 1, outFile); // close the file before exit fclose(outFile); }// ---------------------------------------------------------/** Constructor. Loads tileset and background bitmaps.*/BoardWidget::BoardWidget( QWidget* parent ) : QWidget( parent ), theTiles(preferences.miniTiles()){ setBackgroundColor( QColor( 0,0,0 ) ); TimerState = Stop; gamePaused = false; iTimerStep = 0; matchCount = 0; showMatch = false; MouseClickPos1.e = BoardLayout::depth; // mark tile position as invalid MouseClickPos2.e = BoardLayout::depth; // initially we force a redraw updateBackBuffer=true; // Load tileset. First try to load the last use tileset QString tFile;progress("Loading tileset"); getFileOrDefault(preferences.tileset(), "tileset", tFile); if (!loadTileset(tFile)){ KMessageBox::error(this, i18n("An error ocurred when loading the tileset file\n")+ tFile + i18n("Kmahjongg will now terminate")); exit(0); }progress("loading background image"); getFileOrDefault(preferences.background(), "bgnd", tFile); // Load background if( ! loadBackground(tFile, false ) ) { KMessageBox::error(this, i18n("An error ocurred when loading the background image\n")+ tFile + i18n("Kmahjongg will now terminate")); exit(0); }progress("loading layout"); getFileOrDefault(preferences.layout(), "layout", tFile); if( ! loadBoardLayout(tFile) ) { KMessageBox::error(this, i18n("An error ocurred when loading the board layout\n")+ tFile + i18n("Kmahjongg will now terminate")); exit(0); } setDisplayedWidth(); progress("drawing board"); show();}void BoardWidget::setDisplayedWidth() { if (preferences.showRemoved()){ setFixedSize( requiredWidth() , requiredHeight()); } else { setFixedSize( requiredWidth() - ((theTiles.width())*4) , requiredHeight()); }}// for a given cell x y calc how that cell is shadowed// returnd left = width of left hand side shadow// t = height of top shadow// c = width and height of corner shadowvoid BoardWidget::calcShadow(int e, int y, int x, int &l, int &t, int &c) { l = t = c = 0; if ((Game.shadowHeight(e,y,x) != 0) || (Game.shadowHeight(e,y-1,x) != 0) || (Game.shadowHeight(e,y,x-1) != 0)) { return; } int a,b;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -