11using namespace std;
12
// ---------------------------------------------------------------------------------------------------------------
13
14
// ---------------------------------------------------------------------------------------------------------------
15#define hy_assert_error(str) { std::cerr << str << endl; assert(false); }
16#define hy_assert_warning(str) { std::cout << str << endl; }
17
// ---------------------------------------------------------------------------------------------------------------
18
19namespace hy
20{
21    RedoUndoTool::RedoUndoTool( int depth )
22        : m_iRedoUndoDepth(depth), m_iCurrentStep(-1)
23    {
24        m_aCommandStack.clear();
25    }
26
27    void RedoUndoTool::add( RedoUndoTool::Command * command )
28    {
29        int num = m_aCommandStack.size();
30
31        if ( m_iCurrentStep == num-1 || m_iCurrentStep == -1 )
32        {
33            m_aCommandStack.push_back(command);
34            m_iCurrentStep ++;
35        }
36        else if ( m_iCurrentStep < num-1 )
37        {
38            m_aCommandStack.erase(
39                m_aCommandStack.begin()+m_iCurrentStep+1, m_aCommandStack.end()
40                );
41            m_aCommandStack.push_back(command);
42            m_iCurrentStep ++;
43        }
44        else
45        {
46            hy_assert_error( \" error : RedoUndoTool::add( const RedoUndoTool::Command & command ) :\"
47                << \" invalid m_iCurrentStep\" );
48        }
49
50        // keep the depth
51        num = m_aCommandStack.size();
52        if ( num > m_iRedoUndoDepth )
53        {
54            m_aCommandStack.erase( m_aCommandStack.begin(),
55                m_aCommandStack.begin()+num-m_iRedoUndoDepth );
56            m_iCurrentStep = m_iRedoUndoDepth-1;
57        }
58    }
59
60    bool RedoUndoTool::redo()
61    {
62        int num = m_aCommandStack.size();
63        if ( m_iCurrentStep  == num-1 )
        {
65            hy_assert_warning(\" warning : RedoUndoTool::redo() :\"
66                << \" out of range: default to return directively!! \" );
67        }
68        else if ( m_iCurrentStep < num-1 && m_iCurrentStep >= -1 )
69        {
70            return m_aCommandStack[++m_iCurrentStep]->redo();
71        }
72        else
73        {
74            hy_assert_error(\" error : RedoUndoTool::redo() :\"
75                << \" invalid m_iCurrentStep\" );
76        }
77        return false;
78    }
79
80    bool RedoUndoTool::undo()
81    {
82        int num = m_aCommandStack.size();
83        if ( m_iCurrentStep == -1 )
84        {
85            hy_assert_warning(\" warning : RedoUndoTool::undo() :\"
86                << \" out of range: default to return directively!! \" );
87        }
88        else if ( m_iCurrentStep <= num-1 && m_iCurrentStep > -1 )
        {
90            return m_aCommandStack[m_iCurrentStep--]->undo();
91        }
92        else
93        {
94            hy_assert_error(\" error : RedoUndoTool::undo() :\"
95                << \" invalid m_iCurrentStep\" );
96        }
97        return false;
98    }
99
100}
101
这是显示模块,在这里只显示接口
1/***********************************************************************
2* \\file DrawTransaction.h
3* \\date 2008-07-07
4* \\author HYin
5**********************************************************************/
6
7#ifndef DRAWTRANSACTION_H
8#define DRAWTRANSACTION_H
9
10
// ---------------------------------------------------------------------------------------------------------------
11
// ---------------------------------------------------------------------------------------------------------------
12
13namespace hy
14{
15    class DrawTransaction
16    {
17    public :
18        DrawTransaction();
19        virtual ~DrawTransaction();
20
21        virtual void draw() =0;
22    };
23}
24
25#endif
26