llwldaycycle.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 #include "llwldaycycle.h"
00035 #include "llsdserialize.h"
00036 #include "llwlparammanager.h"
00037 
00038 #include "llviewerwindow.h"
00039 
00040 #include <map>
00041 
00042 LLWLDayCycle::LLWLDayCycle() : mDayRate(120)
00043 {
00044 }
00045 
00046 
00047 LLWLDayCycle::~LLWLDayCycle()
00048 {
00049 }
00050 
00051 void LLWLDayCycle::loadDayCycle(const LLString & fileName)
00052 {
00053         // clear the first few things
00054         mTimeMap.clear();
00055 
00056         // now load the file
00057         LLString pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, 
00058                 "windlight/days", fileName));
00059         llinfos << "Loading DayCycle settings from " << pathName << llendl;
00060         
00061         llifstream day_cycle_xml(pathName.c_str());
00062         if (day_cycle_xml.is_open())
00063         {
00064                 // load and parse it
00065                 LLSD day_data(LLSD::emptyArray());
00066                 LLPointer<LLSDParser> parser = new LLSDXMLParser();
00067                 parser->parse(day_cycle_xml, day_data, LLSDSerialize::SIZE_UNLIMITED);
00068 
00069                 // add each key
00070                 for(S32 i = 0; i < day_data.size(); ++i)
00071                 {
00072                         // make sure it's a two array
00073                         if(day_data[i].size() != 2)
00074                         {
00075                                 continue;
00076                         }
00077                         
00078                         // check each param name exists in param manager
00079                         bool success;
00080                         LLWLParamSet pset;
00081                         success = LLWLParamManager::instance()->getParamSet(day_data[i][1].asString(), pset);
00082                         if(!success)
00083                         {
00084                                 // alert the user
00085                                 LLString::format_map_t args;
00086                                 args["[SKY]"] = day_data[i][1].asString();
00087                                 gViewerWindow->alertXml("WLMissingSky", args);
00088                                 continue;
00089                         }
00090                         
00091                         // then add the key
00092                         addKey((F32)day_data[i][0].asReal(), day_data[i][1].asString());
00093                 }
00094 
00095                 day_cycle_xml.close();
00096         }
00097 }
00098 
00099 void LLWLDayCycle::saveDayCycle(const LLString & fileName)
00100 {
00101         LLSD day_data(LLSD::emptyArray());
00102 
00103         LLString pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight/days", fileName));
00104         //llinfos << "Saving WindLight settings to " << pathName << llendl;
00105 
00106         for(std::map<F32, std::string>::const_iterator mIt = mTimeMap.begin();
00107                 mIt != mTimeMap.end();
00108                 ++mIt) 
00109         {
00110                 LLSD key(LLSD::emptyArray());
00111                 key.append(mIt->first);
00112                 key.append(mIt->second);
00113                 day_data.append(key);
00114         }
00115 
00116         std::ofstream day_cycle_xml(pathName.c_str());
00117         LLPointer<LLSDFormatter> formatter = new LLSDXMLFormatter();
00118         formatter->format(day_data, day_cycle_xml, LLSDFormatter::OPTIONS_PRETTY);
00119 
00120         //day_cycle_xml.close();
00121 }
00122 
00123 
00124 void LLWLDayCycle::clearKeys()
00125 {
00126         mTimeMap.clear();
00127 }
00128 
00129 
00130 bool LLWLDayCycle::addKey(F32 newTime, const LLString & paramName)
00131 {
00132         // no adding negative time
00133         if(newTime < 0) 
00134         {
00135                 newTime = 0;
00136         }
00137 
00138         // if time not being used, add it and return true
00139         if(mTimeMap.find(newTime) == mTimeMap.end()) 
00140         {
00141                 mTimeMap.insert(std::pair<F32, std::string>(newTime, paramName));
00142                 return true;
00143         }
00144 
00145         // otherwise, don't add, and return error
00146         return false;
00147 }
00148 
00149 bool LLWLDayCycle::changeKeyTime(F32 oldTime, F32 newTime)
00150 {
00151         // just remove and add back
00152         std::string name = mTimeMap[oldTime];
00153 
00154         bool stat = removeKey(oldTime);
00155         if(stat == false) 
00156         {
00157                 return stat;
00158         }
00159 
00160         return addKey(newTime, name);
00161 }
00162 
00163 bool LLWLDayCycle::changeKeyParam(F32 time, const LLString & name)
00164 {
00165         // just remove and add back
00166         // make sure param exists
00167         LLWLParamSet tmp;
00168         bool stat = LLWLParamManager::instance()->getParamSet(name, tmp);
00169         if(stat == false) 
00170         {
00171                 return stat;
00172         }
00173 
00174         mTimeMap[time] = name;
00175         return true;
00176 }
00177 
00178 
00179 bool LLWLDayCycle::removeKey(F32 time)
00180 {
00181         // look for the time.  If there, erase it
00182         std::map<F32, std::string>::iterator mIt = mTimeMap.find(time);
00183         if(mIt != mTimeMap.end()) 
00184         {
00185                 mTimeMap.erase(mIt);
00186                 return true;
00187         }
00188 
00189         return false;
00190 }
00191 
00192 bool LLWLDayCycle::getKey(const LLString & name, F32& key)
00193 {
00194         // scroll through till we find the 
00195         std::map<F32, std::string>::iterator mIt = mTimeMap.begin();
00196         for(; mIt != mTimeMap.end(); ++mIt) 
00197         {
00198                 if(name == mIt->second) 
00199                 {
00200                         key = mIt->first;
00201                         return true;
00202                 }
00203         }
00204 
00205         return false;
00206 }
00207 
00208 bool LLWLDayCycle::getKeyedParam(F32 time, LLWLParamSet& param)
00209 {
00210         // just scroll on through till you find it
00211         std::map<F32, std::string>::iterator mIt = mTimeMap.find(time);
00212         if(mIt != mTimeMap.end()) 
00213         {
00214                 return LLWLParamManager::instance()->getParamSet(mIt->second, param);
00215         }
00216 
00217         // return error if not found
00218         return false;
00219 }
00220 
00221 bool LLWLDayCycle::getKeyedParamName(F32 time, LLString & name)
00222 {
00223         // just scroll on through till you find it
00224         std::map<F32, std::string>::iterator mIt = mTimeMap.find(time);
00225         if(mIt != mTimeMap.end()) 
00226         {
00227                 name = mTimeMap[time];
00228                 return true;
00229         }
00230 
00231         // return error if not found
00232         return false;
00233 }

Generated on Fri May 16 08:34:25 2008 for SecondLife by  doxygen 1.5.5