summaryrefslogtreecommitdiff
path: root/indra/newview/llfloatereditdaycycle.cpp
blob: a62f348129fa836dc73f9901223846cfdc5145ff (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/** 
 * @file llfloatereditdaycycle.cpp
 * @brief Floater to create or edit a day cycle
 *
 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2011, 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 "llfloatereditdaycycle.h"

// libs
#include "llbutton.h"
#include "llcheckboxctrl.h"
#include "llcombobox.h"
#include "llloadingindicator.h"
#include "llmultisliderctrl.h"
#include "llnotifications.h"
#include "llnotificationsutil.h"
#include "llspinctrl.h"
#include "lltimectrl.h"

// newview
#include "llagent.h"
#include "lldaycyclemanager.h"
#include "llenvmanager.h"
#include "llregioninfomodel.h"
#include "llviewerregion.h"
#include "llwlparammanager.h"

const F32 LLFloaterEditDayCycle::sHoursPerDay = 24.0f;

LLFloaterEditDayCycle::LLFloaterEditDayCycle(const LLSD &key)
:	LLFloater(key)
,	mDayCycleNameEditor(NULL)
,	mDayCyclesCombo(NULL)
,	mTimeSlider(NULL)
,	mKeysSlider(NULL)
,	mSkyPresetsCombo(NULL)
,	mTimeCtrl(NULL)
,	mMakeDefaultCheckBox(NULL)
,	mSaveButton(NULL)
{
}

// virtual
BOOL LLFloaterEditDayCycle::postBuild()
{
	mDayCycleNameEditor = getChild<LLLineEditor>("day_cycle_name");
	mDayCyclesCombo = getChild<LLComboBox>("day_cycle_combo");

	mTimeSlider = getChild<LLMultiSliderCtrl>("WLTimeSlider");
	mKeysSlider = getChild<LLMultiSliderCtrl>("WLDayCycleKeys");
	mSkyPresetsCombo = getChild<LLComboBox>("WLSkyPresets");
	mTimeCtrl = getChild<LLTimeCtrl>("time");
	mSaveButton = getChild<LLButton>("save");
	mMakeDefaultCheckBox = getChild<LLCheckBoxCtrl>("make_default_cb");

	initCallbacks();

	// add the time slider
	mTimeSlider->addSlider();

	return TRUE;
}

// virtual
void LLFloaterEditDayCycle::onOpen(const LLSD& key)
{
	bool new_day = isNewDay();
	std::string param = key.asString();
	std::string floater_title = getString(std::string("title_") + param);
	std::string hint = getString(std::string("hint_" + param));

	// Update floater title.
	setTitle(floater_title);

	// Update the hint at the top.
	getChild<LLUICtrl>("hint")->setValue(hint);

	// Hide the hint to the right of the combo if we're invoked to create a new preset.
	getChildView("note")->setVisible(!new_day);

	// Switch between the day cycle presets combobox and day cycle name input field.
	mDayCyclesCombo->setVisible(!new_day);
	mDayCycleNameEditor->setVisible(new_day);

	// TODO: Make sure only one instance of the floater exists?

	reset();
}

// virtual
void LLFloaterEditDayCycle::onClose(bool app_quitting)
{
	if (!app_quitting) // there's no point to change environment if we're quitting
	{
		LLEnvManagerNew::instance().usePrefs(); // revert changes made to current day cycle
	}
}

// virtual
void LLFloaterEditDayCycle::draw()
{
	syncTimeSlider();
	LLFloater::draw();
}

void LLFloaterEditDayCycle::initCallbacks(void)
{
	mDayCycleNameEditor->setKeystrokeCallback(boost::bind(&LLFloaterEditDayCycle::onDayCycleNameEdited, this), NULL);
	mDayCyclesCombo->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onDayCycleSelected, this));
	mTimeSlider->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onTimeSliderMoved, this));
	mKeysSlider->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onKeyTimeMoved, this));
	mTimeCtrl->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onKeyTimeChanged, this));
	mSkyPresetsCombo->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onKeyPresetChanged, this));

	getChild<LLButton>("WLAddKey")->setClickedCallback(boost::bind(&LLFloaterEditDayCycle::onAddKey, this));
	getChild<LLButton>("WLDeleteKey")->setClickedCallback(boost::bind(&LLFloaterEditDayCycle::onDeleteKey, this));

	mSaveButton->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onBtnSave, this));
	mSaveButton->setRightMouseDownCallback(boost::bind(&LLFloaterEditDayCycle::dumpTrack, this));
	getChild<LLButton>("cancel")->setCommitCallback(boost::bind(&LLFloaterEditDayCycle::onBtnCancel, this));

	// Connect to env manager events.
	LLEnvManagerNew& env_mgr = LLEnvManagerNew::instance();
	env_mgr.setRegionSettingsChangeCallback(boost::bind(&LLFloaterEditDayCycle::onRegionSettingsChange, this));
	env_mgr.setRegionChangeCallback(boost::bind(&LLFloaterEditDayCycle::onRegionChange, this));
	env_mgr.setRegionSettingsAppliedCallback(boost::bind(&LLFloaterEditDayCycle::onRegionSettingsApplied, this, _1));

	// Connect to region info updates.
	LLRegionInfoModel::instance().setUpdateCallback(boost::bind(&LLFloaterEditDayCycle::onRegionInfoUpdate, this));
}

void LLFloaterEditDayCycle::syncTimeSlider()
{
	// set time
	mTimeSlider->setCurSliderValue((F32)LLWLParamManager::getInstance()->mAnimator.getDayTime() * sHoursPerDay);
}

void LLFloaterEditDayCycle::loadTrack()
{
	// clear the slider
	mKeysSlider->clear();
	mSliderToKey.clear();

	// add sliders

	lldebugs << "Adding " << LLWLParamManager::getInstance()->mDay.mTimeMap.size() << " keys to slider" << llendl;

	LLWLDayCycle& cur_dayp = LLWLParamManager::instance().mDay;
	for (std::map<F32, LLWLParamKey>::iterator it = cur_dayp.mTimeMap.begin(); it != cur_dayp.mTimeMap.end(); ++it)
	{
		addSliderKey(it->first * sHoursPerDay, it->second);
	}

	// set drop-down menu to match preset of currently-selected keyframe (one is automatically selected initially)
	const std::string& cur_sldr = mKeysSlider->getCurSlider();
	if (strlen(cur_sldr.c_str()) > 0)	// only do this if there is a curSldr, otherwise we put an invalid entry into the map
	{
		mSkyPresetsCombo->selectByValue(mSliderToKey[cur_sldr].keyframe.toStringVal());
	}

	syncTimeSlider();
}

void LLFloaterEditDayCycle::applyTrack()
{
	lldebugs << "Applying track (" << mSliderToKey.size() << ")" << llendl;

	// if no keys, do nothing
	if (mSliderToKey.size() == 0)
	{
		lldebugs << "No keys, not syncing" << llendl;
		return;
	}

	llassert_always(mSliderToKey.size() == mKeysSlider->getValue().size());

	// create a new animation track
	LLWLParamManager::getInstance()->mDay.clearKeyframes();

	// add the keys one by one
	for (std::map<std::string, SliderKey>::iterator it = mSliderToKey.begin();
		it != mSliderToKey.end(); ++it)
	{
		LLWLParamManager::getInstance()->mDay.addKeyframe(it->second.time / sHoursPerDay,
			it->second.keyframe);
	}

	// set the param manager's track to the new one
	LL_DEBUGS("Windlight") << "Time slider val: " << mTimeSlider->getCurSliderValue() / sHoursPerDay << " (" << mTimeSlider->getCurSliderValue() << ")" << LL_ENDL;
	LLWLParamManager::getInstance()->resetAnimator(
		mTimeSlider->getCurSliderValue() / sHoursPerDay, false);

	LLWLParamManager::getInstance()->mAnimator.update(
		LLWLParamManager::getInstance()->mCurParams);
}

void LLFloaterEditDayCycle::refreshSkyPresetsList()
{
	LLWLParamManager& sky_mgr = LLWLParamManager::instance();
	for (std::map<LLWLParamKey, LLWLParamSet>::iterator it = sky_mgr.mParamList.begin();
		it != sky_mgr.mParamList.end(); ++it)
	{
		const LLWLParamKey& key = it->first;

		std::string item_title = key.name;
		if (key.scope == LLEnvKey::SCOPE_REGION)
		{
			item_title += " (" + getRegionName() + ")";
		}

		mSkyPresetsCombo->add(item_title, LLSD(key.toStringVal()));
	}

	// set defaults on combo boxes
	mSkyPresetsCombo->selectFirstItem();
}

void LLFloaterEditDayCycle::refreshDayCyclesList()
{
	llassert(isNewDay() == false);

	mDayCyclesCombo->removeall();

	const LLSD& region_day = LLEnvManagerNew::instance().getRegionSettings().getWLDayCycle();
	if (region_day.size() > 0)
	{
		LLWLParamKey key(getRegionName(), LLEnvKey::SCOPE_REGION);
		mDayCyclesCombo->add(key.name, key.toLLSD());
		mDayCyclesCombo->addSeparator();
	}

	const LLDayCycleManager::dc_map_t& map = LLDayCycleManager::instance().getPresets();
	for (LLDayCycleManager::dc_map_t::const_iterator it = map.begin(); it != map.end(); ++it)
	{
		LLWLParamKey key(it->first, LLEnvKey::SCOPE_LOCAL);
		mDayCyclesCombo->add(key.name, key.toLLSD());
	}

	mDayCyclesCombo->setLabel(getString("combo_label"));
}

void LLFloaterEditDayCycle::onTimeSliderMoved()
{
	/// get the slider value
	F32 val = mTimeSlider->getCurSliderValue() / sHoursPerDay;
	LL_DEBUGS("Windlight") << "Time slider val: " << val << " (" << mTimeSlider->getCurSliderValue() << ")" << LL_ENDL;

	// set the value, turn off animation
	LLWLParamManager::getInstance()->mAnimator.setDayTime((F64)val);
	LLWLParamManager::getInstance()->mAnimator.deactivate();

	// then call update once
	LLWLParamManager::getInstance()->mAnimator.update(
		LLWLParamManager::getInstance()->mCurParams);
}

void LLFloaterEditDayCycle::onKeyTimeMoved()
{
	if (mKeysSlider->getValue().size() == 0)
	{
		return;
	}

	// make sure we have a slider
	const std::string& cur_sldr = mKeysSlider->getCurSlider();
	if (cur_sldr == "")
	{
		return;
	}

	F32 time24 = mKeysSlider->getCurSliderValue();

	// check to see if a key exists
	LLWLParamKey key = mSliderToKey[cur_sldr].keyframe;
	lldebugs << "Setting key time: " << time24 << LL_ENDL;
	mSliderToKey[cur_sldr].time = time24;

	// if it exists, turn on check box
	mSkyPresetsCombo->selectByValue(key.toStringVal());

	mTimeCtrl->setTime24(time24);

	applyTrack();
}

void LLFloaterEditDayCycle::onKeyTimeChanged()
{
	// if no keys, skipped
	if (mSliderToKey.size() == 0)
	{
		return;
	}

	F32 time24 = mTimeCtrl->getTime24();

	const std::string& cur_sldr = mKeysSlider->getCurSlider();
	mKeysSlider->setCurSliderValue(time24, TRUE);
	F32 time = mKeysSlider->getCurSliderValue() / sHoursPerDay;

	// now set the key's time in the sliderToKey map
	lldebugs << "Setting key time: " << time << LL_ENDL;
	mSliderToKey[cur_sldr].time = time;

	applyTrack();
}

void LLFloaterEditDayCycle::onKeyPresetChanged()
{
	// do nothing if no sliders
	if (mKeysSlider->getValue().size() == 0)
	{
		return;
	}

	// change the map

	std::string stringVal = mSkyPresetsCombo->getSelectedValue().asString();
	LLWLParamKey new_key(stringVal);
	const std::string& cur_sldr = mKeysSlider->getCurSlider();

	// if null, don't use
	if (cur_sldr == "")
	{
		return;
	}

	mSliderToKey[cur_sldr].keyframe = new_key;

	// Apply changes to current day cycle.
	applyTrack();
}

void LLFloaterEditDayCycle::onAddKey()
{
	llassert_always(mSliderToKey.size() == mKeysSlider->getValue().size());

	S32 max_sliders;
	LLEnvKey::EScope scope = LLEnvKey::SCOPE_LOCAL; // *TODO: editing region day cycle
	switch (scope)
	{
		case LLEnvKey::SCOPE_LOCAL:
			max_sliders = 20; // *HACK this should be LLWLPacketScrubber::MAX_LOCAL_KEY_FRAMES;
			break;
		case LLEnvKey::SCOPE_REGION:
			max_sliders = 12; // *HACK this should be LLWLPacketScrubber::MAX_REGION_KEY_FRAMES;
			break;
		default:
			max_sliders = (S32) mKeysSlider->getMaxValue();
			break;
	}

	if ((S32)mSliderToKey.size() >= max_sliders)
	{
		LLSD args;
		args["SCOPE"] = LLEnvManager::getScopeString(scope);
		args["MAX"] = max_sliders;
		LLNotificationsUtil::add("DayCycleTooManyKeyframes", args, LLSD(), LLNotificationFunctorRegistry::instance().DONOTHING);
		return;
	}

	// add the slider key
	LLWLParamKey sky_params(mSkyPresetsCombo->getSelectedValue());
	F32 time = mTimeSlider->getCurSliderValue();
	addSliderKey(time, sky_params);

	// apply the change to current day cycles
	applyTrack();
}

void LLFloaterEditDayCycle::addSliderKey(F32 time, LLWLParamKey keyframe)
{
	// make a slider
	const std::string& sldr_name = mKeysSlider->addSlider(time);
	if (sldr_name.empty())
	{
		return;
	}

	// set the key
	SliderKey newKey(keyframe, mKeysSlider->getCurSliderValue());

	llassert_always(sldr_name != LLStringUtil::null);

	// add to map
	mSliderToKey.insert(std::pair<std::string, SliderKey>(sldr_name, newKey));

	llassert_always(mSliderToKey.size() == mKeysSlider->getValue().size());
}

LLWLParamKey LLFloaterEditDayCycle::getSelectedDayCycle()
{
	LLWLParamKey dc_key;

	if (mDayCycleNameEditor->getVisible())
	{
		dc_key.name = mDayCycleNameEditor->getText();
		dc_key.scope = LLEnvKey::SCOPE_LOCAL;
	}
	else
	{
		LLSD combo_val = mDayCyclesCombo->getValue();

		if (!combo_val.isArray()) // manually typed text
		{
			dc_key.name = combo_val.asString();
			dc_key.scope = LLEnvKey::SCOPE_LOCAL;
		}
		else
		{
			dc_key.fromLLSD(combo_val);
		}
	}

	return dc_key;
}

void LLFloaterEditDayCycle::deletePreset(LLWLParamKey keyframe)
{
	// *TODO: This should be called when a sky preset gets deleted.

	/// delete any reference
	std::map<std::string, SliderKey>::iterator curr_preset, next_preset;
	for (curr_preset = mSliderToKey.begin(); curr_preset != mSliderToKey.end(); curr_preset = next_preset)
	{
		next_preset = curr_preset;
		++next_preset;
		if (curr_preset->second.keyframe == keyframe)
		{
			mKeysSlider->deleteSlider(curr_preset->first);
			mSliderToKey.erase(curr_preset);
		}
	}
}

bool LLFloaterEditDayCycle::isNewDay() const
{
	return mKey.asString() == "new";
}

void LLFloaterEditDayCycle::dumpTrack()
{
	LL_DEBUGS("Windlight") << "Dumping day cycle" << LL_ENDL;

	LLWLDayCycle& cur_dayp = LLWLParamManager::instance().mDay;
	for (std::map<F32, LLWLParamKey>::iterator it = cur_dayp.mTimeMap.begin(); it != cur_dayp.mTimeMap.end(); ++it)
	{
		F32 time = it->first * 24.0f;
		S32 h = (S32) time;
		S32 m = (S32) ((time - h) * 60.0f);
		LL_DEBUGS("Windlight") << llformat("(%.3f) %02d:%02d", time, h, m) << " => " << it->second.name << LL_ENDL;
	}
}

void LLFloaterEditDayCycle::enableEditing(bool enable)
{
	mSkyPresetsCombo->setEnabled(enable);
	mTimeCtrl->setEnabled(enable);
	getChild<LLPanel>("day_cycle_slider_panel")->setCtrlsEnabled(enable);
	mSaveButton->setEnabled(enable);
	mMakeDefaultCheckBox->setEnabled(enable);
}

void LLFloaterEditDayCycle::reset()
{
	// clear the slider
	mKeysSlider->clear();
	mSliderToKey.clear();

	refreshSkyPresetsList();

	if (isNewDay())
	{
		mDayCycleNameEditor->setValue(LLSD());
		F32 time = 0.5f * sHoursPerDay;
		mSaveButton->setEnabled(FALSE); // will be enabled as soon as users enters a name
		mTimeSlider->setCurSliderValue(time);

		addSliderKey(time, LLWLParamKey("Default", LLEnvKey::SCOPE_LOCAL));
		onKeyTimeMoved(); // update the time control and sky sky combo

		applyTrack();
	}
	else
	{
		refreshDayCyclesList();

		// Disable controls until a day cycle  to edit is selected.
		enableEditing(false);
	}
}

void LLFloaterEditDayCycle::saveRegionDayCycle()
{
	LLEnvManagerNew& env_mgr = LLEnvManagerNew::instance();
	LLWLDayCycle& cur_dayp = LLWLParamManager::instance().mDay; // the day cycle being edited

	// Get current day cycle and the sky preset it references.
	LLSD day_cycle = cur_dayp.asLLSD();
	LLSD sky_map;
	cur_dayp.getSkyMap(sky_map);

	// Apply it to the region.
	LLEnvironmentSettings new_region_settings;
	new_region_settings.saveParams(day_cycle, sky_map, env_mgr.getRegionSettings().getWaterParams(), 0.0f);

	if (!LLEnvManagerNew::instance().sendRegionSettings(new_region_settings))
	{
		llwarns << "Error applying region environment settings" << llendl;
		return;
	}

	setApplyProgress(true);
}

void LLFloaterEditDayCycle::setApplyProgress(bool started)
{
	LLLoadingIndicator* indicator = getChild<LLLoadingIndicator>("progress_indicator");

	indicator->setVisible(started);

	if (started)
	{
		indicator->start();
	}
	else
	{
		indicator->stop();
	}
}

bool LLFloaterEditDayCycle::getApplyProgress() const
{
	return getChild<LLLoadingIndicator>("progress_indicator")->getVisible();
}

void LLFloaterEditDayCycle::onDeleteKey()
{
	if (mSliderToKey.size() == 0)
	{
		return;
	}
	else if (mSliderToKey.size() == 1)
	{
		LLNotifications::instance().add("EnvCannotDeleteLastDayCycleKey", LLSD(), LLSD());
		return;
	}

	// delete from map
	const std::string& sldr_name = mKeysSlider->getCurSlider();
	std::map<std::string, SliderKey>::iterator mIt = mSliderToKey.find(sldr_name);
	mSliderToKey.erase(mIt);

	mKeysSlider->deleteCurSlider();

	if (mSliderToKey.size() == 0)
	{
		return;
	}

	const std::string& name = mKeysSlider->getCurSlider();
	mSkyPresetsCombo->selectByValue(mSliderToKey[name].keyframe.toStringVal());
	F32 time24 = mSliderToKey[name].time;

	mTimeCtrl->setTime24(time24);

	applyTrack();
}

void LLFloaterEditDayCycle::onRegionSettingsChange()
{
	LL_DEBUGS("Windlight") << "Region settings changed" << LL_ENDL;

	if (getApplyProgress()) // our region settings have being applied
	{
		setApplyProgress(false);

		// Change preference if requested.
		if (mMakeDefaultCheckBox->getValue())
		{
			LL_DEBUGS("Windlight") << "Changed environment preference to region settings" << llendl;
			LLEnvManagerNew::instance().setUseRegionSettings(true);
		}

		closeFloater();
	}
}

void LLFloaterEditDayCycle::onRegionChange()
{
	LL_DEBUGS("Windlight") << "Region changed" << LL_ENDL;

	// If we're editing the region day cycle
	if (getSelectedDayCycle().scope == LLEnvKey::SCOPE_REGION)
	{
		reset(); // undoes all unsaved changes
	}
}

void LLFloaterEditDayCycle::onRegionSettingsApplied(bool success)
{
	LL_DEBUGS("Windlight") << "Region settings applied: " << success << LL_ENDL;

	if (!success)
	{
		// stop progress indicator
		setApplyProgress(false);
	}
}

void LLFloaterEditDayCycle::onRegionInfoUpdate()
{
	LL_DEBUGS("Windlight") << "Region info updated" << LL_ENDL;
	bool can_edit = true;

	// If we've selected the region day cycle for editing.
	if (getSelectedDayCycle().scope == LLEnvKey::SCOPE_REGION)
	{
		// check whether we have the access
		can_edit = canEditRegionSettings();
	}

	enableEditing(can_edit);
}

void LLFloaterEditDayCycle::onDayCycleNameEdited()
{
	// Disable saving a day cycle having empty name.
	LLWLParamKey key = getSelectedDayCycle();
	mSaveButton->setEnabled(!key.name.empty());
}

void LLFloaterEditDayCycle::onDayCycleSelected()
{
	LLSD day_data;
	LLWLParamKey dc_key = getSelectedDayCycle();
	bool can_edit = true;

	if (dc_key.scope == LLEnvKey::SCOPE_LOCAL)
	{
		if (!LLDayCycleManager::instance().getPreset(dc_key.name, day_data))
		{
			llwarns << "No day cycle named " << dc_key.name << llendl;
			return;
		}
	}
	else
	{
		day_data = LLEnvManagerNew::instance().getRegionSettings().getWLDayCycle();
		if (day_data.size() == 0)
		{
			llwarns << "Empty region day cycle" << llendl;
			llassert(day_data.size() > 0);
			return;
		}

		can_edit = canEditRegionSettings();
	}

	F32 slider_time = mTimeSlider->getCurSliderValue() / sHoursPerDay;
	LL_DEBUGS("Windlight") << "Time slider val: " << slider_time << " (" << mTimeSlider->getCurSliderValue() << ")" << LL_ENDL;
	LLWLParamManager::instance().applyDayCycleParams(day_data, dc_key.scope, slider_time);
	loadTrack();

	enableEditing(can_edit);
}

void LLFloaterEditDayCycle::onBtnSave()
{
	LLDayCycleManager& day_mgr = LLDayCycleManager::instance();
	LLWLParamKey selected_day = getSelectedDayCycle();

	if (selected_day.scope == LLEnvKey::SCOPE_REGION)
	{
		saveRegionDayCycle();
		return;
	}

	std::string name = selected_day.name;
	if (name.empty())
	{
		// *TODO: show an alert
		llwarns << "Empty day cycle name" << llendl;
		return;
	}

	// Don't allow overwriting system presets.
	if (day_mgr.isSystemPreset(name))
	{
		LLNotificationsUtil::add("WLNoEditDefault");
		return;
	}

	// Save, ask for confirmation for overwriting an existing preset.
	if (day_mgr.presetExists(name))
	{
		LLNotificationsUtil::add("WLSavePresetAlert", LLSD(), LLSD(), boost::bind(&LLFloaterEditDayCycle::onSaveAnswer, this, _1, _2));
	}
	else
	{
		// new preset, hence no confirmation needed
		onSaveConfirmed();
	}
}

void LLFloaterEditDayCycle::onBtnCancel()
{
#if 0 // temporary
	onClose(false);
	reset();
#else
	closeFloater();
#endif
}

bool LLFloaterEditDayCycle::onSaveAnswer(const LLSD& notification, const LLSD& response)
{
	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);

	// If they choose save, do it.  Otherwise, don't do anything
	if (option == 0)
	{
		onSaveConfirmed();
	}

	return false;
}

void LLFloaterEditDayCycle::onSaveConfirmed()
{
	std::string name = getSelectedDayCycle().name;

	// Save preset.
	LLSD data = LLWLParamManager::instance().mDay.asLLSD();
	LL_DEBUGS("Windlight") << "Saving day cycle " << name << ": " << data << LL_ENDL;
	LLDayCycleManager::instance().savePreset(name, data);

	// Change preference if requested.
	if (mMakeDefaultCheckBox->getValue())
	{
		LL_DEBUGS("Windlight") << name << " is now the new preferred day cycle" << llendl;
		LLEnvManagerNew::instance().setUseDayCycle(name);
	}

#if 0 // temporary
	reset();
#else
	closeFloater();
#endif
}

// static
std::string LLFloaterEditDayCycle::getRegionName()
{
	return gAgent.getRegion() ? gAgent.getRegion()->getName() : LLTrans::getString("Unknown");
}

// static
bool LLFloaterEditDayCycle::canEditRegionSettings()
{
	LLViewerRegion* region = gAgent.getRegion();
	BOOL owner_or_god = gAgent.isGodlike() || (region && region->getOwner() == gAgent.getID());
	BOOL owner_or_god_or_manager = owner_or_god || (region && region->isEstateManager());

	LL_DEBUGS("Windlight") << "Can edit region settings: " << owner_or_god_or_manager << LL_ENDL;
	return owner_or_god_or_manager;
}