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"
50 inline scene::Node &entity_for_eclass(EntityClass *eclass)
52 if ((string_compare_nocase_n(eclass->name(), "misc_", 5) == 0 &&
53 string_equal_nocase(eclass->name() + string_length(eclass->name()) - 5,
54 "model")) // misc_*model (also misc_model) // TODO make classname_* wrapper functions for this
55 || classname_equal(eclass->name(), "model_static")) {
56 return New_MiscModel(eclass);
57 } else if (classname_equal(eclass->name(), "light")
58 || classname_equal(eclass->name(), "lightJunior")) {
59 return New_Light(eclass);
61 if (!eclass->fixedsize) {
62 if (g_gameType == eGameTypeDoom3) {
63 return New_Doom3Group(eclass);
65 return New_Group(eclass);
67 } else if (!string_empty(eclass->modelpath())) {
68 return New_EclassModel(eclass);
70 return New_GenericEntity(eclass);
74 void Entity_setName(Entity &entity, const char *name)
76 entity.setKeyValue("name", name);
79 typedef ReferenceCaller<Entity, void(const char *), Entity_setName> EntitySetNameCaller;
81 inline Namespaced *Node_getNamespaced(scene::Node &node)
83 return NodeTypeCast<Namespaced>::cast(node);
86 inline scene::Node &node_for_eclass(EntityClass *eclass)
88 scene::Node &node = entity_for_eclass(eclass);
89 Node_getEntity(node)->setKeyValue("classname", eclass->name());
91 if (g_gameType == eGameTypeDoom3
92 && string_not_empty(eclass->name())
93 && !string_equal(eclass->name(), "worldspawn")
94 && !string_equal(eclass->name(), "UNKNOWN_CLASS")) {
96 strcpy(buffer, eclass->name());
98 GlobalNamespace().makeUnique(buffer, EntitySetNameCaller(*Node_getEntity(node)));
101 Namespaced *namespaced = Node_getNamespaced(node);
102 if (namespaced != 0) {
103 namespaced->setNamespace(GlobalNamespace());
109 EntityCreator::KeyValueChangedFunc EntityKeyValues::m_entityKeyValueChanged = 0;
110 EntityCreator::KeyValueChangedFunc KeyValue::m_entityKeyValueChanged = 0;
111 Counter *EntityKeyValues::m_counter = 0;
113 bool g_showNames = true;
114 bool g_showAngles = true;
115 bool g_newLightDraw = true;
116 bool g_lightRadii = false;
118 class ConnectEntities {
124 ConnectEntities(Entity *e1, Entity *e2, int index) : m_e1(e1), m_e2(e2), m_index(index)
128 const char *keyname()
130 StringOutputStream key(16);
137 key << "target" << m_index;
141 void connect(const char *name)
143 m_e1->setKeyValue(keyname(), name);
144 m_e2->setKeyValue("targetname", name);
147 typedef MemberCaller<ConnectEntities, void(const char *), &ConnectEntities::connect> ConnectCaller;
150 inline Entity *ScenePath_getEntity(const scene::Path &path)
152 Entity *entity = Node_getEntity(path.top());
154 entity = Node_getEntity(path.parent());
159 class Quake3EntityCreator : public EntityCreator {
161 scene::Node &createEntity(EntityClass *eclass)
163 return node_for_eclass(eclass);
166 void setKeyValueChangedFunc(KeyValueChangedFunc func)
168 EntityKeyValues::setKeyValueChangedFunc(func);
171 void setCounter(Counter *counter)
173 EntityKeyValues::setCounter(counter);
176 void connectEntities(const scene::Path &path, const scene::Path &targetPath, int index)
178 Entity *e1 = ScenePath_getEntity(path);
179 Entity *e2 = ScenePath_getEntity(targetPath);
181 if (e1 == 0 || e2 == 0) {
182 globalErrorStream() << "entityConnectSelected: both of the selected instances must be an entity\n";
188 << "entityConnectSelected: the selected instances must not both be from the same entity\n";
193 UndoableCommand undo("entityConnectSelected");
195 if (g_gameType == eGameTypeDoom3) {
196 StringOutputStream key(16);
202 e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
205 for (unsigned int i = 0;; ++i) {
210 const char *value = e1->getKeyValue(key.c_str());
211 if (string_empty(value)) {
212 e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
219 ConnectEntities connector(e1, e2, index);
220 const char *value = e2->getKeyValue("targetname");
221 if (!string_empty(value)) {
222 connector.connect(value);
224 const char *type = e2->getKeyValue("classname");
225 if (string_empty(type)) {
228 StringOutputStream key(64);
230 GlobalNamespace().makeUnique(key.c_str(), ConnectEntities::ConnectCaller(connector));
237 void setLightRadii(bool lightRadii)
239 g_lightRadii = lightRadii;
242 bool getLightRadii() const
247 void setShowNames(bool showNames)
249 g_showNames = showNames;
257 void setShowAngles(bool showAngles)
259 g_showAngles = showAngles;
267 void printStatistics() const
269 StringPool_analyse(EntityKeyValues::getPool());
273 Quake3EntityCreator g_Quake3EntityCreator;
275 EntityCreator &GetEntityCreator()
277 return g_Quake3EntityCreator;
281 class filter_entity_classname : public EntityFilter {
282 const char *m_classname;
284 filter_entity_classname(const char *classname) : m_classname(classname)
288 bool filter(const Entity &entity) const
290 return string_equal(entity.getKeyValue("classname"), m_classname);
294 class filter_entity_classgroup : public EntityFilter {
295 const char *m_classgroup;
296 std::size_t m_length;
298 filter_entity_classgroup(const char *classgroup) : m_classgroup(classgroup), m_length(string_length(m_classgroup))
302 bool filter(const Entity &entity) const
304 return string_equal_n(entity.getKeyValue("classname"), m_classgroup, m_length);
308 filter_entity_classname g_filter_entity_world("worldspawn");
309 filter_entity_classname g_filter_entity_func_group("func_group");
310 filter_entity_classname g_filter_entity_light("light");
311 filter_entity_classname g_filter_entity_misc_model("misc_model");
312 filter_entity_classname g_filter_entity_misc_gamemodel("misc_gamemodel");
313 filter_entity_classgroup g_filter_entity_trigger("trigger_");
314 filter_entity_classgroup g_filter_entity_path("path_");
316 class filter_entity_doom3model : public EntityFilter {
318 bool filter(const Entity &entity) const
320 return string_equal(entity.getKeyValue("classname"), "func_static")
321 && !string_equal(entity.getKeyValue("model"), entity.getKeyValue("name"));
325 filter_entity_doom3model g_filter_entity_doom3model;
328 void Entity_InitFilters()
330 add_entity_filter(g_filter_entity_world, EXCLUDE_WORLD);
331 add_entity_filter(g_filter_entity_func_group, EXCLUDE_WORLD);
332 add_entity_filter(g_filter_entity_world, EXCLUDE_ENT, true);
333 add_entity_filter(g_filter_entity_trigger, EXCLUDE_TRIGGERS);
334 add_entity_filter(g_filter_entity_misc_model, EXCLUDE_MODELS);
335 add_entity_filter(g_filter_entity_misc_gamemodel, EXCLUDE_MODELS);
336 add_entity_filter(g_filter_entity_doom3model, EXCLUDE_MODELS);
337 add_entity_filter(g_filter_entity_light, EXCLUDE_LIGHTS);
338 add_entity_filter(g_filter_entity_path, EXCLUDE_PATHS);
342 #include "preferencesystem.h"
344 void Entity_Construct(EGameType gameType)
346 g_gameType = gameType;
347 if (g_gameType == eGameTypeDoom3) {
348 g_targetable_nameKey = "name";
350 Static<KeyIsName>::instance().m_keyIsName = keyIsNameDoom3;
351 Static<KeyIsName>::instance().m_nameKey = "name";
353 Static<KeyIsName>::instance().m_keyIsName = keyIsNameQuake3;
354 Static<KeyIsName>::instance().m_nameKey = "targetname";
357 GlobalPreferenceSystem().registerPreference("SI_ShowNames", make_property_string(g_showNames));
358 GlobalPreferenceSystem().registerPreference("SI_ShowAngles", make_property_string(g_showAngles));
359 GlobalPreferenceSystem().registerPreference("NewLightStyle", make_property_string(g_newLightDraw));
360 GlobalPreferenceSystem().registerPreference("LightRadiuses", make_property_string(g_lightRadii));
362 Entity_InitFilters();
363 LightType lightType = LIGHTTYPE_DEFAULT;
364 if (g_gameType == eGameTypeRTCW) {
365 lightType = LIGHTTYPE_RTCW;
366 } else if (g_gameType == eGameTypeDoom3) {
367 lightType = LIGHTTYPE_DOOM3;
369 Light_Construct(lightType);
370 MiscModel_construct();
371 Doom3Group_construct();
373 RenderablePivot::StaticShader::instance() = GlobalShaderCache().capture("$PIVOT");
375 GlobalShaderCache().attachRenderable(StaticRenderableConnectionLines::instance());
378 void Entity_Destroy()
380 GlobalShaderCache().detachRenderable(StaticRenderableConnectionLines::instance());
382 GlobalShaderCache().release("$PIVOT");
384 Doom3Group_destroy();