1 | /* |
2 | * Copyright (c) 2010 Ni Hui <shuizhuyuanluo@126.com> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #ifndef UNDO_H |
20 | #define UNDO_H |
21 | |
22 | #include <QPointF> |
23 | #include <QUndoCommand> |
24 | #include <klocale.h> |
25 | #include "piece.h" |
26 | |
27 | const int ID_HIDEPIECE = 100; |
28 | const int ID_SWAPPIECE = 200; |
29 | |
30 | class HidePiece : public QUndoCommand |
31 | { |
32 | public: |
33 | explicit HidePiece( Piece* piece ) : m_piece(piece) { |
34 | setText( QLatin1String( "Hide Piece" ) ); |
35 | } |
36 | virtual int id() const { |
37 | return ID_HIDEPIECE; |
38 | } |
39 | virtual void undo() { |
40 | m_piece->setEnabled( true ); |
41 | m_piece->show(); |
42 | } |
43 | virtual void redo() { |
44 | m_piece->setEnabled( false ); |
45 | m_piece->hide(); |
46 | } |
47 | private: |
48 | friend void GameScene::loadGame( const KConfigGroup& config ); |
49 | friend void GameScene::saveGame( KConfigGroup& config ) const; |
50 | Piece* m_piece; |
51 | }; |
52 | |
53 | class SwapPiece : public QUndoCommand |
54 | { |
55 | public: |
56 | explicit SwapPiece( Piece** a, Piece** b, const QPointF& posA, const QPointF& posB ) |
57 | : m_pieceA(a), m_pieceB(b), m_posA(posA), m_posB(posB) { |
58 | setText( QLatin1String( "Swap Piece" ) ); |
59 | } |
60 | virtual int id() const { |
61 | return ID_SWAPPIECE; |
62 | } |
63 | virtual void undo() { |
64 | redo(); |
65 | } |
66 | virtual void redo() { |
67 | Piece* tmpA = *m_pieceA; |
68 | Piece* tmpB = *m_pieceB; |
69 | int tmpAX = tmpA->m_x; |
70 | int tmpAY = tmpA->m_y; |
71 | int tmpBX = tmpB->m_x; |
72 | int tmpBY = tmpB->m_y; |
73 | *m_pieceA = tmpB; |
74 | (*m_pieceA)->m_x = tmpAX; |
75 | (*m_pieceA)->m_y = tmpAY; |
76 | *m_pieceB = tmpA; |
77 | (*m_pieceB)->m_x = tmpBX; |
78 | (*m_pieceB)->m_y = tmpBY; |
79 | (*m_pieceA)->setPos( m_posA ); |
80 | (*m_pieceB)->setPos( m_posB ); |
81 | } |
82 | private: |
83 | Piece** m_pieceA; |
84 | Piece** m_pieceB; |
85 | const QPointF m_posA; |
86 | const QPointF m_posB; |
87 | }; |
88 | |
89 | #endif // UNDO_H |
90 | |