PageRenderTime 724ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llwldaycycle.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 339 lines | 235 code | 54 blank | 50 comment | 32 complexity | 56662f3c7b6e7ce65382931e8face1ee MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llwldaycycle.cpp
  3. * @brief Implementation for the LLWLDayCycle class.
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llwldaycycle.h"
  28. #include "llsdserialize.h"
  29. #include "llwlparammanager.h"
  30. #include "llnotifications.h"
  31. #include "llviewerwindow.h"
  32. #include <map>
  33. LLWLDayCycle::LLWLDayCycle() : mDayRate(120)
  34. {
  35. }
  36. LLWLDayCycle::~LLWLDayCycle()
  37. {
  38. }
  39. void LLWLDayCycle::loadDayCycle(const LLSD& day_data, LLWLParamKey::EScope scope)
  40. {
  41. lldebugs << "Loading day cycle (day_data.size() = " << day_data.size() << ", scope = " << scope << ")" << llendl;
  42. mTimeMap.clear();
  43. // add each key frame
  44. for(S32 i = 0; i < day_data.size(); ++i)
  45. {
  46. // make sure it's a two array
  47. if(day_data[i].size() != 2)
  48. {
  49. continue;
  50. }
  51. // check each param key exists in param manager
  52. bool success;
  53. LLWLParamSet pset;
  54. LLWLParamKey frame = LLWLParamKey(day_data[i][1].asString(), scope);
  55. success =
  56. LLWLParamManager::getInstance()->getParamSet(frame, pset);
  57. if(!success)
  58. {
  59. // *HACK: If loading region day cycle, try local sky presets as well.
  60. // Local presets may be referenced by a region day cycle after
  61. // it has been edited but the changes have not been uploaded.
  62. if (scope == LLEnvKey::SCOPE_REGION)
  63. {
  64. frame.scope = LLEnvKey::SCOPE_LOCAL;
  65. success = LLWLParamManager::getInstance()->getParamSet(frame, pset);
  66. }
  67. if (!success)
  68. {
  69. // alert the user
  70. LLSD args;
  71. args["SKY"] = day_data[i][1].asString();
  72. LLNotifications::instance().add("WLMissingSky", args, LLSD());
  73. continue;
  74. }
  75. }
  76. // then add the keyframe
  77. addKeyframe((F32)day_data[i][0].asReal(), frame);
  78. }
  79. }
  80. void LLWLDayCycle::loadDayCycleFromFile(const std::string & fileName)
  81. {
  82. loadDayCycle(loadCycleDataFromFile(fileName), LLWLParamKey::SCOPE_LOCAL);
  83. }
  84. /*static*/ LLSD LLWLDayCycle::loadCycleDataFromFile(const std::string & fileName)
  85. {
  86. // *FIX: Cannot load user day cycles.
  87. std::string pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS,
  88. "windlight/days", fileName));
  89. return loadDayCycleFromPath(pathName);
  90. }
  91. // static
  92. LLSD LLWLDayCycle::loadDayCycleFromPath(const std::string& file_path)
  93. {
  94. LL_INFOS("Windlight") << "Loading DayCycle settings from " << file_path << LL_ENDL;
  95. llifstream day_cycle_xml(file_path);
  96. if (day_cycle_xml.is_open())
  97. {
  98. // load and parse it
  99. LLSD day_data(LLSD::emptyArray());
  100. LLPointer<LLSDParser> parser = new LLSDXMLParser();
  101. parser->parse(day_cycle_xml, day_data, LLSDSerialize::SIZE_UNLIMITED);
  102. day_cycle_xml.close();
  103. return day_data;
  104. }
  105. else
  106. {
  107. return LLSD();
  108. }
  109. }
  110. void LLWLDayCycle::saveDayCycle(const std::string & fileName)
  111. {
  112. std::string pathName(gDirUtilp->getExpandedFilename(LL_PATH_APP_SETTINGS, "windlight/days", fileName));
  113. //llinfos << "Saving WindLight settings to " << pathName << llendl;
  114. save(pathName);
  115. }
  116. void LLWLDayCycle::save(const std::string& file_path)
  117. {
  118. LLSD day_data = asLLSD();
  119. llofstream day_cycle_xml(file_path);
  120. LLPointer<LLSDFormatter> formatter = new LLSDXMLFormatter();
  121. formatter->format(day_data, day_cycle_xml, LLSDFormatter::OPTIONS_PRETTY);
  122. day_cycle_xml.close();
  123. }
  124. LLSD LLWLDayCycle::asLLSD()
  125. {
  126. LLSD day_data(LLSD::emptyArray());
  127. for(std::map<F32, LLWLParamKey>::const_iterator mIt = mTimeMap.begin(); mIt != mTimeMap.end(); ++mIt)
  128. {
  129. LLSD key(LLSD::emptyArray());
  130. key.append(mIt->first);
  131. key.append(mIt->second.name);
  132. day_data.append(key);
  133. }
  134. lldebugs << "Dumping day cycle (" << mTimeMap.size() << ") to LLSD: " << day_data << llendl;
  135. return day_data;
  136. }
  137. bool LLWLDayCycle::getSkyRefs(std::map<LLWLParamKey, LLWLParamSet>& refs) const
  138. {
  139. bool result = true;
  140. LLWLParamManager& wl_mgr = LLWLParamManager::instance();
  141. refs.clear();
  142. for (std::map<F32, LLWLParamKey>::const_iterator iter = mTimeMap.begin(); iter != mTimeMap.end(); ++iter)
  143. {
  144. const LLWLParamKey& key = iter->second;
  145. if (!wl_mgr.getParamSet(key, refs[key]))
  146. {
  147. llwarns << "Cannot find sky [" << key.name << "] referenced by a day cycle" << llendl;
  148. result = false;
  149. }
  150. }
  151. return result;
  152. }
  153. bool LLWLDayCycle::getSkyMap(LLSD& sky_map) const
  154. {
  155. std::map<LLWLParamKey, LLWLParamSet> refs;
  156. if (!getSkyRefs(refs))
  157. {
  158. return false;
  159. }
  160. sky_map = LLWLParamManager::createSkyMap(refs);
  161. return true;
  162. }
  163. void LLWLDayCycle::clearKeyframes()
  164. {
  165. lldebugs << "Clearing key frames" << llendl;
  166. mTimeMap.clear();
  167. }
  168. bool LLWLDayCycle::addKeyframe(F32 newTime, LLWLParamKey frame)
  169. {
  170. // no adding negative time
  171. if(newTime < 0)
  172. {
  173. newTime = 0;
  174. }
  175. // if time not being used, add it and return true
  176. if(mTimeMap.find(newTime) == mTimeMap.end())
  177. {
  178. mTimeMap.insert(std::pair<F32, LLWLParamKey>(newTime, frame));
  179. lldebugs << "Adding key frame (" << newTime << ", " << frame.toLLSD() << ")" << llendl;
  180. return true;
  181. }
  182. // otherwise, don't add, and return error
  183. llwarns << "Error adding key frame (" << newTime << ", " << frame.toLLSD() << ")" << llendl;
  184. return false;
  185. }
  186. bool LLWLDayCycle::changeKeyframeTime(F32 oldTime, F32 newTime)
  187. {
  188. lldebugs << "Changing key frame time (" << oldTime << " => " << newTime << ")" << llendl;
  189. // just remove and add back
  190. LLWLParamKey frame = mTimeMap[oldTime];
  191. bool stat = removeKeyframe(oldTime);
  192. if(stat == false)
  193. {
  194. lldebugs << "Failed to change key frame time (" << oldTime << " => " << newTime << ")" << llendl;
  195. return stat;
  196. }
  197. return addKeyframe(newTime, frame);
  198. }
  199. bool LLWLDayCycle::changeKeyframeParam(F32 time, LLWLParamKey key)
  200. {
  201. lldebugs << "Changing key frame param (" << time << ", " << key.toLLSD() << ")" << llendl;
  202. // just remove and add back
  203. // make sure param exists
  204. LLWLParamSet tmp;
  205. bool stat = LLWLParamManager::getInstance()->getParamSet(key, tmp);
  206. if(stat == false)
  207. {
  208. lldebugs << "Failed to change key frame param (" << time << ", " << key.toLLSD() << ")" << llendl;
  209. return stat;
  210. }
  211. mTimeMap[time] = key;
  212. return true;
  213. }
  214. bool LLWLDayCycle::removeKeyframe(F32 time)
  215. {
  216. lldebugs << "Removing key frame (" << time << ")" << llendl;
  217. // look for the time. If there, erase it
  218. std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
  219. if(mIt != mTimeMap.end())
  220. {
  221. mTimeMap.erase(mIt);
  222. return true;
  223. }
  224. return false;
  225. }
  226. bool LLWLDayCycle::getKeytime(LLWLParamKey frame, F32& key_time) const
  227. {
  228. // scroll through till we find the correct value in the map
  229. std::map<F32, LLWLParamKey>::const_iterator mIt = mTimeMap.begin();
  230. for(; mIt != mTimeMap.end(); ++mIt)
  231. {
  232. if(frame == mIt->second)
  233. {
  234. key_time = mIt->first;
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. bool LLWLDayCycle::getKeyedParam(F32 time, LLWLParamSet& param)
  241. {
  242. // just scroll on through till you find it
  243. std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
  244. if(mIt != mTimeMap.end())
  245. {
  246. return LLWLParamManager::getInstance()->getParamSet(mIt->second, param);
  247. }
  248. // return error if not found
  249. lldebugs << "Key " << time << " not found" << llendl;
  250. return false;
  251. }
  252. bool LLWLDayCycle::getKeyedParamName(F32 time, std::string & name)
  253. {
  254. // just scroll on through till you find it
  255. std::map<F32, LLWLParamKey>::iterator mIt = mTimeMap.find(time);
  256. if(mIt != mTimeMap.end())
  257. {
  258. name = mTimeMap[time].name;
  259. return true;
  260. }
  261. // return error if not found
  262. lldebugs << "Key " << time << " not found" << llendl;
  263. return false;
  264. }
  265. bool LLWLDayCycle::hasReferencesTo(const LLWLParamKey& keyframe) const
  266. {
  267. F32 dummy;
  268. return getKeytime(keyframe, dummy);
  269. }
  270. void LLWLDayCycle::removeReferencesTo(const LLWLParamKey& keyframe)
  271. {
  272. lldebugs << "Removing references to key frame " << keyframe.toLLSD() << llendl;
  273. F32 keytime;
  274. bool might_exist;
  275. do
  276. {
  277. // look for it
  278. might_exist = getKeytime(keyframe, keytime);
  279. if(!might_exist)
  280. {
  281. return;
  282. }
  283. might_exist = removeKeyframe(keytime);
  284. } while(might_exist); // might be another one
  285. }