summaryrefslogtreecommitdiff
path: root/indra/newview/llwldaycycle.cpp
blob: e646b605a986a53d97a7205c497e02d49e59dbb1 (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
/**
 * @file llwldaycycle.cpp
 * @brief Implementation for the LLWLDayCycle class.
 *
 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 * Second Life Viewer Source Code
 * 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.
 * 
 * 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.
 * 
 * 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
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llwldaycycle.h"
#include "llsdserialize.h"
#include "llwlparammanager.h"
#include "llfloaterdaycycle.h"
#include "llnotifications.h"

#include "llviewerwindow.h"

#include <map>

LLWLDayCycle::LLWLDayCycle() : mDayRate(120)
{
}


LLWLDayCycle::~LLWLDayCycle()
{
}

void LLWLDayCycle::loadDayCycle(const LLSD& day_data, LLWLParamKey::EScope scope)
{
	lldebugs << "Loading day cycle (day_data.size() = " << day_data.size() << ", scope = " << scope << ")" << llendl;
	mTimeMap.clear();

	// add each key frame
	for(S32 i = 0; i < day_data.size(); ++i)
	{
		// make sure it's a two array
		if(day_data[i].size() != 2)
		{
			continue;
		}
		
		// check each param key exists in param manager
		bool success;
		LLWLParamSet pset;
		LLWLParamKey frame = LLWLParamKey(day_data[i][1].asString(), scope);
		success =
			LLWLParamManager::getInstance()->getParamSet(frame, pset);
		if(!success)
		{
			// *HACK: If loading region day cycle, try local sky presets as well.
			// Local presets may be referenced by a region day cycle after
			// it has been edited but the changes have not been uploaded.
			if (scope == LLEnvKey::SCOPE_REGION)
			{
				frame.scope = LLEnvKey::SCOPE_LOCAL;
				success = LLWLParamManager::getInstance()->getParamSet(frame, pset);
			}

			if (!success)
			{
				// alert the user
				LLSD args;
				args["SKY"] = day_data[i][1].asString();
				LLNotifications::instance().add("WLMissingSky", args, LLSD());
				continue;
			}
		}
		
		// then add the keyframe
		addKeyframe((F32)day_data[i][0].asReal(), frame);
	}
}

void LLWLDayCycle::loadDayCycleFromFile(const std::string & fileName)
{
	loadDayCycle(loadCycleDataFromFile(fileName), LLWLParamKey::SCOPE_LOCAL);
}

/*static*/ LLSD LLWLDayCycle::loadCycleDataFromFile(const std::string & fileName)
{
	// *FIX: Cannot load user day cycles.
	std::string pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, 
		"windlight/days", fileName));

	return loadDayCycleFromPath(pathName);
}

// static
LLSD LLWLDayCycle::loadDayCycleFromPath(const std::string& file_path)
{
	LL_INFOS("Windlight") << "Loading DayCycle settings from " << file_path << LL_ENDL;
	
	llifstream day_cycle_xml(file_path);
	if (day_cycle_xml.is_open())
	{
		// load and parse it
		LLSD day_data(LLSD::emptyArray());
		LLPointer<LLSDParser> parser = new LLSDXMLParser();
		parser->parse(day_cycle_xml, day_data, LLSDSerialize::SIZE_UNLIMITED);
		day_cycle_xml.close();
		return day_data;
	}
	else
	{
		return LLSD();
	}
}

void LLWLDayCycle::saveDayCycle(const std::string & fileName)
{
	std::string pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight/days", fileName));
	//llinfos << "Saving WindLight settings to " << pathName << llendl;

	save(pathName);
}

void LLWLDayCycle::save(const std::string& file_path)
{
	LLSD day_data = asLLSD();

	llofstream day_cycle_xml(file_path);
	LLPointer<LLSDFormatter> formatter = new LLSDXMLFormatter();
	formatter->format(day_data, day_cycle_xml, LLSDFormatter::OPTIONS_PRETTY);
	day_cycle_xml.close();
}

LLSD LLWLDayCycle::asLLSD()
{
	LLSD day_data(LLSD::emptyArray());
	for(std::map<F32, LLWLParamKey>::const_iterator mIt = mTimeMap.begin(); mIt != mTimeMap.end(); ++mIt) 
	{
		LLSD key(LLSD::emptyArray());
		key.append(mIt->first);
		key.append(mIt->second.name);
		day_data.append(key);
	}

	lldebugs << "Dumping day cycle (" << mTimeMap.size() << ") to LLSD: " << day_data << llendl;
	return day_data;
}

bool LLWLDayCycle::getSkyRefs(std::map<LLWLParamKey, LLWLParamSet>& refs) const
{
	bool result = true;
	LLWLParamManager& wl_mgr = LLWLParamManager::instance();

	refs.clear();
	for (std::map<F32, LLWLParamKey>::const_iterator iter = mTimeMap.begin(); iter != mTimeMap.end(); ++iter)
	{
		const LLWLParamKey& key = iter->second;
		if (!wl_mgr.getParamSet(key, refs[key]))
		{
			llwarns << "Cannot find sky [" << key.name << "] referenced by a day cycle" << llendl;
			result = false;
		}
	}

	return result;
}

bool LLWLDayCycle::getSkyMap(LLSD& sky_map) const
{
	std::map<LLWLParamKey, LLWLParamSet> refs;

	if (!getSkyRefs(refs))
	{
		return false;
	}

	sky_map = LLWLParamManager::createSkyMap(refs);
	return true;
}

void LLWLDayCycle::clearKeyframes()
{
	lldebugs << "Clearing key frames" << llendl;
	mTimeMap.clear();
}


bool LLWLDayCycle::addKeyframe(F32 newTime, LLWLParamKey frame)
{
	// no adding negative time
	if(newTime < 0) 
	{
		newTime = 0;
	}

	// if time not being used, add it and return true
	if(mTimeMap.find(newTime) == mTimeMap.end()) 
	{
		mTimeMap.insert(std::pair<F32, LLWLParamKey>(newTime, frame));
		lldebugs << "Adding key frame (" << newTime << ", " << frame.toLLSD() << ")" << llendl;
		return true;
	}

	// otherwise, don't add, and return error
	llwarns << "Error adding key frame (" << newTime << ", " << frame.toLLSD() << ")" << llendl;
	return false;
}

bool LLWLDayCycle::changeKeyframeTime(F32 oldTime, F32 newTime)
{
	lldebugs << "Changing key frame time (" << oldTime << " => " << newTime << ")" << llendl;

	// just remove and add back
	LLWLParamKey frame = mTimeMap[oldTime];

	bool stat = removeKeyframe(oldTime);
	if(stat == false) 
	{
		lldebugs << "Failed to change key frame time (" << oldTime << " => " << newTime << ")" << llendl;
		return stat;
	}

	return addKeyframe(newTime, frame);
}

bool LLWLDayCycle::changeKeyframeParam(F32 time, LLWLParamKey key)
{
	lldebugs << "Changing key frame param (" << time << ", " << key.toLLSD() << ")" << llendl;

	// just remove and add back
	// make sure param exists
	LLWLParamSet tmp;
	bool stat = LLWLParamManager::getInstance()->getParamSet(key, tmp);
	if(stat == false) 
	{
		lldebugs << "Failed to change key frame param (" << time << ", " << key.toLLSD() << ")" << llendl;
		return stat;
	}

	mTimeMap[time] = key;
	return true;
}


bool LLWLDayCycle::removeKeyframe(F32 time)
{
	lldebugs << "Removing key frame (" << time << ")" << llendl;

	// look for the time.  If there, erase it
	std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
	if(mIt != mTimeMap.end()) 
	{
		mTimeMap.erase(mIt);
		return true;
	}

	return false;
}

bool LLWLDayCycle::getKeytime(LLWLParamKey frame, F32& key_time) const
{
	// scroll through till we find the correct value in the map
	std::map<F32, LLWLParamKey>::const_iterator mIt = mTimeMap.begin();
	for(; mIt != mTimeMap.end(); ++mIt) 
	{
		if(frame == mIt->second) 
		{
			key_time = mIt->first;
			return true;
		}
	}

	return false;
}

bool LLWLDayCycle::getKeyedParam(F32 time, LLWLParamSet& param)
{
	// just scroll on through till you find it
	std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
	if(mIt != mTimeMap.end())
	{
		return LLWLParamManager::getInstance()->getParamSet(mIt->second, param);
	}

	// return error if not found
	lldebugs << "Key " << time << " not found" << llendl;
	return false;
}

bool LLWLDayCycle::getKeyedParamName(F32 time, std::string & name)
{
	// just scroll on through till you find it
	std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
	if(mIt != mTimeMap.end()) 
	{
		name = mTimeMap[time].name;
		return true;
	}

	// return error if not found
	lldebugs << "Key " << time << " not found" << llendl;
	return false;
}

bool LLWLDayCycle::hasReferencesTo(const LLWLParamKey& keyframe) const
{
	F32 dummy;
	return getKeytime(keyframe, dummy);
}

void LLWLDayCycle::removeReferencesTo(const LLWLParamKey& keyframe)
{
	lldebugs << "Removing references to key frame " << keyframe.toLLSD() << llendl;
	F32 keytime;
	bool might_exist;
	do 
	{
		// look for it
		might_exist = getKeytime(keyframe, keytime);
		if(!might_exist)
		{
			return;
		}
		might_exist = removeKeyframe(keytime);

	} while(might_exist); // might be another one
}