Cafu Engine
MapDocument.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MAP_DOCUMENT_HPP_INCLUDED
8 #define CAFU_MAP_DOCUMENT_HPP_INCLUDED
9 
10 #include "ObserverPattern.hpp"
11 #include "../DocumentAdapter.hpp"
12 
13 #include "Plants/PlantDescrMan.hpp"
14 #include "Templates/Array.hpp"
15 #include "Templates/Pointer.hpp"
16 #include "SceneGraph/LightMapMan.hpp"
17 #include "UniScriptState.hpp"
18 
19 #include "wx/event.h"
20 
21 
22 class ChildFrameT;
23 class CommandT;
24 class EditorMaterialI;
25 class GameConfigT;
26 class GroupT;
27 class MapPrimitiveT;
28 class OrthoBspTreeT;
29 class wxProgressDialog;
30 namespace MapEditor { class CompMapEntityT; }
31 namespace cf { namespace GameSys { class EntityT; } }
32 namespace cf { namespace GameSys { class WorldT; } }
33 
34 
35 struct PtsPointT
36 {
37  float Time;
38  Vector3fT Pos;
39  unsigned short Heading;
40  wxString Info;
41 };
42 
43 
44 /// This class represents a CaWE "map" document.
45 class MapDocumentT : public wxEvtHandler, public SubjectT
46 {
47  public:
48 
49  static const unsigned int CMAP_FILE_VERSION;
50 
51 
52  /// The regular constructor for loading a cmap file from disk.
53  /// @param GameConfig The game configuration that will be used for the map.
54  /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map.
55  /// @param FileName The name of the file to load the map from.
56  MapDocumentT(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName);
57 
58  /// A named constructor for creating a new, empty map.
59  /// @param GameConfig The game configuration that will be used for the map.
60  static MapDocumentT* CreateNew(GameConfigT* GameConfig);
61 
62  /// A named constructor for importing a map in HL1 map file format.
63  /// @param GameConfig The game configuration that will be used for the map.
64  /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map.
65  /// @param FileName The name of the file to load the map from.
66  static MapDocumentT* ImportHalfLife1Map(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName);
67 
68  /// A named constructor for importing a map in HL2 vmf file format.
69  /// @param GameConfig The game configuration that will be used for the map.
70  /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map.
71  /// @param FileName The name of the file to load the map from.
72  static MapDocumentT* ImportHalfLife2Vmf(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName);
73 
74  /// A named constructor for importing a map in Doom3 map file format.
75  /// @param GameConfig The game configuration that will be used for the map.
76  /// @param ProgressDialog If non-NULL, this dialog is used to show the progress while loading the map.
77  /// @param FileName The name of the file to load the map from.
78  static MapDocumentT* ImportDoom3Map(GameConfigT* GameConfig, wxProgressDialog* ProgressDialog, const wxString& FileName);
79 
80  /// The destructor.
81  ~MapDocumentT();
82 
83 
84  // Inherited methods from the wxDocument base class.
85  static void SaveEntities(std::ostream& OutFile, IntrusivePtrT<cf::GameSys::EntityT> RootEntity);
86  bool OnSaveDocument(const wxString& FileName, bool IsAutoSave, IntrusivePtrT<cf::GameSys::EntityT> RootEntity = NULL); ///< Saves the document. Non-const, as it updates the m_FileName member.
87  bool SaveAs(); ///< Calls OnSaveDocument().
88  bool Save(); ///< Calls OnSaveDocument().
89 
90  void SetChildFrame(ChildFrameT* ChildFrame) { m_ChildFrame=ChildFrame; } // This should be in the ctor!
91  ChildFrameT* GetChildFrame() const { return m_ChildFrame; }
92  const wxString& GetFileName() const { return m_FileName; }
93  MapDocAdapterT& GetAdapter() { return m_DocAdapter; }
94  cf::GameSys::WorldT& GetScriptWorld() { return *m_ScriptWorld; }
95 
96  /// For compatibility only. This method should be removed.
97  bool CompatSubmitCommand(CommandT* Command);
98 
99  /// Returns the root "map" entity of the map.
101 
102  /// Adds all elements in this map (entity representations and primitives) to the given array.
103  /// The `MapEntRepresT` instance of the world entity is always the first element that is added to the list.
104  void GetAllElems(ArrayT<MapElementT*>& Elems) const;
105 
106  /// Inserts the given entity into the map.
107  /// Callers should never attempt to insert an element into the world in a way other than calling this method,
108  /// as it also inserts the element into the internal BSP tree that is used for rendering and culling.
109  void Insert(IntrusivePtrT<cf::GameSys::EntityT> Entity, IntrusivePtrT<cf::GameSys::EntityT> Parent, unsigned long Pos=0xFFFFFFFF);
110 
111  /// Inserts the given primitive into the map, as a child of the given entity (the world or a custom entity).
112  /// Callers should never attempt to insert an element into the world in a way other than calling this method,
113  /// as it also inserts the element into the internal BSP tree that is used for rendering and culling.
115 
116  /// Removes the given entity from the map.
117  /// The entity cannot be the root entity (the world).
119 
120  /// Removes the given primitive from the map.
121  void Remove(MapPrimitiveT* Prim);
122 
123  ArrayT<MapElementT*> GetElementsIn(const BoundingBox3fT& Box, bool InsideOnly, bool CenterOnly) const;
124 
125  /// Determines all materials that are currently being used in the world (in brushes, Bezier patches and terrains),
126  /// and returns the whole list via the UsedMaterials reference parameter.
127  void GetUsedMaterials(ArrayT<EditorMaterialI*>& UsedMaterials) const;
128 
129  OrthoBspTreeT* GetBspTree() const { return m_BspTree; }
130  GameConfigT* GetGameConfig() const { return m_GameConfig; }
131  cf::SceneGraph::LightMapManT& GetLightMapMan() { return m_LightMapMan; }
132  PlantDescrManT& GetPlantDescrMan() { return m_PlantDescrMan; }
133 
134  const ArrayT<PtsPointT>& GetPointFilePoints() const { return m_PointFilePoints; }
135  const ArrayT<wxColour>& GetPointFileColors() const { return m_PointFileColors; }
136 
137  bool IsSnapEnabled() const { return m_SnapToGrid; } ///< Returns whether or not grid snap is enabled. Called by the tools and views to determine snap behavior.
138  int GetGridSpacing() const { return m_GridSpacing>0 ? m_GridSpacing : 1; }
139  bool Is2DGridEnabled() const { return m_ShowGrid; }
140  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.
141  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.
142 
143  bool GetAutoGroupEntities() const { return m_AutoGroupEntities; }
144 
145  /// Methods for managing the set of currently selected map elements.
146  //@{
147  void SetSelection(const ArrayT<MapElementT*>& NewSelection);
148  const ArrayT<MapElementT*>& GetSelection() const { return m_Selection; }
149  ArrayT< IntrusivePtrT<cf::GameSys::EntityT> > GetSelectedEntities() const;
150  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.
151  IntrusivePtrT<cf::GameSys::EntityT> GetPasteParent() const; ///< Returns a suitable parent entity into which new objects from the clipboard should be pasted.
152  void SetPasteParent(unsigned int ID);
153 
154  /// Reduces the given set of map elements by removing each element whose parent is in the set as well.
155  /// This is an important helper method for operations such as delete, copy-to-clipboard, save-as-prefab, etc.
156  static void Reduce(ArrayT<MapElementT*>& Elems);
157  //@}
158 
159  /// Methods for managing the groups.
160  //@{
161  const ArrayT<GroupT*>& GetGroups() const { return m_Groups; }
162  ArrayT<GroupT*>& GetGroups() { return m_Groups; }
163  ArrayT<GroupT*> GetAbandonedGroups() const; ///< Returns only the groups that are empty (have no members).
164  //@}
165 
166 
167  private:
168 
169  MapDocumentT(GameConfigT* GameConfig); ///< An auxiliary constructor that the public named constructors use.
170  MapDocumentT(const MapDocumentT&); ///< Use of the Copy Constructor is not allowed.
171  void operator = (const MapDocumentT&); ///< Use of the Assignment Operator is not allowed.
172 
173  void Init();
174  void PostLoadEntityAlign(unsigned int cmapFileVersion, const ArrayT< IntrusivePtrT<MapEditor::CompMapEntityT> >& AllMapEnts);
175 
176  ChildFrameT* m_ChildFrame; ///< The child frame within which this document lives.
177  wxString m_FileName; ///< This documents file name.
178  MapDocAdapterT m_DocAdapter; ///< Kept here because it sometimes needs the same lifetime as the MapDocumentT itself, e.g. when referenced by a "material" property of the Entity Inspector, or by commands in the command history.
179  cf::UniScriptStateT m_ScriptState; ///< The script state that the script world (`m_ScriptWorld`) lives in.
180  IntrusivePtrT<cf::GameSys::WorldT> m_ScriptWorld; ///< The "script world" contains the entity hierarchy and their components.
181  OrthoBspTreeT* m_BspTree; ///< The BSP tree that spatially organizes the map elements in the m_MapWorld.
182  GameConfigT* m_GameConfig; ///< The game configuration that is used with this map.
183  cf::SceneGraph::LightMapManT m_LightMapMan; ///< The light map manager that is used with this map.
184  PlantDescrManT m_PlantDescrMan; ///< The plant description manager that is used with this map.
185 
186  ArrayT<MapElementT*> m_Selection; ///< The currently selected map elements.
187  mutable BoundingBox3fT m_SelectionBB; ///< The bounding-box of the current selection, or if there is no selection, the bounding-box of the previous selection.
188  unsigned int m_PasteParentID; ///< The ID of a suitable parent entity into which new objects from the clipboard should be pasted.
189  ArrayT<GroupT*> m_Groups; ///< The list of groups in this document.
190  ArrayT<PtsPointT> m_PointFilePoints; ///< The points of the currently loaded point file.
191  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.
192 
193  // General settings that are not specific to a particular tool.
194  // It would make sense to serialize these settings along with the map's geometry.
195  bool m_SnapToGrid; ///< Whether transforms should snap the map elements to the grid.
196  int m_GridSpacing; ///< The spacing between the grid lines.
197  bool m_ShowGrid; ///< Whether the grid is shown (in the 2D views).
198 
199  bool m_AutoGroupEntities; ///< Whether the selection of entities should select their primitives and children as well.
200 
201 
202  /*************************************************************/
203  /*** Event handlers for >>document specific<< menu events. ***/
204  /*************************************************************/
205 
206  void OnMapSnapToGrid (wxCommandEvent& CE);
207  void OnMapToggleGrid2D (wxCommandEvent& CE);
208  void OnMapFinerGrid (wxCommandEvent& CE);
209  void OnMapCoarserGrid (wxCommandEvent& CE);
210  void OnMapAutoGroupEntities (wxCommandEvent& CE);
211  void OnMapGotoPrimitive (wxCommandEvent& CE);
212  void OnMapShowInfo (wxCommandEvent& CE);
213  void OnMapCheckForProblems (wxCommandEvent& CE);
214  void OnMapLoadPointFile (wxCommandEvent& CE);
215  void OnMapUnloadPointFile (wxCommandEvent& CE);
216 
217  void OnUpdateMapAutoGroupEntities (wxUpdateUIEvent& UE);
218 
219  void OnViewShowEntityInfo (wxCommandEvent& CE);
220  void OnViewShowEntityTargets (wxCommandEvent& CE);
221  void OnSelectionGroup (wxCommandEvent& CE);
222  void OnSelectionHideOther (wxCommandEvent& CE);
223 
224  void OnUpdateViewShowEntityInfo (wxUpdateUIEvent& UE);
225  void OnUpdateViewShowEntityTargets (wxUpdateUIEvent& UE);
226 
227  void OnToolsCarve (wxCommandEvent& CE);
228  void OnToolsHollow (wxCommandEvent& CE);
229  void OnSelectionAssignToEntity (wxCommandEvent& CE);
230  void OnToolsApplyMaterial (wxCommandEvent& CE);
231  void OnToolsReplaceMaterials (wxCommandEvent& CE);
232  void OnToolsMaterialLock (wxCommandEvent& CE);
233  void OnToolsTransform (wxCommandEvent& CE);
234  void OnToolsAlign (wxCommandEvent& CE);
235  void OnToolsMirror (wxCommandEvent& CE);
236 
237  void OnUpdateToolsApplyMaterial (wxUpdateUIEvent& UE);
238  void OnUpdateToolsMaterialLock (wxUpdateUIEvent& UE);
239 
240  DECLARE_EVENT_TABLE()
241 };
242 
243 #endif
This class manages lightmaps, e.g. by "allocating" rectangular areas in larger bitmaps.
Definition: LightMapMan.hpp:25
static MapDocumentT * ImportHalfLife1Map(GameConfigT *GameConfig, wxProgressDialog *ProgressDialog, const wxString &FileName)
A named constructor for importing a map in HL1 map file format.
Definition: MapDocument.cpp:334
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
void GetAllElems(ArrayT< MapElementT * > &Elems) const
Adds all elements in this map (entity representations and primitives) to the given array...
Definition: MapDocument.cpp:1444
bool Save()
Calls OnSaveDocument().
Definition: MapDocument.cpp:1421
void SetSelection(const ArrayT< MapElementT * > &NewSelection)
Methods for managing the set of currently selected map elements.
Definition: MapDocument.cpp:1568
IntrusivePtrT< MapEditor::CompMapEntityT > GetRootMapEntity() const
Returns the root "map" entity of the map.
Definition: MapDocument.cpp:1438
void Remove(IntrusivePtrT< cf::GameSys::EntityT > Entity)
Removes the given entity from the map.
Definition: MapDocument.cpp:1496
The plant description manager holds and manages all plant descriptions so they can be shared with mul...
Definition: PlantDescrMan.hpp:19
static MapDocumentT * CreateNew(GameConfigT *GameConfig)
A named constructor for creating a new, empty map.
Definition: MapDocument.cpp:315
Definition: DocumentAdapter.hpp:37
bool SaveAs()
Calls OnSaveDocument().
Definition: MapDocument.cpp:1384
void Insert(IntrusivePtrT< cf::GameSys::EntityT > Entity, IntrusivePtrT< cf::GameSys::EntityT > Parent, unsigned long Pos=0xFFFFFFFF)
Inserts the given entity into the map.
Definition: MapDocument.cpp:1463
Definition: MapDocument.hpp:35
This class adds no functionality of its own, but only exists for proper type separation.
Definition: MapPrimitive.hpp:21
This class represents a child frame.
Definition: ChildFrame.hpp:55
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 ...
Definition: MapDocument.cpp:2434
This class represents groups.
Definition: Group.hpp:20
Definition: World.hpp:85
const BoundingBox3fT & GetMostRecentSelBB() const
Returns the most recent bounding-box of the selection. That is, it returns the bounding-box of the cu...
Definition: MapDocument.cpp:2454
void GetUsedMaterials(ArrayT< EditorMaterialI * > &UsedMaterials) const
Determines all materials that are currently being used in the world (in brushes, Bezier patches and t...
Definition: MapDocument.cpp:1619
IntrusivePtrT< cf::GameSys::EntityT > GetPasteParent() const
Returns a suitable parent entity into which new objects from the clipboard should be pasted...
Definition: MapDocument.cpp:2475
const ArrayT< GroupT * > & GetGroups() const
Methods for managing the groups.
Definition: MapDocument.hpp:161
Definition: EditorMaterial.hpp:21
static MapDocumentT * ImportHalfLife2Vmf(GameConfigT *GameConfig, wxProgressDialog *ProgressDialog, const wxString &FileName)
A named constructor for importing a map in HL2 vmf file format.
Definition: MapDocument.cpp:384
The class describes the settings for a game/MOD.
Definition: GameConfig.hpp:32
Definition: ObserverPattern.hpp:158
static void Reduce(ArrayT< MapElementT * > &Elems)
Reduces the given set of map elements by removing each element whose parent is in the set as well...
Definition: MapDocument.cpp:2490
~MapDocumentT()
The destructor.
Definition: MapDocument.cpp:1085
This class holds the hierarchy of game entities that populate a game world.
Definition: World.hpp:39
MapDocumentT(GameConfigT *GameConfig, wxProgressDialog *ProgressDialog, const wxString &FileName)
The regular constructor for loading a cmap file from disk.
Definition: MapDocument.cpp:188
This class represents a general command for implementing modifications to the applications document...
Definition: CommandPattern.hpp:30
bool OnSaveDocument(const wxString &FileName, bool IsAutoSave, IntrusivePtrT< cf::GameSys::EntityT > RootEntity=NULL)
Saves the document. Non-const, as it updates the m_FileName member.
Definition: MapDocument.cpp:1268
bool IsSnapEnabled() const
Returns whether or not grid snap is enabled. Called by the tools and views to determine snap behavior...
Definition: MapDocument.hpp:137
This class represents an orthogonal BSP tree (axis-aligned split planes) that spatially organizes Map...
Definition: OrthoBspTree.hpp:41
ArrayT< GroupT * > GetAbandonedGroups() const
Returns only the groups that are empty (have no members).
Definition: MapDocument.cpp:2526
This class represents the state of a script: the underlying Lua state, pending coroutines, metatables for C++ class hierarchies, etc.
Definition: UniScriptState.hpp:214
This file provides the classes for the Observer pattern as described in the book by the GoF...
static MapDocumentT * ImportDoom3Map(GameConfigT *GameConfig, wxProgressDialog *ProgressDialog, const wxString &FileName)
A named constructor for importing a map in Doom3 map file format.
Definition: MapDocument.cpp:445
bool CompatSubmitCommand(CommandT *Command)
For compatibility only. This method should be removed.
Definition: MapDocument.cpp:1432