You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. /***************************************************************************
  2. * Copyright (C) 2009 by Stefan Bühler *
  3. * stbuehler@web.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "game.h"
  21. #include <QGridLayout>
  22. #include <QResizeEvent>
  23. #include <QMouseEvent>
  24. #include <QPainter>
  25. #include <QStatusBar>
  26. #include <QMenuBar>
  27. #include <QMenu>
  28. #include <QAction>
  29. #include <QActionGroup>
  30. #include "ai.h"
  31. namespace toruschess {
  32. static QAction *makeAction(QObject *parent, const QString &text, const QObject * receiver, const char * member, const QKeySequence &shortcut = QKeySequence(), QActionGroup *group = NULL) {
  33. QAction *a = new QAction(text, parent);
  34. a->setShortcut(shortcut);
  35. if (group) a->setActionGroup(group);
  36. QObject::connect(a, SIGNAL(triggered(bool)), receiver, member);
  37. return a;
  38. }
  39. GameWidget::GameWidget(QWidget *parent)
  40. : QMainWindow(parent), m_game(new Game()), m_optionDlg(new OptionDlg(m_game, this)), m_viewMode((ViewMode) -1) {
  41. connect(this, SIGNAL(changedViewMode(ViewMode)), m_optionDlg, SLOT(changedViewMode(ViewMode)));
  42. connect(m_optionDlg, SIGNAL(setViewMode(ViewMode)), this, SLOT(setViewMode(ViewMode)));
  43. setWindowTitle("Torus Chess");
  44. QWidget *centralWidget = new QWidget(this);
  45. m_fieldsLayout = new QStackedLayout(centralWidget);
  46. m_field2d = new Field2D(m_game, centralWidget);
  47. m_field3d = new Field3D(m_game, centralWidget);
  48. m_fieldsLayout->addWidget(m_field2d);
  49. m_fieldsLayout->addWidget(m_field3d);
  50. centralWidget->setLayout(m_fieldsLayout);
  51. setCentralWidget(centralWidget);
  52. statusBar()->show();
  53. connect(m_game, SIGNAL(changed(GameState)), this, SLOT(gameChanged(GameState)));
  54. m_game->restart();
  55. QAction
  56. *act_game_newGame = makeAction(this, "&New Game", this, SLOT(menuRestart()), Qt::Key_F2),
  57. *act_game_options = makeAction(this, "&Options", this, SLOT(menuOptions()), Qt::CTRL + Qt::Key_O),
  58. *act_game_quit = makeAction(this, "&Quit", this, SLOT(close()));
  59. QActionGroup *viewGroup = new QActionGroup(this);
  60. m_act_view_2d = makeAction(this, "&2D-View", this, SLOT(menuView2d()), Qt::CTRL + Qt::Key_2, viewGroup);
  61. m_act_view_3d = makeAction(this, "&3D-View", this, SLOT(menuView3d()), Qt::CTRL + Qt::Key_3, viewGroup);
  62. m_act_view_torus = makeAction(this, "&Torus-View", this, SLOT(menuViewTorus()), Qt::CTRL + Qt::Key_4, viewGroup);
  63. act_game_quit->setMenuRole(QAction::QuitRole);
  64. QMenuBar *menuBar = new QMenuBar(this);
  65. QMenu *menuGame = menuBar->addMenu("&Game");
  66. menuGame->addAction(act_game_newGame);
  67. menuGame->addAction(act_game_options);
  68. menuGame->addSeparator();
  69. menuGame->addAction(act_game_quit);
  70. QMenu *menuView = menuBar->addMenu("&View");
  71. menuView->addActions(viewGroup->actions());
  72. setMenuBar(menuBar);
  73. setViewMode(VIEW_2D);
  74. updateGeometry();
  75. }
  76. void GameWidget::gameChanged(GameState state) {
  77. statusBar()->showMessage(state2string(state));
  78. }
  79. void GameWidget::menuRestart() {
  80. m_game->restart();
  81. }
  82. void GameWidget::menuOptions() {
  83. m_optionDlg->exec();
  84. }
  85. void GameWidget::setViewMode(ViewMode viewMode) {
  86. if (m_viewMode == viewMode) return;
  87. m_viewMode = viewMode;
  88. switch (m_viewMode) {
  89. case VIEW_2D:
  90. m_fieldsLayout->setCurrentIndex(0);
  91. m_act_view_2d->setChecked(true);
  92. break;
  93. case VIEW_3D:
  94. m_fieldsLayout->setCurrentIndex(1);
  95. m_act_view_3d->setChecked(true);
  96. m_field3d->setBoardMode(Field3D::PLANE);
  97. break;
  98. case VIEW_TORUS:
  99. m_fieldsLayout->setCurrentIndex(1);
  100. m_act_view_torus->setChecked(true);
  101. m_field3d->setBoardMode(Field3D::TORUS);
  102. break;
  103. }
  104. emit changedViewMode(viewMode);
  105. }
  106. void GameWidget::menuView2d() {
  107. setViewMode(VIEW_2D);
  108. }
  109. void GameWidget::menuView3d() {
  110. setViewMode(VIEW_3D);
  111. }
  112. void GameWidget::menuViewTorus() {
  113. setViewMode(VIEW_TORUS);
  114. }
  115. }