llstatbar.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * @file llstatbar.cpp
  3. * @brief A little map of the world with network information
  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. #include "linden_common.h"
  33. #include "llstatbar.h"
  34. #include "llgl.h"
  35. #include "llstat.h"
  36. LLStatBar::LLStatBar(const std::string& name, const LLRect& rect,
  37. const std::string& setting,
  38. bool default_bar, bool default_history)
  39. : LLView(name, rect, true),
  40. mSetting(setting),
  41. mMinBar(0.f),
  42. mMaxBar(50.f),
  43. mStatp(NULL),
  44. mTickSpacing(10.f),
  45. mLabelSpacing(10.f),
  46. mPrecision(0),
  47. mUpdatesPerSec(5),
  48. mLabel(name),
  49. mPerSec(true),
  50. mValue(0.f),
  51. mDisplayMean(true),
  52. mNoResize(false)
  53. {
  54. S32 mode = -1;
  55. if (mSetting.length() > 0 && LLUI::sConfigGroup)
  56. {
  57. mode = LLUI::sConfigGroup->getS32(mSetting.c_str());
  58. }
  59. if (mode != -1)
  60. {
  61. mDisplayBar = (mode & STAT_BAR_FLAG) != 0;
  62. mDisplayHistory = (mode & STAT_HISTORY_FLAG) != 0;
  63. }
  64. else
  65. {
  66. mDisplayBar = default_bar;
  67. mDisplayHistory = default_history;
  68. }
  69. }
  70. bool LLStatBar::handleMouseDown(S32 x, S32 y, MASK mask)
  71. {
  72. if (mNoResize)
  73. {
  74. return false;
  75. }
  76. if (mDisplayBar)
  77. {
  78. if (mDisplayHistory)
  79. {
  80. mDisplayBar = false;
  81. mDisplayHistory = false;
  82. }
  83. else
  84. {
  85. mDisplayHistory = true;
  86. }
  87. }
  88. else
  89. {
  90. mDisplayBar = true;
  91. }
  92. LLView* parent = getParent();
  93. parent->reshape(parent->getRect().getWidth(),
  94. parent->getRect().getHeight(), false);
  95. // Save view mode
  96. if (mSetting.length() > 0 && LLUI::sConfigGroup)
  97. {
  98. S32 mode = 0;
  99. if (mDisplayBar)
  100. {
  101. mode |= STAT_BAR_FLAG;
  102. }
  103. if (mDisplayHistory)
  104. {
  105. mode |= STAT_HISTORY_FLAG;
  106. }
  107. LLUI::sConfigGroup->setS32(mSetting.c_str(), mode);
  108. }
  109. return true;
  110. }
  111. void LLStatBar::draw()
  112. {
  113. if (!mStatp)
  114. {
  115. return;
  116. }
  117. // Get the values.
  118. F32 current, min, max, mean;
  119. if (mPerSec)
  120. {
  121. current = mStatp->getCurrentPerSec();
  122. min = mStatp->getMinPerSec();
  123. max = mStatp->getMaxPerSec();
  124. mean = mStatp->getMeanPerSec();
  125. }
  126. else
  127. {
  128. current = mStatp->getCurrent();
  129. min = mStatp->getMin();
  130. max = mStatp->getMax();
  131. mean = mStatp->getMean();
  132. }
  133. if (mUpdatesPerSec == 0.f || mValue == 0.f ||
  134. mUpdateTimer.getElapsedTimeF32() > 1.f / mUpdatesPerSec)
  135. {
  136. if (mDisplayMean)
  137. {
  138. mValue = mean;
  139. }
  140. else
  141. {
  142. mValue = current;
  143. }
  144. mUpdateTimer.reset();
  145. }
  146. S32 height = getRect().getHeight();
  147. S32 width = getRect().getWidth() - 40;
  148. S32 max_width = width;
  149. S32 bar_top = height - 15; // 16 pixels from top.
  150. S32 bar_height = bar_top - 20;
  151. S32 tick_height = 4;
  152. S32 tick_width = 1;
  153. S32 left, top, right, bottom;
  154. F32 value_scale = max_width/(mMaxBar - mMinBar);
  155. LLFontGL::getFontMonospace()->renderUTF8(mLabel, 0, 0, height,
  156. LLColor4(1.f, 1.f, 1.f, 1.f),
  157. LLFontGL::LEFT, LLFontGL::TOP);
  158. std::string value_format;
  159. std::string value_str;
  160. if (!mUnitLabel.empty())
  161. {
  162. value_format = llformat("%%.%df%%s", mPrecision);
  163. value_str = llformat(value_format.c_str(), mValue, mUnitLabel.c_str());
  164. }
  165. else
  166. {
  167. value_format = llformat("%%.%df", mPrecision);
  168. value_str = llformat(value_format.c_str(), mValue);
  169. }
  170. // Draw the value.
  171. LLFontGL::getFontMonospace()->renderUTF8(value_str, 0, width, height,
  172. LLColor4(1.f, 1.f, 1.f, 0.5f),
  173. LLFontGL::RIGHT, LLFontGL::TOP);
  174. value_format = llformat("%%.%df", mPrecision);
  175. if (mDisplayBar)
  176. {
  177. std::string tick_label;
  178. // Draw the tick marks.
  179. F32 tick_value;
  180. top = bar_top;
  181. bottom = bar_top - bar_height - tick_height/2;
  182. LLGLSUIDefault gls_ui;
  183. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  184. for (tick_value = mMinBar; tick_value <= mMaxBar;
  185. tick_value += mTickSpacing)
  186. {
  187. static const LLColor4 color0 = LLColor4(1.f, 1.f, 1.f, 0.1f);
  188. left = llfloor((tick_value - mMinBar) * value_scale);
  189. right = left + tick_width;
  190. gl_rect_2d(left, top, right, bottom, color0);
  191. }
  192. // Draw the tick labels (and big ticks).
  193. bottom = bar_top - bar_height - tick_height;
  194. for (tick_value = mMinBar; tick_value <= mMaxBar;
  195. tick_value += mLabelSpacing)
  196. {
  197. static const LLColor4 color1 = LLColor4(1.f, 1.f, 1.f, 0.5f);
  198. static const LLColor4 color2 = LLColor4(1.f, 1.f, 1.f, 0.25f);
  199. left = llfloor((tick_value - mMinBar)*value_scale);
  200. right = left + tick_width;
  201. gl_rect_2d(left, top, right, bottom, color2);
  202. tick_label = llformat(value_format.c_str(), tick_value);
  203. // Draw labels for the tick marks
  204. LLFontGL::getFontMonospace()->renderUTF8(tick_label, 0, left - 1,
  205. bar_top - bar_height -
  206. tick_height, color1,
  207. LLFontGL::LEFT,
  208. LLFontGL::TOP);
  209. }
  210. // Now, draw the bars
  211. top = bar_top;
  212. bottom = bar_top - bar_height;
  213. // Draw background bar.
  214. left = 0;
  215. right = width;
  216. gl_rect_2d(left, top, right, bottom, LLColor4(0.f, 0.f, 0.f, 0.25f));
  217. if (mStatp->getNumValues() == 0)
  218. {
  219. // No data, don't draw anything...
  220. return;
  221. }
  222. // Draw min and max
  223. left = (S32)((min - mMinBar) * value_scale);
  224. if (left < 0)
  225. {
  226. left = 0;
  227. llwarns << "Min:" << min << llendl;
  228. }
  229. right = (S32) ((max - mMinBar) * value_scale);
  230. gl_rect_2d(left, top, right, bottom, LLColor4(1.f, 0.f, 0.f, 0.25f));
  231. S32 num_values = mStatp->getNumValues() - 1;
  232. if (mDisplayHistory)
  233. {
  234. for (S32 i = 0; i < num_values; i++)
  235. {
  236. if (i == mStatp->getNextBin())
  237. {
  238. continue;
  239. }
  240. if (mPerSec)
  241. {
  242. left = (S32)((mStatp->getPrevPerSec(i) - mMinBar) *
  243. value_scale);
  244. right = (S32)((mStatp->getPrevPerSec(i) - mMinBar) *
  245. value_scale) + 1;
  246. gl_rect_2d(left, bottom + i + 1, right, bottom + i,
  247. LLColor4::red);
  248. }
  249. else
  250. {
  251. left = (S32)((mStatp->getPrev(i) - mMinBar) * value_scale);
  252. right = (S32)((mStatp->getPrev(i) - mMinBar) *
  253. value_scale) + 1;
  254. gl_rect_2d(left, bottom + i + 1, right, bottom + i,
  255. LLColor4::red);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. // Draw current
  262. left = (S32) ((current - mMinBar) * value_scale) - 1;
  263. right = (S32) ((current - mMinBar) * value_scale) + 1;
  264. gl_rect_2d(left, top, right, bottom, LLColor4::red);
  265. }
  266. // Draw mean bar
  267. top = bar_top + 2;
  268. bottom = bar_top - bar_height - 2;
  269. left = (S32)((mean - mMinBar) * value_scale) - 1;
  270. right = (S32)((mean - mMinBar) * value_scale) + 1;
  271. gl_rect_2d(left, top, right, bottom, LLColor4::green);
  272. }
  273. LLView::draw();
  274. }
  275. const std::string& LLStatBar::getLabel() const
  276. {
  277. return mLabel;
  278. }
  279. void LLStatBar::setLabel(const std::string& label)
  280. {
  281. mLabel = label;
  282. }
  283. void LLStatBar::setUnitLabel(const std::string& unit_label)
  284. {
  285. mUnitLabel = unit_label;
  286. }
  287. LLRect LLStatBar::getRequiredRect()
  288. {
  289. LLRect rect;
  290. if (mDisplayBar)
  291. {
  292. if (mDisplayHistory)
  293. {
  294. rect.mTop = 67;
  295. }
  296. else
  297. {
  298. rect.mTop = 40;
  299. }
  300. }
  301. else
  302. {
  303. rect.mTop = 14;
  304. }
  305. return rect;
  306. }