editor/action/action_base.hpp

Go to the documentation of this file.
00001 /* $Id: action_base.hpp 53522 2012-03-13 18:19:16Z fendrin $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Tomasz Sniatowski <kailoran@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Base class for editor actions
00019  *
00020  * An action is constructed in response to a user command, then executed on
00021  * the map. An undo action is returned by default via pointer, caller-owned).
00022  * It is possible to call an action without creating the undo action.
00023  * Actions report failure via exceptions.
00024  * Code that only deals with actions polymorphically should only need to
00025  * include this header file.
00026  */
00027 
00028 #ifndef EDITOR_ACTION_BASE_HPP_INCLUDED
00029 #define EDITOR_ACTION_BASE_HPP_INCLUDED
00030 
00031 #include "../editor_common.hpp"
00032 
00033 namespace editor {
00034 
00035 /**
00036  * Base class for all editor actions. An editor_action object
00037  * encapsulates the action to be performed on a map, in separation
00038  * from the user interface or display, and provides a way of reversing
00039  * its affects by creating an "undo" action.  All the actions can be
00040  * processed via this base class' virtual functions.
00041  */
00042 class editor_action
00043 {
00044     public:
00045         editor_action();
00046         virtual ~editor_action();
00047 
00048         /**
00049          * Action cloning
00050          */
00051         virtual editor_action* clone() const = 0;
00052 
00053         /**
00054          * Perform the action, returning an undo action that,
00055          * when performed, shall reverse any effects of this
00056          * action.  The undo action object is owned by the
00057          * caller. Default behaviour is to create a whole-map
00058          * undo, call the perform_without_undo function and
00059          * return the undo object.
00060          */
00061         virtual editor_action* perform(map_context&) const;
00062 
00063         /**
00064          * Perform the action without creating an undo action.
00065          */
00066         virtual void perform_without_undo(map_context&) const = 0;
00067 
00068         /**
00069          * @return the actual number of actions contained within
00070          */
00071         virtual int action_count() const;
00072 
00073         /**
00074          * @return a short name of this action type, defaults to unknown
00075          */
00076         virtual const char* get_name() const { return "unknown"; }
00077 
00078         /**
00079          * A textual description of the action. For use
00080          * e.g. in the undo menu, to have a "Undo: Fill with
00081          * Grassland" item rather than just "Undo". Should be
00082          * overridden by derived Actions, defaults to a debug
00083          * message.
00084          */
00085         virtual std::string get_description() const;
00086 
00087         /**
00088          * Debugging aid. Return an unique identifier of this Action.
00089          */
00090         int get_id() const { return id_; }
00091 
00092         /**
00093          * Debugging aid. Return number of existing instances of Actions.
00094          */
00095         static int get_instance_count() { return instance_count_; }
00096 
00097     private:
00098         static int next_id_;
00099         static int instance_count_;
00100         const int id_;
00101 };
00102 
00103 
00104 //TODO: add messages etc
00105 struct editor_action_exception : public editor_exception
00106 {
00107     editor_action_exception(const std::string& msg)
00108     : editor_exception(msg)
00109     {
00110     }
00111 };
00112 
00113 } //end namespace editor
00114 
00115 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Tue May 22 2012 01:03:42 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs