File indexing completed on 2024-04-28 07:53:25

0001 /*
0002     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0003     SPDX-FileCopyrightText: 2010 Brian Croom <brian.s.croom@gmail.com>
0004     SPDX-FileCopyrightText: 2013 Denis Kuplyakov <dener.kup@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KREVERSI_VIEW_H
0010 #define KREVERSI_VIEW_H
0011 
0012 // game
0013 #include "commondefs.h"
0014 #include "kreversigame.h"
0015 // KDEGames
0016 #include <KGameThemeProvider>
0017 // Qt
0018 #include <QQuickWidget>
0019 
0020 /**
0021  *  This class provides graphical representation of KReversiGame
0022  *  using QML for graphics display.
0023  *  It displays the reversi board in its current state,
0024  *  receives a mouse events, translates them with signals,
0025  *  receives board-changed notifications, nicely animates them.
0026  */
0027 class KReversiView : public QQuickWidget
0028 {
0029     Q_OBJECT
0030 public:
0031     explicit KReversiView(KReversiGame* game, QWidget *parent, KGameThemeProvider *provider);
0032     /**
0033      *  Destructor used to delete game object owned by class
0034      */
0035     ~KReversiView() override;
0036 
0037     /**
0038      *  Sets the game object which this view will visualize/use
0039      *
0040      *  @param game pointer to game object for visualization. View takes
0041      *              ownership over game object and will delete it
0042      */
0043     void setGame(KReversiGame* game);
0044 
0045     /**
0046      *  Sets the chips prefix to @p chipsPrefix
0047      */
0048     void setChipsPrefix(ChipsPrefix chipsPrefix);
0049 
0050     /**
0051      *  Sets whether to show board labels.
0052      *
0053      *  @param show @c true to show labels
0054      *              @c false to hide labels
0055      */
0056     void setShowBoardLabels(bool show);
0057 
0058     /**
0059      *  Sets the animation speed
0060      *
0061      *  @param speed 0 - slow, 1 - normal, 2 - fast
0062      *
0063      *  @return time for animation in milliseconds to pass it to KReversiGame
0064      */
0065     void setAnimationSpeed(int speed);
0066 
0067 public Q_SLOTS:
0068     /**
0069     *   This will make view visually mark the last made move
0070     *
0071     *   @param show @c true to show last move
0072     *               @c false to don't show last move
0073      */
0074     void setShowLastMove(bool show);
0075 
0076     /**
0077      *  This will make view visually mark squares with possible moves
0078      *
0079      *  @param show @c true to show legal moves
0080      *              @c false to don't show legal moves
0081      */
0082     void setShowLegalMoves(bool show);
0083 
0084     /**
0085      *  Shows hint for player
0086      */
0087     void slotHint();
0088 
0089 private Q_SLOTS:
0090     /**
0091      *  Triggered on user click on board, connected to QML signal
0092      *
0093      *  @param row index of the clicked cell row (starting from 0)
0094      *  @param col index of the clicked cell column (starting from 0)
0095      */
0096     void onPlayerMove(int row, int col);
0097     /**
0098      *  Synchronizes graphical board with m_game's board
0099      */
0100     void updateBoard();
0101     void gameMoveFinished();
0102     void gameOver();
0103     void whitePlayerCantMove();
0104     void blackPlayerCantMove();
0105 Q_SIGNALS:
0106     void userMove(KReversiPos);
0107 
0108 private:
0109     /**
0110      *  40 ms time per frame for animation
0111      */
0112     static const int ANIMATION_SPEED_SLOW = 40 * 12;
0113 
0114     /**
0115      *  25 ms time per frame for animation
0116      */
0117     static const int ANIMATION_SPEED_NORMAL = 25 * 12;
0118 
0119     /**
0120      *  15 ms time per frame for animation
0121      */
0122     static const int ANIMATION_SPEED_FAST = 15 * 12;
0123 
0124     /**
0125      *  Used to provide access to QML-implemented board
0126      */
0127     QObject *m_qml_root;
0128 
0129     /**
0130      *  Used to access theme engine from QML
0131      */
0132     KGameThemeProvider *m_provider;
0133 
0134     /**
0135      *  Position of calculated hint. It is not valid if there is no hint
0136      */
0137     KReversiMove m_hint;
0138 
0139     /**
0140      *  Current animation time
0141      */
0142     int m_delay;
0143 
0144     /**
0145      *  Pointer to game object
0146      */
0147     KReversiGame *m_game;
0148 
0149     /**
0150      *  The SVG element prefix for the current chip set
0151      */
0152     ChipsPrefix m_ColouredChips;
0153 
0154     /**
0155      *  If true, then last made turn will be shown to the player
0156      */
0157     bool m_showLastMove;
0158 
0159     /**
0160      *  If true, then all possible moves will be shown to the player
0161      */
0162     bool m_showLegalMoves;
0163 
0164     /**
0165      *  If true board labels will be rendered
0166      */
0167     bool m_showLabels;
0168     
0169     /**
0170      *  Used to handle animation duration due to sequental turning of chips
0171      */
0172     int m_maxDelay;
0173 };
0174 #endif