2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "selectable.h"
26 #include "namespace.h"
29 #include "entitylib.h"
30 #include "eclasslib.h"
33 #include "targetable.h"
34 #include "uniquenames.h"
36 #include "stream/stringstream.h"
40 #include "miscmodel.h"
43 #include "eclassmodel.h"
45 #include "doom3group.h"
51 inline scene::Node& entity_for_eclass(EntityClass* eclass)
53 if(classname_equal(eclass->name(), "misc_model")
54 || classname_equal(eclass->name(), "misc_gamemodel")
55 || classname_equal(eclass->name(), "model_static"))
57 return New_MiscModel(eclass);
59 else if(classname_equal(eclass->name(), "light")
60 || classname_equal(eclass->name(), "lightJunior"))
62 return New_Light(eclass);
64 if(!eclass->fixedsize)
66 if(g_gameType == eGameTypeDoom3)
68 return New_Doom3Group(eclass);
72 return New_Group(eclass);
75 else if(!string_empty(eclass->modelpath()))
77 return New_EclassModel(eclass);
81 return New_GenericEntity(eclass);
85 void Entity_setName(Entity& entity, const char* name)
87 entity.setKeyValue("name", name);
89 typedef ReferenceCaller1<Entity, const char*, Entity_setName> EntitySetNameCaller;
91 inline Namespaced* Node_getNamespaced(scene::Node& node)
93 return NodeTypeCast<Namespaced>::cast(node);
96 inline scene::Node& node_for_eclass(EntityClass* eclass)
98 scene::Node& node = entity_for_eclass(eclass);
99 Node_getEntity(node)->setKeyValue("classname", eclass->name());
101 if(g_gameType == eGameTypeDoom3
102 && string_not_empty(eclass->name())
103 && !string_equal(eclass->name(), "worldspawn")
104 && !string_equal(eclass->name(), "UNKNOWN_CLASS"))
107 strcpy(buffer, eclass->name());
108 strcat(buffer, "_1");
109 GlobalNamespace().makeUnique(buffer, EntitySetNameCaller(*Node_getEntity(node)));
112 Namespaced* namespaced = Node_getNamespaced(node);
115 namespaced->setNamespace(GlobalNamespace());
121 EntityCreator::KeyValueChangedFunc EntityKeyValues::m_entityKeyValueChanged = 0;
122 EntityCreator::KeyValueChangedFunc KeyValue::m_entityKeyValueChanged = 0;
123 Counter* EntityKeyValues::m_counter = 0;
125 bool g_showNames = true;
126 bool g_showAngles = true;
127 bool g_newLightDraw = true;
128 bool g_lightRadii = false;
130 class ConnectEntities
135 ConnectEntities(Entity* e1, Entity* e2) : m_e1(e1), m_e2(e2)
138 void connect(const char* name)
140 m_e1->setKeyValue("target", name);
141 m_e2->setKeyValue("targetname", name);
143 typedef MemberCaller1<ConnectEntities, const char*, &ConnectEntities::connect> ConnectCaller;
146 inline Entity* ScenePath_getEntity(const scene::Path& path)
148 Entity* entity = Node_getEntity(path.top());
151 entity = Node_getEntity(path.parent());
156 class Quake3EntityCreator : public EntityCreator
159 scene::Node& createEntity(EntityClass* eclass)
161 return node_for_eclass(eclass);
163 void setKeyValueChangedFunc(KeyValueChangedFunc func)
165 EntityKeyValues::setKeyValueChangedFunc(func);
167 void setCounter(Counter* counter)
169 EntityKeyValues::setCounter(counter);
171 void connectEntities(const scene::Path& path, const scene::Path& targetPath)
173 Entity* e1 = ScenePath_getEntity(path);
174 Entity* e2 = ScenePath_getEntity(targetPath);
176 if(e1 == 0 || e2 == 0)
178 globalErrorStream() << "entityConnectSelected: both of the selected instances must be an entity\n";
184 globalErrorStream() << "entityConnectSelected: the selected instances must not both be from the same entity\n";
189 UndoableCommand undo("entityConnectSelected");
191 if(g_gameType == eGameTypeDoom3)
193 StringOutputStream key(16);
194 for(unsigned int i = 0; ; ++i)
201 const char* value = e1->getKeyValue(key.c_str());
202 if(string_empty(value))
204 e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
212 ConnectEntities connector(e1, e2);
213 const char* value = e2->getKeyValue("targetname");
214 if(string_empty(value))
216 value = e1->getKeyValue("target");
218 if(!string_empty(value))
220 connector.connect(value);
224 const char* type = e2->getKeyValue("classname");
225 if(string_empty(type))
229 StringOutputStream key(64);
231 GlobalNamespace().makeUnique(key.c_str(), ConnectEntities::ConnectCaller(connector));
237 void setLightRadii(bool lightRadii)
239 g_lightRadii = lightRadii;
245 void setShowNames(bool showNames)
247 g_showNames = showNames;
253 void setShowAngles(bool showAngles)
255 g_showAngles = showAngles;
262 void printStatistics() const
264 StringPool_analyse(EntityKeyValues::getPool());
268 Quake3EntityCreator g_Quake3EntityCreator;
270 EntityCreator& GetEntityCreator()
272 return g_Quake3EntityCreator;
277 class filter_entity_classname : public EntityFilter
279 const char* m_classname;
281 filter_entity_classname(const char* classname) : m_classname(classname)
284 bool filter(const Entity& entity) const
286 return string_equal(entity.getKeyValue("classname"), m_classname);
290 class filter_entity_classgroup : public EntityFilter
292 const char* m_classgroup;
293 std::size_t m_length;
295 filter_entity_classgroup(const char* classgroup) : m_classgroup(classgroup), m_length(string_length(m_classgroup))
298 bool filter(const Entity& entity) const
300 return string_equal_n(entity.getKeyValue("classname"), m_classgroup, m_length);
304 filter_entity_classname g_filter_entity_world("worldspawn");
305 filter_entity_classname g_filter_entity_func_group("func_group");
306 filter_entity_classname g_filter_entity_light("light");
307 filter_entity_classname g_filter_entity_misc_model("misc_model");
308 filter_entity_classgroup g_filter_entity_trigger("trigger_");
309 filter_entity_classgroup g_filter_entity_path("path_");
311 class filter_entity_doom3model : public EntityFilter
314 bool filter(const Entity& entity) const
316 return string_equal(entity.getKeyValue("classname"), "func_static")
317 && !string_equal(entity.getKeyValue("model"), entity.getKeyValue("name"));
321 filter_entity_doom3model g_filter_entity_doom3model;
324 void Entity_InitFilters()
326 add_entity_filter(g_filter_entity_world, EXCLUDE_WORLD);
327 add_entity_filter(g_filter_entity_func_group, EXCLUDE_WORLD);
328 add_entity_filter(g_filter_entity_world, EXCLUDE_ENT, true);
329 add_entity_filter(g_filter_entity_trigger, EXCLUDE_TRIGGERS);
330 add_entity_filter(g_filter_entity_misc_model, EXCLUDE_MODELS);
331 add_entity_filter(g_filter_entity_doom3model, EXCLUDE_MODELS);
332 add_entity_filter(g_filter_entity_light, EXCLUDE_LIGHTS);
333 add_entity_filter(g_filter_entity_path, EXCLUDE_PATHS);
337 #include "preferencesystem.h"
339 void Entity_Construct(EGameType gameType)
341 g_gameType = gameType;
342 if(g_gameType == eGameTypeDoom3)
344 g_targetable_nameKey = "name";
346 Static<KeyIsName>::instance().m_keyIsName = keyIsNameDoom3;
347 Static<KeyIsName>::instance().m_nameKey = "name";
351 Static<KeyIsName>::instance().m_keyIsName = keyIsNameQuake3;
352 Static<KeyIsName>::instance().m_nameKey = "targetname";
355 GlobalPreferenceSystem().registerPreference("SI_ShowNames", BoolImportStringCaller(g_showNames), BoolExportStringCaller(g_showNames));
356 GlobalPreferenceSystem().registerPreference("SI_ShowAngles", BoolImportStringCaller(g_showAngles), BoolExportStringCaller(g_showAngles));
357 GlobalPreferenceSystem().registerPreference("NewLightStyle", BoolImportStringCaller(g_newLightDraw), BoolExportStringCaller(g_newLightDraw));
358 GlobalPreferenceSystem().registerPreference("LightRadiuses", BoolImportStringCaller(g_lightRadii), BoolExportStringCaller(g_lightRadii));
360 Entity_InitFilters();
361 LightType lightType = LIGHTTYPE_DEFAULT;
362 if(g_gameType == eGameTypeRTCW)
364 lightType = LIGHTTYPE_RTCW;
366 else if(g_gameType == eGameTypeDoom3)
368 lightType = LIGHTTYPE_DOOM3;
370 Light_Construct(lightType);
371 MiscModel_construct();
372 Doom3Group_construct();
374 RenderablePivot::StaticShader::instance() = GlobalShaderCache().capture("$PIVOT");
376 GlobalShaderCache().attachRenderable(StaticRenderableConnectionLines::instance());
379 void Entity_Destroy()
381 GlobalShaderCache().detachRenderable(StaticRenderableConnectionLines::instance());
383 GlobalShaderCache().release("$PIVOT");
385 Doom3Group_destroy();