llvisualparam.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * @file llvisualparam.cpp
  3. * @brief Implementation of LLPolyMesh class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. //-----------------------------------------------------------------------------
  33. // Header Files
  34. //-----------------------------------------------------------------------------
  35. #include "linden_common.h"
  36. #include "llvisualparam.h"
  37. //-----------------------------------------------------------------------------
  38. // LLVisualParamInfo()
  39. //-----------------------------------------------------------------------------
  40. LLVisualParamInfo::LLVisualParamInfo()
  41. : mID(-1),
  42. mGroup(VISUAL_PARAM_GROUP_TWEAKABLE),
  43. mMinWeight(0.f),
  44. mMaxWeight(1.f),
  45. mDefaultWeight(0.f),
  46. mSex(SEX_BOTH)
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. // parseXml()
  51. //-----------------------------------------------------------------------------
  52. bool LLVisualParamInfo::parseXml(LLXmlTreeNode* node)
  53. {
  54. // attribute: id
  55. static LLStdStringHandle id_string = LLXmlTree::addAttributeString("id");
  56. node->getFastAttributeS32(id_string, mID);
  57. // attribute: group
  58. U32 group = 0;
  59. static LLStdStringHandle group_string = LLXmlTree::addAttributeString("group");
  60. if (node->getFastAttributeU32(group_string, group))
  61. {
  62. if (group < NUM_VISUAL_PARAM_GROUPS)
  63. {
  64. mGroup = (EVisualParamGroup)group;
  65. }
  66. }
  67. // attribute: value_min, value_max
  68. static LLStdStringHandle value_min_string = LLXmlTree::addAttributeString("value_min");
  69. static LLStdStringHandle value_max_string = LLXmlTree::addAttributeString("value_max");
  70. node->getFastAttributeF32(value_min_string, mMinWeight);
  71. node->getFastAttributeF32(value_max_string, mMaxWeight);
  72. // attribute: value_default
  73. F32 default_weight = 0;
  74. static LLStdStringHandle value_default_string = LLXmlTree::addAttributeString("value_default");
  75. if (node->getFastAttributeF32(value_default_string, default_weight))
  76. {
  77. mDefaultWeight = llclamp(default_weight, mMinWeight, mMaxWeight);
  78. if (default_weight != mDefaultWeight)
  79. {
  80. llwarns << "value_default attribute is out of range in node "
  81. << mName << " " << default_weight << llendl;
  82. }
  83. }
  84. // attribute: sex
  85. std::string sex = "both";
  86. static LLStdStringHandle sex_string = LLXmlTree::addAttributeString("sex");
  87. node->getFastAttributeString(sex_string, sex); // optional
  88. if (sex == "both")
  89. {
  90. mSex = SEX_BOTH;
  91. }
  92. else if (sex == "male")
  93. {
  94. mSex = SEX_MALE;
  95. }
  96. else if (sex == "female")
  97. {
  98. mSex = SEX_FEMALE;
  99. }
  100. else
  101. {
  102. llwarns << "Avatar file: <param> has invalid sex attribute: " << sex
  103. << llendl;
  104. return false;
  105. }
  106. // attribute: name
  107. static LLStdStringHandle name_string = LLXmlTree::addAttributeString("name");
  108. if (!node->getFastAttributeString(name_string, mName))
  109. {
  110. llwarns << "Avatar file: <param> is missing name attribute" << llendl;
  111. return false;
  112. }
  113. // attribute: label
  114. static LLStdStringHandle label_string = LLXmlTree::addAttributeString("label");
  115. if (!node->getFastAttributeString(label_string, mDisplayName))
  116. {
  117. mDisplayName = mName;
  118. }
  119. // JC - make sure the display name includes the capitalization in the XML file,
  120. // not the lowercased version.
  121. LLStringUtil::toLower(mName);
  122. // attribute: label_min
  123. static LLStdStringHandle label_min_string = LLXmlTree::addAttributeString("label_min");
  124. if (!node->getFastAttributeString(label_min_string, mMinName))
  125. {
  126. mMinName = "Less";
  127. }
  128. // attribute: label_max
  129. static LLStdStringHandle label_max_string = LLXmlTree::addAttributeString("label_max");
  130. if (!node->getFastAttributeString(label_max_string, mMaxName))
  131. {
  132. mMaxName = "More";
  133. }
  134. return true;
  135. }
  136. //-----------------------------------------------------------------------------
  137. // LLVisualParam()
  138. //-----------------------------------------------------------------------------
  139. LLVisualParam::LLVisualParam()
  140. : mCurWeight(0.f),
  141. mLastWeight(0.f),
  142. mNext(NULL),
  143. mTargetWeight(0.f),
  144. mIsAnimating(false),
  145. mIsDummy(false),
  146. mID(-1),
  147. mInfo(0),
  148. mParamLocation(LOC_UNKNOWN)
  149. {
  150. }
  151. LLVisualParam::LLVisualParam(const LLVisualParam& other)
  152. : mCurWeight(other.mCurWeight),
  153. mLastWeight(other.mLastWeight),
  154. mNext(other.mNext),
  155. mTargetWeight(other.mTargetWeight),
  156. mIsAnimating(other.mIsAnimating),
  157. mIsDummy(other.mIsDummy),
  158. mID(other.mID),
  159. mInfo(other.mInfo),
  160. mParamLocation(other.mParamLocation)
  161. {
  162. }
  163. //-----------------------------------------------------------------------------
  164. // ~LLVisualParam()
  165. //-----------------------------------------------------------------------------
  166. LLVisualParam::~LLVisualParam()
  167. {
  168. if (mNext)
  169. {
  170. delete mNext;
  171. mNext = NULL;
  172. }
  173. }
  174. //=============================================================================
  175. // This virtual functions should always be overridden, but is included here for
  176. // use as templates
  177. //=============================================================================
  178. #if 0
  179. //-----------------------------------------------------------------------------
  180. // setInfo()
  181. //-----------------------------------------------------------------------------
  182. bool LLVisualParam::setInfo(LLVisualParamInfo* info)
  183. {
  184. llassert(mInfo == NULL);
  185. if (info->mID < 0)
  186. {
  187. return false;
  188. }
  189. mInfo = info;
  190. mID = info->mID;
  191. setWeight(getDefaultWeight(), false);
  192. return true;
  193. }
  194. #endif
  195. //-----------------------------------------------------------------------------
  196. // setWeight()
  197. //-----------------------------------------------------------------------------
  198. void LLVisualParam::setWeight(F32 weight, bool upload_bake)
  199. {
  200. if (mIsAnimating)
  201. {
  202. //RN: allow overshoot
  203. mCurWeight = weight;
  204. }
  205. else if (mInfo)
  206. {
  207. mCurWeight = llclamp(weight, mInfo->mMinWeight, mInfo->mMaxWeight);
  208. }
  209. else
  210. {
  211. mCurWeight = weight;
  212. }
  213. if (mNext)
  214. {
  215. mNext->setWeight(weight, upload_bake);
  216. }
  217. }
  218. //-----------------------------------------------------------------------------
  219. // setAnimationTarget()
  220. //-----------------------------------------------------------------------------
  221. void LLVisualParam::setAnimationTarget(F32 target_value, bool upload_bake)
  222. {
  223. // don't animate dummy parameters
  224. if (mIsDummy)
  225. {
  226. setWeight(target_value, upload_bake);
  227. mTargetWeight = mCurWeight;
  228. return;
  229. }
  230. if (mInfo)
  231. {
  232. if (isTweakable())
  233. {
  234. mTargetWeight = llclamp(target_value, mInfo->mMinWeight,
  235. mInfo->mMaxWeight);
  236. }
  237. }
  238. else
  239. {
  240. mTargetWeight = target_value;
  241. }
  242. mIsAnimating = true;
  243. if (mNext)
  244. {
  245. mNext->setAnimationTarget(target_value, upload_bake);
  246. }
  247. }
  248. //-----------------------------------------------------------------------------
  249. // setNextParam()
  250. //-----------------------------------------------------------------------------
  251. void LLVisualParam::setNextParam(LLVisualParam* next)
  252. {
  253. // need to establish mNext before we start changing values on this, else
  254. // initial value won't get mirrored (we can fix that, but better to forbid
  255. // this pattern):
  256. llassert(!mNext && getWeight() == getDefaultWeight());
  257. mNext = next;
  258. }
  259. //-----------------------------------------------------------------------------
  260. // animate()
  261. //-----------------------------------------------------------------------------
  262. void LLVisualParam::animate(F32 delta, bool upload_bake)
  263. {
  264. if (mIsAnimating)
  265. {
  266. F32 new_weight = (mTargetWeight - mCurWeight) * delta + mCurWeight;
  267. setWeight(new_weight, upload_bake);
  268. }
  269. }
  270. //-----------------------------------------------------------------------------
  271. // stopAnimating()
  272. //-----------------------------------------------------------------------------
  273. void LLVisualParam::stopAnimating(bool upload_bake)
  274. {
  275. if (mIsAnimating && isTweakable())
  276. {
  277. mIsAnimating = false;
  278. setWeight(mTargetWeight, upload_bake);
  279. }
  280. }
  281. const std::string param_location_name(const EParamLocation& loc)
  282. {
  283. switch (loc)
  284. {
  285. case LOC_UNKNOWN: return "unknown";
  286. case LOC_AV_SELF: return "self";
  287. case LOC_AV_OTHER: return "other";
  288. case LOC_WEARABLE: return "wearable";
  289. default: return "error";
  290. }
  291. }
  292. void LLVisualParam::setParamLocation(EParamLocation loc)
  293. {
  294. if (mParamLocation == LOC_UNKNOWN || loc == LOC_UNKNOWN)
  295. {
  296. mParamLocation = loc;
  297. }
  298. else if (mParamLocation != loc)
  299. {
  300. LL_DEBUGS("VisualParams") << "Param location is already "
  301. << mParamLocation << ", not slamming to "
  302. << loc << LL_ENDL;
  303. }
  304. }