- Radiant is now Vista compatible (Aero must be disabled)
[xonotic/netradiant.git] / contrib / bobtoolz / DMap.cpp
1 /*
2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 // DMap.cpp: implementation of the DMap class.
21 //
22 //////////////////////////////////////////////////////////////////////
23
24 #include "DMap.h"
25
26 #include "str.h"
27 #include <list>
28
29 #include "DPoint.h"
30 #include "DPlane.h"
31 #include "DBrush.h"
32 #include "DEPair.h"
33 #include "DPatch.h"
34 #include "DEntity.h"
35
36 #include "iundo.h"
37
38 #include "generic/referencecounted.h"
39
40 #include <vector>
41 #include <list>
42 #include <map>
43 #include <algorithm>
44
45 #include "scenelib.h"
46
47
48 //////////////////////////////////////////////////////////////////////
49 // Construction/Destruction
50 //////////////////////////////////////////////////////////////////////
51
52 DMap::DMap()
53 {
54         m_nNextEntity = 1;
55         AddEntity("worldspawn", 0);
56 }
57
58 DMap::~DMap()
59 {
60         ClearEntities();
61 }
62
63 DEntity* DMap::AddEntity(char *classname, int ID)
64 {
65         DEntity* newEntity;
66         if(ID == -1)
67                 newEntity = new DEntity(classname, m_nNextEntity++);
68         else
69                 newEntity = new DEntity(classname, ID);
70
71         entityList.push_back(newEntity);
72         
73         return newEntity;
74 }
75
76 void DMap::ClearEntities()
77 {
78         m_nNextEntity = 1;
79
80         for(std::list<DEntity *>::const_iterator deadEntity=entityList.begin(); deadEntity!=entityList.end(); deadEntity++)
81                 delete *deadEntity;
82
83         entityList.clear();
84 }
85
86 DEntity* DMap::GetEntityForID(int ID)
87 {
88         DEntity* findEntity = NULL;
89
90         for(std::list<DEntity *>::const_iterator chkEntity=entityList.begin(); chkEntity!=entityList.end(); chkEntity++)
91         {
92                 if((*chkEntity)->m_nID == ID)
93                 {
94                         findEntity = (*chkEntity);
95                         break;
96                 }
97         }
98
99         if(!findEntity)
100                 findEntity = AddEntity("worldspawn", ID);
101
102         return findEntity;
103 }
104
105
106 DEntity* DMap::GetWorldSpawn()
107 {
108         return GetEntityForID(0);
109 }
110
111 void DMap::BuildInRadiant(bool bAllowDestruction)
112 {
113         for(std::list<DEntity *>::const_iterator buildEntity=entityList.begin(); buildEntity!=entityList.end(); buildEntity++)
114                 (*buildEntity)->BuildInRadiant(bAllowDestruction);
115 }
116
117 void DMap::LoadAll(bool bLoadPatches)
118 {
119         ClearEntities();
120
121         GlobalSelectionSystem().setSelectedAll(false);
122
123   class load_entities_t : public scene::Traversable::Walker
124   {
125     DMap* m_map;
126     bool m_bLoadPatches;
127   public:
128     load_entities_t(DMap* map, bool bLoadPatches)
129       : m_map(map), m_bLoadPatches(bLoadPatches)
130     {
131     }
132     bool pre(scene::Node& node) const
133     {
134       if(Node_isEntity(node))
135       {
136         DEntity* loadEntity = m_map->AddEntity("", 0);
137                                 loadEntity->LoadFromEntity(node, m_bLoadPatches);
138       }
139       return false;
140     }
141   } load_entities(this, bLoadPatches);
142
143   Node_getTraversable(GlobalSceneGraph().root())->traverse(load_entities);
144 }
145
146 int DMap::FixBrushes()
147 {
148         int count = 0;
149         for(std::list<DEntity *>::const_iterator fixEntity=entityList.begin(); fixEntity!=entityList.end(); fixEntity++)
150         {
151                 count += (*fixEntity)->FixBrushes();
152         }
153
154         return count;
155 }
156
157 void DMap::ResetTextures( const char* textureName, float fScale[2],      float fShift[2],      int rotation, const char* newTextureName, 
158                           int bResetTextureName,  int bResetScale[2],  int bResetShift[2],  int bResetRotation)
159 {
160         for(std::list<DEntity *>::const_iterator texEntity=entityList.begin(); texEntity!=entityList.end(); texEntity++)
161         {
162                 if(string_equal_nocase("worldspawn", (*texEntity)->m_Classname))
163                         (*texEntity)->ResetTextures(textureName,        fScale,       fShift,       rotation, newTextureName, 
164                                   bResetTextureName,  bResetScale,  bResetShift,  bResetRotation, true);
165                 else
166                 {
167                         if((*texEntity)->ResetTextures( textureName,        fScale,       fShift,       rotation, newTextureName, 
168                                       bResetTextureName,  bResetScale,  bResetShift,  bResetRotation, false))
169                                 RebuildEntity(*texEntity);
170                 }
171         }       
172 }
173
174 void DMap::RebuildEntity(DEntity *ent)
175 {
176         ent->RemoveFromRadiant();
177         ent->BuildInRadiant(false);
178 }