summaryrefslogtreecommitdiff
path: root/indra/newview/llviewermediafocus.cpp
blob: d81c332d546bdf54f00d4ebe109688870e56ea32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/** 
 * @file llviewermediafocus.cpp
 * @brief Governs focus on Media prims
 *
 * $LicenseInfo:firstyear=2003&license=viewergpl$
 * 
 * Copyright (c) 2003-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llviewermediafocus.h"

//LLViewerMediaFocus
#include "llviewerobjectlist.h"
#include "llpanelmediahud.h"
#include "llpluginclassmedia.h"
#include "llagent.h"
#include "lltoolpie.h"
#include "llviewercamera.h"
#include "llviewermedia.h"
#include "llhudview.h"
#include "lluictrlfactory.h"
#include "lldrawable.h"
#include "llparcel.h"
#include "llviewerparcelmgr.h"
#include "llweb.h"
#include "llmediaentry.h"
//
// LLViewerMediaFocus
//

LLViewerMediaFocus::LLViewerMediaFocus()
: mMouseOverFlag(false)
{
}

LLViewerMediaFocus::~LLViewerMediaFocus()
{
	// The destructor for LLSingletons happens at atexit() time, which is too late to do much.
	// Clean up in cleanupClass() instead.
}

void LLViewerMediaFocus::cleanupClass()
{
	LLViewerMediaFocus *self = LLViewerMediaFocus::getInstance();
	
	if(self)
	{
		// mMediaHUD will have been deleted by this point -- don't try to delete it.

		/* Richard says:
			all widgets are supposed to be destroyed at the same time
			you shouldn't hold on to pointer to them outside of ui code		
			you can use the LLHandle approach
			if you want to be type safe, you'll need to add a LLRootHandle to whatever derived class you are pointing to
			look at llview::gethandle
			its our version of a weak pointer			
		*/
		if(self->mMediaHUD.get())
		{
			self->mMediaHUD.get()->setMediaImpl(NULL);
		}
		self->mMediaImpl = NULL;
	}
	
}


void LLViewerMediaFocus::setFocusFace( BOOL b, LLPointer<LLViewerObject> objectp, S32 face, viewer_media_t media_impl )
{
	LLParcel *parcel = LLViewerParcelMgr::getInstance()->getAgentParcel();

	if(mMediaImpl.notNull())
	{
		mMediaImpl->focus(false);
	}

	if (b && media_impl.notNull())
	{
		bool face_auto_zoom = false;
		mMediaImpl = media_impl;
		mMediaImpl->focus(true);

		LLSelectMgr::getInstance()->deselectAll();
		LLSelectMgr::getInstance()->selectObjectOnly(objectp, face);

		if(objectp.notNull())
		{
			LLTextureEntry* tep = objectp->getTE(face);
			if(! tep->hasMedia())
			{
				// Error condition
			}
			LLMediaEntry* mep = tep->getMediaData();
			face_auto_zoom = mep->getAutoZoom();
			if(! mep->getAutoPlay())
			{
				std::string url = mep->getCurrentURL().empty() ? mep->getHomeURL() : mep->getCurrentURL();
				media_impl->navigateTo(url, "", true);
			}
		}
		mFocus = LLSelectMgr::getInstance()->getSelection();
		if(mMediaHUD.get() && face_auto_zoom && ! parcel->getMediaPreventCameraZoom())
		{
			mMediaHUD.get()->resetZoomLevel();
			mMediaHUD.get()->nextZoomLevel();
		}
		if (!mFocus->isEmpty())
		{
			gFocusMgr.setKeyboardFocus(this);
		}
		mObjectID = objectp->getID();
		mObjectFace = face;
		// LLViewerMedia::addObserver(this, mObjectID);


	}
	else
	{
		gFocusMgr.setKeyboardFocus(NULL);
		if(! parcel->getMediaPreventCameraZoom())
		{
			if (!mFocus->isEmpty())
			{
				gAgent.setFocusOnAvatar(TRUE, ANIMATE);
			}
		}
		mFocus = NULL;
		// LLViewerMedia::remObserver(this, mObjectID);
		
		// Null out the media hud media pointer
		if(mMediaHUD.get())
		{
			mMediaHUD.get()->setMediaImpl(NULL);
		}

		// and null out the media impl
		mMediaImpl = NULL;
		mObjectID = LLUUID::null;
		mObjectFace = 0;
	}
	if(mMediaHUD.get())
	{
		mMediaHUD.get()->setMediaFocus(b);
	}
}
bool LLViewerMediaFocus::getFocus()
{
	if (gFocusMgr.getKeyboardFocus() == this)
	{
		return true;
	}
	return false;
}

// This function selects an ideal viewing distance given a selection bounding box, normal, and padding value
void LLViewerMediaFocus::setCameraZoom(F32 padding_factor)
{
	LLPickInfo& pick = LLToolPie::getInstance()->getPick();

	if(LLSelectMgr::getInstance()->getSelection()->isEmpty())
	{
		pick = mPickInfo;
		setFocusFace(true, pick.getObject(), pick.mObjectFace, mMediaImpl);
	}

	if (!LLSelectMgr::getInstance()->getSelection()->isEmpty())
	{
		gAgent.setFocusOnAvatar(FALSE, ANIMATE);

		LLBBox selection_bbox = LLSelectMgr::getInstance()->getBBoxOfSelection();
		F32 height;
		F32 width;
		F32 depth;
		F32 angle_of_view;
		F32 distance;

		// We need the aspect ratio, and the 3 components of the bbox as height, width, and depth.
		F32 aspect_ratio = getBBoxAspectRatio(selection_bbox, pick.mNormal, &height, &width, &depth);
		F32 camera_aspect = LLViewerCamera::getInstance()->getAspect();

		// We will normally use the side of the volume aligned with the short side of the screen (i.e. the height for 
		// a screen in a landscape aspect ratio), however there is an edge case where the aspect ratio of the object is 
		// more extreme than the screen.  In this case we invert the logic, using the longer component of both the object
		// and the screen.  
		bool invert = (camera_aspect > 1.0f && aspect_ratio > camera_aspect) ||
			(camera_aspect < 1.0f && aspect_ratio < camera_aspect);

		// To calculate the optimum viewing distance we will need the angle of the shorter side of the view rectangle.
		// In portrait mode this is the width, and in landscape it is the height.
		// We then calculate the distance based on the corresponding side of the object bbox (width for portrait, height for landscape)
		// We will add half the depth of the bounding box, as the distance projection uses the center point of the bbox.
		if(camera_aspect < 1.0f || invert)
		{
			angle_of_view = llmax(0.1f, LLViewerCamera::getInstance()->getView() * LLViewerCamera::getInstance()->getAspect());
			distance = width * 0.5 * padding_factor / tan(angle_of_view * 0.5f );
		}
		else
		{
			angle_of_view = llmax(0.1f, LLViewerCamera::getInstance()->getView());
			distance = height * 0.5 * padding_factor / tan(angle_of_view * 0.5f );
		}

		distance += depth * 0.5;

		// Finally animate the camera to this new position and focal point
		LLVector3d camera_pos, target_pos;
		// The target lookat position is the center of the selection (in global coords)
		target_pos = LLSelectMgr::getInstance()->getSelectionCenterGlobal();
		// Target look-from (camera) position is "distance" away from the target along the normal 
		LLVector3d pickNormal = LLVector3d(pick.mNormal);
		pickNormal.normalize();
        camera_pos = target_pos + pickNormal * distance;
        if (pickNormal == LLVector3d::z_axis || pickNormal == LLVector3d::z_axis_neg)
        {
			// If the normal points directly up, the camera will "flip" around.
			// We try to avoid this by adjusting the target camera position a 
			// smidge towards current camera position
			// *NOTE: this solution is not perfect.  All it attempts to solve is the
			// "looking down" problem where the camera flips around when it animates
			// to that position.  You still are not guaranteed to be looking at the
			// media in the correct orientation.  What this solution does is it will
			// put the camera into position keeping as best it can the current 
			// orientation with respect to the face.  In other words, if before zoom
			// the media appears "upside down" from the camera, after zooming it will
			// still be upside down, but at least it will not flip.
            LLVector3d cur_camera_pos = LLVector3d(gAgent.getCameraPositionGlobal());
            LLVector3d delta = (cur_camera_pos - camera_pos);
            F64 len = delta.length();
            delta.normalize();
            // Move 1% of the distance towards original camera location
            camera_pos += 0.01 * len * delta;
        }

		gAgent.setCameraPosAndFocusGlobal(camera_pos, target_pos, LLSelectMgr::getInstance()->getSelection()->getFirstObject()->mID );
	}
}
void LLViewerMediaFocus::onFocusReceived()
{
	if(mMediaImpl.notNull())
		mMediaImpl->focus(true);

	LLFocusableElement::onFocusReceived();
}

void LLViewerMediaFocus::onFocusLost()
{
	if(mMediaImpl.notNull())
		mMediaImpl->focus(false);
	gViewerWindow->focusClient();
	mFocus = NULL;
	LLFocusableElement::onFocusLost();
}
void LLViewerMediaFocus::setMouseOverFlag(bool b, viewer_media_t media_impl)
{
	if (b && media_impl.notNull())
	{
		if(! mMediaHUD.get())
		{
			LLPanelMediaHUD* media_hud = new LLPanelMediaHUD(mMediaImpl);
			mMediaHUD = media_hud->getHandle();
			gHUDView->addChild(media_hud);	
		}
		mMediaHUD.get()->setMediaImpl(media_impl);

		if(mMediaImpl.notNull() && (mMediaImpl != media_impl))
		{
			mMediaImpl->focus(false);
		}

		mMediaImpl = media_impl;
	}
	mMouseOverFlag = b;
}
LLUUID LLViewerMediaFocus::getSelectedUUID() 
{ 
	LLViewerObject* object = mFocus->getFirstObject();
	return object ? object->getID() : LLUUID::null; 
}
#if 0 // Must re-implement when the new media api event system is ready
void LLViewerMediaFocus::onNavigateComplete( const EventType& event_in )
{
	if (hasFocus() && mLastURL != event_in.getStringValue())
	{
		LLViewerMedia::focus(true, mObjectID);
		// spoof mouse event to reassert focus
		LLViewerMedia::mouseDown(1,1, mObjectID);
		LLViewerMedia::mouseUp(1,1, mObjectID);
	}
	mLastURL = event_in.getStringValue();
}
#endif
BOOL LLViewerMediaFocus::handleKey(KEY key, MASK mask, BOOL called_from_parent)
{
	if(mMediaImpl.notNull())
		mMediaImpl->handleKeyHere(key, mask);

	if (key == KEY_ESCAPE && mMediaHUD.get())
	{
		mMediaHUD.get()->close();
	}
	return true;
}

BOOL LLViewerMediaFocus::handleUnicodeChar(llwchar uni_char, BOOL called_from_parent)
{
	if(mMediaImpl.notNull())
		mMediaImpl->handleUnicodeCharHere(uni_char);
	return true;
}
BOOL LLViewerMediaFocus::handleScrollWheel(S32 x, S32 y, S32 clicks)
{
	BOOL retval = FALSE;
	if(mFocus.notNull() && mMediaImpl.notNull() && mMediaImpl->hasMedia())
	{
        // the scrollEvent() API's x and y are not the same as handleScrollWheel's x and y.
        // The latter is the position of the mouse at the time of the event
        // The former is the 'scroll amount' in x and y, respectively.
        // All we have for 'scroll amount' here is 'clicks', and no mask.
		mMediaImpl->getMediaPlugin()->scrollEvent(0, clicks, /*mask*/0);
		retval = TRUE;
	}
	return retval;
}

void LLViewerMediaFocus::update()
{
	if (mMediaHUD.get())
	{
		if(mFocus.notNull() || mMouseOverFlag || mMediaHUD.get()->isMouseOver())
		{
			// mMediaHUD.get()->setVisible(true);
			mMediaHUD.get()->updateShape();
		}
		else
		{
			mMediaHUD.get()->setVisible(false);
		}
	}
}
// This function calculates the aspect ratio and the world aligned components of a selection bounding box.
F32 LLViewerMediaFocus::getBBoxAspectRatio(const LLBBox& bbox, const LLVector3& normal, F32* height, F32* width, F32* depth)
{
	// Convert the selection normal and an up vector to local coordinate space of the bbox
	LLVector3 local_normal = bbox.agentToLocalBasis(normal);
	LLVector3 z_vec = bbox.agentToLocalBasis(LLVector3(0.0f, 0.0f, 1.0f));
	
	LLVector3 comp1(0.f,0.f,0.f);
	LLVector3 comp2(0.f,0.f,0.f);
	LLVector3 bbox_max = bbox.getExtentLocal();
	F32 dot1 = 0.f;
	F32 dot2 = 0.f;

	// The largest component of the localized normal vector is the depth component
	// meaning that the other two are the legs of the rectangle.
	local_normal.abs();
	if(local_normal.mV[VX] > local_normal.mV[VY])
	{
		if(local_normal.mV[VX] > local_normal.mV[VZ])
		{
			// Use the y and z comps
			comp1.mV[VY] = bbox_max.mV[VY];
			comp2.mV[VZ] = bbox_max.mV[VZ];
			*depth = bbox_max.mV[VX];
		}
		else
		{
			// Use the x and y comps
			comp1.mV[VY] = bbox_max.mV[VY];
			comp2.mV[VZ] = bbox_max.mV[VZ];
			*depth = bbox_max.mV[VZ];
		}
	}
	else if(local_normal.mV[VY] > local_normal.mV[VZ])
	{
		// Use the x and z comps
		comp1.mV[VX] = bbox_max.mV[VX];
		comp2.mV[VZ] = bbox_max.mV[VZ];
		*depth = bbox_max.mV[VY];
	}
	else
	{
		// Use the x and y comps
		comp1.mV[VY] = bbox_max.mV[VY];
		comp2.mV[VZ] = bbox_max.mV[VZ];
		*depth = bbox_max.mV[VX];
	}
	
	// The height is the vector closest to vertical in the bbox coordinate space (highest dot product value)
	dot1 = comp1 * z_vec;
	dot2 = comp2 * z_vec;
	if(fabs(dot1) > fabs(dot2))
	{
		*height = comp1.length();
		*width = comp2.length();
	}
	else
	{
		*height = comp2.length();
		*width = comp1.length();
	}

	// Return the aspect ratio.
	return *width / *height;
}

bool LLViewerMediaFocus::isFocusedOnFace(LLPointer<LLViewerObject> objectp, S32 face)
{
	return objectp->getID() == mObjectID && face == mObjectFace;
}