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
136 ConnectEntities(Entity* e1, Entity* e2, int index) : m_e1(e1), m_e2(e2), m_index(index)
139 const char *keyname()
141 StringOutputStream key(16);
146 key << "target" << m_index;
149 void connect(const char* name)
151 m_e1->setKeyValue(keyname(), name);
152 m_e2->setKeyValue("targetname", name);
154 typedef MemberCaller1<ConnectEntities, const char*, &ConnectEntities::connect> ConnectCaller;
157 inline Entity* ScenePath_getEntity(const scene::Path& path)
159 Entity* entity = Node_getEntity(path.top());
162 entity = Node_getEntity(path.parent());
167 class Quake3EntityCreator : public EntityCreator
170 scene::Node& createEntity(EntityClass* eclass)
172 return node_for_eclass(eclass);
174 void setKeyValueChangedFunc(KeyValueChangedFunc func)
176 EntityKeyValues::setKeyValueChangedFunc(func);
178 void setCounter(Counter* counter)
180 EntityKeyValues::setCounter(counter);
182 void connectEntities(const scene::Path& path, const scene::Path& targetPath, int index)
184 Entity* e1 = ScenePath_getEntity(path);
185 Entity* e2 = ScenePath_getEntity(targetPath);
187 if(e1 == 0 || e2 == 0)
189 globalErrorStream() << "entityConnectSelected: both of the selected instances must be an entity\n";
195 globalErrorStream() << "entityConnectSelected: the selected instances must not both be from the same entity\n";
200 UndoableCommand undo("entityConnectSelected");
202 if(g_gameType == eGameTypeDoom3)
204 StringOutputStream key(16);
212 e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
217 for(unsigned int i = 0; ; ++i)
224 const char* value = e1->getKeyValue(key.c_str());
225 if(string_empty(value))
227 e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
236 ConnectEntities connector(e1, e2, index);
237 const char* value = e2->getKeyValue("targetname");
238 if(string_empty(value))
240 value = e1->getKeyValue(connector.keyname());
242 if(!string_empty(value))
244 connector.connect(value);
248 const char* type = e2->getKeyValue("classname");
249 if(string_empty(type))
253 StringOutputStream key(64);
255 GlobalNamespace().makeUnique(key.c_str(), ConnectEntities::ConnectCaller(connector));
261 void setLightRadii(bool lightRadii)
263 g_lightRadii = lightRadii;
269 void setShowNames(bool showNames)
271 g_showNames = showNames;
277 void setShowAngles(bool showAngles)
279 g_showAngles = showAngles;
286 void printStatistics() const
288 StringPool_analyse(EntityKeyValues::getPool());
292 Quake3EntityCreator g_Quake3EntityCreator;
294 EntityCreator& GetEntityCreator()
296 return g_Quake3EntityCreator;
301 class filter_entity_classname : public EntityFilter
303 const char* m_classname;
305 filter_entity_classname(const char* classname) : m_classname(classname)
308 bool filter(const Entity& entity) const
310 return string_equal(entity.getKeyValue("classname"), m_classname);
314 class filter_entity_classgroup : public EntityFilter
316 const char* m_classgroup;
317 std::size_t m_length;
319 filter_entity_classgroup(const char* classgroup) : m_classgroup(classgroup), m_length(string_length(m_classgroup))
322 bool filter(const Entity& entity) const
324 return string_equal_n(entity.getKeyValue("classname"), m_classgroup, m_length);
328 filter_entity_classname g_filter_entity_world("worldspawn");
329 filter_entity_classname g_filter_entity_func_group("func_group");
330 filter_entity_classname g_filter_entity_light("light");
331 filter_entity_classname g_filter_entity_misc_model("misc_model");
332 filter_entity_classname g_filter_entity_misc_gamemodel("misc_gamemodel");
333 filter_entity_classgroup g_filter_entity_trigger("trigger_");
334 filter_entity_classgroup g_filter_entity_path("path_");
336 class filter_entity_doom3model : public EntityFilter
339 bool filter(const Entity& entity) const
341 return string_equal(entity.getKeyValue("classname"), "func_static")
342 && !string_equal(entity.getKeyValue("model"), entity.getKeyValue("name"));
346 filter_entity_doom3model g_filter_entity_doom3model;
349 void Entity_InitFilters()
351 add_entity_filter(g_filter_entity_world, EXCLUDE_WORLD);
352 add_entity_filter(g_filter_entity_func_group, EXCLUDE_WORLD);
353 add_entity_filter(g_filter_entity_world, EXCLUDE_ENT, true);
354 add_entity_filter(g_filter_entity_trigger, EXCLUDE_TRIGGERS);
355 add_entity_filter(g_filter_entity_misc_model, EXCLUDE_MODELS);
356 add_entity_filter(g_filter_entity_misc_gamemodel, EXCLUDE_MODELS);
357 add_entity_filter(g_filter_entity_doom3model, EXCLUDE_MODELS);
358 add_entity_filter(g_filter_entity_light, EXCLUDE_LIGHTS);
359 add_entity_filter(g_filter_entity_path, EXCLUDE_PATHS);
363 #include "preferencesystem.h"
365 void Entity_Construct(EGameType gameType)
367 g_gameType = gameType;
368 if(g_gameType == eGameTypeDoom3)
370 g_targetable_nameKey = "name";
372 Static<KeyIsName>::instance().m_keyIsName = keyIsNameDoom3;
373 Static<KeyIsName>::instance().m_nameKey = "name";
377 Static<KeyIsName>::instance().m_keyIsName = keyIsNameQuake3;
378 Static<KeyIsName>::instance().m_nameKey = "targetname";
381 GlobalPreferenceSystem().registerPreference("SI_ShowNames", BoolImportStringCaller(g_showNames), BoolExportStringCaller(g_showNames));
382 GlobalPreferenceSystem().registerPreference("SI_ShowAngles", BoolImportStringCaller(g_showAngles), BoolExportStringCaller(g_showAngles));
383 GlobalPreferenceSystem().registerPreference("NewLightStyle", BoolImportStringCaller(g_newLightDraw), BoolExportStringCaller(g_newLightDraw));
384 GlobalPreferenceSystem().registerPreference("LightRadiuses", BoolImportStringCaller(g_lightRadii), BoolExportStringCaller(g_lightRadii));
386 Entity_InitFilters();
387 LightType lightType = LIGHTTYPE_DEFAULT;
388 if(g_gameType == eGameTypeRTCW)
390 lightType = LIGHTTYPE_RTCW;
392 else if(g_gameType == eGameTypeDoom3)
394 lightType = LIGHTTYPE_DOOM3;
396 Light_Construct(lightType);
397 MiscModel_construct();
398 Doom3Group_construct();
400 RenderablePivot::StaticShader::instance() = GlobalShaderCache().capture("$PIVOT");
402 GlobalShaderCache().attachRenderable(StaticRenderableConnectionLines::instance());
405 void Entity_Destroy()
407 GlobalShaderCache().detachRenderable(StaticRenderableConnectionLines::instance());
409 GlobalShaderCache().release("$PIVOT");
411 Doom3Group_destroy();