aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/newview/llfloaterclothing.cpp
blob: 627b2c6f5e8d9fb1af687d3ae31cf324f87831b0 (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
/** 
 * @file llfloaterclothing.cpp
 * @author James Cook
 * @brief Read-only list of clothing from your inventory.
 *
 * Copyright (c) 2004-2007, Linden Research, Inc.
 * 
 * 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.
 */

#include "llviewerprecompiledheaders.h"

#include "llfloaterclothing.h"

#include "lldir.h"
#include "llinventory.h"

#include "llagent.h"
#include "llinventorymodel.h"
#include "llinventoryview.h"
#include "llkeyboard.h"
#include "llscrollbar.h"
#include "llscrolllistctrl.h"
#include "llvieweruictrlfactory.h"
#include "llviewerinventory.h"
#include "llvoavatar.h"
#include "llviewercontrol.h"

const LLString LOADING_STRING("Loading...");

// static
LLFloaterClothing* LLFloaterClothing::sInstance = NULL;
LLFloaterClothingObserver* LLFloaterClothing::sObserver = NULL;

class LLFloaterClothingObserver : public LLInventoryObserver
{
public:
	LLFloaterClothingObserver() {}
	virtual ~LLFloaterClothingObserver() {}

	virtual void changed(U32 mask)
	{
		LLFloaterClothing::refreshAll();
	}
};

//---------------------------------------------------------------------------
// LLFloaterClothing
//---------------------------------------------------------------------------
LLFloaterClothing::LLFloaterClothing()
:	LLFloater("floater_clothing", "FloaterClothingRect", ""),
	mSelectedID(),
	mAllowSelection(FALSE)
{
	gUICtrlFactory->buildFloater(this, "floater_clothing.xml");

	sInstance = this;

	sObserver = new LLFloaterClothingObserver;
	gInventory.addObserver(sObserver);

	childSetAction("take_off_btn", onClickTakeOff, this);
	childSetAction("wear_btn", onClickWear, this);

	childSetDoubleClickCallback("clothing_list", onClickWear);
	childSetCommitCallback("clothing_list", onCommitList, this);

	LLCtrlListInterface* list = childGetListInterface("clothing_list");
	if (list)
	{
		list->addSimpleElement(LOADING_STRING);
	}

	setDefaultBtn("wear_btn");

	gInventory.startBackgroundFetch();
}

// virtual
LLFloaterClothing::~LLFloaterClothing()
{
	gInventory.removeObserver(sObserver);
	delete sObserver;
	sObserver = NULL;

	sInstance = NULL;
}


// virtual
void LLFloaterClothing::onClose(bool app_quitting)
{
	gSavedSettings.setBOOL("ClothingBtnState", FALSE);

	LLFloater::onClose(app_quitting);
}


// static
void LLFloaterClothing::show(void*)
{
	if (sInstance)
	{
		sInstance->setVisibleAndFrontmost();
	}
	else
	{
		LLFloaterClothing *self = new LLFloaterClothing();

		self->buildClothingList();

		if (self->mAllowSelection)
		{
			LLCtrlSelectionInterface* iface = self->childGetSelectionInterface("clothing_list");
			if (iface) iface->selectFirstItem();
		}
		self->childSetFocus("clothing_list");
		
		self->mSelectedID = LLUUID::null;

		// Update button labels
		onCommitList(NULL, self);
	}

	gSavedSettings.setBOOL("ClothingBtnState", TRUE);
}

// static
void LLFloaterClothing::toggleVisibility()
{
	if(sInstance && sInstance->getVisible())
	{
		sInstance->close();
	}
	else
	{
		show();
	}
}

// static
void LLFloaterClothing::refreshAll()
{
	if (sInstance)
	{
		sInstance->buildClothingList();

		LLCtrlListInterface* list = sInstance->childGetListInterface("clothing_list");
		if (list)
		{
			if (!sInstance->mAllowSelection)
			{
				// no selection, no commit on change
				list->operateOnSelection(LLCtrlListInterface::OP_DESELECT);
			}
			else if (sInstance->mSelectedID.isNull())
			{
				list->selectFirstItem();
			}
			else
			{
				if (list->setCurrentByID(sInstance->mSelectedID))
				{
					LLCtrlScrollInterface *scroll = sInstance->childGetScrollInterface("clothing_list");
					if (scroll)
					{
						scroll->scrollToShowSelected();
					}
				}
				else
				{
					list->selectFirstItem();
				}
			}

			// Update button labels
			onCommitList(NULL, sInstance);
		}
	}
}

class LLIsClothing : public LLInventoryCollectFunctor
{
public:
	LLIsClothing() {}
	virtual ~LLIsClothing() {}
	virtual bool operator()(LLInventoryCategory* cat,
							LLInventoryItem* item)
	{
		if (item)
		{
			LLAssetType::EType at = item->getType();
			if (at == LLAssetType::AT_CLOTHING
				|| at == LLAssetType::AT_BODYPART)
			{
				U32 flags = item->getFlags();
				switch(flags)
				{
				case WT_SHAPE:
				case WT_SKIN:
				case WT_HAIR:
				case WT_EYES:
					return FALSE;
				case WT_SHIRT:
				case WT_PANTS:
				case WT_SKIRT:
				case WT_UNDERSHIRT:
				case WT_UNDERPANTS:
					return TRUE;
				default:
					return TRUE;
				}
			}
		}
		return FALSE;
	}
};



void LLFloaterClothing::buildClothingList()
{
	//llinfos << "buildClothingList" << llendl;

	LLCtrlListInterface *list = childGetListInterface("clothing_list");
	if (!list) return;

	list->operateOnAll(LLCtrlListInterface::OP_DELETE);

	LLInventoryModel::cat_array_t cats;
	LLInventoryModel::item_array_t items;

	LLIsClothing is_clothing;
	gInventory.collectDescendentsIf(gAgent.getInventoryRootID(),
									cats,
									items,
									LLInventoryModel::EXCLUDE_TRASH,
									is_clothing);

	S32 count = items.count();
	for(S32 i = 0; i < count; ++i)
	{
		LLInventoryItem* item = items.get(i);

		LLSD row;
		row["id"] = item->getUUID();

		LLUUID image_id = get_item_icon_uuid(item->getType(),
											 item->getInventoryType(),
											 item->getFlags());		// flags = wearable type
		row["columns"][0]["column"] = "icon";
		row["columns"][0]["type"] = "icon";
		row["columns"][0]["value"] = image_id;

		LLString text = item->getName();
		LLString style = "NORMAL";
		if( gAgent.isWearingItem( item->getUUID() ) )
		{
			text.append(" (worn)");
			style = "BOLD";
		}
		row["columns"][1]["column"] = "name";
		row["columns"][1]["value"] = text;
		row["columns"][1]["font"] = "SANSSERIFSMALL";
		row["columns"][1]["font-style"] = style;

		// hidden column for sorting
		U32 flags = item->getFlags();	// flags = wearable type
		enum EWearableType wearable_type = (enum EWearableType)flags;
		const char* wearable_label = LLWearable::typeToTypeLabel(wearable_type);
		//line->addColumn(wearable_label, FONT, -1);	// invisible
		row["columns"][2]["column"] = "sort";
		row["columns"][2]["value"] = wearable_label;

		list->addElement(row);
	}

	if (count > 0)
	{
		mAllowSelection = TRUE;
	}
	else if (LLInventoryModel::backgroundFetchActive())
	{
		// We're loading
		list->addSimpleElement(LOADING_STRING);
		mAllowSelection = FALSE;
	}
	else
	{
		// Weird case, we're done loading but have no clothing
		list->addSimpleElement("No clothing found.");
		mAllowSelection = FALSE;
	}
}

// static
void LLFloaterClothing::onClickWear(void* data)
{
	LLFloaterClothing* self = (LLFloaterClothing*)data;

	const LLUUID& item_id = self->childGetValue("clothing_list").asUUID();

	LLViewerInventoryItem* inv_item = gInventory.getItem(item_id);
	if (!inv_item)
	{
		llwarns << "Can't find inventory item to wear" << llendl;
		return;
	}

	wear_inventory_item_on_avatar(inv_item);
}

BOOL wearable_can_take_off(EWearableType wearable_type)
{
	switch(wearable_type)
	{
	default:
	case WT_SHAPE:
	case WT_SKIN:
	case WT_HAIR:
	case WT_EYES:
		return FALSE;
	case WT_SHIRT:
	case WT_PANTS:
	case WT_SHOES:
	case WT_SOCKS:
	case WT_JACKET:
	case WT_GLOVES:
	case WT_SKIRT:
		return TRUE;
	case WT_UNDERSHIRT:
	case WT_UNDERPANTS:
		// can't take off underpants with PG accounts
		return (gAgent.mAccess >= SIM_ACCESS_MATURE);
	}
}

// static
void LLFloaterClothing::onClickTakeOff(void* data)
{
	LLFloaterClothing* self = (LLFloaterClothing*)data;

	const LLUUID& item_id = self->childGetValue("clothing_list").asUUID();

	LLInventoryItem* inv_item = gInventory.getItem(item_id);
	if (!inv_item)
	{
		llwarns << "Can't find inventory item to wear" << llendl;
		return;
	}

	LLVOAvatar* avatar = gAgent.getAvatarObject();
	if( !avatar ) return;

	EWearableType wearable_type = (EWearableType)inv_item->getFlags();

	// Can only take off certain types
	if (!wearable_can_take_off(wearable_type)) return;

	LLWearable* wearable = gAgent.getWearable(wearable_type);
	if( !wearable ) return;

	gAgent.removeWearable(wearable_type);
}


// static
void LLFloaterClothing::onCommitList(LLUICtrl* ctrl, void* data)
{
	LLFloaterClothing* self = (LLFloaterClothing*)data;

	const LLUUID& item_id = self->childGetValue("clothing_list").asUUID();
	self->mSelectedID = item_id;

	LLVOAvatar* avatar = gAgent.getAvatarObject();
	if( !avatar ) return;

	if(gAgent.isWearingItem( item_id ) )
	{
		// If already wearing, can't wear again
		self->childDisable("wear_btn");

		LLInventoryItem* inv_item = gInventory.getItem(item_id);
		if (!inv_item) return;

		EWearableType wearable_type = (EWearableType)inv_item->getFlags();
		BOOL can_take_off = wearable_can_take_off(wearable_type);
		self->childSetEnabled("take_off_btn", can_take_off);
	}
	else
	{
		// Can always wear something you're not wearing now
		self->childEnable("wear_btn");
		self->childEnable("take_off_btn");
	}
}