aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llfloaterassetbrowser.cpp
blob: af81c4a68fd0a0f380256ff4d38bfa71b8d58bcd (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/** 
 * @file llfloaterassetbrowser.cpp
 * @brief LLFloaterAssetBrowser class implementation
 * Phox wuz hurr
 */

#include "llviewerprecompiledheaders.h"
#include "llfloaterassetbrowser.h"
#include "llinventoryview.h"
#include "llinventorymodel.h"
#include "llviewerimagelist.h"
#include "llbutton.h"
#include "lltextbox.h"
#include "llpreview.h"
#include "llinventorybridge.h"
#include "llagent.h"
#include "lltooldraganddrop.h"
#include "llfocusmgr.h"
#include "llview.h"

// Externs
extern LLInventoryModel gInventory;

// Statics
LLFloaterAssetBrowser* LLFloaterAssetBrowser::sInstance = NULL;

LLFloaterAssetBrowser::LLFloaterAssetBrowser()
:   LLFloater("floater_asset_browser")
{
    LLUICtrlFactory::getInstance()->buildFloater(this, "floater_asset_browser.xml");

	mInventoryPanel = getChild<LLInventoryPanel>("inventory panel");

	//Open all folders and close them in order to make item list up-to-date
	//if(gInventory.getItemCount()==0)
		mInventoryPanel->openAllFolders();

	//gInventory.startBackgroundFetch(gInventory.findCategoryUUIDForType(LLAssetType::AT_TEXTURE));
	
	
	childSetAction("button next", onClickNext, this);
	childSetAction("button previous", onClickPrevious, this);
	childSetAction("button refresh", onClickRefresh, this);
	
	initialize();
	createThumbnails();
	mInventoryPanel->closeAllFolders();
}

// static
void LLFloaterAssetBrowser::show(void*)
{
    if (!sInstance)
	sInstance = new LLFloaterAssetBrowser();

	sInstance->open();
}

// virtual
LLFloaterAssetBrowser::~LLFloaterAssetBrowser()
{
	clearAssetInfoTexts();
	mTextureAssets.clear();
	mMaxIndex = 0;
	mFirstIndex = 0;
	mMouseOverIndex = 0;
	mMouseOverUUID = LLUUID::null;
	mMouseOverAssetUUID = LLUUID::null;
	mFloaterTitle = "";
	clearNoAssetsText();
	sInstance=NULL;
}

void LLFloaterAssetBrowser::initialize()
{
	mMaxIndex = ITEMS_PER_PAGE;
	mFirstIndex = 0;
	mAssetInfoLabelList.clear();
	mAssetInfoIndex = 0;
	mFloaterHeight = getRect().getHeight();
	mFloaterWidth = getRect().getWidth();
	mMouseOverIndex = 0;
	mMouseOverUUID = LLUUID::null;
	mMouseOverAssetUUID = LLUUID::null;
	mFloaterTitle = "";

	S32 image_top = getRect().getHeight();
	S32 image_bottom = BTN_HEIGHT_SMALL;
	S32 image_middle = (image_top + image_bottom) / 2;
	S32 line_height = llround(LLFontGL::getFontSansSerifSmall()->getLineHeight());
	
	mNoAssetsLabel = new LLTextBox("mNoAssetsLabel.", 
										LLRect(getRect().getWidth()/2,
												image_middle + line_height / 2,
												getRect().getWidth(),
												image_middle - line_height / 2 ),
										"No assets found.",
										LLFontGL::getFontSansSerifSmall() );
	mNoAssetsLabel->setFollowsAll();
	addChild(mNoAssetsLabel);
	mNoAssetsLabel->setVisible(FALSE);
}

void LLFloaterAssetBrowser::createThumbnails()
{
	LLViewerInventoryCategory::cat_array_t cats;
	LLViewerInventoryItem::item_array_t items;
	LLIsType isType(LLAssetType::AT_TEXTURE);

	gInventory.collectDescendentsIf(LLUUID::null,
							cats,
							items,
							LLInventoryModel::INCLUDE_TRASH,
							isType);
	
	//Get UUID, asset UUID and name
	for(S32 i = 0; i < items.count(); i++)
	{
		LLInventoryItem* itemp = items[i];
		LLAssetSelection temp;
		temp.mAssetUUID = itemp->getAssetUUID();
		temp.mUUID = itemp->getUUID();
		temp.mName = itemp->getName();
		temp.mTexturep = NULL;
		temp.mAssetRect = LLRect::null;
		mTextureAssets.push_back(temp);
	}
	
	//Get Texturep
	for(S32 i = 0; i < items.count(); i++)
	{
			mTextureAssets[i].mTexturep = gImageList.getImage(mTextureAssets[i].mAssetUUID, MIPMAP_YES, IMMEDIATE_NO);
			mTextureAssets[i].mTexturep->setBoostLevel(LLViewerImageBoostLevel::BOOST_PREVIEW);
			//mTextureAssets[i].mTexturep->processTextureStats();
	}

	//Generate the asset info text
	/*for(S32 i = 0; i < items.count(); i++)
	{
		LLString asset_info;
		LLString dimensions;
		
		asset_info.append(mTextureAssets[i].mName);
		
		//if(mTextureAssets[i].mTexturep->mFullWidth == 0
			//|| mTextureAssets[i].mTexturep->mFullHeight == 0)

		dimensions = llformat("\n%d x %d",
						mTextureAssets[i].mTexturep->mFullWidth,
						mTextureAssets[i].mTexturep->mFullHeight);
		asset_info.append(dimensions);
		
		mTextureAssets[i].mAssetInfo = asset_info;
	}*/
	
	mFloaterTitle = llformat("Asset Browser (%d assets fetched)", mTextureAssets.size());
	setTitle(mFloaterTitle);
}

// virtual
BOOL LLFloaterAssetBrowser::handleHover(S32 x, S32 y, MASK mask)
{
	if(mTextureAssets.size() > 0)
	{
		for(U32 i = mFirstIndex; i <  mMaxIndex; i++)
		{
			if(i < mTextureAssets.size())
			{
    			if(mTextureAssets[i].mAssetRect.pointInRect(x,y))
			    {
					mMouseOverUUID = mTextureAssets[i].mUUID;
				    mMouseOverIndex = i;

				    if(hasMouseCapture())
				    {
    					S32 screen_x;
					    S32 screen_y;
					    LLUUID mObjectUUID= LLUUID::null;
    						const LLViewerInventoryItem *item = gInventory.getItem(mMouseOverUUID);
    
					    localPointToScreen(x, y, &screen_x, &screen_y );
    
					    if(item && item->getPermissions().allowCopyBy(gAgent.getID(), gAgent.getGroupID())
    						&& LLToolDragAndDrop::getInstance()->isOverThreshold(screen_x, screen_y))
					    {
    						EDragAndDropType type;
						    type = LLAssetType::lookupDragAndDropType(item->getType());
						    LLToolDragAndDrop::ESource src = LLToolDragAndDrop::SOURCE_LIBRARY;
    						
						    if(!mObjectUUID.isNull())
						    {
    							src = LLToolDragAndDrop::SOURCE_WORLD;
						    }
						    else if(item->getPermissions().getOwner() == gAgent.getID())
						    {
    							src = LLToolDragAndDrop::SOURCE_AGENT;
						    }
						    LLToolDragAndDrop::getInstance()->beginDrag(type,
													    item->getUUID(),
													    src,
													    mObjectUUID);
						    return LLToolDragAndDrop::getInstance()->handleHover(x, y, mask);
					    }
					    return TRUE;
				    }
				    return LLFloater::handleHover(x, y, mask);
			    }
		    }
		}
		return LLFloater::handleHover(x, y, mask);
	}
	else
	{
		mMouseOverUUID = LLUUID::null;
		return LLFloater::handleHover(x, y, mask);
	}
}

// virtual
BOOL LLFloaterAssetBrowser::handleMouseDown(S32 x, S32 y, MASK mask)
{
	if(mTextureAssets.size() > 0)
	{
		if(mTextureAssets[mMouseOverIndex].mAssetRect.pointInRect(x,y))
		{
			if(mMouseOverUUID.notNull())
			{
				gFocusMgr.setMouseCapture(this);
				S32 screen_x;
				S32 screen_y;
				localPointToScreen(x, y, &screen_x, &screen_y);
				LLToolDragAndDrop::getInstance()->setDragStart(screen_x, screen_y);
				return TRUE;
			}
		}
	}
	return LLFloater::handleMouseDown(x, y, mask);
}

// virtual
BOOL LLFloaterAssetBrowser::handleMouseUp(S32 x, S32 y, MASK mask)
{
	if(hasMouseCapture())
	{
		gFocusMgr.setMouseCapture(NULL);
		return TRUE;
	}
	return LLFloater::handleMouseUp(x, y, mask);
}

// virtual
BOOL LLFloaterAssetBrowser::handleDoubleClick(S32 x, S32 y, MASK mask)
{
	if(mTextureAssets.size() > 0)
	{
		if(mTextureAssets[mMouseOverIndex].mAssetRect.pointInRect(x,y))
		{	
			if(mMouseOverUUID.notNull())
			{
				open_texture(mMouseOverUUID, mTextureAssets[mMouseOverIndex].mName, FALSE);
				return TRUE;
			}
		}
	}
	return LLFloater::handleDoubleClick(x, y, mask);
}

// virtual
void LLFloaterAssetBrowser::draw()
{		
	LLFloater::draw();
	
	if(mTextureAssets.size() > 0)
	{
		S32 hor_pos = 0;
		S32 ver_pos = 0;
		U32 items_per_row = 0;
		S32 height = getRect().getHeight()/8;
		S32 width = height;

		for(U32 i = mFirstIndex; i < mMaxIndex; i++)
		{
			if(i < mTextureAssets.size())
			{
				mTexturep = NULL;
				mImageAssetID = mTextureAssets[i].mAssetUUID;
				
				if(mImageAssetID.notNull())
				{
					mTexturep = gImageList.getImage(mImageAssetID, MIPMAP_YES, IMMEDIATE_NO);
					//mTexturep->setBoostLevel(LLViewerImage::BOOST_PREVIEW);
					mTexturep->processTextureStats();
					mTextureAssets[i].mWidth = mTexturep->mFullWidth;
					mTextureAssets[i].mHeight = mTexturep->mFullHeight;
				}

				if(isMinimized())
				{
					return;
				}

				//Border
				LLRect border;
				border.mLeft = START_POS + ver_pos;
				border.mTop = getRect().getHeight() - LLFLOATER_HEADER_SIZE - BORDER_PAD - hor_pos;
				border.mRight = border.mLeft + width;
				border.mBottom = border.mTop - height;
				
				//Save the rect for each thumbnail
				mTextureAssets[i].mAssetRect = border;
				
				//Asset info rect
				LLRect info_rect;
				info_rect.mLeft = border.mLeft;
				info_rect.mTop = border.mBottom - 3;
				info_rect.mRight = border.mLeft + 130;
				info_rect.mBottom = border.mBottom - 2*llround(LLFontGL::getFontSansSerifSmall()->getLineHeight());

				gl_rect_2d(border, LLColor4::black, FALSE);
				
				// Interior
				LLRect interior = border;
				interior.stretch(1); 
				
				//Draw the image
				if(mTexturep)
				{
					if(mTexturep->getComponents() == 4)
					{
						gl_rect_2d_checkerboard(interior);
					}
					
					gl_draw_scaled_image(interior.mLeft, interior.mBottom, interior.getWidth(), interior.getHeight(), mTexturep);
					// Pump the priority
					mTexturep->addTextureStats((F32)(interior.getWidth() * interior.getHeight()));
					
					//Generate the asset info text
					std::string asset_info;
					asset_info.append(mTextureAssets[i].mName);
					std::string dimensions = llformat("\n%d x %d",
													mTexturep->mFullWidth /*mTextureAssets[i].mWidth*/,
													mTexturep->mFullHeight /* mTextureAssets[i].mHeight*/);
					asset_info.append(dimensions);
					
					// Draw material info below the asset
					// LLTextBox object has to be drawn only once, not non-stop like image
					if(mAssetInfoIndex < ITEMS_PER_PAGE && 
						mAssetInfoIndex < mTextureAssets.size() - mFirstIndex)
					{
						mAssetInfoLabel = new LLTextBox("Asset Info"/*mTextureAssets[i].mAssetInfo*/,
														info_rect,
														asset_info,
														LLFontGL::getFontSansSerifSmall());
						mAssetInfoLabel->setFollowsAll();
						mAssetInfoLabelList.push_back(mAssetInfoLabel);
						addChild(mAssetInfoLabelList[mAssetInfoIndex]);
						mAssetInfoLabelList[mAssetInfoIndex]->setVisible(TRUE);
						mAssetInfoIndex++;
					}
					
				}
				else
				{
					// Draw X
					gl_rect_2d(interior, LLColor4::grey, TRUE);
					gl_draw_x(interior, LLColor4::black);
				}
				//Move to the right
				ver_pos += getRect().getWidth()/6;
				items_per_row++;

				//Change the row
				if(items_per_row % 4 == 0)
				{
					ver_pos = 0;
					hor_pos += getRect().getHeight()/4;
				}
			}
		}//for

		//If the size of the floater has changed, clear the asset info texts
		//in order to draw them again into the new position
		if(getRect().getWidth() != mFloaterWidth || getRect().getHeight() != mFloaterHeight)
		{
			clearAssetInfoTexts();
			//Save the size of the current floater
			mFloaterWidth = getRect().getWidth();
			mFloaterHeight = getRect().getHeight();
		}
		if(mMaxIndex >= mTextureAssets.size())
		{
			childDisable("button next");
			childEnable("button previous");
		}
		else if(mFirstIndex <= 0)
		{
			childEnable("button next");
			childDisable("button previous");
		}
		else
		{
			childEnable("button next");
			childEnable("button previous");
		}
	}
	else
	{
		//No assets found
		mNoAssetsLabel->setVisible(TRUE);
		childDisable("button next");
		childDisable("button previous");
	}
}

void LLFloaterAssetBrowser::clearAssetInfoTexts()
{
	for(U32 i = 0; i < mAssetInfoLabelList.size(); i++)
	{
		delete mAssetInfoLabelList[i];
		mAssetInfoLabelList[i] = NULL;
	}
	mAssetInfoLabelList.clear();
	mAssetInfoIndex = 0;
}

void LLFloaterAssetBrowser::clearNoAssetsText()
{
	if(mTextureAssets.size() > 0)
	{
		delete mNoAssetsLabel;
		mNoAssetsLabel = NULL;
	}
}

// static
void LLFloaterAssetBrowser::onClickNext(void *userdata)
{
	LLFloaterAssetBrowser* self = (LLFloaterAssetBrowser*)userdata;
	self->mFirstIndex = self->mMaxIndex;
	self->mMaxIndex = self->mFirstIndex + ITEMS_PER_PAGE;
	self->clearAssetInfoTexts();
}

// static
void LLFloaterAssetBrowser::onClickPrevious(void *userdata)
{
	LLFloaterAssetBrowser* self = (LLFloaterAssetBrowser*)userdata;
	self->mFirstIndex = self->mMaxIndex - (2 * ITEMS_PER_PAGE);
	self->mMaxIndex = self->mMaxIndex - ITEMS_PER_PAGE;
	self->clearAssetInfoTexts();
}

// static
void LLFloaterAssetBrowser::onClickRefresh(void *userdata)
{
	LLFloaterAssetBrowser* self = (LLFloaterAssetBrowser*)userdata;	
	for(U32 i = 0; i < self->mTextureAssets.size(); i++)
	{
		self->mTextureAssets[i].mTexturep = NULL;
	}
	
	self->mTextureAssets.clear();
	self->createThumbnails();
	self->clearNoAssetsText();
	self->clearAssetInfoTexts();
}