2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 "iselection.h"
27 #include "ifilesystem.h"
31 #include "eclasslib.h"
35 #include "stream/stringstream.h"
38 #include "gtkutil/filechooser.h"
42 #include "preferences.h"
44 #include "mainframe.h"
48 struct entity_globals_t
53 color_entity(0.0f, 0.0f, 0.0f)
58 entity_globals_t g_entity_globals;
60 class EntitySetKeyValueSelected : public scene::Graph::Walker
65 EntitySetKeyValueSelected(const char* key, const char* value)
66 : m_key(key), m_value(value)
69 bool pre(const scene::Path& path, scene::Instance& instance) const
73 void post(const scene::Path& path, scene::Instance& instance) const
75 Entity* entity = Node_getEntity(path.top());
77 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected()))
79 entity->setKeyValue(m_key, m_value);
84 class EntitySetClassnameSelected : public scene::Graph::Walker
86 const char* m_classname;
88 EntitySetClassnameSelected(const char* classname)
89 : m_classname(classname)
92 bool pre(const scene::Path& path, scene::Instance& instance) const
96 void post(const scene::Path& path, scene::Instance& instance) const
98 Entity* entity = Node_getEntity(path.top());
100 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected()))
102 NodeSmartReference node(GlobalEntityCreator().createEntity(GlobalEntityClassManager().findOrInsert(m_classname, node_is_group(path.top()))));
104 EntityCopyingVisitor visitor(*Node_getEntity(node));
106 entity->forEachKeyValue(visitor);
108 NodeSmartReference child(path.top().get());
109 NodeSmartReference parent(path.parent().get());
110 Node_getTraversable(parent)->erase(child);
111 if(Node_getTraversable(child) != 0
112 && Node_getTraversable(node) != 0
113 && node_is_group(node))
115 parentBrushes(child, node);
117 Node_getTraversable(parent)->insert(node);
122 void Scene_EntitySetKeyValue_Selected(const char* key, const char* value)
124 GlobalSceneGraph().traverse(EntitySetKeyValueSelected(key, value));
127 void Scene_EntitySetClassname_Selected(const char* classname)
129 GlobalSceneGraph().traverse(EntitySetClassnameSelected(classname));
133 class EntityUngroupVisitor : public SelectionSystem::Visitor
135 const scene::Path& m_parent;
137 EntityUngroupVisitor(const scene::Path& parent) : m_parent(parent)
140 void visit(scene::Instance& instance) const
142 if(Node_getEntity(instance.path().top()) != 0
143 && node_is_group(instance.path().top()))
145 if(m_parent.top().get_pointer() != instance.path().top().get_pointer())
147 parentBrushes(instance.path().top(), m_parent.top());
148 Path_deleteTop(instance.path());
154 void Entity_ungroupSelected()
156 UndoableCommand undo("ungroupSelectedEntities");
158 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
159 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
161 GlobalSelectionSystem().foreachSelected(EntityUngroupVisitor(world_path));
166 void Entity_connectSelected()
168 if(GlobalSelectionSystem().countSelected() == 2)
170 GlobalEntityCreator().connectEntities(
171 GlobalSelectionSystem().penultimateSelected().path(),
172 GlobalSelectionSystem().ultimateSelected().path()
177 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
181 AABB Doom3Light_getBounds(const AABB& workzone)
185 Vector3 defaultRadius(300, 300, 300);
186 if(!string_parse_vector3(EntityClass_valueForKey(*GlobalEntityClassManager().findOrInsert("light", false), "light_radius"), defaultRadius))
188 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
191 if(aabb.extents[0] == 0)
193 aabb.extents[0] = defaultRadius[0];
195 if(aabb.extents[1] == 0)
197 aabb.extents[1] = defaultRadius[1];
199 if(aabb.extents[2] == 0)
201 aabb.extents[2] = defaultRadius[2];
208 return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
211 int g_iLastLightIntensity;
213 void Entity_createFromSelection(const char* name, const Vector3& origin)
216 if(string_equal_nocase(name, "worldspawn"))
218 gtk_MessageBox(GTK_WIDGET(MainFrame_getWindow()), "Can't create an entity with worldspawn.", "info");
223 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert(name, true);
225 bool isModel = string_equal_nocase(name, "misc_model")
226 || string_equal_nocase(name, "misc_gamemodel")
227 || string_equal_nocase(name, "model_static")
228 || (GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase(name, "func_static"));
230 bool brushesSelected = Scene_countSelectedBrushes(GlobalSceneGraph()) != 0;
232 if(!(entityClass->fixedsize || isModel) && !brushesSelected)
234 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
238 AABB workzone(aabb_for_minmax(Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max));
241 NodeSmartReference node(GlobalEntityCreator().createEntity(entityClass));
243 Node_getTraversable(GlobalSceneGraph().root())->insert(node);
245 scene::Path entitypath(makeReference(GlobalSceneGraph().root()));
246 entitypath.push(makeReference(node.get()));
247 scene::Instance& instance = findInstance(entitypath);
249 if(entityClass->fixedsize || (isModel && !brushesSelected))
253 Transformable* transform = Instance_getTransformable(instance);
256 transform->setType(TRANSFORM_PRIMITIVE);
257 transform->setTranslation(origin);
258 transform->freezeTransform();
261 GlobalSelectionSystem().setSelectedAll(false);
263 Instance_setSelected(instance, true);
267 if (g_pGameDescription->mGameType == "doom3")
269 Node_getEntity(node)->setKeyValue("model", Node_getEntity(node)->getKeyValue("name"));
272 Scene_parentSelectedBrushesToEntity(GlobalSceneGraph(), node);
273 Scene_forEachChildSelectable(SelectableSetSelected(true), instance.path());
276 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
279 if (g_pGameDescription->mGameType == "hl")
281 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
282 if (string_equal_nocase(name, "light")
283 || string_equal_nocase(name, "light_environment")
284 || string_equal_nocase(name, "light_spot"))
286 int intensity = g_iLastLightIntensity;
288 if (DoLightIntensityDlg (&intensity) == eIDOK)
290 g_iLastLightIntensity = intensity;
292 sprintf( buf, "255 255 255 %d", intensity );
293 Node_getEntity(node)->setKeyValue("_light", buf);
297 else if(string_equal_nocase(name, "light"))
299 if(g_pGameDescription->mGameType != "doom3")
301 int intensity = g_iLastLightIntensity;
303 if (DoLightIntensityDlg (&intensity) == eIDOK)
305 g_iLastLightIntensity = intensity;
307 sprintf( buf, "%d", intensity );
308 Node_getEntity(node)->setKeyValue("light", buf);
311 else if(brushesSelected) // use workzone to set light position/size for doom3 lights, if there are brushes selected
313 AABB bounds(Doom3Light_getBounds(workzone));
314 StringOutputStream key(64);
315 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
316 Node_getEntity(node)->setKeyValue("origin", key.c_str());
318 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
319 Node_getEntity(node)->setKeyValue("light_radius", key.c_str());
325 const char* model = misc_model_dialog(GTK_WIDGET(MainFrame_getWindow()));
328 Node_getEntity(node)->setKeyValue("model", model);
334 bool DoNormalisedColor(Vector3& color)
336 if(!color_dialog(GTK_WIDGET(MainFrame_getWindow()), color))
339 ** scale colors so that at least one component is at 1.0F
342 float largest = 0.0F;
344 if ( color[0] > largest )
346 if ( color[1] > largest )
348 if ( color[2] > largest )
351 if ( largest == 0.0F )
359 float scaler = 1.0F / largest;
369 void Entity_setColour()
371 if(GlobalSelectionSystem().countSelected() != 0)
373 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
374 Entity* entity = Node_getEntity(path.top());
377 const char* strColor = entity->getKeyValue("_color");
378 if(!string_empty(strColor))
381 if (string_parse_vector3(strColor, rgb))
383 g_entity_globals.color_entity = rgb;
387 if(g_pGameDescription->mGameType == "doom3"
388 ? color_dialog(GTK_WIDGET(MainFrame_getWindow()), g_entity_globals.color_entity)
389 : DoNormalisedColor(g_entity_globals.color_entity))
392 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
393 g_entity_globals.color_entity[1],
394 g_entity_globals.color_entity[2]);
396 Scene_EntitySetKeyValue_Selected("_color", buffer);
402 const char* misc_model_dialog(GtkWidget* parent)
404 StringOutputStream buffer(1024);
406 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
408 if(!file_readable(buffer.c_str()))
412 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
415 const char *filename = file_dialog (parent, TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name());
418 // use VFS to get the correct relative path
419 const char* relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
420 if(relative == filename)
422 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
429 void LightRadiiImport(EntityCreator& self, bool value)
431 self.setLightRadii(value);
433 typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
435 void LightRadiiExport(EntityCreator& self, const BoolImportCallback& importer)
437 importer(self.getLightRadii());
439 typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
441 void Entity_constructPreferences(PreferencesPage& page)
444 "Show", "Light Radii",
445 LightRadiiImportCaller(GlobalEntityCreator()),
446 LightRadiiExportCaller(GlobalEntityCreator())
449 void Entity_constructPage(PreferenceGroup& group)
451 PreferencesPage page(group.createPage("Entities", "Entity Display Preferences"));
452 Entity_constructPreferences(page);
454 void Entity_registerPreferencesPage()
456 PreferencesDialog_addDisplayPage(FreeCaller1<PreferenceGroup&, Entity_constructPage>());
461 void Entity_constructMenu(GtkMenu* menu)
463 create_menu_item_with_mnemonic(menu, "_Ungroup", "UngroupSelection");
464 create_menu_item_with_mnemonic(menu, "_Connect", "ConnectSelection");
465 create_menu_item_with_mnemonic(menu, "_Select Color...", "EntityColor");
470 #include "preferencesystem.h"
471 #include "stringio.h"
473 void Entity_Construct()
475 GlobalCommands_insert("EntityColor", FreeCaller<Entity_setColour>(), Accelerator('K'));
476 GlobalCommands_insert("ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator('K', (GdkModifierType)GDK_CONTROL_MASK));
477 GlobalCommands_insert("UngroupSelection", FreeCaller<Entity_ungroupSelected>());
479 GlobalPreferenceSystem().registerPreference("SI_Colors5", Vector3ImportStringCaller(g_entity_globals.color_entity), Vector3ExportStringCaller(g_entity_globals.color_entity));
480 GlobalPreferenceSystem().registerPreference("LastLightIntensity", IntImportStringCaller(g_iLastLightIntensity), IntExportStringCaller(g_iLastLightIntensity));
482 Entity_registerPreferencesPage();
485 void Entity_Destroy()