| 1 | /* |
|---|
| 2 | ================================================================================= |
|---|
| 3 | This file is part of Cafu, the open-source game engine and graphics engine |
|---|
| 4 | for multiplayer, cross-platform, real-time 3D action. |
|---|
| 5 | Copyright (C) 2002-2012 Carsten Fuchs Software. |
|---|
| 6 | |
|---|
| 7 | Cafu is free software: you can redistribute it and/or modify it under the terms |
|---|
| 8 | of the GNU General Public License as published by the Free Software Foundation, |
|---|
| 9 | either version 3 of the License, or (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|---|
| 12 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 13 | PURPOSE. See the GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with Cafu. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | For support and more information about Cafu, visit us at <http://www.cafu.de>. |
|---|
| 19 | ================================================================================= |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #ifndef CAFU_COMMAND_PATTERN_HPP_INCLUDED |
|---|
| 23 | #define CAFU_COMMAND_PATTERN_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | /// \file |
|---|
| 26 | /// This file provides the classes for the Command pattern as described in the book by the GoF. |
|---|
| 27 | |
|---|
| 28 | #include "wx/wx.h" |
|---|
| 29 | #include "Templates/Array.hpp" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /// This class represents a general command for implementing modifications to the applications document. |
|---|
| 33 | /// Commands are usually kept in an undo/redo history in order to provide the user with undo and redo. |
|---|
| 34 | /// |
|---|
| 35 | /// The use of commands - especially with an undo/redo history - implies several inherent "rules and considerations" |
|---|
| 36 | /// that must be followed and taken into account for a successful implementation. |
|---|
| 37 | /// - It is obvious that when undo/redo is desired, documents can ONLY be modified by a closed series of commands |
|---|
| 38 | /// that can be played forth and back in its respective, proper order only. |
|---|
| 39 | /// - VERY IMPORTANT: The time between the constructor and calling Do() is problematic, and should be null: |
|---|
| 40 | /// there should be NO code between the construction of a command and the related call to Do(). |
|---|
| 41 | /// This is especially problematic when commands are (improperly) queued, e.g. while macro commands are being constructed. |
|---|
| 42 | /// As keeping the ctor empty is usually quasi impossible (we normally want to use it to "configure" the command), this |
|---|
| 43 | /// implies that we must make sure that the command is run and submitted to the undo/redo history IMMEDIATELY after construction. |
|---|
| 44 | /// As a help, both macro commands and the undo/redo history are able to accept commands whose Do() has already been run by the caller. |
|---|
| 45 | class CommandT |
|---|
| 46 | { |
|---|
| 47 | public: |
|---|
| 48 | |
|---|
| 49 | /// The constructor. |
|---|
| 50 | /// @param ShowInHistory If the command should be visible in the history. |
|---|
| 51 | /// @param SuggestsSave If the command should suggest that the document has to be saved |
|---|
| 52 | /// if it is closed and the command has been executed without a |
|---|
| 53 | /// following save. |
|---|
| 54 | CommandT(bool ShowInHistory=true, bool SuggestsSave=true); |
|---|
| 55 | |
|---|
| 56 | /// The virtual destructor. |
|---|
| 57 | virtual ~CommandT() {} |
|---|
| 58 | |
|---|
| 59 | /// This method executes the command. |
|---|
| 60 | /// @returns true if the command succeeded, false if it failed. |
|---|
| 61 | virtual bool Do()=0; |
|---|
| 62 | |
|---|
| 63 | /// This method un-does the command. |
|---|
| 64 | virtual void Undo()=0; |
|---|
| 65 | |
|---|
| 66 | /// Returns the name (a description) of the command. |
|---|
| 67 | virtual wxString GetName() const=0; |
|---|
| 68 | |
|---|
| 69 | /// Whether the command should be shown in the undo/redo history. |
|---|
| 70 | /// Commands not shown are still part of the history but are not undoable/redoable |
|---|
| 71 | /// by the user. Instead they are silently undone/redone when the preceding command |
|---|
| 72 | /// is undone/redone. |
|---|
| 73 | bool ShowInHistory() const { return m_ShowInHistory; } |
|---|
| 74 | |
|---|
| 75 | /// Whether the command suggests to save the document when its closed and hasn't |
|---|
| 76 | /// been saved between the command execution and the closing (selection changes |
|---|
| 77 | /// for example don't suggest to save the document since no important changes |
|---|
| 78 | /// have been made). |
|---|
| 79 | bool SuggestsSave() const { return m_SuggestsSave; } |
|---|
| 80 | |
|---|
| 81 | /// Whether the command has been executed. |
|---|
| 82 | bool IsDone() const { return m_Done; } |
|---|
| 83 | |
|---|
| 84 | /// Returns the commands unique ID. |
|---|
| 85 | unsigned long GetID() const { return m_ID; } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | protected: |
|---|
| 89 | |
|---|
| 90 | bool m_Done; ///< Whether the command has been executed. |
|---|
| 91 | const bool m_ShowInHistory; ///< Whether the command should have an entry in the undo/redo history. |
|---|
| 92 | const bool m_SuggestsSave; ///< Whether the command suggests saving of the document on close. |
|---|
| 93 | const unsigned long m_ID; ///< The commands unique ID. |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | private: |
|---|
| 97 | |
|---|
| 98 | /// Use of the Copy Constructor is not allowed. See "svn log -r 174" and the top of "svn cat -r 174 DocumentCommands.hpp" for a discussion. |
|---|
| 99 | CommandT(const CommandT&); |
|---|
| 100 | |
|---|
| 101 | /// Use of the Assignment Operator is not allowed. See "svn log -r 174" and the top of "svn cat -r 174 DocumentCommands.hpp" for a discussion. |
|---|
| 102 | void operator = (const CommandT&); |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | class CommandMacroT : public CommandT |
|---|
| 107 | { |
|---|
| 108 | public: |
|---|
| 109 | |
|---|
| 110 | CommandMacroT(const ArrayT<CommandT*>& SubCommands, const wxString& Name="Macro Command", bool ShowInHistory=true, bool SuggestsSave=true); |
|---|
| 111 | ~CommandMacroT(); |
|---|
| 112 | |
|---|
| 113 | // Implement the CommandT interface. |
|---|
| 114 | virtual bool Do(); |
|---|
| 115 | virtual void Undo(); |
|---|
| 116 | virtual wxString GetName() const; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | private: |
|---|
| 120 | |
|---|
| 121 | const ArrayT<CommandT*> m_SubCommands; ///< The sub-commands that this macro is composed of. |
|---|
| 122 | const wxString m_Name; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | #endif |
|---|