| 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_MAP_DOCUMENT_HPP_INCLUDED |
|---|
| 23 | #define CAFU_MAP_DOCUMENT_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | #include "ObserverPattern.hpp" |
|---|
| 26 | #include "CommandHistory.hpp" |
|---|
| 27 | #include "Plants/PlantDescrMan.hpp" |
|---|
| 28 | #include "Templates/Array.hpp" |
|---|
| 29 | #include "SceneGraph/LightMapMan.hpp" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | class ChildFrameT; |
|---|
| 33 | class EditorMaterialI; |
|---|
| 34 | class EntityClassT; |
|---|
| 35 | class GameConfigT; |
|---|
| 36 | class GroupT; |
|---|
| 37 | class MapEntityBaseT; |
|---|
| 38 | class MapEntityT; |
|---|
| 39 | class MapPrimitiveT; |
|---|
| 40 | class OrthoBspTreeT; |
|---|
| 41 | class wxProgressDialog; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /// This class handles each iteration element when the children of a MapElementT object are iterated. |
|---|
| 45 | /// It quasi serves as a call-back function that is called on the element in each iteration. |
|---|
| 46 | /// User code is supposed to derive from this class in order to implement custom behaviour. |
|---|
| 47 | class IterationHandlerI |
|---|
| 48 | { |
|---|
| 49 | public: |
|---|
| 50 | |
|---|
| 51 | /// The actual method that is called back on each element of the iteration. |
|---|
| 52 | virtual bool Handle(MapElementT* Child)=0; |
|---|
| 53 | |
|---|
| 54 | /// The virtual destructor. |
|---|
| 55 | virtual ~IterationHandlerI() { } |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | struct PtsPointT |
|---|
| 60 | { |
|---|
| 61 | float Time; |
|---|
| 62 | Vector3fT Pos; |
|---|
| 63 | unsigned short Heading; |
|---|
| 64 | wxString Info; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /// This class represents a CaWE "map" document. |
|---|
| 69 | class MapDocumentT : public wxEvtHandler, public SubjectT |
|---|
| 70 | { |
|---|
| 71 | public: |
|---|
| 72 | |
|---|
| 73 | static const unsigned int CMAP_FILE_VERSION; |
|---|
| 74 | |
|---|
| 75 | // A class for throwing exceptions on load errors. |
|---|
| 76 | class LoadErrorT { }; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /// The constructor for creating a new, clean, empty map. |
|---|
| 80 | /// @param GameConfig The game configuration that will be used for the map. |
|---|
| 81 | MapDocumentT(GameConfigT* GameConfig); |
|---|
| 82 | |
|---|
| 83 | /// The regular constructor for loading a cmap file from disk. |
|---|
| 84 | /// @param GameConfig The game configuration that will be used for the map. |
|---|
| 85 | /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map. |
|---|
| 86 | /// @param FileName The name of the file to load the map from. |
|---|
| 87 | MapDocumentT(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName); |
|---|
| 88 | |
|---|
| 89 | /// A named constructor for importing a map in HL1 map file format. |
|---|
| 90 | /// @param GameConfig The game configuration that will be used for the map. |
|---|
| 91 | /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map. |
|---|
| 92 | /// @param FileName The name of the file to load the map from. |
|---|
| 93 | static MapDocumentT* ImportHalfLife1Map(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName); |
|---|
| 94 | |
|---|
| 95 | /// A named constructor for importing a map in HL2 vmf file format. |
|---|
| 96 | /// @param GameConfig The game configuration that will be used for the map. |
|---|
| 97 | /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map. |
|---|
| 98 | /// @param FileName The name of the file to load the map from. |
|---|
| 99 | static MapDocumentT* ImportHalfLife2Vmf(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName); |
|---|
| 100 | |
|---|
| 101 | /// A named constructor for importing a map in Doom3 map file format. |
|---|
| 102 | /// @param GameConfig The game configuration that will be used for the map. |
|---|
| 103 | /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map. |
|---|
| 104 | /// @param FileName The name of the file to load the map from. |
|---|
| 105 | static MapDocumentT* ImportDoom3Map(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName); |
|---|
| 106 | |
|---|
| 107 | /// The destructor. |
|---|
| 108 | ~MapDocumentT(); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | // Inherited methods from the wxDocument base class. |
|---|
| 112 | bool OnSaveDocument(const wxString& FileName, bool IsAutoSave); ///< Saves the document. Non-const, as it updates the m_FileName member. |
|---|
| 113 | bool SaveAs(); ///< Calls OnSaveDocument(). |
|---|
| 114 | bool Save(); ///< Calls OnSaveDocument(). |
|---|
| 115 | |
|---|
| 116 | void SetChildFrame(ChildFrameT* ChildFrame) { m_ChildFrame=ChildFrame; } // This should be in the ctor! |
|---|
| 117 | ChildFrameT* GetChildFrame() const { return m_ChildFrame; } |
|---|
| 118 | const wxString& GetFileName() const { return m_FileName; } |
|---|
| 119 | |
|---|
| 120 | /// Returns all entities in the map. The world is always at index 0, followed by the "regular" entities. |
|---|
| 121 | const ArrayT<MapEntityBaseT*>& GetEntities() const { return m_Entities; } |
|---|
| 122 | |
|---|
| 123 | /// Adds all elements (entities and primitives) in this map to the given array. |
|---|
| 124 | /// Note that the world entity is always the first element that is added to the list. |
|---|
| 125 | void GetAllElems(ArrayT<MapElementT*>& Elems) const; |
|---|
| 126 | |
|---|
| 127 | /// Iterates over all elements (entities and primitives) in this map, calling the IH.Handle() call-back method for each. |
|---|
| 128 | /// Note that for backwards-compatibility, the world entity itself is *skipped*: IH.Handle() is called for all its |
|---|
| 129 | /// primitives, but not the world entity itself. |
|---|
| 130 | /// @param IH The iteration handler whose Handle() method is called once per map element. |
|---|
| 131 | /// User code is expected to pass a derived class instance in order to achieve custom behaviour. |
|---|
| 132 | /// @returns true if the iteration completed fully, or false if it was aborted early. |
|---|
| 133 | bool IterateElems(IterationHandlerI& IH); |
|---|
| 134 | |
|---|
| 135 | /// Inserts the given entity into the map. |
|---|
| 136 | /// Callers should never attempt to insert an element into the world in a way other than calling this method, |
|---|
| 137 | /// as it also inserts the element into the internal BSP tree that is used for rendering and culling. |
|---|
| 138 | void Insert(MapEntityT* Ent); |
|---|
| 139 | |
|---|
| 140 | /// Inserts the given primitive into the map, as a child of the given entity (the world or a custom entity). |
|---|
| 141 | /// Callers should never attempt to insert an element into the world in a way other than calling this method, |
|---|
| 142 | /// as it also inserts the element into the internal BSP tree that is used for rendering and culling. |
|---|
| 143 | void Insert(MapPrimitiveT* Prim, MapEntityBaseT* ParentEnt=NULL); |
|---|
| 144 | |
|---|
| 145 | /// Removes the given element from the map. |
|---|
| 146 | /// The element can be any primitive or custom entity (but never the MapWorldT instance). |
|---|
| 147 | void Remove(MapElementT* Elem); |
|---|
| 148 | |
|---|
| 149 | ArrayT<MapElementT*> GetElementsIn(const BoundingBox3fT& Box, bool InsideOnly, bool CenterOnly) const; |
|---|
| 150 | |
|---|
| 151 | /// Determines all materials that are currently being used in the world (in brushes, Bezier patches and terrains), |
|---|
| 152 | /// and returns the whole list via the UsedMaterials reference parameter. |
|---|
| 153 | void GetUsedMaterials(ArrayT<EditorMaterialI*>& UsedMaterials) const; |
|---|
| 154 | |
|---|
| 155 | OrthoBspTreeT* GetBspTree() const { return m_BspTree; } |
|---|
| 156 | GameConfigT* GetGameConfig() const { return m_GameConfig; } |
|---|
| 157 | const EntityClassT* FindOrCreateUnknownClass(const wxString& Name, bool HasOrigin); |
|---|
| 158 | cf::SceneGraph::LightMapManT& GetLightMapMan() { return m_LightMapMan; } |
|---|
| 159 | PlantDescrManT& GetPlantDescrMan() { return m_PlantDescrMan; } |
|---|
| 160 | |
|---|
| 161 | CommandHistoryT& GetHistory() { return m_History; } |
|---|
| 162 | const ArrayT<PtsPointT>& GetPointFilePoints() const { return m_PointFilePoints; } |
|---|
| 163 | const ArrayT<wxColour>& GetPointFileColors() const { return m_PointFileColors; } |
|---|
| 164 | |
|---|
| 165 | bool IsSnapEnabled() const { return m_SnapToGrid; } ///< Returns whether or not grid snap is enabled. Called by the tools and views to determine snap behavior. |
|---|
| 166 | int GetGridSpacing() const { return m_GridSpacing>0 ? m_GridSpacing : 1; } |
|---|
| 167 | bool Is2DGridEnabled() const { return m_ShowGrid; } |
|---|
| 168 | float SnapToGrid(float f, bool Toggle) const; ///< Returns the given number f snapped to the grid if the grid is active, rounded to the nearest integer otherwise. If Toggle is true, the grid activity is considered toggled. |
|---|
| 169 | Vector3fT SnapToGrid(const Vector3fT& Pos, bool Toggle, int AxisNoSnap) const; ///< Returns the given vector Pos snapped to the grid if the grid is active, rounded to the nearest integer otherwise. If Toggle is true, the grid activity is considered toggled. If AxisNoSnap is -1, all components of Pos are snapped to the grid. If AxisNoSnap is 0, 1 or 2, the corresponding component of Pos is returned unchanged. |
|---|
| 170 | |
|---|
| 171 | /// Methods for managing the set of currently selected map elements. |
|---|
| 172 | //@{ |
|---|
| 173 | void SetSelection(const ArrayT<MapElementT*>& NewSelection); |
|---|
| 174 | const ArrayT<MapElementT*>& GetSelection() const { return m_Selection; } |
|---|
| 175 | const BoundingBox3fT& GetMostRecentSelBB() const; ///< Returns the most recent bounding-box of the selection. That is, it returns the bounding-box of the current selection, or (if nothing is selected) the bounding-box of the previous selection. |
|---|
| 176 | //@} |
|---|
| 177 | |
|---|
| 178 | /// Methods for managing the groups. |
|---|
| 179 | //@{ |
|---|
| 180 | const ArrayT<GroupT*>& GetGroups() const { return m_Groups; } |
|---|
| 181 | ArrayT<GroupT*>& GetGroups() { return m_Groups; } |
|---|
| 182 | ArrayT<GroupT*> GetAbandonedGroups() const; ///< Returns only the groups that are empty (have no members). |
|---|
| 183 | //@} |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | private: |
|---|
| 187 | |
|---|
| 188 | MapDocumentT(const MapDocumentT&); ///< Use of the Copy Constructor is not allowed. |
|---|
| 189 | void operator = (const MapDocumentT&); ///< Use of the Assignment Operator is not allowed. |
|---|
| 190 | |
|---|
| 191 | ChildFrameT* m_ChildFrame; ///< The child frame within which this document lives. |
|---|
| 192 | wxString m_FileName; ///< This documents file name. |
|---|
| 193 | ArrayT<MapEntityBaseT*> m_Entities; ///< All the entities that exist in this map. The world entity is always first at index 0, followed by an arbitrary number of regular entities. |
|---|
| 194 | OrthoBspTreeT* m_BspTree; ///< The BSP tree that spatially organizes the map elements in the m_MapWorld. |
|---|
| 195 | GameConfigT* m_GameConfig; ///< The game configuration that is used with this map. |
|---|
| 196 | ArrayT<const EntityClassT*> m_UnknownEntClasses; ///< The entity classes that are used by entities loaded into this map but who are unknown/undefined in this game config. This list complements GameConfigT::m_EntityClasses. |
|---|
| 197 | cf::SceneGraph::LightMapManT m_LightMapMan; ///< The light map manager that is used with this map. |
|---|
| 198 | PlantDescrManT m_PlantDescrMan; ///< The plant description manager that is used with this map. |
|---|
| 199 | |
|---|
| 200 | CommandHistoryT m_History; ///< The command history. |
|---|
| 201 | |
|---|
| 202 | ArrayT<MapElementT*> m_Selection; ///< The currently selected map elements. |
|---|
| 203 | mutable BoundingBox3fT m_SelectionBB; ///< The bounding-box of the current selection, or if there is no selection, the bounding-box of the previous selection. |
|---|
| 204 | ArrayT<GroupT*> m_Groups; ///< The list of groups in this document. |
|---|
| 205 | ArrayT<PtsPointT> m_PointFilePoints; ///< The points of the currently loaded point file. |
|---|
| 206 | ArrayT<wxColour> m_PointFileColors; ///< The colors for items (columns) of a point in the pointfile. A color can be invalid if the associated column should not be visualized at all. |
|---|
| 207 | |
|---|
| 208 | bool m_SnapToGrid; ///< Snap things to grid. Kept here because the other two are kept here as well. ;-) |
|---|
| 209 | int m_GridSpacing; ///< The spacing of the grid. Could also be kept in the related ChildFrameT, but as the observers depent on it, its properly stored here in the MapDocumentT. |
|---|
| 210 | bool m_ShowGrid; ///< Show or hide the 2D grid. Could also be kept in the related ChildFrameT, but as the observers depent on it, its properly stored here in the MapDocumentT. |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | /*************************************************************/ |
|---|
| 214 | /*** Event handlers for >>document specific<< menu events. ***/ |
|---|
| 215 | /*************************************************************/ |
|---|
| 216 | |
|---|
| 217 | void OnEditUndoRedo (wxCommandEvent& CE); |
|---|
| 218 | void OnEditCut (wxCommandEvent& CE); |
|---|
| 219 | void OnEditCopy (wxCommandEvent& CE); |
|---|
| 220 | void OnEditPaste (wxCommandEvent& CE); |
|---|
| 221 | void OnEditPasteSpecial (wxCommandEvent& CE); |
|---|
| 222 | void OnEditDelete (wxCommandEvent& CE); |
|---|
| 223 | void OnEditSelectNone (wxCommandEvent& CE); |
|---|
| 224 | void OnEditSelectAll (wxCommandEvent& CE); |
|---|
| 225 | |
|---|
| 226 | void OnUpdateEditUndoRedo (wxUpdateUIEvent& UE); // For Undo and Redo. |
|---|
| 227 | void OnUpdateEditCutCopyDelete (wxUpdateUIEvent& UE); // For Cut, Copy and Delete. |
|---|
| 228 | void OnUpdateEditPasteSpecial (wxUpdateUIEvent& UE); // For Paste and PasteSpecial. |
|---|
| 229 | |
|---|
| 230 | void OnSelectionApplyMaterial (wxCommandEvent& CE); |
|---|
| 231 | |
|---|
| 232 | void OnUpdateSelectionApplyMaterial(wxUpdateUIEvent& UE); |
|---|
| 233 | |
|---|
| 234 | void OnMapSnapToGrid (wxCommandEvent& CE); |
|---|
| 235 | void OnMapToggleGrid2D (wxCommandEvent& CE); |
|---|
| 236 | void OnMapFinerGrid (wxCommandEvent& CE); |
|---|
| 237 | void OnMapCoarserGrid (wxCommandEvent& CE); |
|---|
| 238 | void OnMapGotoPrimitive (wxCommandEvent& CE); |
|---|
| 239 | void OnMapShowInfo (wxCommandEvent& CE); |
|---|
| 240 | void OnMapCheckForProblems (wxCommandEvent& CE); |
|---|
| 241 | void OnMapProperties (wxCommandEvent& CE); |
|---|
| 242 | void OnMapLoadPointFile (wxCommandEvent& CE); |
|---|
| 243 | void OnMapUnloadPointFile (wxCommandEvent& CE); |
|---|
| 244 | |
|---|
| 245 | void OnViewShowEntityInfo (wxCommandEvent& CE); |
|---|
| 246 | void OnViewShowEntityTargets (wxCommandEvent& CE); |
|---|
| 247 | void OnViewHideSelectedObjects (wxCommandEvent& CE); |
|---|
| 248 | void OnViewHideUnselectedObjects (wxCommandEvent& CE); |
|---|
| 249 | void OnViewShowHiddenObjects (wxCommandEvent& CE); |
|---|
| 250 | |
|---|
| 251 | void OnUpdateViewShowEntityInfo (wxUpdateUIEvent& UE); |
|---|
| 252 | void OnUpdateViewShowEntityTargets (wxUpdateUIEvent& UE); |
|---|
| 253 | |
|---|
| 254 | void OnToolsCarve (wxCommandEvent& CE); |
|---|
| 255 | void OnToolsHollow (wxCommandEvent& CE); |
|---|
| 256 | void OnToolsAssignPrimToEntity (wxCommandEvent& CE); |
|---|
| 257 | void OnToolsAssignPrimToWorld (wxCommandEvent& CE); |
|---|
| 258 | void OnToolsReplaceMaterials (wxCommandEvent& CE); |
|---|
| 259 | void OnToolsMaterialLock (wxCommandEvent& CE); |
|---|
| 260 | void OnToolsSnapSelectionToGrid (wxCommandEvent& CE); |
|---|
| 261 | void OnToolsTransform (wxCommandEvent& CE); |
|---|
| 262 | void OnToolsAlign (wxCommandEvent& CE); |
|---|
| 263 | void OnToolsMirror (wxCommandEvent& CE); |
|---|
| 264 | |
|---|
| 265 | void OnUpdateToolsMaterialLock (wxUpdateUIEvent& UE); |
|---|
| 266 | |
|---|
| 267 | DECLARE_EVENT_TABLE() |
|---|
| 268 | }; |
|---|
| 269 | |
|---|
| 270 | #endif |
|---|