| 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_OBSERVER_PATTERN_HPP_INCLUDED |
|---|
| 23 | #define CAFU_OBSERVER_PATTERN_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | /// \file |
|---|
| 26 | /// This file provides the classes for the Observer pattern as described in the book by the GoF. |
|---|
| 27 | /// Note that implementations of ObserverT normally maintain a pointer to the subject(s) that they observe, |
|---|
| 28 | /// e.g. in order to be able to redraw themselves also outside of and independent from the NotifySubjectChanged() method. |
|---|
| 29 | /// This however in turn creates problems when the life of the observer begins before or ends after the life of the observed subject(s). |
|---|
| 30 | /// In fact, it seems that the observers have to maintain lists of valid, observed subjects as the subjects maintain lists of observers. |
|---|
| 31 | /// Fortunately, these possibly tough problems can (and apparently must) be addressed by the concrete implementation classes of |
|---|
| 32 | /// observers and subjects, not by the base classes that the Observer pattern describes and provided herein. |
|---|
| 33 | |
|---|
| 34 | #include "Templates/Array.hpp" |
|---|
| 35 | #include "Math3D/BoundingBox.hpp" |
|---|
| 36 | |
|---|
| 37 | #include "wx/string.h" |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | class SubjectT; |
|---|
| 41 | class MapElementT; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | //##################################### |
|---|
| 45 | //# New observer notifications hints. # |
|---|
| 46 | //##################################### |
|---|
| 47 | enum MapElemModDetailE |
|---|
| 48 | { |
|---|
| 49 | MEMD_GENERIC, ///< Generic change of map elements (useful if the subject doesn't know what exactly has been changed). |
|---|
| 50 | // MEMD_ENTITY_PROPERTY, |
|---|
| 51 | MEMD_ENTITY_PROPERTY_CREATED, ///< A new property has been created for an entity. |
|---|
| 52 | MEMD_ENTITY_PROPERTY_DELETED, ///< A property has been deleted from an entity. |
|---|
| 53 | MEMD_ENTITY_PROPERTY_MODIFIED, ///< An entities property has been modified. |
|---|
| 54 | MEMD_ENTITY_CLASS_CHANGED, ///< An entities class has changed. |
|---|
| 55 | MEMD_TRANSFORM, ///< A map element has been transformed. |
|---|
| 56 | MEMD_PRIMITIVE_PROPS_CHANGED, ///< The properties of a map primitve have been modified. |
|---|
| 57 | MEMD_SURFACE_INFO_CHANGED, ///< The surface info of a map element has changed. Note that surface info changes also include the selection of faces. |
|---|
| 58 | MEMD_MORPH, ///< The vertices of a map primitive have been transformed. This is actually UNUSED now that \c CommandMorphT has been removed (r447, 2011-12-20). |
|---|
| 59 | MEMD_ASSIGN_PRIM_TO_ENTITY, ///< A map primitive has been assigned to another entity (the world or any custom entity). |
|---|
| 60 | MEMD_VISIBILITY ///< The visibility of a map element has changed. |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | enum MapDocOtherDetailT |
|---|
| 64 | { |
|---|
| 65 | UPDATE_GRID, |
|---|
| 66 | UPDATE_POINTFILE, |
|---|
| 67 | UPDATE_GLOBALOPTIONS |
|---|
| 68 | }; |
|---|
| 69 | //############################################ |
|---|
| 70 | //# End of new observer notifications hints. # |
|---|
| 71 | //############################################ |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | class ObserverT |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | |
|---|
| 78 | //################################################################################################################################### |
|---|
| 79 | //# New observer notifications methods. These methods differ from the basic observer pattern from the GoF and are CaWE specific! # |
|---|
| 80 | //# Note that the new methods are NOT pure virtual since a concrete observer might not be interested in some of these notifications # |
|---|
| 81 | //################################################################################################################################### |
|---|
| 82 | |
|---|
| 83 | /// Notifies the observer that some other detail than those specifically addressed below has changed. |
|---|
| 84 | virtual void NotifySubjectChanged(SubjectT* Subject, MapDocOtherDetailT OtherDetail) { } |
|---|
| 85 | |
|---|
| 86 | /// Notifies the observer that the selection in the current subject has been changed. |
|---|
| 87 | /// @param Subject The map document in which the selection has been changed. |
|---|
| 88 | /// @param OldSelection Array of the previously selected objects. |
|---|
| 89 | /// @param NewSelection Array of the new selected objects. |
|---|
| 90 | virtual void NotifySubjectChanged_Selection(SubjectT* Subject, const ArrayT<MapElementT*>& OldSelection, const ArrayT<MapElementT*>& NewSelection) { } |
|---|
| 91 | |
|---|
| 92 | /// Notifies the observer that the groups in the current subject have been changed (new group added, group deleted, visibility changed, anything). |
|---|
| 93 | /// @param Subject The map document in which the group inventory has been changed. |
|---|
| 94 | virtual void NotifySubjectChanged_Groups(SubjectT* Subject) { } |
|---|
| 95 | |
|---|
| 96 | /// Notifies the observer that one or more map elements have been created. |
|---|
| 97 | /// @param Subject The map document in which the elements have been created. |
|---|
| 98 | /// @param MapElements List of created map elements. |
|---|
| 99 | virtual void NotifySubjectChanged_Created(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements) { } |
|---|
| 100 | |
|---|
| 101 | /// Notifies the observer that one or more map elements have been deleted. |
|---|
| 102 | /// @param Subject The map document in which the elements have been deleted. |
|---|
| 103 | /// @param MapElements List of deleted map elements. |
|---|
| 104 | virtual void NotifySubjectChanged_Deleted(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements) { } |
|---|
| 105 | |
|---|
| 106 | /// @name Modification notification method and overloads. |
|---|
| 107 | /// These methods notify the observer that one or more map elements have been modified. |
|---|
| 108 | /// The first 3 parameters are common to all of these methods since they are the basic information needed to communicate |
|---|
| 109 | /// an object modification. |
|---|
| 110 | //@{ |
|---|
| 111 | |
|---|
| 112 | /// @param Subject The map document in which the elements have been modified. |
|---|
| 113 | /// @param MapElements List of modified map elements. |
|---|
| 114 | /// @param Detail Information about what has been modified: |
|---|
| 115 | /// Can be one of MEMD_PRIMITIVE_PROPS_CHANGED, MEMD_GENERIC, MEMD_ASSIGN_PRIM_TO_ENTITY and MEMD_MATERIAL_CHANGED. |
|---|
| 116 | virtual void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail) { } |
|---|
| 117 | |
|---|
| 118 | /// @param Subject The map document in which the elements have been modified. |
|---|
| 119 | /// @param MapElements List of modified map elements. |
|---|
| 120 | /// @param Detail Information about what has been modified: |
|---|
| 121 | /// Can be one of MEMD_TRANSFORM, MEMD_PRIMITIVE_PROPS_CHANGED and MEMD_MORPH. |
|---|
| 122 | /// In the case of MEMD_PRIMITIVE_PROPS_CHANGED one can assume that |
|---|
| 123 | /// only map primitives (no entities) are in the MapElements array. |
|---|
| 124 | /// @param OldBounds Holds the bounds of each objects BEFORE the modification (has the same size as MapElements). |
|---|
| 125 | virtual void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const ArrayT<BoundingBox3fT>& OldBounds) { } |
|---|
| 126 | |
|---|
| 127 | /// Since this overload is used exclusively for entities, one can assume that all map elements are entities. |
|---|
| 128 | /// @param Subject The map document in which the elements have been modified. |
|---|
| 129 | /// @param MapElements List of modified map elements. |
|---|
| 130 | /// @param Detail Information about what has been modified: |
|---|
| 131 | /// Can be one of MEMD_ENTITY_PROPERTY_CREATED, MEMD_ENTITY_PROPERTY_DELETED, MEMD_ENTITY_PROPERTY_MODIFIED or MEMD_ENTITY_CLASS_CHANGED. |
|---|
| 132 | /// @param Key Holds the keyname of the changed property or an empty string if Detail is MEMD_ENTITY_CLASS_CHANGED. |
|---|
| 133 | /// @todo move MEMD_ENTITY_CLASS_CHANGED into general modification method. |
|---|
| 134 | virtual void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const wxString& Key) { } |
|---|
| 135 | //@} |
|---|
| 136 | |
|---|
| 137 | //############################################ |
|---|
| 138 | //# End of new observer notification methods # |
|---|
| 139 | //############################################ |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | /// This method is called whenever a subject is about the be destroyed (and become unavailable). |
|---|
| 143 | /// \param dyingSubject The subject that is being destroyed. |
|---|
| 144 | virtual void NotifySubjectDies(SubjectT* dyingSubject)=0; |
|---|
| 145 | |
|---|
| 146 | /// The virtual destructor. |
|---|
| 147 | virtual ~ObserverT(); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | protected: |
|---|
| 151 | |
|---|
| 152 | /// The constructor. It is protected so that only derived classes can create instances of this class. |
|---|
| 153 | ObserverT(); |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | class SubjectT |
|---|
| 158 | { |
|---|
| 159 | public: |
|---|
| 160 | |
|---|
| 161 | /// Registers the observer Obs for notification on updates of this class. |
|---|
| 162 | /// \param Obs The observer that is to be registered. |
|---|
| 163 | virtual void RegisterObserver(ObserverT* Obs); |
|---|
| 164 | |
|---|
| 165 | /// Unregisters the observer Obs from further notification on updates of this class. |
|---|
| 166 | /// \param Obs The observer that is to be unregistered. |
|---|
| 167 | virtual void UnregisterObserver(ObserverT* Obs); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | //################################################################################################################################### |
|---|
| 171 | //# New observer notifications methods. These methods differ from the basic observer pattern from the GoF and are CaWE specific! # |
|---|
| 172 | //# For detailed comments on the parameters, see the equivalent methods in ObserverT # |
|---|
| 173 | //################################################################################################################################### |
|---|
| 174 | virtual void UpdateAllObservers(MapDocOtherDetailT OtherDetail); |
|---|
| 175 | virtual void UpdateAllObservers_SelectionChanged(const ArrayT<MapElementT*>& OldSelection, const ArrayT<MapElementT*>& NewSelection); |
|---|
| 176 | virtual void UpdateAllObservers_GroupsChanged(); |
|---|
| 177 | virtual void UpdateAllObservers_Created(const ArrayT<MapElementT*>& MapElements); |
|---|
| 178 | virtual void UpdateAllObservers_Deleted(const ArrayT<MapElementT*>& MapElements); |
|---|
| 179 | virtual void UpdateAllObservers_Modified(const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail); |
|---|
| 180 | virtual void UpdateAllObservers_Modified(const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const ArrayT<BoundingBox3fT>& OldBounds); |
|---|
| 181 | virtual void UpdateAllObservers_Modified(const ArrayT<MapElementT*>& MapElements, MapElemModDetailE Detail, const wxString& Key); |
|---|
| 182 | //############################################ |
|---|
| 183 | //# End of new observer notification methods # |
|---|
| 184 | //############################################ |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | /// The virtual destructor. |
|---|
| 188 | virtual ~SubjectT(); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | protected: |
|---|
| 192 | |
|---|
| 193 | /// The constructor. It is protected so that only derived classes can create instances of this class. |
|---|
| 194 | SubjectT(); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | private: |
|---|
| 198 | |
|---|
| 199 | /// The list of the observers that have registered for notification on updates of this class. |
|---|
| 200 | ArrayT<ObserverT*> m_Observers; |
|---|
| 201 | }; |
|---|
| 202 | |
|---|
| 203 | #endif |
|---|