llselectmgr.cpp 218 KB
Newer Older
James Cook's avatar
James Cook committed
1 2 3 4
/** 
 * @file llselectmgr.cpp
 * @brief A manager for selected objects and faces.
 *
5
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
6
 * Second Life Viewer Source Code
7 8 9 10 11 12
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
Richard Linden's avatar
Richard Linden committed
13
 *  
14 15 16 17
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
18
 * 
19 20 21
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
 * 
23
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
24
 * $/LicenseInfo$
James Cook's avatar
James Cook committed
25 26 27 28 29
 */

#include "llviewerprecompiledheaders.h"

// file include
30
#define LLSELECTMGR_CPP
James Cook's avatar
James Cook committed
31
#include "llselectmgr.h"
Graham Madarasz's avatar
Graham Madarasz committed
32
#include "llmaterialmgr.h"
James Cook's avatar
James Cook committed
33 34 35

// library includes
#include "llcachename.h"
36
#include "llavatarnamecache.h"
James Cook's avatar
James Cook committed
37 38 39
#include "lldbstrings.h"
#include "lleconomy.h"
#include "llgl.h"
40
#include "llmediaentry.h"
41
#include "llrender.h"
42
#include "llnotifications.h"
James Cook's avatar
James Cook committed
43 44
#include "llpermissions.h"
#include "llpermissionsflags.h"
45
#include "lltrans.h"
James Cook's avatar
James Cook committed
46 47 48
#include "llundo.h"
#include "lluuid.h"
#include "llvolume.h"
49
#include "llcontrolavatar.h"
James Cook's avatar
James Cook committed
50 51 52 53 54 55
#include "message.h"
#include "object_flags.h"
#include "llquaternion.h"

// viewer includes
#include "llagent.h"
56
#include "llagentcamera.h"
57
#include "llattachmentsmgr.h"
James Cook's avatar
James Cook committed
58 59
#include "llviewerwindow.h"
#include "lldrawable.h"
60
#include "llfloaterinspect.h"
James Cook's avatar
James Cook committed
61 62
#include "llfloaterproperties.h"
#include "llfloaterreporter.h"
63
#include "llfloaterreg.h"
James Cook's avatar
James Cook committed
64 65
#include "llfloatertools.h"
#include "llframetimer.h"
66
#include "llfocusmgr.h"
James Cook's avatar
James Cook committed
67 68 69 70
#include "llhudeffecttrail.h"
#include "llhudmanager.h"
#include "llinventorymodel.h"
#include "llmenugl.h"
71
#include "llmeshrepository.h"
72
#include "llmutelist.h"
73
#include "llnotificationsutil.h"
74
#include "llsidepaneltaskinfo.h"
75
#include "llslurl.h"
James Cook's avatar
James Cook committed
76 77 78 79 80 81 82 83 84
#include "llstatusbar.h"
#include "llsurface.h"
#include "lltool.h"
#include "lltooldraganddrop.h"
#include "lltoolmgr.h"
#include "lltoolpie.h"
#include "llui.h"
#include "llviewercamera.h"
#include "llviewercontrol.h"
85
#include "llviewertexturelist.h"
86 87
#include "llviewermedia.h"
#include "llviewermediafocus.h"
James Cook's avatar
James Cook committed
88 89 90 91 92
#include "llviewermenu.h"
#include "llviewerobject.h"
#include "llviewerobjectlist.h"
#include "llviewerregion.h"
#include "llviewerstats.h"
93
#include "llvoavatarself.h"
James Cook's avatar
James Cook committed
94 95
#include "llvovolume.h"
#include "pipeline.h"
96
#include "llviewershadermgr.h"
97
#include "llpanelface.h"
James Cook's avatar
James Cook committed
98 99
#include "llglheaders.h"

100
LLViewerObject* getSelectedParentObject(LLViewerObject *object) ;
James Cook's avatar
James Cook committed
101 102 103 104 105 106 107
//
// Consts
//

const F32 SILHOUETTE_UPDATE_THRESHOLD_SQUARED = 0.02f;
const S32 MAX_SILS_PER_FRAME = 50;
const S32 MAX_OBJECTS_PER_PACKET = 254;
108 109
// For linked sets
const S32 MAX_CHILDREN_PER_TASK = 255;
James Cook's avatar
James Cook committed
110 111 112 113 114

//
// Globals
//

115
//BOOL gDebugSelectMgr = FALSE;
James Cook's avatar
James Cook committed
116

117 118
//BOOL gHideSelectedObjects = FALSE;
//BOOL gAllowSelectAvatar = FALSE;
James Cook's avatar
James Cook committed
119 120 121 122 123 124 125 126 127 128 129 130 131

BOOL LLSelectMgr::sRectSelectInclusive = TRUE;
BOOL LLSelectMgr::sRenderHiddenSelections = TRUE;
BOOL LLSelectMgr::sRenderLightRadius = FALSE;
F32	LLSelectMgr::sHighlightThickness = 0.f;
F32	LLSelectMgr::sHighlightUScale = 0.f;
F32	LLSelectMgr::sHighlightVScale = 0.f;
F32	LLSelectMgr::sHighlightAlpha = 0.f;
F32	LLSelectMgr::sHighlightAlphaTest = 0.f;
F32	LLSelectMgr::sHighlightUAnim = 0.f;
F32	LLSelectMgr::sHighlightVAnim = 0.f;
LLColor4 LLSelectMgr::sSilhouetteParentColor;
LLColor4 LLSelectMgr::sSilhouetteChildColor;
132
LLColor4 LLSelectMgr::sHighlightInspectColor;
James Cook's avatar
James Cook committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
LLColor4 LLSelectMgr::sHighlightParentColor;
LLColor4 LLSelectMgr::sHighlightChildColor;
LLColor4 LLSelectMgr::sContextSilhouetteColor;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// struct LLDeRezInfo
//
// Used to keep track of important derez info. 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

struct LLDeRezInfo
{
	EDeRezDestination mDestination;
	LLUUID mDestinationID;
	LLDeRezInfo(EDeRezDestination dest, const LLUUID& dest_id) :
		mDestination(dest), mDestinationID(dest_id) {}
};

//
// Imports
//


//
// Functions
//

160 161
void LLSelectMgr::cleanupGlobals()
{
162
	LLSelectMgr::getInstance()->clearSelections();
163 164
}

165 166
// Build time optimization, generate this function once here
template class LLSelectMgr* LLSingleton<class LLSelectMgr>::getInstance();
James Cook's avatar
James Cook committed
167 168 169 170
//-----------------------------------------------------------------------------
// LLSelectMgr()
//-----------------------------------------------------------------------------
LLSelectMgr::LLSelectMgr()
171
 : mHideSelectedObjects(LLCachedControl<bool>(gSavedSettings, "HideSelectedObjects", FALSE)),
172
   mRenderHighlightSelections(LLCachedControl<bool>(gSavedSettings, "RenderHighlightSelections", TRUE)),
173 174
   mAllowSelectAvatar( LLCachedControl<bool>(gSavedSettings, "AllowSelectAvatar", FALSE)),
   mDebugSelectMgr(LLCachedControl<bool>(gSavedSettings, "DebugSelectMgr", FALSE))
James Cook's avatar
James Cook committed
175 176
{
	mTEMode = FALSE;
177
	mTextureChannel = LLRender::DIFFUSE_MAP;
James Cook's avatar
James Cook committed
178 179 180 181 182
	mLastCameraPos.clearVec();

	sHighlightThickness	= gSavedSettings.getF32("SelectionHighlightThickness");
	sHighlightUScale	= gSavedSettings.getF32("SelectionHighlightUScale");
	sHighlightVScale	= gSavedSettings.getF32("SelectionHighlightVScale");
183
	sHighlightAlpha		= gSavedSettings.getF32("SelectionHighlightAlpha") * 2;
James Cook's avatar
James Cook committed
184 185 186 187
	sHighlightAlphaTest	= gSavedSettings.getF32("SelectionHighlightAlphaTest");
	sHighlightUAnim		= gSavedSettings.getF32("SelectionHighlightUAnim");
	sHighlightVAnim		= gSavedSettings.getF32("SelectionHighlightVAnim");

188 189 190 191 192 193
	sSilhouetteParentColor =LLUIColorTable::instance().getColor("SilhouetteParentColor");
	sSilhouetteChildColor = LLUIColorTable::instance().getColor("SilhouetteChildColor");
	sHighlightParentColor = LLUIColorTable::instance().getColor("HighlightParentColor");
	sHighlightChildColor = LLUIColorTable::instance().getColor("HighlightChildColor");
	sHighlightInspectColor = LLUIColorTable::instance().getColor("HighlightInspectColor");
	sContextSilhouetteColor = LLUIColorTable::instance().getColor("ContextSilhouetteColor")*0.5f;
James Cook's avatar
James Cook committed
194 195 196 197 198 199 200 201

	sRenderLightRadius = gSavedSettings.getBOOL("RenderLightRadius");
	
	mRenderSilhouettes = TRUE;

	mGridMode = GRID_MODE_WORLD;
	gSavedSettings.setS32("GridMode", (S32)GRID_MODE_WORLD);

202 203 204
	mSelectedObjects = new LLObjectSelection();
	mHoverObjects = new LLObjectSelection();
	mHighlightedObjects = new LLObjectSelection();
205

Tofu Linden's avatar
CID-351  
Tofu Linden committed
206 207
	mForceSelection = FALSE;
	mShowSelection = FALSE;
James Cook's avatar
James Cook committed
208 209 210 211 212 213 214
}


//-----------------------------------------------------------------------------
// ~LLSelectMgr()
//-----------------------------------------------------------------------------
LLSelectMgr::~LLSelectMgr()
215 216 217 218 219
{
	clearSelections();
}

void LLSelectMgr::clearSelections()
James Cook's avatar
James Cook committed
220
{
221 222 223
	mHoverObjects->deleteAllNodes();
	mSelectedObjects->deleteAllNodes();
	mHighlightedObjects->deleteAllNodes();
James Cook's avatar
James Cook committed
224 225
	mRectSelectedObjects.clear();
	mGridObjects.deleteAllNodes();
226 227

	LLPipeline::setRenderHighlightTextureChannel(LLRender::DIFFUSE_MAP);
James Cook's avatar
James Cook committed
228 229
}

230
void LLSelectMgr::update()
James Cook's avatar
James Cook committed
231
{
232 233
	mSelectedObjects->cleanupNodes();
}
234

235 236
void LLSelectMgr::updateEffects()
{
237
	//keep reference grid objects active
238
	struct f : public LLSelectedObjectFunctor
239
	{
240
		virtual bool apply(LLViewerObject* object)
241
		{
242 243 244 245 246 247
			LLDrawable* drawable = object->mDrawable;
			if (drawable)
			{
				gPipeline.markMoved(drawable);
			}
			return true;
248
		}
249 250
	} func;
	mGridObjects.applyToObjects(&func);
251

James Cook's avatar
James Cook committed
252 253
	if (mEffectsTimer.getElapsedTimeF32() > 1.f)
	{
254
		mSelectedObjects->updateEffects();
James Cook's avatar
James Cook committed
255 256 257 258
		mEffectsTimer.reset();
	}
}

259 260 261
void LLSelectMgr::overrideObjectUpdates()
{
	//override any position updates from simulator on objects being edited
262
	struct f : public LLSelectedNodeFunctor
263
	{
264
		virtual bool apply(LLSelectNode* selectNode)
265
		{
266
			LLViewerObject* object = selectNode->getObject();
267
			if (object && object->permMove() && !object->isPermanentEnforced())
268
			{
269 270 271 272 273 274 275 276 277 278 279 280
				if (!selectNode->mLastPositionLocal.isExactlyZero())
				{
					object->setPosition(selectNode->mLastPositionLocal);
				}
				if (selectNode->mLastRotation != LLQuaternion())
				{
					object->setRotation(selectNode->mLastRotation);
				}
				if (!selectNode->mLastScale.isExactlyZero())
				{
					object->setScale(selectNode->mLastScale);
				}
281
			}
282
			return true;
283
		}
284 285
	} func;
	getSelection()->applyToNodes(&func);
286 287
}

James Cook's avatar
James Cook committed
288 289 290
//-----------------------------------------------------------------------------
// Select just the object, not any other group members.
//-----------------------------------------------------------------------------
291
LLObjectSelectionHandle LLSelectMgr::selectObjectOnly(LLViewerObject* object, S32 face)
James Cook's avatar
James Cook committed
292 293 294
{
	llassert( object );

295 296 297
	//remember primary object
	mSelectedObjects->mPrimaryObject = object;

James Cook's avatar
James Cook committed
298 299 300 301 302
	// Don't add an object that is already in the list
	if (object->isSelected() ) {
		// make sure point at position is updated
		updatePointAt();
		gEditMenuHandler = this;
303
		return NULL;
James Cook's avatar
James Cook committed
304 305 306 307 308
	}

	if (!canSelectObject(object))
	{
		//make_ui_sound("UISndInvalidOp");
309
		return NULL;
James Cook's avatar
James Cook committed
310 311
	}

Richard Linden's avatar
Richard Linden committed
312
	// LL_INFOS() << "Adding object to selected object list" << LL_ENDL;
James Cook's avatar
James Cook committed
313 314 315 316 317 318 319

	// Place it in the list and tag it.
	// This will refresh dialogs.
	addAsIndividual(object, face);

	// Stop the object from moving (this anticipates changes on the
	// simulator in LLTask::userSelect)
320
	// *FIX: shouldn't zero out these either
James Cook's avatar
James Cook committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	object->setVelocity(LLVector3::zero);
	object->setAcceleration(LLVector3::zero);
	//object->setAngularVelocity(LLVector3::zero);
	object->resetRot();

	// Always send to simulator, so you get a copy of the 
	// permissions structure back.
	gMessageSystem->newMessageFast(_PREHASH_ObjectSelect);
	gMessageSystem->nextBlockFast(_PREHASH_AgentData);
	gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
	gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
	gMessageSystem->nextBlockFast(_PREHASH_ObjectData);
	gMessageSystem->addU32Fast(_PREHASH_ObjectLocalID, object->getLocalID() );
	LLViewerRegion* regionp = object->getRegion();
	gMessageSystem->sendReliable( regionp->getHost());

	updatePointAt();
	updateSelectionCenter();
	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);

	// have selection manager handle edit menu immediately after 
	// user selects an object
343
	if (mSelectedObjects->getObjectCount())
James Cook's avatar
James Cook committed
344 345 346
	{
		gEditMenuHandler = this;
	}
347 348

	return mSelectedObjects;
James Cook's avatar
James Cook committed
349 350 351 352 353
}

//-----------------------------------------------------------------------------
// Select the object, parents and children.
//-----------------------------------------------------------------------------
354
LLObjectSelectionHandle LLSelectMgr::selectObjectAndFamily(LLViewerObject* obj, BOOL add_to_end, BOOL ignore_select_owned)
James Cook's avatar
James Cook committed
355 356 357
{
	llassert( obj );

358 359 360
	//remember primary object
	mSelectedObjects->mPrimaryObject = obj;

James Cook's avatar
James Cook committed
361 362 363 364 365 366 367
	// This may be incorrect if things weren't family selected before... - djs 07/08/02
	// Don't add an object that is already in the list
	if (obj->isSelected() ) 
	{
		// make sure pointat position is updated
		updatePointAt();
		gEditMenuHandler = this;
368
		return NULL;
James Cook's avatar
James Cook committed
369 370
	}

371
	if (!canSelectObject(obj,ignore_select_owned))
James Cook's avatar
James Cook committed
372 373
	{
		//make_ui_sound("UISndInvalidOp");
374
		return NULL;
James Cook's avatar
James Cook committed
375 376 377 378 379 380
	}

	// Since we're selecting a family, start at the root, but
	// don't include an avatar.
	LLViewerObject* root = obj;
	
381
	while(!root->isAvatar() && root->getParent())
James Cook's avatar
James Cook committed
382 383 384 385 386 387 388 389 390 391
	{
		LLViewerObject* parent = (LLViewerObject*)root->getParent();
		if (parent->isAvatar())
		{
			break;
		}
		root = parent;
	}

	// Collect all of the objects
392
	std::vector<LLViewerObject*> objects;
James Cook's avatar
James Cook committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

	root->addThisAndNonJointChildren(objects);
	addAsFamily(objects, add_to_end);

	updateSelectionCenter();
	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	updatePointAt();

	dialog_refresh_all();

	// Always send to simulator, so you get a copy of the permissions
	// structure back.
	sendSelect();

	// Stop the object from moving (this anticipates changes on the
	// simulator in LLTask::userSelect)
	root->setVelocity(LLVector3::zero);
	root->setAcceleration(LLVector3::zero);
	//root->setAngularVelocity(LLVector3::zero);
	root->resetRot();

	// leave component mode
415
	if (gSavedSettings.getBOOL("EditLinkedParts"))
James Cook's avatar
James Cook committed
416
	{
417
		gSavedSettings.setBOOL("EditLinkedParts", FALSE);
James Cook's avatar
James Cook committed
418 419 420 421 422
		promoteSelectionToRoot();
	}

	// have selection manager handle edit menu immediately after 
	// user selects an object
423
	if (mSelectedObjects->getObjectCount())
James Cook's avatar
James Cook committed
424 425 426
	{
		gEditMenuHandler = this;
	}
427 428

	return mSelectedObjects;
James Cook's avatar
James Cook committed
429 430 431 432 433
}

//-----------------------------------------------------------------------------
// Select the object, parents and children.
//-----------------------------------------------------------------------------
434 435
LLObjectSelectionHandle LLSelectMgr::selectObjectAndFamily(const std::vector<LLViewerObject*>& object_list,
														   BOOL send_to_sim)
James Cook's avatar
James Cook committed
436 437
{
	// Collect all of the objects, children included
438
	std::vector<LLViewerObject*> objects;
James Cook's avatar
James Cook committed
439

440 441 442
	//clear primary object (no primary object)
	mSelectedObjects->mPrimaryObject = NULL;

443 444 445 446 447
	if (object_list.size() < 1)
	{
		return NULL;
	}
	
James Cook's avatar
James Cook committed
448 449
	// NOTE -- we add the objects in REVERSE ORDER 
	// to preserve the order in the mSelectedObjects list
450 451
	for (std::vector<LLViewerObject*>::const_reverse_iterator riter = object_list.rbegin();
		 riter != object_list.rend(); ++riter)
James Cook's avatar
James Cook committed
452
	{
453
		LLViewerObject *object = *riter;
James Cook's avatar
James Cook committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

		llassert( object );

		if (!canSelectObject(object)) continue;

		object->addThisAndNonJointChildren(objects);
		addAsFamily(objects);

		// Stop the object from moving (this anticipates changes on the
		// simulator in LLTask::userSelect)
		object->setVelocity(LLVector3::zero);
		object->setAcceleration(LLVector3::zero);
		//object->setAngularVelocity(LLVector3::zero);
		object->resetRot();
	}

	updateSelectionCenter();
	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	updatePointAt();
	dialog_refresh_all();

	// Almost always send to simulator, so you get a copy of the permissions
	// structure back.
	// JC: The one case where you don't want to do this is if you're selecting
	// all the objects on a sim.
	if (send_to_sim)
	{
		sendSelect();
	}

	// leave component mode
485
	if (gSavedSettings.getBOOL("EditLinkedParts"))
James Cook's avatar
James Cook committed
486
	{		
487
		gSavedSettings.setBOOL("EditLinkedParts", FALSE);
James Cook's avatar
James Cook committed
488 489 490 491 492
		promoteSelectionToRoot();
	}

	// have selection manager handle edit menu immediately after 
	// user selects an object
493
	if (mSelectedObjects->getObjectCount())
James Cook's avatar
James Cook committed
494 495 496
	{
		gEditMenuHandler = this;
	}
497 498

	return mSelectedObjects;
James Cook's avatar
James Cook committed
499 500 501 502 503 504
}

// Use for when the simulator kills an object.  This version also
// handles informing the current tool of the object's deletion.
//
// Caller needs to call dialog_refresh_all if necessary.
505
BOOL LLSelectMgr::removeObjectFromSelections(const LLUUID &id)
James Cook's avatar
James Cook committed
506 507 508 509
{
	BOOL object_found = FALSE;
	LLTool *tool = NULL;

510 511 512 513 514 515 516 517
	tool = LLToolMgr::getInstance()->getCurrentTool();

	// It's possible that the tool is editing an object that is not selected
	LLViewerObject* tool_editing_object = tool->getEditingObject();
	if( tool_editing_object && tool_editing_object->mID == id)
	{
		tool->stopEditing();
		object_found = TRUE;
James Cook's avatar
James Cook committed
518 519 520 521 522
	}

	// Iterate through selected objects list and kill the object
	if( !object_found )
	{
523 524
		for (LLObjectSelection::iterator iter = getSelection()->begin();
			 iter != getSelection()->end(); )
James Cook's avatar
James Cook committed
525
		{
526 527 528
			LLObjectSelection::iterator curiter = iter++;
			LLViewerObject* object = (*curiter)->getObject();
			if (object->mID == id)
James Cook's avatar
James Cook committed
529
			{
530
				if (tool)
James Cook's avatar
James Cook committed
531 532 533 534 535
				{
					tool->stopEditing();
				}

				// lose the selection, don't tell simulator, it knows
536 537 538 539
				deselectObjectAndFamily(object, FALSE);
				object_found = TRUE;
				break; // must break here, may have removed multiple objects from list
			}
540
			else if (object->isAvatar() && object->getParent() && ((LLViewerObject*)object->getParent())->mID == id)
541 542 543 544 545
			{
				// It's possible the item being removed has an avatar sitting on it
				// So remove the avatar that is sitting on the object.
				deselectObjectAndFamily(object, FALSE);
				break; // must break here, may have removed multiple objects from list
James Cook's avatar
James Cook committed
546 547 548 549 550 551 552
			}
		}
	}

	return object_found;
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
bool LLSelectMgr::linkObjects()
{
	if (!LLSelectMgr::getInstance()->selectGetAllRootsValid())
	{
		LLNotificationsUtil::add("UnableToLinkWhileDownloading");
		return true;
	}

	S32 object_count = LLSelectMgr::getInstance()->getSelection()->getObjectCount();
	if (object_count > MAX_CHILDREN_PER_TASK + 1)
	{
		LLSD args;
		args["COUNT"] = llformat("%d", object_count);
		int max = MAX_CHILDREN_PER_TASK+1;
		args["MAX"] = llformat("%d", max);
		LLNotificationsUtil::add("UnableToLinkObjects", args);
		return true;
	}

	if (LLSelectMgr::getInstance()->getSelection()->getRootObjectCount() < 2)
	{
		LLNotificationsUtil::add("CannotLinkIncompleteSet");
		return true;
	}

	if (!LLSelectMgr::getInstance()->selectGetRootsModify())
	{
		LLNotificationsUtil::add("CannotLinkModify");
		return true;
	}

584
	if (!LLSelectMgr::getInstance()->selectGetRootsNonPermanentEnforced())
585 586 587 588 589
	{
		LLNotificationsUtil::add("CannotLinkPermanent");
		return true;
	}

590 591 592 593 594 595 596 597 598 599 600
	LLUUID owner_id;
	std::string owner_name;
	if (!LLSelectMgr::getInstance()->selectGetOwner(owner_id, owner_name))
	{
		// we don't actually care if you're the owner, but novices are
		// the most likely to be stumped by this one, so offer the
		// easiest and most likely solution.
		LLNotificationsUtil::add("CannotLinkDifferentOwners");
		return true;
	}

601 602 603 604 605 606
	if (!LLSelectMgr::getInstance()->selectGetSameRegion())
	{
		LLNotificationsUtil::add("CannotLinkAcrossRegions");
		return true;
	}

607 608 609 610 611 612 613
	LLSelectMgr::getInstance()->sendLink();

	return true;
}

bool LLSelectMgr::unlinkObjects()
{
614
	S32 min_objects_for_confirm = gSavedSettings.getS32("MinObjectsForUnlinkConfirm");
615 616 617
	S32 unlink_object_count = mSelectedObjects->getObjectCount(); // clears out nodes with NULL objects
	if (unlink_object_count >= min_objects_for_confirm
		&& unlink_object_count > mSelectedObjects->getRootObjectCount())
618
	{
619 620 621
		// total count > root count means that there are childer inside and that there are linksets that will be unlinked
		LLNotificationsUtil::add("ConfirmUnlink", LLSD(), LLSD(), boost::bind(&LLSelectMgr::confirmUnlinkObjects, this, _1, _2));
		return true;
622 623
	}

624 625 626 627
	LLSelectMgr::getInstance()->sendDelink();
	return true;
}

628 629 630 631 632 633 634 635 636 637 638 639 640
void LLSelectMgr::confirmUnlinkObjects(const LLSD& notification, const LLSD& response)
{
	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
	// if Cancel pressed
	if (option == 1)
	{
		return;
	}

	LLSelectMgr::getInstance()->sendDelink();
	return;
}

641 642 643 644 645 646 647 648
// in order to link, all objects must have the same owner, and the
// agent must have the ability to modify all of the objects. However,
// we're not answering that question with this method. The question
// we're answering is: does the user have a reasonable expectation
// that a link operation should work? If so, return true, false
// otherwise. this allows the handle_link method to more finely check
// the selection and give an error message when the uer has a
// reasonable expectation for the link to work, but it will fail.
649
//
650 651 652
// For animated objects, there's additional check that if the
// selection includes at least one animated object, the total mesh
// triangle count cannot exceed the designated limit.
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
bool LLSelectMgr::enableLinkObjects()
{
	bool new_value = false;
	// check if there are at least 2 objects selected, and that the
	// user can modify at least one of the selected objects.

	// in component mode, can't link
	if (!gSavedSettings.getBOOL("EditLinkedParts"))
	{
		if(LLSelectMgr::getInstance()->selectGetAllRootsValid() && LLSelectMgr::getInstance()->getSelection()->getRootObjectCount() >= 2)
		{
			struct f : public LLSelectedObjectFunctor
			{
				virtual bool apply(LLViewerObject* object)
				{
668 669 670
					LLViewerObject *root_object = (object == NULL) ? NULL : object->getRootEdit();
					return object->permModify() && !object->isPermanentEnforced() &&
						((root_object == NULL) || !root_object->isPermanentEnforced());
671 672 673 674 675 676
				}
			} func;
			const bool firstonly = true;
			new_value = LLSelectMgr::getInstance()->getSelection()->applyToRootObjects(&func, firstonly);
		}
	}
677
    if (!LLSelectMgr::getInstance()->getSelection()->checkAnimatedObjectLinkable())
678 679 680
    {
        new_value = false;
    }
681 682 683 684 685 686
	return new_value;
}

bool LLSelectMgr::enableUnlinkObjects()
{
	LLViewerObject* first_editable_object = LLSelectMgr::getInstance()->getSelection()->getFirstEditableObject();
687
	LLViewerObject *root_object = (first_editable_object == NULL) ? NULL : first_editable_object->getRootEdit();
688 689 690

	bool new_value = LLSelectMgr::getInstance()->selectGetAllRootsValid() &&
		first_editable_object &&
691 692
		!first_editable_object->isAttachment() && !first_editable_object->isPermanentEnforced() &&
		((root_object == NULL) || !root_object->isPermanentEnforced());
693 694 695 696

	return new_value;
}

697
void LLSelectMgr::deselectObjectAndFamily(LLViewerObject* object, BOOL send_to_sim, BOOL include_entire_object)
James Cook's avatar
James Cook committed
698 699 700 701 702 703
{
	// bail if nothing selected or if object wasn't selected in the first place
	if(!object) return;
	if(!object->isSelected()) return;

	// Collect all of the objects, and remove them
704
	std::vector<LLViewerObject*> objects;
705 706 707 708 709 710 711

	if (include_entire_object)
	{
		// Since we're selecting a family, start at the root, but
		// don't include an avatar.
		LLViewerObject* root = object;
	
712
		while(!root->isAvatar() && root->getParent())
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
		{
			LLViewerObject* parent = (LLViewerObject*)root->getParent();
			if (parent->isAvatar())
			{
				break;
			}
			root = parent;
		}
	
		object = root;
	}
	else
	{
		object = (LLViewerObject*)object->getRoot();
	}

James Cook's avatar
James Cook committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742
	object->addThisAndAllChildren(objects);
	remove(objects);

	if (!send_to_sim) return;

	//-----------------------------------------------------------
	// Inform simulator of deselection
	//-----------------------------------------------------------
	LLViewerRegion* regionp = object->getRegion();

	BOOL start_new_message = TRUE;
	S32 select_count = 0;

	LLMessageSystem* msg = gMessageSystem;
743
	for (U32 i = 0; i < objects.size(); i++)
James Cook's avatar
James Cook committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	{
		if (start_new_message)
		{
			msg->newMessageFast(_PREHASH_ObjectDeselect);
			msg->nextBlockFast(_PREHASH_AgentData);
			msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
			msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
			select_count++;
			start_new_message = FALSE;
		}

		msg->nextBlockFast(_PREHASH_ObjectData);
		msg->addU32Fast(_PREHASH_ObjectLocalID, (objects[i])->getLocalID());
		select_count++;

759 760 761 762
		// Zap the angular velocity, as the sim will set it to zero
		objects[i]->setAngularVelocity( 0,0,0 );
		objects[i]->setVelocity( 0,0,0 );

763
		if(msg->isSendFull(NULL) || select_count >= MAX_OBJECTS_PER_PACKET)
James Cook's avatar
James Cook committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
		{
			msg->sendReliable(regionp->getHost() );
			select_count = 0;
			start_new_message = TRUE;
		}
	}

	if (!start_new_message)
	{
		msg->sendReliable(regionp->getHost() );
	}

	updatePointAt();
	updateSelectionCenter();
}

void LLSelectMgr::deselectObjectOnly(LLViewerObject* object, BOOL send_to_sim)
{
	// bail if nothing selected or if object wasn't selected in the first place
	if (!object) return;
	if (!object->isSelected() ) return;

786 787 788 789
	// Zap the angular velocity, as the sim will set it to zero
	object->setAngularVelocity( 0,0,0 );
	object->setVelocity( 0,0,0 );

James Cook's avatar
James Cook committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	if (send_to_sim)
	{
		LLViewerRegion* region = object->getRegion();
		gMessageSystem->newMessageFast(_PREHASH_ObjectDeselect);
		gMessageSystem->nextBlockFast(_PREHASH_AgentData);
		gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
		gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
		gMessageSystem->nextBlockFast(_PREHASH_ObjectData);
		gMessageSystem->addU32Fast(_PREHASH_ObjectLocalID, object->getLocalID() );
		gMessageSystem->sendReliable(region->getHost());
	}

	// This will refresh dialogs.
	remove( object );

	updatePointAt();
	updateSelectionCenter();
}


//-----------------------------------------------------------------------------
// addAsFamily
//-----------------------------------------------------------------------------

814
void LLSelectMgr::addAsFamily(std::vector<LLViewerObject*>& objects, BOOL add_to_end)
James Cook's avatar
James Cook committed
815
{
816 817
	for (std::vector<LLViewerObject*>::iterator iter = objects.begin();
		 iter != objects.end(); ++iter)
James Cook's avatar
James Cook committed
818
	{
819
		LLViewerObject* objectp = *iter;
James Cook's avatar
James Cook committed
820 821 822
		
		// Can't select yourself
		if (objectp->mID == gAgentID
823
			&& !LLSelectMgr::getInstance()->mAllowSelectAvatar)
James Cook's avatar
James Cook committed
824 825 826 827 828 829
		{
			continue;
		}

		if (!objectp->isSelected())
		{
830
			LLSelectNode *nodep = new LLSelectNode(objectp, TRUE);
James Cook's avatar
James Cook committed
831 832
			if (add_to_end)
			{
833
				mSelectedObjects->addNodeAtEnd(nodep);
James Cook's avatar
James Cook committed
834 835 836
			}
			else
			{
837
				mSelectedObjects->addNode(nodep);
James Cook's avatar
James Cook committed
838 839 840 841 842 843
			}
			objectp->setSelected(TRUE);

			if (objectp->getNumTEs() > 0)
			{
				nodep->selectAllTEs(TRUE);
844
				objectp->setAllTESelected(true);
James Cook's avatar
James Cook committed
845 846 847 848 849 850 851 852 853 854
			}
			else
			{
				// object has no faces, so don't mess with faces
			}
		}
		else
		{
			// we want this object to be selected for real
			// so clear transient flag
855
			LLSelectNode* select_node = mSelectedObjects->findNode(objectp);
James Cook's avatar
James Cook committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
			if (select_node)
			{
				select_node->setTransient(FALSE);
			}
		}
	}
	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
}

//-----------------------------------------------------------------------------
// addAsIndividual() - a single object, face, etc
//-----------------------------------------------------------------------------
void LLSelectMgr::addAsIndividual(LLViewerObject *objectp, S32 face, BOOL undoable)
{
	// check to see if object is already in list
871
	LLSelectNode *nodep = mSelectedObjects->findNode(objectp);
James Cook's avatar
James Cook committed
872

873 874 875 876
	// Reset (in anticipation of being set to an appropriate value by panel refresh, if they're up)
	//
	setTextureChannel(LLRender::DIFFUSE_MAP);

James Cook's avatar
James Cook committed
877 878 879 880
	// if not in list, add it
	if (!nodep)
	{
		nodep = new LLSelectNode(objectp, TRUE);
881
		mSelectedObjects->addNode(nodep);
882
		llassert_always(nodep->getObject());
James Cook's avatar
James Cook committed
883 884 885 886 887 888
	}
	else
	{
		// make this a full-fledged selection
		nodep->setTransient(FALSE);
		// Move it to the front of the list
889
		mSelectedObjects->moveNodeToFront(nodep);
James Cook's avatar
James Cook committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
	}

	// Make sure the object is tagged as selected
	objectp->setSelected( TRUE );

	// And make sure we don't consider it as part of a family
	nodep->mIndividualSelection = TRUE;

	// Handle face selection
	if (objectp->getNumTEs() <= 0)
	{
		// object has no faces, so don't do anything
	}
	else if (face == SELECT_ALL_TES)
	{
		nodep->selectAllTEs(TRUE);
906
		objectp->setAllTESelected(true);
James Cook's avatar
James Cook committed
907 908 909 910
	}
	else if (0 <= face && face < SELECT_MAX_TES)
	{
		nodep->selectTE(face, TRUE);
911
		objectp->setTESelected(face, true);
James Cook's avatar
James Cook committed
912 913 914
	}
	else
	{
Richard Linden's avatar
Richard Linden committed
915
		LL_ERRS() << "LLSelectMgr::add face " << face << " out-of-range" << LL_ENDL;
James Cook's avatar
James Cook committed
916 917 918 919 920 921 922 923 924
		return;
	}

	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	updateSelectionCenter();
	dialog_refresh_all();
}


925
LLObjectSelectionHandle LLSelectMgr::setHoverObject(LLViewerObject *objectp, S32 face)
James Cook's avatar
James Cook committed
926 927 928
{
	if (!objectp)
	{
929
		mHoverObjects->deleteAllNodes();
930
		return NULL;
James Cook's avatar
James Cook committed
931 932 933 934 935
	}

	// Can't select yourself
	if (objectp->mID == gAgentID)
	{
936
		mHoverObjects->deleteAllNodes();
937
		return NULL;
James Cook's avatar
James Cook committed
938 939 940 941 942
	}

	// Can't select land
	if (objectp->getPCode() == LLViewerObject::LL_VO_SURFACE_PATCH)
	{
943
		mHoverObjects->deleteAllNodes();
944
		return NULL;
James Cook's avatar
James Cook committed
945 946
	}

947 948
	mHoverObjects->mPrimaryObject = objectp; 

James Cook's avatar
James Cook committed
949 950
	objectp = objectp->getRootEdit();

951 952 953
	// is the requested object the same as the existing hover object root?
	// NOTE: there is only ever one linked set in mHoverObjects
	if (mHoverObjects->getFirstRootObject() != objectp) 
James Cook's avatar
James Cook committed
954
	{
955

956 957 958 959 960
		// Collect all of the objects
		std::vector<LLViewerObject*> objects;
		objectp = objectp->getRootEdit();
		objectp->addThisAndNonJointChildren(objects);

961
		mHoverObjects->deleteAllNodes();
962 963 964 965
		for (std::vector<LLViewerObject*>::iterator iter = objects.begin();
			 iter != objects.end(); ++iter)
		{
			LLViewerObject* cur_objectp = *iter;
Xiaohong Bao's avatar
Xiaohong Bao committed
966 967 968 969
			if(!cur_objectp || cur_objectp->isDead())
			{
				continue;
			}
970 971 972 973
			LLSelectNode* nodep = new LLSelectNode(cur_objectp, FALSE);
			nodep->selectTE(face, TRUE);
			mHoverObjects->addNodeAtEnd(nodep);
		}
974

975
		requestObjectPropertiesFamily(objectp);
James Cook's avatar
James Cook committed
976 977
	}

978
	return mHoverObjects;
James Cook's avatar
James Cook committed
979 980 981 982
}

LLSelectNode *LLSelectMgr::getHoverNode()
{
983
	return mHoverObjects->getFirstRootNode();
James Cook's avatar
James Cook committed
984 985
}

986 987 988 989 990
LLSelectNode *LLSelectMgr::getPrimaryHoverNode()
{
	return mHoverObjects->mSelectNodeMap[mHoverObjects->mPrimaryObject];
}

James Cook's avatar
James Cook committed
991 992 993 994 995 996 997 998 999 1000 1001 1002
void LLSelectMgr::highlightObjectOnly(LLViewerObject* objectp)
{
	if (!objectp)
	{
		return;
	}

	if (objectp->getPCode() != LL_PCODE_VOLUME)
	{
		return;
	}
	
1003
	if ((gSavedSettings.getBOOL("SelectOwnedOnly") && !objectp->permYouOwner()) 
1004
		|| (gSavedSettings.getBOOL("SelectMovableOnly") && (!objectp->permMove() ||  objectp->isPermanentEnforced())))
James Cook's avatar
James Cook committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	{
		// only select my own objects
		return;
	}

	mRectSelectedObjects.insert(objectp);
}

void LLSelectMgr::highlightObjectAndFamily(LLViewerObject* objectp)
{
	if (!objectp)
	{
		return;
	}

	LLViewerObject* root_obj = (LLViewerObject*)objectp->getRoot();

	highlightObjectOnly(root_obj);

1024 1025 1026
	LLViewerObject::const_child_list_t& child_list = root_obj->getChildren();
	for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
		 iter != child_list.end(); iter++)
James Cook's avatar
James Cook committed
1027
	{
1028 1029
		LLViewerObject* child = *iter;
		highlightObjectOnly(child);
James Cook's avatar
James Cook committed
1030 1031 1032 1033
	}
}

// Note that this ignores the "select owned only" flag
1034 1035
// It's also more efficient than calling the single-object version over and over.
void LLSelectMgr::highlightObjectAndFamily(const std::vector<LLViewerObject*>& objects)
James Cook's avatar
James Cook committed
1036
{
1037 1038
	for (std::vector<LLViewerObject*>::const_iterator iter1 = objects.begin();
		 iter1 != objects.end(); ++iter1)
James Cook's avatar
James Cook committed
1039
	{
1040
		LLViewerObject* object = *iter1;
James Cook's avatar
James Cook committed
1041

1042 1043 1044 1045 1046 1047 1048 1049
		if (!object)
		{
			continue;
		}
		if (object->getPCode() != LL_PCODE_VOLUME)
		{
			continue;
		}
James Cook's avatar
James Cook committed
1050 1051 1052 1053

		LLViewerObject* root = (LLViewerObject*)object->getRoot();
		mRectSelectedObjects.insert(root);

1054 1055 1056
		LLViewerObject::const_child_list_t& child_list = root->getChildren();
		for (LLViewerObject::child_list_t::const_iterator iter2 = child_list.begin();
			 iter2 != child_list.end(); iter2++)
James Cook's avatar
James Cook committed
1057
		{
1058
			LLViewerObject* child = *iter2;
James Cook's avatar
James Cook committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
			mRectSelectedObjects.insert(child);
		}
	}
}

void LLSelectMgr::unhighlightObjectOnly(LLViewerObject* objectp)
{
	if (!objectp || (objectp->getPCode() != LL_PCODE_VOLUME))
	{
		return;
	}

	mRectSelectedObjects.erase(objectp);
}

void LLSelectMgr::unhighlightObjectAndFamily(LLViewerObject* objectp)
{
	if (!objectp)
	{
		return;
	}

	LLViewerObject* root_obj = (LLViewerObject*)objectp->getRoot();

	unhighlightObjectOnly(root_obj);

1085 1086 1087
	LLViewerObject::const_child_list_t& child_list = root_obj->getChildren();
	for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
		 iter != child_list.end(); iter++)
James Cook's avatar
James Cook committed
1088
	{
1089
		LLViewerObject* child = *iter;
1090
		unhighlightObjectOnly(child);
James Cook's avatar
James Cook committed
1091 1092 1093 1094 1095 1096 1097
	}
}


void LLSelectMgr::unhighlightAll()
{
	mRectSelectedObjects.clear();
1098
	mHighlightedObjects->deleteAllNodes();
James Cook's avatar
James Cook committed
1099 1100
}

1101
LLObjectSelectionHandle LLSelectMgr::selectHighlightedObjects()
James Cook's avatar
James Cook committed
1102
{
1103
	if (!mHighlightedObjects->getNumNodes())
James Cook's avatar
James Cook committed
1104
	{
1105
		return NULL;
James Cook's avatar
James Cook committed
1106 1107
	}

1108 1109 1110
	//clear primary object
	mSelectedObjects->mPrimaryObject = NULL;

1111 1112
	for (LLObjectSelection::iterator iter = getHighlightedObjects()->begin();
		 iter != getHighlightedObjects()->end(); )
James Cook's avatar
James Cook committed
1113
	{
1114 1115 1116
		LLObjectSelection::iterator curiter = iter++;
	
		LLSelectNode *nodep = *curiter;
James Cook's avatar
James Cook committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
		LLViewerObject* objectp = nodep->getObject();

		if (!canSelectObject(objectp))
		{
			continue;
		}

		// already selected
		if (objectp->isSelected())
		{
			continue;
		}

		LLSelectNode* new_nodep = new LLSelectNode(*nodep);
1131
		mSelectedObjects->addNode(new_nodep);
James Cook's avatar
James Cook committed
1132 1133 1134

		// flag this object as selected
		objectp->setSelected(TRUE);
1135
		objectp->setAllTESelected(true);
James Cook's avatar
James Cook committed
1136

1137
		mSelectedObjects->mSelectType = getSelectTypeForObject(objectp);
James Cook's avatar
James Cook committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

		// request properties on root objects
		if (objectp->isRootEdit())
		{
			requestObjectPropertiesFamily(objectp);
		}
	}

	// pack up messages to let sim know these objects are selected
	sendSelect();
	unhighlightAll();
	updateSelectionCenter();
	saveSelectedObjectTransform(SELECT_ACTION_TYPE_PICK);
	updatePointAt();

1153
	if (mSelectedObjects->getObjectCount())
James Cook's avatar
James Cook committed
1154 1155 1156
	{
		gEditMenuHandler = this;
	}
1157 1158

	return mSelectedObjects;
James Cook's avatar
James Cook committed
1159 1160 1161 1162
}

void LLSelectMgr::deselectHighlightedObjects()
{
1163
	BOOL select_linked_set = !gSavedSettings.getBOOL("EditLinkedParts");
James Cook's avatar
James Cook committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	for (std::set<LLPointer<LLViewerObject> >::iterator iter = mRectSelectedObjects.begin();
		 iter != mRectSelectedObjects.end(); iter++)
	{
		LLViewerObject *objectp = *iter;
		if (!select_linked_set)
		{
			deselectObjectOnly(objectp);
		}
		else
		{
			LLViewerObject* root_object = (LLViewerObject*)objectp->getRoot();
			if (root_object->isSelected())
			{
				deselectObjectAndFamily(root_object);
			}
		}
	}