PageRenderTime 48ms CodeModel.GetById 2ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lltoolfocus.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 462 lines | 330 code | 71 blank | 61 comment | 88 complexity | f7cee15745ff55f860b31e5255938917 MD5 | raw file
  1/** 
  2 * @file lltoolfocus.cpp
  3 * @brief A tool to set the build focus point.
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * 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$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29// File includes
 30#include "lltoolfocus.h" 
 31
 32// Library includes
 33#include "v3math.h"
 34#include "llfontgl.h"
 35#include "llui.h"
 36
 37// Viewer includes
 38#include "llagent.h"
 39#include "llagentcamera.h"
 40#include "llbutton.h"
 41#include "llviewercontrol.h"
 42#include "lldrawable.h"
 43#include "lltooltip.h"
 44#include "llhudmanager.h"
 45#include "llfloatertools.h"
 46#include "llselectmgr.h"
 47#include "llstatusbar.h"
 48#include "lltoolmgr.h"
 49#include "llviewercamera.h"
 50#include "llviewerobject.h"
 51#include "llviewerwindow.h"
 52#include "llvoavatarself.h"
 53#include "llmorphview.h"
 54#include "llfloaterreg.h"
 55#include "llfloatercamera.h"
 56
 57// Globals
 58BOOL gCameraBtnZoom = TRUE;
 59BOOL gCameraBtnOrbit = FALSE;
 60BOOL gCameraBtnPan = FALSE;
 61
 62const S32 SLOP_RANGE = 4;
 63const F32 FOCUS_OFFSET_FACTOR = 1.f;
 64
 65//
 66// Camera - shared functionality
 67//
 68
 69LLToolCamera::LLToolCamera()
 70:	LLTool(std::string("Camera")),
 71	mAccumX(0),
 72	mAccumY(0),
 73	mMouseDownX(0),
 74	mMouseDownY(0),
 75	mOutsideSlopX(FALSE),
 76	mOutsideSlopY(FALSE),
 77	mValidClickPoint(FALSE),
 78	mMouseSteering(FALSE),
 79	mMouseUpX(0),
 80	mMouseUpY(0),
 81	mMouseUpMask(MASK_NONE)
 82{ }
 83
 84
 85LLToolCamera::~LLToolCamera()
 86{ }
 87
 88// virtual
 89void LLToolCamera::handleSelect()
 90{
 91	if (gFloaterTools)
 92	{
 93		gFloaterTools->setStatusText("camera");
 94	}
 95}
 96
 97// virtual
 98void LLToolCamera::handleDeselect()
 99{
100//	gAgent.setLookingAtAvatar(FALSE);
101}
102
103BOOL LLToolCamera::handleMouseDown(S32 x, S32 y, MASK mask)
104{
105	// Ensure a mouseup
106	setMouseCapture(TRUE);
107
108	// call the base class to propogate info to sim
109	LLTool::handleMouseDown(x, y, mask);
110
111	mAccumX = 0;
112	mAccumY = 0;
113
114	mOutsideSlopX = FALSE;
115	mOutsideSlopY = FALSE;
116
117	mValidClickPoint = FALSE;
118
119	// If mouse capture gets ripped away, claim we moused up
120	// at the point we moused down. JC
121	mMouseUpX = x;
122	mMouseUpY = y;
123	mMouseUpMask = mask;
124
125	gViewerWindow->hideCursor();
126
127	gViewerWindow->pickAsync(x, y, mask, pickCallback);
128
129	return TRUE;
130}
131
132void LLToolCamera::pickCallback(const LLPickInfo& pick_info)
133{
134	if (!LLToolCamera::getInstance()->hasMouseCapture())
135	{
136		return;
137	}
138
139	LLToolCamera::getInstance()->mMouseDownX = pick_info.mMousePt.mX;
140	LLToolCamera::getInstance()->mMouseDownY = pick_info.mMousePt.mY;
141
142	gViewerWindow->moveCursorToCenter();
143
144	// Potentially recenter if click outside rectangle
145	LLViewerObject* hit_obj = pick_info.getObject();
146
147	// Check for hit the sky, or some other invalid point
148	if (!hit_obj && pick_info.mPosGlobal.isExactlyZero())
149	{
150		LLToolCamera::getInstance()->mValidClickPoint = FALSE;
151		return;
152	}
153
154	// check for hud attachments
155	if (hit_obj && hit_obj->isHUDAttachment())
156	{
157		LLObjectSelectionHandle selection = LLSelectMgr::getInstance()->getSelection();
158		if (!selection->getObjectCount() || selection->getSelectType() != SELECT_TYPE_HUD)
159		{
160			LLToolCamera::getInstance()->mValidClickPoint = FALSE;
161			return;
162		}
163	}
164
165	if( CAMERA_MODE_CUSTOMIZE_AVATAR == gAgentCamera.getCameraMode() )
166	{
167		BOOL good_customize_avatar_hit = FALSE;
168		if( hit_obj )
169		{
170			if (isAgentAvatarValid() && (hit_obj == gAgentAvatarp))
171			{
172				// It's you
173				good_customize_avatar_hit = TRUE;
174			}
175			else if (hit_obj->isAttachment() && hit_obj->permYouOwner())
176			{
177				// It's an attachment that you're wearing
178				good_customize_avatar_hit = TRUE;
179			}
180		}
181
182		if( !good_customize_avatar_hit )
183		{
184			LLToolCamera::getInstance()->mValidClickPoint = FALSE;
185			return;
186		}
187
188		if( gMorphView )
189		{
190			gMorphView->setCameraDrivenByKeys( FALSE );
191		}
192	}
193	//RN: check to see if this is mouse-driving as opposed to ALT-zoom or Focus tool
194	else if (pick_info.mKeyMask & MASK_ALT || 
195			(LLToolMgr::getInstance()->getCurrentTool()->getName() == "Camera")) 
196	{
197		LLViewerObject* hit_obj = pick_info.getObject();
198		if (hit_obj)
199		{
200			// ...clicked on a world object, so focus at its position
201			if (!hit_obj->isHUDAttachment())
202			{
203				gAgentCamera.setFocusOnAvatar(FALSE, ANIMATE);
204				gAgentCamera.setFocusGlobal(pick_info);
205			}
206		}
207		else if (!pick_info.mPosGlobal.isExactlyZero())
208		{
209			// Hit the ground
210			gAgentCamera.setFocusOnAvatar(FALSE, ANIMATE);
211			gAgentCamera.setFocusGlobal(pick_info);
212		}
213
214		if (!(pick_info.mKeyMask & MASK_ALT) &&
215			gAgentCamera.cameraThirdPerson() &&
216			gViewerWindow->getLeftMouseDown() && 
217			!gSavedSettings.getBOOL("FreezeTime") &&
218			(hit_obj == gAgentAvatarp || 
219			 (hit_obj && hit_obj->isAttachment() && LLVOAvatar::findAvatarFromAttachment(hit_obj)->isSelf())))
220		{
221			LLToolCamera::getInstance()->mMouseSteering = TRUE;
222		}
223
224	}
225
226	LLToolCamera::getInstance()->mValidClickPoint = TRUE;
227
228	if( CAMERA_MODE_CUSTOMIZE_AVATAR == gAgentCamera.getCameraMode() )
229	{
230		gAgentCamera.setFocusOnAvatar(FALSE, FALSE);
231		
232		LLVector3d cam_pos = gAgentCamera.getCameraPositionGlobal();
233
234		gAgentCamera.setCameraPosAndFocusGlobal( cam_pos, pick_info.mPosGlobal, pick_info.mObjectID);
235	}
236}
237
238
239// "Let go" of the mouse, for example on mouse up or when
240// we lose mouse capture.  This ensures that cursor becomes visible
241// if a modal dialog pops up during Alt-Zoom. JC
242void LLToolCamera::releaseMouse()
243{
244	// Need to tell the sim that the mouse button is up, since this
245	// tool is no longer working and cursor is visible (despite actual
246	// mouse button status).
247	LLTool::handleMouseUp(mMouseUpX, mMouseUpY, mMouseUpMask);
248
249	gViewerWindow->showCursor();
250
251	//for the situation when left click was performed on the Agent
252	if (!LLFloaterCamera::inFreeCameraMode())
253	{
254		LLToolMgr::getInstance()->clearTransientTool();
255	}
256
257	mMouseSteering = FALSE;
258	mValidClickPoint = FALSE;
259	mOutsideSlopX = FALSE;
260	mOutsideSlopY = FALSE;
261}
262
263
264BOOL LLToolCamera::handleMouseUp(S32 x, S32 y, MASK mask)
265{
266	// Claim that we're mousing up somewhere
267	mMouseUpX = x;
268	mMouseUpY = y;
269	mMouseUpMask = mask;
270
271	if (hasMouseCapture())
272	{
273		if (mValidClickPoint)
274		{
275			if( CAMERA_MODE_CUSTOMIZE_AVATAR == gAgentCamera.getCameraMode() )
276			{
277				LLCoordGL mouse_pos;
278				LLVector3 focus_pos = gAgent.getPosAgentFromGlobal(gAgentCamera.getFocusGlobal());
279				BOOL success = LLViewerCamera::getInstance()->projectPosAgentToScreen(focus_pos, mouse_pos);
280				if (success)
281				{
282					LLUI::setMousePositionScreen(mouse_pos.mX, mouse_pos.mY);
283				}
284			}
285			else if (mMouseSteering)
286			{
287				LLUI::setMousePositionScreen(mMouseDownX, mMouseDownY);
288			}
289			else
290			{
291				gViewerWindow->moveCursorToCenter();
292			}
293		}
294		else
295		{
296			// not a valid zoomable object
297			LLUI::setMousePositionScreen(mMouseDownX, mMouseDownY);
298		}
299
300		// calls releaseMouse() internally
301		setMouseCapture(FALSE);
302	}
303	else
304	{
305		releaseMouse();
306	}
307
308	return TRUE;
309}
310
311
312BOOL LLToolCamera::handleHover(S32 x, S32 y, MASK mask)
313{
314	S32 dx = gViewerWindow->getCurrentMouseDX();
315	S32 dy = gViewerWindow->getCurrentMouseDY();
316	
317	BOOL moved_outside_slop = FALSE;
318	
319	if (hasMouseCapture() && mValidClickPoint)
320	{
321		mAccumX += llabs(dx);
322		mAccumY += llabs(dy);
323
324		if (mAccumX >= SLOP_RANGE)
325		{
326			if (!mOutsideSlopX)
327			{
328				moved_outside_slop = TRUE;
329			}
330			mOutsideSlopX = TRUE;
331		}
332
333		if (mAccumY >= SLOP_RANGE)
334		{
335			if (!mOutsideSlopY)
336			{
337				moved_outside_slop = TRUE;
338			}
339			mOutsideSlopY = TRUE;
340		}
341	}
342
343	if (mOutsideSlopX || mOutsideSlopY)
344	{
345		if (!mValidClickPoint)
346		{
347			lldebugst(LLERR_USER_INPUT) << "hover handled by LLToolFocus [invalid point]" << llendl;
348			gViewerWindow->setCursor(UI_CURSOR_NO);
349			gViewerWindow->showCursor();
350			return TRUE;
351		}
352
353		if (gCameraBtnOrbit ||
354			mask == MASK_ORBIT || 
355			mask == (MASK_ALT | MASK_ORBIT))
356		{
357			// Orbit tool
358			if (hasMouseCapture())
359			{
360				const F32 RADIANS_PER_PIXEL = 360.f * DEG_TO_RAD / gViewerWindow->getWorldViewWidthScaled();
361
362				if (dx != 0)
363				{
364					gAgentCamera.cameraOrbitAround( -dx * RADIANS_PER_PIXEL );
365				}
366
367				if (dy != 0)
368				{
369					gAgentCamera.cameraOrbitOver( -dy * RADIANS_PER_PIXEL );
370				}
371
372				gViewerWindow->moveCursorToCenter();
373			}
374			lldebugst(LLERR_USER_INPUT) << "hover handled by LLToolFocus [active]" << llendl;
375		}
376		else if (	gCameraBtnPan ||
377					mask == MASK_PAN ||
378					mask == (MASK_PAN | MASK_ALT) )
379		{
380			// Pan tool
381			if (hasMouseCapture())
382			{
383				LLVector3d camera_to_focus = gAgentCamera.getCameraPositionGlobal();
384				camera_to_focus -= gAgentCamera.getFocusGlobal();
385				F32 dist = (F32) camera_to_focus.normVec();
386
387				// Fudge factor for pan
388				F32 meters_per_pixel = 3.f * dist / gViewerWindow->getWorldViewWidthScaled();
389
390				if (dx != 0)
391				{
392					gAgentCamera.cameraPanLeft( dx * meters_per_pixel );
393				}
394
395				if (dy != 0)
396				{
397					gAgentCamera.cameraPanUp( -dy * meters_per_pixel );
398				}
399
400				gViewerWindow->moveCursorToCenter();
401			}
402			lldebugst(LLERR_USER_INPUT) << "hover handled by LLToolPan" << llendl;
403		}
404		else if (gCameraBtnZoom)
405		{
406			// Zoom tool
407			if (hasMouseCapture())
408			{
409
410				const F32 RADIANS_PER_PIXEL = 360.f * DEG_TO_RAD / gViewerWindow->getWorldViewWidthScaled();
411
412				if (dx != 0)
413				{
414					gAgentCamera.cameraOrbitAround( -dx * RADIANS_PER_PIXEL );
415				}
416
417				const F32 IN_FACTOR = 0.99f;
418
419				if (dy != 0 && mOutsideSlopY )
420				{
421					if (mMouseSteering)
422					{
423						gAgentCamera.cameraOrbitOver( -dy * RADIANS_PER_PIXEL );
424					}
425					else
426					{
427						gAgentCamera.cameraZoomIn( pow( IN_FACTOR, dy ) );
428					}
429				}
430
431				gViewerWindow->moveCursorToCenter();
432			}
433
434			lldebugst(LLERR_USER_INPUT) << "hover handled by LLToolZoom" << llendl;		
435		}
436	}
437
438	if (gCameraBtnOrbit ||
439		mask == MASK_ORBIT || 
440		mask == (MASK_ALT | MASK_ORBIT))
441	{
442		gViewerWindow->setCursor(UI_CURSOR_TOOLCAMERA);
443	}
444	else if (	gCameraBtnPan ||
445				mask == MASK_PAN ||
446				mask == (MASK_PAN | MASK_ALT) )
447	{
448		gViewerWindow->setCursor(UI_CURSOR_TOOLPAN);
449	}
450	else
451	{
452		gViewerWindow->setCursor(UI_CURSOR_TOOLZOOMIN);
453	}
454	
455	return TRUE;
456}
457
458
459void LLToolCamera::onMouseCaptureLost()
460{
461	releaseMouse();
462}