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 void Entity_ungroupSelected()
135 if (GlobalSelectionSystem().countSelected() < 1) return;
137 UndoableCommand undo("ungroupSelectedEntities");
139 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
140 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
142 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
143 scene::Path path = instance.path();
145 if (!Node_isEntity(path.top())) path.pop();
147 if(Node_getEntity(path.top()) != 0
148 && node_is_group(path.top()))
150 if(world_path.top().get_pointer() != path.top().get_pointer())
152 parentBrushes(path.top(), world_path.top());
153 Path_deleteTop(path);
159 class EntityFindSelected : public scene::Graph::Walker
162 mutable const scene::Path *groupPath;
163 mutable scene::Instance *groupInstance;
164 EntityFindSelected(): groupPath(0), groupInstance(0)
167 bool pre(const scene::Path& path, scene::Instance& instance) const
171 void post(const scene::Path& path, scene::Instance& instance) const
173 Entity* entity = Node_getEntity(path.top());
175 && Instance_getSelectable(instance)->isSelected()
176 && node_is_group(path.top())
180 groupInstance = &instance;
185 class EntityGroupSelected : public scene::Graph::Walker
187 NodeSmartReference group;
188 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
189 //Stack<DeletionPair> deleteme;
191 EntityGroupSelected(const scene::Path &p): group(p.top().get())
194 bool pre(const scene::Path& path, scene::Instance& instance) const
198 void post(const scene::Path& path, scene::Instance& instance) const
200 Selectable *selectable = Instance_getSelectable(instance);
201 if(selectable && selectable->isSelected())
203 Entity* entity = Node_getEntity(path.top());
204 if(entity == 0 && Node_isPrimitive(path.top()))
206 NodeSmartReference child(path.top().get());
207 NodeSmartReference parent(path.parent().get());
209 if(path.size() >= 3 && parent != Map_FindOrInsertWorldspawn(g_map))
211 NodeSmartReference parentparent(path[path.size() - 3].get());
213 Node_getTraversable(parent)->erase(child);
214 Node_getTraversable(group)->insert(child);
216 if(Node_getTraversable(parent)->empty())
218 //deleteme.push(DeletionPair(parentparent, parent));
219 Node_getTraversable(parentparent)->erase(parent);
224 Node_getTraversable(parent)->erase(child);
225 Node_getTraversable(group)->insert(child);
232 void Entity_groupSelected()
234 if (GlobalSelectionSystem().countSelected() < 1) return;
236 UndoableCommand undo("groupSelectedEntities");
238 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
239 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
241 EntityFindSelected fse;
242 GlobalSceneGraph().traverse(fse);
245 GlobalSceneGraph().traverse(EntityGroupSelected(*fse.groupPath));
249 GlobalSceneGraph().traverse(EntityGroupSelected(world_path));
255 void Entity_connectSelected()
257 if(GlobalSelectionSystem().countSelected() == 2)
259 GlobalEntityCreator().connectEntities(
260 GlobalSelectionSystem().penultimateSelected().path(),
261 GlobalSelectionSystem().ultimateSelected().path(),
267 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
271 void Entity_killconnectSelected()
273 if(GlobalSelectionSystem().countSelected() == 2)
275 GlobalEntityCreator().connectEntities(
276 GlobalSelectionSystem().penultimateSelected().path(),
277 GlobalSelectionSystem().ultimateSelected().path(),
283 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
287 AABB Doom3Light_getBounds(const AABB& workzone)
291 Vector3 defaultRadius(300, 300, 300);
292 if(!string_parse_vector3(EntityClass_valueForKey(*GlobalEntityClassManager().findOrInsert("light", false), "light_radius"), defaultRadius))
294 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
297 if(aabb.extents[0] == 0)
299 aabb.extents[0] = defaultRadius[0];
301 if(aabb.extents[1] == 0)
303 aabb.extents[1] = defaultRadius[1];
305 if(aabb.extents[2] == 0)
307 aabb.extents[2] = defaultRadius[2];
314 return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
317 int g_iLastLightIntensity;
319 void Entity_createFromSelection(const char* name, const Vector3& origin)
322 if(string_equal_nocase(name, "worldspawn"))
324 gtk_MessageBox(GTK_WIDGET(MainFrame_getWindow()), "Can't create an entity with worldspawn.", "info");
329 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert(name, true);
331 bool isModel = string_equal_nocase(name, "misc_model")
332 || string_equal_nocase(name, "misc_gamemodel")
333 || string_equal_nocase(name, "model_static")
334 || (GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase(name, "func_static"));
336 bool brushesSelected = Scene_countSelectedBrushes(GlobalSceneGraph()) != 0;
338 if(!(entityClass->fixedsize || isModel) && !brushesSelected)
340 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
344 AABB workzone(aabb_for_minmax(Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max));
347 NodeSmartReference node(GlobalEntityCreator().createEntity(entityClass));
349 Node_getTraversable(GlobalSceneGraph().root())->insert(node);
351 scene::Path entitypath(makeReference(GlobalSceneGraph().root()));
352 entitypath.push(makeReference(node.get()));
353 scene::Instance& instance = findInstance(entitypath);
355 if(entityClass->fixedsize || (isModel && !brushesSelected))
359 Transformable* transform = Instance_getTransformable(instance);
362 transform->setType(TRANSFORM_PRIMITIVE);
363 transform->setTranslation(origin);
364 transform->freezeTransform();
367 GlobalSelectionSystem().setSelectedAll(false);
369 Instance_setSelected(instance, true);
373 if (g_pGameDescription->mGameType == "doom3")
375 Node_getEntity(node)->setKeyValue("model", Node_getEntity(node)->getKeyValue("name"));
378 Scene_parentSelectedBrushesToEntity(GlobalSceneGraph(), node);
379 Scene_forEachChildSelectable(SelectableSetSelected(true), instance.path());
382 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
385 if (g_pGameDescription->mGameType == "hl")
387 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
388 if (string_equal_nocase(name, "light")
389 || string_equal_nocase(name, "light_environment")
390 || string_equal_nocase(name, "light_spot"))
392 int intensity = g_iLastLightIntensity;
394 if (DoLightIntensityDlg (&intensity) == eIDOK)
396 g_iLastLightIntensity = intensity;
398 sprintf( buf, "255 255 255 %d", intensity );
399 Node_getEntity(node)->setKeyValue("_light", buf);
403 else if(string_equal_nocase(name, "light"))
405 if(g_pGameDescription->mGameType != "doom3")
407 int intensity = g_iLastLightIntensity;
409 if (DoLightIntensityDlg (&intensity) == eIDOK)
411 g_iLastLightIntensity = intensity;
413 sprintf( buf, "%d", intensity );
414 Node_getEntity(node)->setKeyValue("light", buf);
417 else if(brushesSelected) // use workzone to set light position/size for doom3 lights, if there are brushes selected
419 AABB bounds(Doom3Light_getBounds(workzone));
420 StringOutputStream key(64);
421 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
422 Node_getEntity(node)->setKeyValue("origin", key.c_str());
424 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
425 Node_getEntity(node)->setKeyValue("light_radius", key.c_str());
431 const char* model = misc_model_dialog(GTK_WIDGET(MainFrame_getWindow()));
434 Node_getEntity(node)->setKeyValue("model", model);
440 bool DoNormalisedColor(Vector3& color)
442 if(!color_dialog(GTK_WIDGET(MainFrame_getWindow()), color))
445 ** scale colors so that at least one component is at 1.0F
448 float largest = 0.0F;
450 if ( color[0] > largest )
452 if ( color[1] > largest )
454 if ( color[2] > largest )
457 if ( largest == 0.0F )
465 float scaler = 1.0F / largest;
476 void NormalizeColor(Vector3& color)
478 // scale colors so that at least one component is at 1.0F
480 float largest = 0.0F;
482 if ( color[0] > largest )
484 if ( color[1] > largest )
486 if ( color[2] > largest )
489 if ( largest == 0.0F )
497 float scaler = 1.0F / largest;
505 void Entity_normalizeColor()
507 if(GlobalSelectionSystem().countSelected() != 0)
509 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
510 Entity* entity = Node_getEntity(path.top());
514 const char* strColor = entity->getKeyValue("_color");
515 if(!string_empty(strColor))
518 if (string_parse_vector3(strColor, rgb))
520 g_entity_globals.color_entity = rgb;
521 NormalizeColor(g_entity_globals.color_entity);
524 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
525 g_entity_globals.color_entity[1],
526 g_entity_globals.color_entity[2]);
528 Scene_EntitySetKeyValue_Selected("_color", buffer);
535 void Entity_setColour()
537 if(GlobalSelectionSystem().countSelected() != 0)
539 bool normalize = false;
540 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
541 Entity* entity = Node_getEntity(path.top());
545 const char* strColor = entity->getKeyValue("_color");
546 if(!string_empty(strColor))
549 if (string_parse_vector3(strColor, rgb))
551 g_entity_globals.color_entity = rgb;
555 if( g_pGameDescription->mGameType == "doom3" )
558 if(color_dialog(GTK_WIDGET(MainFrame_getWindow()), g_entity_globals.color_entity))
561 NormalizeColor(g_entity_globals.color_entity);
564 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
565 g_entity_globals.color_entity[1],
566 g_entity_globals.color_entity[2]);
568 Scene_EntitySetKeyValue_Selected("_color", buffer);
574 const char* misc_model_dialog(GtkWidget* parent)
576 StringOutputStream buffer(1024);
578 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
580 if(!file_readable(buffer.c_str()))
584 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
587 const char *filename = file_dialog (parent, TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name());
590 // use VFS to get the correct relative path
591 const char* relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
592 if(relative == filename)
594 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
601 void LightRadiiImport(EntityCreator& self, bool value)
603 self.setLightRadii(value);
605 typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
607 void LightRadiiExport(EntityCreator& self, const BoolImportCallback& importer)
609 importer(self.getLightRadii());
611 typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
613 void Entity_constructPreferences(PreferencesPage& page)
616 "Show", "Light Radii",
617 LightRadiiImportCaller(GlobalEntityCreator()),
618 LightRadiiExportCaller(GlobalEntityCreator())
621 void Entity_constructPage(PreferenceGroup& group)
623 PreferencesPage page(group.createPage("Entities", "Entity Display Preferences"));
624 Entity_constructPreferences(page);
626 void Entity_registerPreferencesPage()
628 PreferencesDialog_addDisplayPage(FreeCaller1<PreferenceGroup&, Entity_constructPage>());
633 void Entity_constructMenu(GtkMenu* menu)
635 create_menu_item_with_mnemonic(menu, "_Regroup", "GroupSelection");
636 create_menu_item_with_mnemonic(menu, "_Ungroup", "UngroupSelection");
637 create_menu_item_with_mnemonic(menu, "_Connect", "ConnectSelection");
638 create_menu_item_with_mnemonic(menu, "_KillConnect", "KillConnectSelection");
639 create_menu_item_with_mnemonic(menu, "_Select Color...", "EntityColor");
640 create_menu_item_with_mnemonic(menu, "_Normalize Color...", "NormalizeColor");
645 #include "preferencesystem.h"
646 #include "stringio.h"
648 void Entity_Construct()
650 GlobalCommands_insert("EntityColor", FreeCaller<Entity_setColour>(), Accelerator('K'));
651 GlobalCommands_insert("NormalizeColor", FreeCaller<Entity_normalizeColor>());
652 GlobalCommands_insert("ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator('K', (GdkModifierType)GDK_CONTROL_MASK));
653 GlobalCommands_insert("KillConnectSelection", FreeCaller<Entity_killconnectSelected>(), Accelerator('K', (GdkModifierType)(GDK_SHIFT_MASK)));
654 GlobalCommands_insert("GroupSelection", FreeCaller<Entity_groupSelected>());
655 GlobalCommands_insert("UngroupSelection", FreeCaller<Entity_ungroupSelected>());
657 GlobalPreferenceSystem().registerPreference("SI_Colors5", Vector3ImportStringCaller(g_entity_globals.color_entity), Vector3ExportStringCaller(g_entity_globals.color_entity));
658 GlobalPreferenceSystem().registerPreference("LastLightIntensity", IntImportStringCaller(g_iLastLightIntensity), IntExportStringCaller(g_iLastLightIntensity));
660 Entity_registerPreferencesPage();
663 void Entity_Destroy()