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"
43 #include "preferencesystem.h"
46 #include "mainframe.h"
50 #include "uilib/uilib.h"
52 struct entity_globals_t {
56 color_entity(0.0f, 0.0f, 0.0f)
61 entity_globals_t g_entity_globals;
63 class EntitySetKeyValueSelected : public scene::Graph::Walker {
67 EntitySetKeyValueSelected(const char *key, const char *value)
68 : m_key(key), m_value(value)
72 bool pre(const scene::Path &path, scene::Instance &instance) const
77 void post(const scene::Path &path, scene::Instance &instance) const
79 Entity *entity = Node_getEntity(path.top());
81 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected())) {
82 entity->setKeyValue(m_key, m_value);
87 class EntitySetClassnameSelected : public scene::Graph::Walker {
88 const char *m_classname;
90 EntitySetClassnameSelected(const char *classname)
91 : m_classname(classname)
95 bool pre(const scene::Path &path, scene::Instance &instance) const
100 void post(const scene::Path &path, scene::Instance &instance) const
102 Entity *entity = Node_getEntity(path.top());
104 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected())) {
105 NodeSmartReference node(GlobalEntityCreator().createEntity(
106 GlobalEntityClassManager().findOrInsert(m_classname, node_is_group(path.top()))));
108 EntityCopyingVisitor visitor(*Node_getEntity(node));
110 entity->forEachKeyValue(visitor);
112 NodeSmartReference child(path.top().get());
113 NodeSmartReference parent(path.parent().get());
114 Node_getTraversable(parent)->erase(child);
115 if (Node_getTraversable(child) != 0
116 && Node_getTraversable(node) != 0
117 && node_is_group(node)) {
118 parentBrushes(child, node);
120 Node_getTraversable(parent)->insert(node);
125 void Scene_EntitySetKeyValue_Selected(const char *key, const char *value)
127 GlobalSceneGraph().traverse(EntitySetKeyValueSelected(key, value));
130 void Scene_EntitySetClassname_Selected(const char *classname)
132 GlobalSceneGraph().traverse(EntitySetClassnameSelected(classname));
136 void Entity_ungroupSelected()
138 if (GlobalSelectionSystem().countSelected() < 1) {
142 UndoableCommand undo("ungroupSelectedEntities");
144 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
145 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
147 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
148 scene::Path path = instance.path();
150 if (!Node_isEntity(path.top())) {
154 if (Node_getEntity(path.top()) != 0
155 && node_is_group(path.top())) {
156 if (world_path.top().get_pointer() != path.top().get_pointer()) {
157 parentBrushes(path.top(), world_path.top());
158 Path_deleteTop(path);
164 class EntityFindSelected : public scene::Graph::Walker {
166 mutable const scene::Path *groupPath;
167 mutable scene::Instance *groupInstance;
169 EntityFindSelected() : groupPath(0), groupInstance(0)
173 bool pre(const scene::Path &path, scene::Instance &instance) const
178 void post(const scene::Path &path, scene::Instance &instance) const
180 Entity *entity = Node_getEntity(path.top());
182 && Instance_getSelectable(instance)->isSelected()
183 && node_is_group(path.top())
186 groupInstance = &instance;
191 class EntityGroupSelected : public scene::Graph::Walker {
192 NodeSmartReference group, worldspawn;
193 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
194 //Stack<DeletionPair> deleteme;
196 EntityGroupSelected(const scene::Path &p) : group(p.top().get()), worldspawn(Map_FindOrInsertWorldspawn(g_map))
200 bool pre(const scene::Path &path, scene::Instance &instance) const
205 void post(const scene::Path &path, scene::Instance &instance) const
207 Selectable *selectable = Instance_getSelectable(instance);
208 if (selectable && selectable->isSelected()) {
209 Entity *entity = Node_getEntity(path.top());
210 if (entity == 0 && Node_isPrimitive(path.top())) {
211 NodeSmartReference child(path.top().get());
212 NodeSmartReference parent(path.parent().get());
214 if (path.size() >= 3 && parent != worldspawn) {
215 NodeSmartReference parentparent(path[path.size() - 3].get());
217 Node_getTraversable(parent)->erase(child);
218 Node_getTraversable(group)->insert(child);
220 if (Node_getTraversable(parent)->empty()) {
221 //deleteme.push(DeletionPair(parentparent, parent));
222 Node_getTraversable(parentparent)->erase(parent);
225 Node_getTraversable(parent)->erase(child);
226 Node_getTraversable(group)->insert(child);
233 void Entity_groupSelected()
235 if (GlobalSelectionSystem().countSelected() < 1) {
239 UndoableCommand undo("groupSelectedEntities");
241 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
242 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
244 EntityFindSelected fse;
245 GlobalSceneGraph().traverse(fse);
247 GlobalSceneGraph().traverse(EntityGroupSelected(*fse.groupPath));
249 GlobalSceneGraph().traverse(EntityGroupSelected(world_path));
254 void Entity_connectSelected()
256 if (GlobalSelectionSystem().countSelected() == 2) {
257 GlobalEntityCreator().connectEntities(
258 GlobalSelectionSystem().penultimateSelected().path(),
259 GlobalSelectionSystem().ultimateSelected().path(),
263 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
267 void Entity_killconnectSelected()
269 if (GlobalSelectionSystem().countSelected() == 2) {
270 GlobalEntityCreator().connectEntities(
271 GlobalSelectionSystem().penultimateSelected().path(),
272 GlobalSelectionSystem().ultimateSelected().path(),
276 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
280 AABB Doom3Light_getBounds(const AABB &workzone)
284 Vector3 defaultRadius(300, 300, 300);
285 if (!string_parse_vector3(
286 EntityClass_valueForKey(*GlobalEntityClassManager().findOrInsert("light", false), "light_radius"),
288 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
291 if (aabb.extents[0] == 0) {
292 aabb.extents[0] = defaultRadius[0];
294 if (aabb.extents[1] == 0) {
295 aabb.extents[1] = defaultRadius[1];
297 if (aabb.extents[2] == 0) {
298 aabb.extents[2] = defaultRadius[2];
301 if (aabb_valid(aabb)) {
304 return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
307 int g_iLastLightIntensity;
309 void Entity_createFromSelection(const char *name, const Vector3 &origin)
312 if ( string_equal_nocase( name, "worldspawn" ) ) {
313 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
318 EntityClass *entityClass = GlobalEntityClassManager().findOrInsert(name, true);
320 bool isModel = (string_compare_nocase_n(name, "misc_", 5) == 0 &&
321 string_equal_nocase(name + string_length(name) - 5, "model")) // misc_*model (also misc_model)
322 || string_equal_nocase(name, "model_static")
323 || (GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase(name, "func_static"));
325 bool brushesSelected = Scene_countSelectedBrushes(GlobalSceneGraph()) != 0;
327 if (!(entityClass->fixedsize || isModel) && !brushesSelected) {
328 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
332 AABB workzone(aabb_for_minmax(Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max));
335 NodeSmartReference node(GlobalEntityCreator().createEntity(entityClass));
337 Node_getTraversable(GlobalSceneGraph().root())->insert(node);
339 scene::Path entitypath(makeReference(GlobalSceneGraph().root()));
340 entitypath.push(makeReference(node.get()));
341 scene::Instance &instance = findInstance(entitypath);
343 if (entityClass->fixedsize || (isModel && !brushesSelected)) {
346 Transformable *transform = Instance_getTransformable(instance);
347 if (transform != 0) {
348 transform->setType(TRANSFORM_PRIMITIVE);
349 transform->setTranslation(origin);
350 transform->freezeTransform();
353 GlobalSelectionSystem().setSelectedAll(false);
355 Instance_setSelected(instance, true);
357 if (g_pGameDescription->mGameType == "doom3") {
358 Node_getEntity(node)->setKeyValue("model", Node_getEntity(node)->getKeyValue("name"));
361 Scene_parentSelectedBrushesToEntity(GlobalSceneGraph(), node);
362 Scene_forEachChildSelectable(SelectableSetSelected(true), instance.path());
365 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
368 if (g_pGameDescription->mGameType == "hl") {
369 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
370 if (string_equal_nocase(name, "light")
371 || string_equal_nocase(name, "light_environment")
372 || string_equal_nocase(name, "light_spot")) {
373 int intensity = g_iLastLightIntensity;
375 if (DoLightIntensityDlg(&intensity) == eIDOK) {
376 g_iLastLightIntensity = intensity;
378 sprintf(buf, "255 255 255 %d", intensity);
379 Node_getEntity(node)->setKeyValue("_light", buf);
382 } else if (string_equal_nocase(name, "light")) {
383 if (g_pGameDescription->mGameType != "doom3") {
384 int intensity = g_iLastLightIntensity;
386 if (DoLightIntensityDlg(&intensity) == eIDOK) {
387 g_iLastLightIntensity = intensity;
389 sprintf(buf, "%d", intensity);
390 Node_getEntity(node)->setKeyValue("light", buf);
392 } else if (brushesSelected) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
393 AABB bounds(Doom3Light_getBounds(workzone));
394 StringOutputStream key(64);
395 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
396 Node_getEntity(node)->setKeyValue("origin", key.c_str());
398 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
399 Node_getEntity(node)->setKeyValue("light_radius", key.c_str());
404 const char *model = misc_model_dialog(MainFrame_getWindow());
406 Node_getEntity(node)->setKeyValue("model", model);
412 bool DoNormalisedColor( Vector3& color ){
413 if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
417 ** scale colors so that at least one component is at 1.0F
420 float largest = 0.0F;
422 if ( color[0] > largest ) {
425 if ( color[1] > largest ) {
428 if ( color[2] > largest ) {
432 if ( largest == 0.0F ) {
439 float scaler = 1.0F / largest;
450 void NormalizeColor(Vector3 &color)
452 // scale colors so that at least one component is at 1.0F
454 float largest = 0.0F;
456 if (color[0] > largest) {
459 if (color[1] > largest) {
462 if (color[2] > largest) {
466 if (largest == 0.0F) {
471 float scaler = 1.0F / largest;
479 void Entity_normalizeColor()
481 if (GlobalSelectionSystem().countSelected() != 0) {
482 const scene::Path &path = GlobalSelectionSystem().ultimateSelected().path();
483 Entity *entity = Node_getEntity(path.top());
486 const char *strColor = entity->getKeyValue("_color");
487 if (!string_empty(strColor)) {
489 if (string_parse_vector3(strColor, rgb)) {
490 g_entity_globals.color_entity = rgb;
491 NormalizeColor(g_entity_globals.color_entity);
494 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
495 g_entity_globals.color_entity[1],
496 g_entity_globals.color_entity[2]);
498 Scene_EntitySetKeyValue_Selected("_color", buffer);
505 void Entity_setColour()
507 if (GlobalSelectionSystem().countSelected() != 0) {
508 bool normalize = false;
509 const scene::Path &path = GlobalSelectionSystem().ultimateSelected().path();
510 Entity *entity = Node_getEntity(path.top());
513 const char *strColor = entity->getKeyValue("_color");
514 if (!string_empty(strColor)) {
516 if (string_parse_vector3(strColor, rgb)) {
517 g_entity_globals.color_entity = rgb;
521 if (g_pGameDescription->mGameType == "doom3") {
525 if (color_dialog(MainFrame_getWindow(), g_entity_globals.color_entity)) {
527 NormalizeColor(g_entity_globals.color_entity);
531 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
532 g_entity_globals.color_entity[1],
533 g_entity_globals.color_entity[2]);
535 Scene_EntitySetKeyValue_Selected("_color", buffer);
541 CopiedString g_strLastModelFolder = "";
543 const char *getLastModelFolderPath()
545 if (g_strLastModelFolder.empty()) {
546 GlobalPreferenceSystem().registerPreference("LastModelFolder", make_property_string(g_strLastModelFolder));
547 if (g_strLastModelFolder.empty()) {
548 StringOutputStream buffer(1024);
549 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
550 if (!file_readable(buffer.c_str())) {
553 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
555 g_strLastModelFolder = buffer.c_str();
558 return g_strLastModelFolder.c_str();
561 const char *misc_model_dialog(ui::Widget parent)
563 const char *filename = parent.file_dialog(TRUE, "Choose Model", getLastModelFolderPath(), ModelLoader::Name());
565 if (filename != NULL) {
566 g_strLastModelFolder = g_path_get_dirname(filename);
567 // use VFS to get the correct relative path
568 const char *relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
569 if (relative == filename) {
570 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
578 static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz)
580 returnz(self.getLightRadii());
583 static void Import(EntityCreator &self, bool value)
585 self.setLightRadii(value);
589 void Entity_constructPreferences(PreferencesPage &page)
592 "Show", "Light Radii",
593 make_property<LightRadii>(GlobalEntityCreator())
597 void Entity_constructPage(PreferenceGroup &group)
599 PreferencesPage page(group.createPage("Entities", "Entity Display Preferences"));
600 Entity_constructPreferences(page);
603 void Entity_registerPreferencesPage()
605 PreferencesDialog_addDisplayPage(makeCallbackF(Entity_constructPage));
609 void Entity_constructMenu(ui::Menu menu)
611 create_menu_item_with_mnemonic(menu, "_Regroup", "GroupSelection");
612 create_menu_item_with_mnemonic(menu, "_Ungroup", "UngroupSelection");
613 create_menu_item_with_mnemonic(menu, "_Connect", "ConnectSelection");
614 create_menu_item_with_mnemonic(menu, "_KillConnect", "KillConnectSelection");
615 create_menu_item_with_mnemonic(menu, "_Select Color...", "EntityColor");
616 create_menu_item_with_mnemonic(menu, "_Normalize Color...", "NormalizeColor");
620 void Entity_Construct()
622 GlobalCommands_insert("EntityColor", makeCallbackF(Entity_setColour), Accelerator('K'));
623 GlobalCommands_insert("NormalizeColor", makeCallbackF(Entity_normalizeColor));
624 GlobalCommands_insert("ConnectSelection", makeCallbackF(Entity_connectSelected),
625 Accelerator('K', (GdkModifierType) GDK_CONTROL_MASK));
626 GlobalCommands_insert("KillConnectSelection", makeCallbackF(Entity_killconnectSelected),
627 Accelerator('K', (GdkModifierType) (GDK_SHIFT_MASK)));
628 GlobalCommands_insert("GroupSelection", makeCallbackF(Entity_groupSelected));
629 GlobalCommands_insert("UngroupSelection", makeCallbackF(Entity_ungroupSelected));
631 GlobalPreferenceSystem().registerPreference("SI_Colors5", make_property_string(g_entity_globals.color_entity));
632 GlobalPreferenceSystem().registerPreference("LastLightIntensity", make_property_string(g_iLastLightIntensity));
634 Entity_registerPreferencesPage();
637 void Entity_Destroy()